xref: /petsc/src/dm/interface/dm.c (revision 8892b4c328940c3f3f8ea9104af951a1aa006857)
1b45d2f2cSJed Brown #include <petsc-private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
32764a2aaSMatthew G. Knepley #include <petscds.h>
447c6ae99SBarry Smith 
5732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
6f089877aSRichard Tran Mills PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal;
767a56275SMatthew G Knepley 
8bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
9bff4a2f0SMatthew G. Knepley 
1047c6ae99SBarry Smith #undef __FUNCT__
11a4121054SBarry Smith #define __FUNCT__ "DMCreate"
12a4121054SBarry Smith /*@
13de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
14a4121054SBarry Smith 
15a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
16a4121054SBarry Smith    error when you try to use the vector.
17a4121054SBarry Smith 
18a4121054SBarry Smith   Collective on MPI_Comm
19a4121054SBarry Smith 
20a4121054SBarry Smith   Input Parameter:
21a4121054SBarry Smith . comm - The communicator for the DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Output Parameter:
24a4121054SBarry Smith . dm - The DM object
25a4121054SBarry Smith 
26a4121054SBarry Smith   Level: beginner
27a4121054SBarry Smith 
28a4121054SBarry Smith .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE
29a4121054SBarry Smith @*/
307087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
31a4121054SBarry Smith {
32a4121054SBarry Smith   DM             v;
33a4121054SBarry Smith   PetscErrorCode ierr;
34a4121054SBarry Smith 
35a4121054SBarry Smith   PetscFunctionBegin;
361411c6eeSJed Brown   PetscValidPointer(dm,2);
370298fd71SBarry Smith   *dm = NULL;
388b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
39607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
42a4121054SBarry Smith 
4367c2884eSBarry Smith   ierr = PetscHeaderCreate(v, _p_DM, struct _DMOps, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
44a4121054SBarry Smith   ierr = PetscMemzero(v->ops, sizeof(struct _DMOps));CHKERRQ(ierr);
451411c6eeSJed Brown 
46e7c4fc90SDmitry Karpeev 
470298fd71SBarry Smith   v->ltogmap                  = NULL;
481411c6eeSJed Brown   v->bs                       = 1;
49171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5188ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
520298fd71SBarry Smith   v->defaultSection           = NULL;
530298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
54fba222abSToby Isaac   v->defaultConstraintSection = NULL;
55fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
56c6b900c6SMatthew G. Knepley   v->L                        = NULL;
57c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
585dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
599a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
60435a35e8SMatthew G Knepley   {
61435a35e8SMatthew G Knepley     PetscInt i;
62435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
630298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
64435a35e8SMatthew G Knepley     }
65435a35e8SMatthew G Knepley   }
662764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6714f150ffSMatthew G. Knepley   v->dmBC = NULL;
68f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
69cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
70c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
71b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
721411c6eeSJed Brown   *dm = v;
73a4121054SBarry Smith   PetscFunctionReturn(0);
74a4121054SBarry Smith }
75a4121054SBarry Smith 
76a4121054SBarry Smith #undef __FUNCT__
7738221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
7838221697SMatthew G. Knepley /*@
7938221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8038221697SMatthew G. Knepley 
8138221697SMatthew G. Knepley   Collective on MPI_Comm
8238221697SMatthew G. Knepley 
8338221697SMatthew G. Knepley   Input Parameter:
8438221697SMatthew G. Knepley . dm - The original DM object
8538221697SMatthew G. Knepley 
8638221697SMatthew G. Knepley   Output Parameter:
8738221697SMatthew G. Knepley . newdm  - The new DM object
8838221697SMatthew G. Knepley 
8938221697SMatthew G. Knepley   Level: beginner
9038221697SMatthew G. Knepley 
9138221697SMatthew G. Knepley .keywords: DM, topology, create
9238221697SMatthew G. Knepley @*/
9338221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9438221697SMatthew G. Knepley {
9538221697SMatthew G. Knepley   PetscSF        sf;
9638221697SMatthew G. Knepley   Vec            coords;
9738221697SMatthew G. Knepley   void          *ctx;
981de53e9aSMatthew G. Knepley   PetscInt       dim;
9938221697SMatthew G. Knepley   PetscErrorCode ierr;
10038221697SMatthew G. Knepley 
10138221697SMatthew G. Knepley   PetscFunctionBegin;
10238221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10338221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10438221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
1051de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1061de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
10738221697SMatthew G. Knepley   if (dm->ops->clone) {
10838221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10938221697SMatthew G. Knepley   }
110cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
11138221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11238221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
11338221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
11438221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
11538221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
11638221697SMatthew G. Knepley   if (coords) {
11738221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
11838221697SMatthew G. Knepley   } else {
11938221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
12038221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
12138221697SMatthew G. Knepley   }
122c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
123c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1245dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1255dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1265dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
127c6b900c6SMatthew G. Knepley   }
12838221697SMatthew G. Knepley   PetscFunctionReturn(0);
12938221697SMatthew G. Knepley }
13038221697SMatthew G. Knepley 
13138221697SMatthew G. Knepley #undef __FUNCT__
1329a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1339a42bb27SBarry Smith /*@C
134564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1359a42bb27SBarry Smith 
136aa219208SBarry Smith    Logically Collective on DMDA
1379a42bb27SBarry Smith 
1389a42bb27SBarry Smith    Input Parameter:
1399a42bb27SBarry Smith +  da - initial distributed array
1408154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1419a42bb27SBarry Smith 
1429a42bb27SBarry Smith    Options Database:
143dd85299cSBarry Smith .   -dm_vec_type ctype
1449a42bb27SBarry Smith 
1459a42bb27SBarry Smith    Level: intermediate
1469a42bb27SBarry Smith 
147c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType, DMGetVecType()
1489a42bb27SBarry Smith @*/
14919fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1509a42bb27SBarry Smith {
1519a42bb27SBarry Smith   PetscErrorCode ierr;
1529a42bb27SBarry Smith 
1539a42bb27SBarry Smith   PetscFunctionBegin;
1549a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1559a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
15619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1579a42bb27SBarry Smith   PetscFunctionReturn(0);
1589a42bb27SBarry Smith }
1599a42bb27SBarry Smith 
1609a42bb27SBarry Smith #undef __FUNCT__
161c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
162c0dedaeaSBarry Smith /*@C
163c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
164c0dedaeaSBarry Smith 
165c0dedaeaSBarry Smith    Logically Collective on DMDA
166c0dedaeaSBarry Smith 
167c0dedaeaSBarry Smith    Input Parameter:
168c0dedaeaSBarry Smith .  da - initial distributed array
169c0dedaeaSBarry Smith 
170c0dedaeaSBarry Smith    Output Parameter:
171c0dedaeaSBarry Smith .  ctype - the vector type
172c0dedaeaSBarry Smith 
173c0dedaeaSBarry Smith    Level: intermediate
174c0dedaeaSBarry Smith 
175c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMDestroy(), DMDA, DMDAInterpolationType, VecType
176c0dedaeaSBarry Smith @*/
177c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
178c0dedaeaSBarry Smith {
179c0dedaeaSBarry Smith   PetscFunctionBegin;
180c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
181c0dedaeaSBarry Smith   *ctype = da->vectype;
182c0dedaeaSBarry Smith   PetscFunctionReturn(0);
183c0dedaeaSBarry Smith }
184c0dedaeaSBarry Smith 
185c0dedaeaSBarry Smith #undef __FUNCT__
1865f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1875f1ad066SMatthew G Knepley /*@
18834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1895f1ad066SMatthew G Knepley 
1905f1ad066SMatthew G Knepley   Not collective
1915f1ad066SMatthew G Knepley 
1925f1ad066SMatthew G Knepley   Input Parameter:
1935f1ad066SMatthew G Knepley . v - The Vec
1945f1ad066SMatthew G Knepley 
1955f1ad066SMatthew G Knepley   Output Parameter:
1965f1ad066SMatthew G Knepley . dm - The DM
1975f1ad066SMatthew G Knepley 
1985f1ad066SMatthew G Knepley   Level: intermediate
1995f1ad066SMatthew G Knepley 
2005f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2015f1ad066SMatthew G Knepley @*/
2025f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2035f1ad066SMatthew G Knepley {
2045f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2055f1ad066SMatthew G Knepley 
2065f1ad066SMatthew G Knepley   PetscFunctionBegin;
2075f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2085f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2095f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2105f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2115f1ad066SMatthew G Knepley }
2125f1ad066SMatthew G Knepley 
2135f1ad066SMatthew G Knepley #undef __FUNCT__
2145f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2155f1ad066SMatthew G Knepley /*@
2165f1ad066SMatthew G Knepley   VecSetDM - Sets the DM defining the data layout of the vector
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley   Not collective
2195f1ad066SMatthew G Knepley 
2205f1ad066SMatthew G Knepley   Input Parameters:
2215f1ad066SMatthew G Knepley + v - The Vec
2225f1ad066SMatthew G Knepley - dm - The DM
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley   Level: intermediate
2255f1ad066SMatthew G Knepley 
2265f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2275f1ad066SMatthew G Knepley @*/
2285f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2295f1ad066SMatthew G Knepley {
2305f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley   PetscFunctionBegin;
2335f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
234d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2355f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2365f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2375f1ad066SMatthew G Knepley }
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley #undef __FUNCT__
240521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
241521d9a4cSLisandro Dalcin /*@C
242521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
243521d9a4cSLisandro Dalcin 
244521d9a4cSLisandro Dalcin    Logically Collective on DM
245521d9a4cSLisandro Dalcin 
246521d9a4cSLisandro Dalcin    Input Parameter:
247521d9a4cSLisandro Dalcin +  dm - the DM context
248521d9a4cSLisandro Dalcin .  ctype - the matrix type
249521d9a4cSLisandro Dalcin 
250521d9a4cSLisandro Dalcin    Options Database:
251521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
252521d9a4cSLisandro Dalcin 
253521d9a4cSLisandro Dalcin    Level: intermediate
254521d9a4cSLisandro Dalcin 
255c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
256521d9a4cSLisandro Dalcin @*/
25719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
258521d9a4cSLisandro Dalcin {
259521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
26088f0584fSBarry Smith 
261521d9a4cSLisandro Dalcin   PetscFunctionBegin;
262521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
263521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
26419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
265521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
266521d9a4cSLisandro Dalcin }
267521d9a4cSLisandro Dalcin 
268521d9a4cSLisandro Dalcin #undef __FUNCT__
269c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
270c0dedaeaSBarry Smith /*@C
271c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
272c0dedaeaSBarry Smith 
273c0dedaeaSBarry Smith    Logically Collective on DM
274c0dedaeaSBarry Smith 
275c0dedaeaSBarry Smith    Input Parameter:
276c0dedaeaSBarry Smith .  dm - the DM context
277c0dedaeaSBarry Smith 
278c0dedaeaSBarry Smith    Output Parameter:
279c0dedaeaSBarry Smith .  ctype - the matrix type
280c0dedaeaSBarry Smith 
281c0dedaeaSBarry Smith    Options Database:
282c0dedaeaSBarry Smith .   -dm_mat_type ctype
283c0dedaeaSBarry Smith 
284c0dedaeaSBarry Smith    Level: intermediate
285c0dedaeaSBarry Smith 
286c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
287c0dedaeaSBarry Smith @*/
288c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
289c0dedaeaSBarry Smith {
290c0dedaeaSBarry Smith   PetscFunctionBegin;
291c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
292c0dedaeaSBarry Smith   *ctype = dm->mattype;
293c0dedaeaSBarry Smith   PetscFunctionReturn(0);
294c0dedaeaSBarry Smith }
295c0dedaeaSBarry Smith 
296c0dedaeaSBarry Smith #undef __FUNCT__
297c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
298c688c046SMatthew G Knepley /*@
29934f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
300c688c046SMatthew G Knepley 
301c688c046SMatthew G Knepley   Not collective
302c688c046SMatthew G Knepley 
303c688c046SMatthew G Knepley   Input Parameter:
304c688c046SMatthew G Knepley . A - The Mat
305c688c046SMatthew G Knepley 
306c688c046SMatthew G Knepley   Output Parameter:
307c688c046SMatthew G Knepley . dm - The DM
308c688c046SMatthew G Knepley 
309c688c046SMatthew G Knepley   Level: intermediate
310c688c046SMatthew G Knepley 
311c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
312c688c046SMatthew G Knepley @*/
313c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
314c688c046SMatthew G Knepley {
315c688c046SMatthew G Knepley   PetscErrorCode ierr;
316c688c046SMatthew G Knepley 
317c688c046SMatthew G Knepley   PetscFunctionBegin;
318c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
319c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
320c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
321c688c046SMatthew G Knepley   PetscFunctionReturn(0);
322c688c046SMatthew G Knepley }
323c688c046SMatthew G Knepley 
324c688c046SMatthew G Knepley #undef __FUNCT__
325c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
326c688c046SMatthew G Knepley /*@
327c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley   Not collective
330c688c046SMatthew G Knepley 
331c688c046SMatthew G Knepley   Input Parameters:
332c688c046SMatthew G Knepley + A - The Mat
333c688c046SMatthew G Knepley - dm - The DM
334c688c046SMatthew G Knepley 
335c688c046SMatthew G Knepley   Level: intermediate
336c688c046SMatthew G Knepley 
337c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
338c688c046SMatthew G Knepley @*/
339c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
340c688c046SMatthew G Knepley {
341c688c046SMatthew G Knepley   PetscErrorCode ierr;
342c688c046SMatthew G Knepley 
343c688c046SMatthew G Knepley   PetscFunctionBegin;
344c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3458865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
346c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
347c688c046SMatthew G Knepley   PetscFunctionReturn(0);
348c688c046SMatthew G Knepley }
349c688c046SMatthew G Knepley 
350c688c046SMatthew G Knepley #undef __FUNCT__
3519a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3529a42bb27SBarry Smith /*@C
3539a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
354aa219208SBarry Smith    DMDA options in the database.
3559a42bb27SBarry Smith 
356aa219208SBarry Smith    Logically Collective on DMDA
3579a42bb27SBarry Smith 
3589a42bb27SBarry Smith    Input Parameter:
359aa219208SBarry Smith +  da - the DMDA context
3609a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3619a42bb27SBarry Smith 
3629a42bb27SBarry Smith    Notes:
3639a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3649a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3659a42bb27SBarry Smith 
3669a42bb27SBarry Smith    Level: advanced
3679a42bb27SBarry Smith 
368aa219208SBarry Smith .keywords: DMDA, set, options, prefix, database
3699a42bb27SBarry Smith 
3709a42bb27SBarry Smith .seealso: DMSetFromOptions()
3719a42bb27SBarry Smith @*/
3727087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3739a42bb27SBarry Smith {
3749a42bb27SBarry Smith   PetscErrorCode ierr;
3759a42bb27SBarry Smith 
3769a42bb27SBarry Smith   PetscFunctionBegin;
3779a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3789a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
3799a42bb27SBarry Smith   PetscFunctionReturn(0);
3809a42bb27SBarry Smith }
3819a42bb27SBarry Smith 
3829a42bb27SBarry Smith #undef __FUNCT__
38347c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
38447c6ae99SBarry Smith /*@
385aa219208SBarry Smith     DMDestroy - Destroys a vector packer or DMDA.
38647c6ae99SBarry Smith 
38747c6ae99SBarry Smith     Collective on DM
38847c6ae99SBarry Smith 
38947c6ae99SBarry Smith     Input Parameter:
39047c6ae99SBarry Smith .   dm - the DM object to destroy
39147c6ae99SBarry Smith 
39247c6ae99SBarry Smith     Level: developer
39347c6ae99SBarry Smith 
394e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
39547c6ae99SBarry Smith 
39647c6ae99SBarry Smith @*/
397fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
39847c6ae99SBarry Smith {
399c83e3748SMatthew G. Knepley   PetscInt       i, cnt = 0;
400dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
40147c6ae99SBarry Smith   PetscErrorCode ierr;
40247c6ae99SBarry Smith 
40347c6ae99SBarry Smith   PetscFunctionBegin;
4046bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4056bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
40687e657c6SBarry Smith 
40787e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
408732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4098865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4108865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
411732e2eb9SMatthew G Knepley   }
412dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4132348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4142348bcf4SPeter Brune   if ((*dm)->x) {
4152348bcf4SPeter Brune     DM obj;
4162348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4172348bcf4SPeter Brune     if (obj == *dm) cnt++;
4182348bcf4SPeter Brune   }
419732e2eb9SMatthew G Knepley 
4206bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
421732e2eb9SMatthew G Knepley   /*
422732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
423732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
424732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
425732e2eb9SMatthew G Knepley   */
4266bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4276bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
428732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4296bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
4306bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
431732e2eb9SMatthew G Knepley   }
432f490541aSPeter Brune   nnext=(*dm)->namedglobal;
4330298fd71SBarry Smith   (*dm)->namedglobal = NULL;
434f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
4352348bcf4SPeter Brune     nnext = nlink->next;
4362348bcf4SPeter 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);
4372348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
4382348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
4392348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
4402348bcf4SPeter Brune   }
441f490541aSPeter Brune   nnext=(*dm)->namedlocal;
4420298fd71SBarry Smith   (*dm)->namedlocal = NULL;
443f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
444f490541aSPeter Brune     nnext = nlink->next;
445f490541aSPeter 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);
446f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
447f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
448f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
449f490541aSPeter Brune   }
4502348bcf4SPeter Brune 
451b17ce1afSJed Brown   /* Destroy the list of hooks */
452c833c3b5SJed Brown   {
453c833c3b5SJed Brown     DMCoarsenHookLink link,next;
454b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
455b17ce1afSJed Brown       next = link->next;
456b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
457b17ce1afSJed Brown     }
4580298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
459c833c3b5SJed Brown   }
460c833c3b5SJed Brown   {
461c833c3b5SJed Brown     DMRefineHookLink link,next;
462c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
463c833c3b5SJed Brown       next = link->next;
464c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
465c833c3b5SJed Brown     }
4660298fd71SBarry Smith     (*dm)->refinehook = NULL;
467c833c3b5SJed Brown   }
468be081cd6SPeter Brune   {
469be081cd6SPeter Brune     DMSubDomainHookLink link,next;
470be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
471be081cd6SPeter Brune       next = link->next;
472be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
473be081cd6SPeter Brune     }
4740298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
475be081cd6SPeter Brune   }
476baf369e7SPeter Brune   {
477baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
478baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
479baf369e7SPeter Brune       next = link->next;
480baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
481baf369e7SPeter Brune     }
4820298fd71SBarry Smith     (*dm)->gtolhook = NULL;
483baf369e7SPeter Brune   }
484d4d07f1eSToby Isaac   {
485d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
486d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
487d4d07f1eSToby Isaac       next = link->next;
488d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
489d4d07f1eSToby Isaac     }
490d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
491d4d07f1eSToby Isaac   }
492aa1993deSMatthew G Knepley   /* Destroy the work arrays */
493aa1993deSMatthew G Knepley   {
494aa1993deSMatthew G Knepley     DMWorkLink link,next;
495aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
496aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
497aa1993deSMatthew G Knepley       next = link->next;
498aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
499aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
500aa1993deSMatthew G Knepley     }
5010298fd71SBarry Smith     (*dm)->workin = NULL;
502aa1993deSMatthew G Knepley   }
503b17ce1afSJed Brown 
50452536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
50552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
50652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
50752536dc3SBarry Smith 
5081a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5091a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5101a266240SBarry Smith   }
51187e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
51271cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5134dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5146bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5156bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
516073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
51788ed4aceSMatthew G Knepley 
51888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
51988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5208b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
521fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
522fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
52388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
52488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
525af122d2aSMatthew G Knepley 
5266636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5276636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
5286636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
5295dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
5306636e97aSMatthew G Knepley 
5312764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
53214f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
533e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
534e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
535732e2eb9SMatthew G Knepley 
5366bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
537435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
538732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
53947c6ae99SBarry Smith   PetscFunctionReturn(0);
54047c6ae99SBarry Smith }
54147c6ae99SBarry Smith 
54247c6ae99SBarry Smith #undef __FUNCT__
543d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
544d7bf68aeSBarry Smith /*@
545d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
546d7bf68aeSBarry Smith 
547d7bf68aeSBarry Smith     Collective on DM
548d7bf68aeSBarry Smith 
549d7bf68aeSBarry Smith     Input Parameter:
550d7bf68aeSBarry Smith .   dm - the DM object to setup
551d7bf68aeSBarry Smith 
552d7bf68aeSBarry Smith     Level: developer
553d7bf68aeSBarry Smith 
554e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
555d7bf68aeSBarry Smith 
556d7bf68aeSBarry Smith @*/
5577087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
558d7bf68aeSBarry Smith {
559d7bf68aeSBarry Smith   PetscErrorCode ierr;
560d7bf68aeSBarry Smith 
561d7bf68aeSBarry Smith   PetscFunctionBegin;
562171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5638387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
564d7bf68aeSBarry Smith   if (dm->ops->setup) {
565d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
566d7bf68aeSBarry Smith   }
5678387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
568d7bf68aeSBarry Smith   PetscFunctionReturn(0);
569d7bf68aeSBarry Smith }
570d7bf68aeSBarry Smith 
571d7bf68aeSBarry Smith #undef __FUNCT__
572d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
573d7bf68aeSBarry Smith /*@
574d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
575d7bf68aeSBarry Smith 
576d7bf68aeSBarry Smith     Collective on DM
577d7bf68aeSBarry Smith 
578d7bf68aeSBarry Smith     Input Parameter:
579d7bf68aeSBarry Smith .   dm - the DM object to set options for
580d7bf68aeSBarry Smith 
581732e2eb9SMatthew G Knepley     Options Database:
5824164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
5834164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
5844164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
5854164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
586732e2eb9SMatthew G Knepley 
587d7bf68aeSBarry Smith     Level: developer
588d7bf68aeSBarry Smith 
589e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
590d7bf68aeSBarry Smith 
591d7bf68aeSBarry Smith @*/
5927087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
593d7bf68aeSBarry Smith {
5947781c08eSBarry Smith   char           typeName[256];
595ca266f36SBarry Smith   PetscBool      flg;
596d7bf68aeSBarry Smith   PetscErrorCode ierr;
597d7bf68aeSBarry Smith 
598d7bf68aeSBarry Smith   PetscFunctionBegin;
599171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6003194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
6010298fd71SBarry 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);
602a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
603f9ba7244SBarry Smith   if (flg) {
604f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
605f9ba7244SBarry Smith   }
606a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_mat_type","Matrix type used for created matrices","DMSetMatType",MatList,dm->mattype ? dm->mattype : typeName,typeName,sizeof(typeName),&flg);CHKERRQ(ierr);
607073dac72SJed Brown   if (flg) {
608521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
609073dac72SJed Brown   }
6100298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
611f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
612e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
613f9ba7244SBarry Smith   }
614f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
615f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
61682fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
617d7bf68aeSBarry Smith   PetscFunctionReturn(0);
618d7bf68aeSBarry Smith }
619d7bf68aeSBarry Smith 
620d7bf68aeSBarry Smith #undef __FUNCT__
62147c6ae99SBarry Smith #define __FUNCT__ "DMView"
622fc9bc008SSatish Balay /*@C
623aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
62447c6ae99SBarry Smith 
62547c6ae99SBarry Smith     Collective on DM
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith     Input Parameter:
62847c6ae99SBarry Smith +   dm - the DM object to view
62947c6ae99SBarry Smith -   v - the viewer
63047c6ae99SBarry Smith 
63147c6ae99SBarry Smith     Level: developer
63247c6ae99SBarry Smith 
633e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
63447c6ae99SBarry Smith 
63547c6ae99SBarry Smith @*/
6367087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
63747c6ae99SBarry Smith {
63847c6ae99SBarry Smith   PetscErrorCode ierr;
63932c0f0efSBarry Smith   PetscBool      isbinary;
64047c6ae99SBarry Smith 
64147c6ae99SBarry Smith   PetscFunctionBegin;
642171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6433014e516SBarry Smith   if (!v) {
644ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
6453014e516SBarry Smith   }
64698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
64732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
64832c0f0efSBarry Smith   if (isbinary) {
64955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
65032c0f0efSBarry Smith     char     type[256];
65132c0f0efSBarry Smith 
65232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
65332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
65432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
65532c0f0efSBarry Smith   }
6560c010503SBarry Smith   if (dm->ops->view) {
6570c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
65847c6ae99SBarry Smith   }
65947c6ae99SBarry Smith   PetscFunctionReturn(0);
66047c6ae99SBarry Smith }
66147c6ae99SBarry Smith 
66247c6ae99SBarry Smith #undef __FUNCT__
66347c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
66447c6ae99SBarry Smith /*@
665aa219208SBarry Smith     DMCreateGlobalVector - Creates a global vector from a DMDA or DMComposite object
66647c6ae99SBarry Smith 
66747c6ae99SBarry Smith     Collective on DM
66847c6ae99SBarry Smith 
66947c6ae99SBarry Smith     Input Parameter:
67047c6ae99SBarry Smith .   dm - the DM object
67147c6ae99SBarry Smith 
67247c6ae99SBarry Smith     Output Parameter:
67347c6ae99SBarry Smith .   vec - the global vector
67447c6ae99SBarry Smith 
675073dac72SJed Brown     Level: beginner
67647c6ae99SBarry Smith 
677e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
67847c6ae99SBarry Smith 
67947c6ae99SBarry Smith @*/
6807087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
68147c6ae99SBarry Smith {
68247c6ae99SBarry Smith   PetscErrorCode ierr;
68347c6ae99SBarry Smith 
68447c6ae99SBarry Smith   PetscFunctionBegin;
685171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68647c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
68747c6ae99SBarry Smith   PetscFunctionReturn(0);
68847c6ae99SBarry Smith }
68947c6ae99SBarry Smith 
69047c6ae99SBarry Smith #undef __FUNCT__
69147c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
69247c6ae99SBarry Smith /*@
693aa219208SBarry Smith     DMCreateLocalVector - Creates a local vector from a DMDA or DMComposite object
69447c6ae99SBarry Smith 
69547c6ae99SBarry Smith     Not Collective
69647c6ae99SBarry Smith 
69747c6ae99SBarry Smith     Input Parameter:
69847c6ae99SBarry Smith .   dm - the DM object
69947c6ae99SBarry Smith 
70047c6ae99SBarry Smith     Output Parameter:
70147c6ae99SBarry Smith .   vec - the local vector
70247c6ae99SBarry Smith 
703073dac72SJed Brown     Level: beginner
70447c6ae99SBarry Smith 
705e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
70647c6ae99SBarry Smith 
70747c6ae99SBarry Smith @*/
7087087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
70947c6ae99SBarry Smith {
71047c6ae99SBarry Smith   PetscErrorCode ierr;
71147c6ae99SBarry Smith 
71247c6ae99SBarry Smith   PetscFunctionBegin;
713171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71447c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
71547c6ae99SBarry Smith   PetscFunctionReturn(0);
71647c6ae99SBarry Smith }
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith #undef __FUNCT__
7191411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7201411c6eeSJed Brown /*@
7211411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
7221411c6eeSJed Brown 
7231411c6eeSJed Brown    Collective on DM
7241411c6eeSJed Brown 
7251411c6eeSJed Brown    Input Parameter:
7261411c6eeSJed Brown .  dm - the DM that provides the mapping
7271411c6eeSJed Brown 
7281411c6eeSJed Brown    Output Parameter:
7291411c6eeSJed Brown .  ltog - the mapping
7301411c6eeSJed Brown 
7311411c6eeSJed Brown    Level: intermediate
7321411c6eeSJed Brown 
7331411c6eeSJed Brown    Notes:
7341411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
7351411c6eeSJed Brown    MatSetLocalToGlobalMapping().
7361411c6eeSJed Brown 
737fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
7381411c6eeSJed Brown @*/
7397087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
7401411c6eeSJed Brown {
7411411c6eeSJed Brown   PetscErrorCode ierr;
7421411c6eeSJed Brown 
7431411c6eeSJed Brown   PetscFunctionBegin;
7441411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7451411c6eeSJed Brown   PetscValidPointer(ltog,2);
7461411c6eeSJed Brown   if (!dm->ltogmap) {
74737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
74837d0c07bSMatthew G Knepley 
74937d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
75037d0c07bSMatthew G Knepley     if (section) {
75137d0c07bSMatthew G Knepley       PetscInt *ltog;
75237d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
75337d0c07bSMatthew G Knepley 
75437d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
75537d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
75637d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
757785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
75837d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
75937d0c07bSMatthew G Knepley         PetscInt dof, off, c;
76037d0c07bSMatthew G Knepley 
76137d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
76237d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
76337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
76437d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
76537d0c07bSMatthew G Knepley           ltog[l] = off+c;
76637d0c07bSMatthew G Knepley         }
76737d0c07bSMatthew G Knepley       }
768f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
7693bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
77037d0c07bSMatthew G Knepley     } else {
771184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
772184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
7731411c6eeSJed Brown     }
77437d0c07bSMatthew G Knepley   }
7751411c6eeSJed Brown   *ltog = dm->ltogmap;
7761411c6eeSJed Brown   PetscFunctionReturn(0);
7771411c6eeSJed Brown }
7781411c6eeSJed Brown 
7791411c6eeSJed Brown #undef __FUNCT__
7801411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
7811411c6eeSJed Brown /*@
7821411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
7831411c6eeSJed Brown 
7841411c6eeSJed Brown    Not Collective
7851411c6eeSJed Brown 
7861411c6eeSJed Brown    Input Parameter:
7871411c6eeSJed Brown .  dm - the DM with block structure
7881411c6eeSJed Brown 
7891411c6eeSJed Brown    Output Parameter:
7901411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
7911411c6eeSJed Brown 
7921411c6eeSJed Brown    Level: intermediate
7931411c6eeSJed Brown 
794fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
7951411c6eeSJed Brown @*/
7967087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
7971411c6eeSJed Brown {
7981411c6eeSJed Brown   PetscFunctionBegin;
7991411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8001411c6eeSJed Brown   PetscValidPointer(bs,2);
8011411c6eeSJed 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");
8021411c6eeSJed Brown   *bs = dm->bs;
8031411c6eeSJed Brown   PetscFunctionReturn(0);
8041411c6eeSJed Brown }
8051411c6eeSJed Brown 
8061411c6eeSJed Brown #undef __FUNCT__
807e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
80847c6ae99SBarry Smith /*@
809e727c939SJed Brown     DMCreateInterpolation - Gets interpolation matrix between two DMDA or DMComposite objects
81047c6ae99SBarry Smith 
81147c6ae99SBarry Smith     Collective on DM
81247c6ae99SBarry Smith 
81347c6ae99SBarry Smith     Input Parameter:
81447c6ae99SBarry Smith +   dm1 - the DM object
81547c6ae99SBarry Smith -   dm2 - the second, finer DM object
81647c6ae99SBarry Smith 
81747c6ae99SBarry Smith     Output Parameter:
81847c6ae99SBarry Smith +  mat - the interpolation
81947c6ae99SBarry Smith -  vec - the scaling (optional)
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith     Level: developer
82247c6ae99SBarry Smith 
82385afcc9aSBarry 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
82485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
825d52bd9f3SBarry Smith 
8261f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
827d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
82885afcc9aSBarry Smith 
82985afcc9aSBarry Smith 
830e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
83147c6ae99SBarry Smith 
83247c6ae99SBarry Smith @*/
833e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
83447c6ae99SBarry Smith {
83547c6ae99SBarry Smith   PetscErrorCode ierr;
83647c6ae99SBarry Smith 
83747c6ae99SBarry Smith   PetscFunctionBegin;
838171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
839171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
84025296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
84147c6ae99SBarry Smith   PetscFunctionReturn(0);
84247c6ae99SBarry Smith }
84347c6ae99SBarry Smith 
84447c6ae99SBarry Smith #undef __FUNCT__
845e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
84647c6ae99SBarry Smith /*@
847e727c939SJed Brown     DMCreateInjection - Gets injection matrix between two DMDA or DMComposite objects
84847c6ae99SBarry Smith 
84947c6ae99SBarry Smith     Collective on DM
85047c6ae99SBarry Smith 
85147c6ae99SBarry Smith     Input Parameter:
85247c6ae99SBarry Smith +   dm1 - the DM object
85347c6ae99SBarry Smith -   dm2 - the second, finer DM object
85447c6ae99SBarry Smith 
85547c6ae99SBarry Smith     Output Parameter:
8566dbf9973SLawrence Mitchell .   mat - the injection
85747c6ae99SBarry Smith 
85847c6ae99SBarry Smith     Level: developer
85947c6ae99SBarry Smith 
86085afcc9aSBarry 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
86185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
86285afcc9aSBarry Smith 
863e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
86447c6ae99SBarry Smith 
86547c6ae99SBarry Smith @*/
8666dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
86747c6ae99SBarry Smith {
86847c6ae99SBarry Smith   PetscErrorCode ierr;
86947c6ae99SBarry Smith 
87047c6ae99SBarry Smith   PetscFunctionBegin;
871171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
872171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
8736dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
87447c6ae99SBarry Smith   PetscFunctionReturn(0);
87547c6ae99SBarry Smith }
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith #undef __FUNCT__
878e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
879b412c318SBarry Smith /*@
880b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
88147c6ae99SBarry Smith 
88247c6ae99SBarry Smith     Collective on DM
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith     Input Parameter:
88547c6ae99SBarry Smith +   dm - the DM object
886b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith     Output Parameter:
88947c6ae99SBarry Smith .   coloring - the coloring
89047c6ae99SBarry Smith 
89147c6ae99SBarry Smith     Level: developer
89247c6ae99SBarry Smith 
893b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
89447c6ae99SBarry Smith 
895aab9d709SJed Brown @*/
896b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
89747c6ae99SBarry Smith {
89847c6ae99SBarry Smith   PetscErrorCode ierr;
89947c6ae99SBarry Smith 
90047c6ae99SBarry Smith   PetscFunctionBegin;
901171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
902ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
903b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
90447c6ae99SBarry Smith   PetscFunctionReturn(0);
90547c6ae99SBarry Smith }
90647c6ae99SBarry Smith 
90747c6ae99SBarry Smith #undef __FUNCT__
908950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
909b412c318SBarry Smith /*@
910950540a4SJed Brown     DMCreateMatrix - Gets empty Jacobian for a DMDA or DMComposite
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Collective on DM
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith     Input Parameter:
915b412c318SBarry Smith .   dm - the DM object
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Output Parameter:
91847c6ae99SBarry Smith .   mat - the empty Jacobian
91947c6ae99SBarry Smith 
920073dac72SJed Brown     Level: beginner
92147c6ae99SBarry Smith 
92294013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
92394013140SBarry Smith        do not need to do it yourself.
92494013140SBarry Smith 
92594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
926aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
92794013140SBarry Smith 
92894013140SBarry 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
92994013140SBarry Smith        internally by PETSc.
93094013140SBarry Smith 
93194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
932aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
93394013140SBarry Smith 
934b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
93547c6ae99SBarry Smith 
936aab9d709SJed Brown @*/
937b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
93847c6ae99SBarry Smith {
93947c6ae99SBarry Smith   PetscErrorCode ierr;
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith   PetscFunctionBegin;
942171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
943607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
944c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
945c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
946b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
94747c6ae99SBarry Smith   PetscFunctionReturn(0);
94847c6ae99SBarry Smith }
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith #undef __FUNCT__
951732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
952732e2eb9SMatthew G Knepley /*@
953950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
954732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
955732e2eb9SMatthew G Knepley 
956732e2eb9SMatthew G Knepley   Logically Collective on DMDA
957732e2eb9SMatthew G Knepley 
958732e2eb9SMatthew G Knepley   Input Parameter:
959732e2eb9SMatthew G Knepley + dm - the DM
960732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
961732e2eb9SMatthew G Knepley 
962732e2eb9SMatthew G Knepley   Level: developer
963950540a4SJed Brown .seealso DMCreateMatrix()
964732e2eb9SMatthew G Knepley @*/
965732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
966732e2eb9SMatthew G Knepley {
967732e2eb9SMatthew G Knepley   PetscFunctionBegin;
968732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
969732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
970732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
971732e2eb9SMatthew G Knepley }
972732e2eb9SMatthew G Knepley 
973732e2eb9SMatthew G Knepley #undef __FUNCT__
974a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
975a89ea682SMatthew G Knepley /*@C
976aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
977a89ea682SMatthew G Knepley 
978a89ea682SMatthew G Knepley   Not Collective
979a89ea682SMatthew G Knepley 
980a89ea682SMatthew G Knepley   Input Parameters:
981a89ea682SMatthew G Knepley + dm - the DM object
982aa1993deSMatthew G Knepley . count - The minium size
983aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
984a89ea682SMatthew G Knepley 
985a89ea682SMatthew G Knepley   Output Parameter:
986a89ea682SMatthew G Knepley . array - the work array
987a89ea682SMatthew G Knepley 
988a89ea682SMatthew G Knepley   Level: developer
989a89ea682SMatthew G Knepley 
990a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
991a89ea682SMatthew G Knepley @*/
992aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
993a89ea682SMatthew G Knepley {
994a89ea682SMatthew G Knepley   PetscErrorCode ierr;
995aa1993deSMatthew G Knepley   DMWorkLink     link;
996854ce69bSBarry Smith   size_t         dsize;
997a89ea682SMatthew G Knepley 
998a89ea682SMatthew G Knepley   PetscFunctionBegin;
999a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1000aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1001aa1993deSMatthew G Knepley   if (dm->workin) {
1002aa1993deSMatthew G Knepley     link       = dm->workin;
1003aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1004aa1993deSMatthew G Knepley   } else {
1005b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1006a89ea682SMatthew G Knepley   }
1007854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1008854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1009aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1010854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1011854ce69bSBarry Smith     link->bytes = dsize*count;
1012aa1993deSMatthew G Knepley   }
1013aa1993deSMatthew G Knepley   link->next   = dm->workout;
1014aa1993deSMatthew G Knepley   dm->workout  = link;
1015aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1016a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1017a89ea682SMatthew G Knepley }
1018a89ea682SMatthew G Knepley 
1019aa1993deSMatthew G Knepley #undef __FUNCT__
1020aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1021aa1993deSMatthew G Knepley /*@C
1022aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1023aa1993deSMatthew G Knepley 
1024aa1993deSMatthew G Knepley   Not Collective
1025aa1993deSMatthew G Knepley 
1026aa1993deSMatthew G Knepley   Input Parameters:
1027aa1993deSMatthew G Knepley + dm - the DM object
1028aa1993deSMatthew G Knepley . count - The minium size
1029aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1030aa1993deSMatthew G Knepley 
1031aa1993deSMatthew G Knepley   Output Parameter:
1032aa1993deSMatthew G Knepley . array - the work array
1033aa1993deSMatthew G Knepley 
1034aa1993deSMatthew G Knepley   Level: developer
1035aa1993deSMatthew G Knepley 
1036aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1037aa1993deSMatthew G Knepley @*/
1038aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1039aa1993deSMatthew G Knepley {
1040aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1041aa1993deSMatthew G Knepley 
1042aa1993deSMatthew G Knepley   PetscFunctionBegin;
1043aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1044aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1045aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1046aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1047aa1993deSMatthew G Knepley       *p           = link->next;
1048aa1993deSMatthew G Knepley       link->next   = dm->workin;
1049aa1993deSMatthew G Knepley       dm->workin   = link;
10500298fd71SBarry Smith       *(void**)mem = NULL;
1051aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1052aa1993deSMatthew G Knepley     }
1053aa1993deSMatthew G Knepley   }
1054aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1055aa1993deSMatthew G Knepley   PetscFunctionReturn(0);
1056aa1993deSMatthew G Knepley }
1057e7c4fc90SDmitry Karpeev 
1058e7c4fc90SDmitry Karpeev #undef __FUNCT__
1059435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1060435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1061435a35e8SMatthew G Knepley {
1062435a35e8SMatthew G Knepley   PetscFunctionBegin;
1063435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
106482f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1065435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1066435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1067435a35e8SMatthew G Knepley }
1068435a35e8SMatthew G Knepley 
1069435a35e8SMatthew G Knepley #undef __FUNCT__
10704d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
10714f3b5142SJed Brown /*@C
10724d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
10734d343eeaSMatthew G Knepley 
10744d343eeaSMatthew G Knepley   Not collective
10754d343eeaSMatthew G Knepley 
10764d343eeaSMatthew G Knepley   Input Parameter:
10774d343eeaSMatthew G Knepley . dm - the DM object
10784d343eeaSMatthew G Knepley 
10794d343eeaSMatthew G Knepley   Output Parameters:
10800298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
10810298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
10820298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
10834d343eeaSMatthew G Knepley 
10844d343eeaSMatthew G Knepley   Level: intermediate
10854d343eeaSMatthew G Knepley 
108621c9b008SJed Brown   Notes:
108721c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
108821c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
108921c9b008SJed Brown   PetscFree().
109021c9b008SJed Brown 
10914d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
10924d343eeaSMatthew G Knepley @*/
109337d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
10944d343eeaSMatthew G Knepley {
109537d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
10964d343eeaSMatthew G Knepley   PetscErrorCode ierr;
10974d343eeaSMatthew G Knepley 
10984d343eeaSMatthew G Knepley   PetscFunctionBegin;
10994d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
110069ca1f37SDmitry Karpeev   if (numFields) {
110169ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
110269ca1f37SDmitry Karpeev     *numFields = 0;
110369ca1f37SDmitry Karpeev   }
110437d0c07bSMatthew G Knepley   if (fieldNames) {
110537d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11060298fd71SBarry Smith     *fieldNames = NULL;
110769ca1f37SDmitry Karpeev   }
110869ca1f37SDmitry Karpeev   if (fields) {
110969ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11100298fd71SBarry Smith     *fields = NULL;
111169ca1f37SDmitry Karpeev   }
111237d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
111337d0c07bSMatthew G Knepley   if (section) {
111437d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
111537d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
111637d0c07bSMatthew G Knepley 
111737d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
111837d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1119dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
112037d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
112137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
112237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
112337d0c07bSMatthew G Knepley     }
112437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
112537d0c07bSMatthew G Knepley       PetscInt gdof;
112637d0c07bSMatthew G Knepley 
112737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
112837d0c07bSMatthew G Knepley       if (gdof > 0) {
112937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
113037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
113137d0c07bSMatthew G Knepley 
113237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
113337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
113437d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
113537d0c07bSMatthew G Knepley         }
113637d0c07bSMatthew G Knepley       }
113737d0c07bSMatthew G Knepley     }
113837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1139785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
114037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
114137d0c07bSMatthew G Knepley     }
114237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
114337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
114437d0c07bSMatthew G Knepley 
114537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
114637d0c07bSMatthew G Knepley       if (gdof > 0) {
114737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
114837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
114937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
115037d0c07bSMatthew G Knepley 
115137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
115237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
115337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
115437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
115537d0c07bSMatthew G Knepley           }
115637d0c07bSMatthew G Knepley         }
115737d0c07bSMatthew G Knepley       }
115837d0c07bSMatthew G Knepley     }
11598865f1eaSKarl Rupp     if (numFields) *numFields = nF;
116037d0c07bSMatthew G Knepley     if (fieldNames) {
1161785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
116237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
116337d0c07bSMatthew G Knepley         const char *fieldName;
116437d0c07bSMatthew G Knepley 
116537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
116637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
116737d0c07bSMatthew G Knepley       }
116837d0c07bSMatthew G Knepley     }
116937d0c07bSMatthew G Knepley     if (fields) {
1170785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
117137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
117282f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
117337d0c07bSMatthew G Knepley       }
117437d0c07bSMatthew G Knepley     }
117537d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
11768865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
11778865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
117869ca1f37SDmitry Karpeev   }
11794d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
11804d343eeaSMatthew G Knepley }
11814d343eeaSMatthew G Knepley 
118216621825SDmitry Karpeev 
118316621825SDmitry Karpeev #undef __FUNCT__
118416621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
118516621825SDmitry Karpeev /*@C
118616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
118716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
118816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1189e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1190e7c4fc90SDmitry Karpeev 
1191e7c4fc90SDmitry Karpeev   Not collective
1192e7c4fc90SDmitry Karpeev 
1193e7c4fc90SDmitry Karpeev   Input Parameter:
1194e7c4fc90SDmitry Karpeev . dm - the DM object
1195e7c4fc90SDmitry Karpeev 
1196e7c4fc90SDmitry Karpeev   Output Parameters:
11970298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
11980298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
11990298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12000298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1201e7c4fc90SDmitry Karpeev 
1202e7c4fc90SDmitry Karpeev   Level: intermediate
1203e7c4fc90SDmitry Karpeev 
1204e7c4fc90SDmitry Karpeev   Notes:
1205e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1206e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1207e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1208e7c4fc90SDmitry Karpeev 
1209e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1210e7c4fc90SDmitry Karpeev @*/
121116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1212e7c4fc90SDmitry Karpeev {
1213e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1214e7c4fc90SDmitry Karpeev 
1215e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1216e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12178865f1eaSKarl Rupp   if (len) {
12188865f1eaSKarl Rupp     PetscValidPointer(len,2);
12198865f1eaSKarl Rupp     *len = 0;
12208865f1eaSKarl Rupp   }
12218865f1eaSKarl Rupp   if (namelist) {
12228865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
12238865f1eaSKarl Rupp     *namelist = 0;
12248865f1eaSKarl Rupp   }
12258865f1eaSKarl Rupp   if (islist) {
12268865f1eaSKarl Rupp     PetscValidPointer(islist,4);
12278865f1eaSKarl Rupp     *islist = 0;
12288865f1eaSKarl Rupp   }
12298865f1eaSKarl Rupp   if (dmlist) {
12308865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
12318865f1eaSKarl Rupp     *dmlist = 0;
12328865f1eaSKarl Rupp   }
1233f3f0edfdSDmitry Karpeev   /*
1234f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1235f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1236f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1237f3f0edfdSDmitry Karpeev    */
1238ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
123916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1240435a35e8SMatthew G Knepley     PetscSection section;
1241435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1242435a35e8SMatthew G Knepley 
1243435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1244435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1245435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1246435a35e8SMatthew G Knepley       *len = numFields;
124703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
124803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
124903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1250435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1251435a35e8SMatthew G Knepley         const char *fieldName;
1252435a35e8SMatthew G Knepley 
125303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
125403dc3394SMatthew G. Knepley         if (namelist) {
1255435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1256435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1257435a35e8SMatthew G Knepley         }
125803dc3394SMatthew G. Knepley       }
1259435a35e8SMatthew G Knepley     } else {
126069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1261e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
12620298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1263e7c4fc90SDmitry Karpeev     }
12648865f1eaSKarl Rupp   } else {
126516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
126616621825SDmitry Karpeev   }
126716621825SDmitry Karpeev   PetscFunctionReturn(0);
126816621825SDmitry Karpeev }
126916621825SDmitry Karpeev 
127016621825SDmitry Karpeev #undef __FUNCT__
1271435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1272435a35e8SMatthew G Knepley /*@C
1273435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1274435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1275435a35e8SMatthew G Knepley 
1276435a35e8SMatthew G Knepley   Not collective
1277435a35e8SMatthew G Knepley 
1278435a35e8SMatthew G Knepley   Input Parameters:
1279435a35e8SMatthew G Knepley + dm - the DM object
1280435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
12810298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1282435a35e8SMatthew G Knepley 
1283435a35e8SMatthew G Knepley   Output Parameters:
1284435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1285435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1286435a35e8SMatthew G Knepley 
1287435a35e8SMatthew G Knepley   Level: intermediate
1288435a35e8SMatthew G Knepley 
1289435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1290435a35e8SMatthew G Knepley @*/
1291435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1292435a35e8SMatthew G Knepley {
1293435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1294435a35e8SMatthew G Knepley 
1295435a35e8SMatthew G Knepley   PetscFunctionBegin;
1296435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1297435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
12988865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
12998865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1300435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1301435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
130282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1303435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1304435a35e8SMatthew G Knepley }
1305435a35e8SMatthew G Knepley 
130616621825SDmitry Karpeev 
130716621825SDmitry Karpeev #undef __FUNCT__
130816621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
130916621825SDmitry Karpeev /*@C
13108d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13118d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13128d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13138d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13148d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
131516621825SDmitry Karpeev 
131616621825SDmitry Karpeev   Not collective
131716621825SDmitry Karpeev 
131816621825SDmitry Karpeev   Input Parameter:
131916621825SDmitry Karpeev . dm - the DM object
132016621825SDmitry Karpeev 
132116621825SDmitry Karpeev   Output Parameters:
13220298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
13230298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
13240298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
13250298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
13260298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
132716621825SDmitry Karpeev 
132816621825SDmitry Karpeev   Level: intermediate
132916621825SDmitry Karpeev 
133016621825SDmitry Karpeev   Notes:
133116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
133216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
133316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
133416621825SDmitry Karpeev 
13358d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
133616621825SDmitry Karpeev @*/
13378d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
133816621825SDmitry Karpeev {
133916621825SDmitry Karpeev   PetscErrorCode      ierr;
1340be081cd6SPeter Brune   DMSubDomainHookLink link;
1341be081cd6SPeter Brune   PetscInt            i,l;
134216621825SDmitry Karpeev 
134316621825SDmitry Karpeev   PetscFunctionBegin;
134416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
134514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
13460298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
13470298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
13480298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
13490298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1350f3f0edfdSDmitry Karpeev   /*
1351f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1352f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1353f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1354f3f0edfdSDmitry Karpeev    */
1355ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
135616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1357be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
135814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
135914a18fd3SPeter Brune     if (dmlist) {
1360be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1361be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1362be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1363be081cd6SPeter Brune         }
1364d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1365e7c4fc90SDmitry Karpeev       }
136614a18fd3SPeter Brune     }
136714a18fd3SPeter Brune     if (len) *len = l;
136814a18fd3SPeter Brune   }
1369e30e807fSPeter Brune   PetscFunctionReturn(0);
1370e30e807fSPeter Brune }
1371e30e807fSPeter Brune 
1372e30e807fSPeter Brune 
1373e30e807fSPeter Brune #undef __FUNCT__
1374e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1375e30e807fSPeter Brune /*@C
1376e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1377e30e807fSPeter Brune 
1378e30e807fSPeter Brune   Not collective
1379e30e807fSPeter Brune 
1380e30e807fSPeter Brune   Input Parameters:
1381e30e807fSPeter Brune + dm - the DM object
1382e30e807fSPeter Brune . n  - the number of subdomain scatters
1383e30e807fSPeter Brune - subdms - the local subdomains
1384e30e807fSPeter Brune 
1385e30e807fSPeter Brune   Output Parameters:
1386e30e807fSPeter Brune + n     - the number of scatters returned
1387e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1388e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1389e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1390e30e807fSPeter Brune 
1391e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1392e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1393e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1394e30e807fSPeter Brune   solution and residual data.
1395e30e807fSPeter Brune 
1396e30e807fSPeter Brune   Level: developer
1397e30e807fSPeter Brune 
1398e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1399e30e807fSPeter Brune @*/
1400e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1401e30e807fSPeter Brune {
1402e30e807fSPeter Brune   PetscErrorCode ierr;
1403e30e807fSPeter Brune 
1404e30e807fSPeter Brune   PetscFunctionBegin;
1405e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1406e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1407e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1408e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
140982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1410e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1411e7c4fc90SDmitry Karpeev }
1412e7c4fc90SDmitry Karpeev 
1413731c8d9eSDmitry Karpeev #undef __FUNCT__
141447c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
141547c6ae99SBarry Smith /*@
141647c6ae99SBarry Smith   DMRefine - Refines a DM object
141747c6ae99SBarry Smith 
141847c6ae99SBarry Smith   Collective on DM
141947c6ae99SBarry Smith 
142047c6ae99SBarry Smith   Input Parameter:
142147c6ae99SBarry Smith + dm   - the DM object
142291d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
142347c6ae99SBarry Smith 
142447c6ae99SBarry Smith   Output Parameter:
14250298fd71SBarry Smith . dmf - the refined DM, or NULL
1426ae0a1c52SMatthew G Knepley 
14270298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
142847c6ae99SBarry Smith 
142947c6ae99SBarry Smith   Level: developer
143047c6ae99SBarry Smith 
1431e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
143247c6ae99SBarry Smith @*/
14337087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
143447c6ae99SBarry Smith {
143547c6ae99SBarry Smith   PetscErrorCode   ierr;
1436c833c3b5SJed Brown   DMRefineHookLink link;
143747c6ae99SBarry Smith 
143847c6ae99SBarry Smith   PetscFunctionBegin;
1439732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
144047c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
14414057135bSMatthew G Knepley   if (*dmf) {
144243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
14438865f1eaSKarl Rupp 
14448cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
14458865f1eaSKarl Rupp 
1446644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
14470598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1448656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
14498865f1eaSKarl Rupp 
1450e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1451c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
14528865f1eaSKarl Rupp       if (link->refinehook) {
14538865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
14548865f1eaSKarl Rupp       }
1455c833c3b5SJed Brown     }
1456c833c3b5SJed Brown   }
1457c833c3b5SJed Brown   PetscFunctionReturn(0);
1458c833c3b5SJed Brown }
1459c833c3b5SJed Brown 
1460c833c3b5SJed Brown #undef __FUNCT__
1461c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1462bb9467b5SJed Brown /*@C
1463c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1464c833c3b5SJed Brown 
1465c833c3b5SJed Brown    Logically Collective
1466c833c3b5SJed Brown 
1467c833c3b5SJed Brown    Input Arguments:
1468c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1469c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1470c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
14710298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1472c833c3b5SJed Brown 
1473c833c3b5SJed Brown    Calling sequence of refinehook:
1474c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1475c833c3b5SJed Brown 
1476c833c3b5SJed Brown +  coarse - coarse level DM
1477c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1478c833c3b5SJed Brown -  ctx - optional user-defined function context
1479c833c3b5SJed Brown 
1480c833c3b5SJed Brown    Calling sequence for interphook:
1481c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1482c833c3b5SJed Brown 
1483c833c3b5SJed Brown +  coarse - coarse level DM
1484c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1485c833c3b5SJed Brown .  fine - fine level DM to update
1486c833c3b5SJed Brown -  ctx - optional user-defined function context
1487c833c3b5SJed Brown 
1488c833c3b5SJed Brown    Level: advanced
1489c833c3b5SJed Brown 
1490c833c3b5SJed Brown    Notes:
1491c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1492c833c3b5SJed Brown 
1493c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1494c833c3b5SJed Brown 
1495bb9467b5SJed Brown    This function is currently not available from Fortran.
1496bb9467b5SJed Brown 
1497c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1498c833c3b5SJed Brown @*/
1499c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1500c833c3b5SJed Brown {
1501c833c3b5SJed Brown   PetscErrorCode   ierr;
1502c833c3b5SJed Brown   DMRefineHookLink link,*p;
1503c833c3b5SJed Brown 
1504c833c3b5SJed Brown   PetscFunctionBegin;
1505c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1506c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1507c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1508c833c3b5SJed Brown   link->refinehook = refinehook;
1509c833c3b5SJed Brown   link->interphook = interphook;
1510c833c3b5SJed Brown   link->ctx        = ctx;
15110298fd71SBarry Smith   link->next       = NULL;
1512c833c3b5SJed Brown   *p               = link;
1513c833c3b5SJed Brown   PetscFunctionReturn(0);
1514c833c3b5SJed Brown }
1515c833c3b5SJed Brown 
1516c833c3b5SJed Brown #undef __FUNCT__
1517c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1518c833c3b5SJed Brown /*@
1519c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1520c833c3b5SJed Brown 
1521c833c3b5SJed Brown    Collective if any hooks are
1522c833c3b5SJed Brown 
1523c833c3b5SJed Brown    Input Arguments:
1524c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1525c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1526c833c3b5SJed Brown -  fine - finer DM to update
1527c833c3b5SJed Brown 
1528c833c3b5SJed Brown    Level: developer
1529c833c3b5SJed Brown 
1530c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1531c833c3b5SJed Brown @*/
1532c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1533c833c3b5SJed Brown {
1534c833c3b5SJed Brown   PetscErrorCode   ierr;
1535c833c3b5SJed Brown   DMRefineHookLink link;
1536c833c3b5SJed Brown 
1537c833c3b5SJed Brown   PetscFunctionBegin;
1538c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
15398865f1eaSKarl Rupp     if (link->interphook) {
15408865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
15418865f1eaSKarl Rupp     }
15424057135bSMatthew G Knepley   }
154347c6ae99SBarry Smith   PetscFunctionReturn(0);
154447c6ae99SBarry Smith }
154547c6ae99SBarry Smith 
154647c6ae99SBarry Smith #undef __FUNCT__
1547eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1548eb3f98d2SBarry Smith /*@
1549eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1550eb3f98d2SBarry Smith 
1551eb3f98d2SBarry Smith     Not Collective
1552eb3f98d2SBarry Smith 
1553eb3f98d2SBarry Smith     Input Parameter:
1554eb3f98d2SBarry Smith .   dm - the DM object
1555eb3f98d2SBarry Smith 
1556eb3f98d2SBarry Smith     Output Parameter:
1557eb3f98d2SBarry Smith .   level - number of refinements
1558eb3f98d2SBarry Smith 
1559eb3f98d2SBarry Smith     Level: developer
1560eb3f98d2SBarry Smith 
15616a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1562eb3f98d2SBarry Smith 
1563eb3f98d2SBarry Smith @*/
1564eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1565eb3f98d2SBarry Smith {
1566eb3f98d2SBarry Smith   PetscFunctionBegin;
1567eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1568eb3f98d2SBarry Smith   *level = dm->levelup;
1569eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1570eb3f98d2SBarry Smith }
1571eb3f98d2SBarry Smith 
1572eb3f98d2SBarry Smith #undef __FUNCT__
1573baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1574bb9467b5SJed Brown /*@C
1575baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1576baf369e7SPeter Brune 
1577baf369e7SPeter Brune    Logically Collective
1578baf369e7SPeter Brune 
1579baf369e7SPeter Brune    Input Arguments:
1580baf369e7SPeter Brune +  dm - the DM
1581baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1582baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
15830298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1584baf369e7SPeter Brune 
1585baf369e7SPeter Brune    Calling sequence for beginhook:
1586baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1587baf369e7SPeter Brune 
1588baf369e7SPeter Brune +  dm - global DM
1589baf369e7SPeter Brune .  g - global vector
1590baf369e7SPeter Brune .  mode - mode
1591baf369e7SPeter Brune .  l - local vector
1592baf369e7SPeter Brune -  ctx - optional user-defined function context
1593baf369e7SPeter Brune 
1594baf369e7SPeter Brune 
1595baf369e7SPeter Brune    Calling sequence for endhook:
1596ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1597baf369e7SPeter Brune 
1598baf369e7SPeter Brune +  global - global DM
1599baf369e7SPeter Brune -  ctx - optional user-defined function context
1600baf369e7SPeter Brune 
1601baf369e7SPeter Brune    Level: advanced
1602baf369e7SPeter Brune 
1603baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1604baf369e7SPeter Brune @*/
1605baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1606baf369e7SPeter Brune {
1607baf369e7SPeter Brune   PetscErrorCode          ierr;
1608baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1609baf369e7SPeter Brune 
1610baf369e7SPeter Brune   PetscFunctionBegin;
1611baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1612baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1613baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1614baf369e7SPeter Brune   link->beginhook = beginhook;
1615baf369e7SPeter Brune   link->endhook   = endhook;
1616baf369e7SPeter Brune   link->ctx       = ctx;
16170298fd71SBarry Smith   link->next      = NULL;
1618baf369e7SPeter Brune   *p              = link;
1619baf369e7SPeter Brune   PetscFunctionReturn(0);
1620baf369e7SPeter Brune }
1621baf369e7SPeter Brune 
1622baf369e7SPeter Brune #undef __FUNCT__
16234c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
16244c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
16254c274da1SToby Isaac {
16264c274da1SToby Isaac   Mat cMat;
16274c274da1SToby Isaac   Vec cVec;
16284c274da1SToby Isaac   PetscSection section, cSec;
16294c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
16304c274da1SToby Isaac   PetscErrorCode ierr;
16314c274da1SToby Isaac 
16324c274da1SToby Isaac   PetscFunctionBegin;
16334c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
16344c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
16354c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
16364c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
16377711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
16384c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
16394c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
16404c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
16414c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
16424c274da1SToby Isaac       if (dof) {
16434c274da1SToby Isaac         PetscScalar *vals;
16444c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
16454c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
16464c274da1SToby Isaac       }
16474c274da1SToby Isaac     }
16484c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
16494c274da1SToby Isaac   }
16504c274da1SToby Isaac   PetscFunctionReturn(0);
16514c274da1SToby Isaac }
16524c274da1SToby Isaac 
16534c274da1SToby Isaac #undef __FUNCT__
165447c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
165547c6ae99SBarry Smith /*@
165647c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
165747c6ae99SBarry Smith 
165847c6ae99SBarry Smith     Neighbor-wise Collective on DM
165947c6ae99SBarry Smith 
166047c6ae99SBarry Smith     Input Parameters:
166147c6ae99SBarry Smith +   dm - the DM object
166247c6ae99SBarry Smith .   g - the global vector
166347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
166447c6ae99SBarry Smith -   l - the local vector
166547c6ae99SBarry Smith 
166647c6ae99SBarry Smith 
166747c6ae99SBarry Smith     Level: beginner
166847c6ae99SBarry Smith 
1669e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
167047c6ae99SBarry Smith 
167147c6ae99SBarry Smith @*/
16727087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
167347c6ae99SBarry Smith {
16747128ae9fSMatthew G Knepley   PetscSF                 sf;
167547c6ae99SBarry Smith   PetscErrorCode          ierr;
1676baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
167747c6ae99SBarry Smith 
167847c6ae99SBarry Smith   PetscFunctionBegin;
1679171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1680baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
16818865f1eaSKarl Rupp     if (link->beginhook) {
16828865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
16838865f1eaSKarl Rupp     }
1684baf369e7SPeter Brune   }
16857128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
16867128ae9fSMatthew G Knepley   if (sf) {
1687ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1688ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
16897128ae9fSMatthew G Knepley 
169082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
16917128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1692ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
16937128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
16947128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1695ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
16967128ae9fSMatthew G Knepley   } else {
1697843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
16987128ae9fSMatthew G Knepley   }
169947c6ae99SBarry Smith   PetscFunctionReturn(0);
170047c6ae99SBarry Smith }
170147c6ae99SBarry Smith 
170247c6ae99SBarry Smith #undef __FUNCT__
170347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
170447c6ae99SBarry Smith /*@
170547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
170647c6ae99SBarry Smith 
170747c6ae99SBarry Smith     Neighbor-wise Collective on DM
170847c6ae99SBarry Smith 
170947c6ae99SBarry Smith     Input Parameters:
171047c6ae99SBarry Smith +   dm - the DM object
171147c6ae99SBarry Smith .   g - the global vector
171247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
171347c6ae99SBarry Smith -   l - the local vector
171447c6ae99SBarry Smith 
171547c6ae99SBarry Smith 
171647c6ae99SBarry Smith     Level: beginner
171747c6ae99SBarry Smith 
1718e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
171947c6ae99SBarry Smith 
172047c6ae99SBarry Smith @*/
17217087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
172247c6ae99SBarry Smith {
17237128ae9fSMatthew G Knepley   PetscSF                 sf;
172447c6ae99SBarry Smith   PetscErrorCode          ierr;
1725ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1726ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1727baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
172847c6ae99SBarry Smith 
172947c6ae99SBarry Smith   PetscFunctionBegin;
1730171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17317128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17327128ae9fSMatthew G Knepley   if (sf) {
173382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17347128ae9fSMatthew G Knepley 
17357128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1736ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17377128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17387128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1739ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17407128ae9fSMatthew G Knepley   } else {
1741843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17427128ae9fSMatthew G Knepley   }
17434c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1744baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1745baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1746baf369e7SPeter Brune   }
174747c6ae99SBarry Smith   PetscFunctionReturn(0);
174847c6ae99SBarry Smith }
174947c6ae99SBarry Smith 
175047c6ae99SBarry Smith #undef __FUNCT__
1751d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1752d4d07f1eSToby Isaac /*@C
1753d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1754d4d07f1eSToby Isaac 
1755d4d07f1eSToby Isaac    Logically Collective
1756d4d07f1eSToby Isaac 
1757d4d07f1eSToby Isaac    Input Arguments:
1758d4d07f1eSToby Isaac +  dm - the DM
1759d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1760d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1761d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1762d4d07f1eSToby Isaac 
1763d4d07f1eSToby Isaac    Calling sequence for beginhook:
1764d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1765d4d07f1eSToby Isaac 
1766d4d07f1eSToby Isaac +  dm - global DM
1767d4d07f1eSToby Isaac .  l - local vector
1768d4d07f1eSToby Isaac .  mode - mode
1769d4d07f1eSToby Isaac .  g - global vector
1770d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1771d4d07f1eSToby Isaac 
1772d4d07f1eSToby Isaac 
1773d4d07f1eSToby Isaac    Calling sequence for endhook:
1774d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1775d4d07f1eSToby Isaac 
1776d4d07f1eSToby Isaac +  global - global DM
1777d4d07f1eSToby Isaac .  l - local vector
1778d4d07f1eSToby Isaac .  mode - mode
1779d4d07f1eSToby Isaac .  g - global vector
1780d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1781d4d07f1eSToby Isaac 
1782d4d07f1eSToby Isaac    Level: advanced
1783d4d07f1eSToby Isaac 
1784d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1785d4d07f1eSToby Isaac @*/
1786d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1787d4d07f1eSToby Isaac {
1788d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1789d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1790d4d07f1eSToby Isaac 
1791d4d07f1eSToby Isaac   PetscFunctionBegin;
1792d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1793d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1794d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1795d4d07f1eSToby Isaac   link->beginhook = beginhook;
1796d4d07f1eSToby Isaac   link->endhook   = endhook;
1797d4d07f1eSToby Isaac   link->ctx       = ctx;
1798d4d07f1eSToby Isaac   link->next      = NULL;
1799d4d07f1eSToby Isaac   *p              = link;
1800d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1801d4d07f1eSToby Isaac }
1802d4d07f1eSToby Isaac 
1803d4d07f1eSToby Isaac #undef __FUNCT__
18044c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
18054c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
18064c274da1SToby Isaac {
18074c274da1SToby Isaac   Mat cMat;
18084c274da1SToby Isaac   Vec cVec;
18094c274da1SToby Isaac   PetscSection section, cSec;
18104c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18114c274da1SToby Isaac   PetscErrorCode ierr;
18124c274da1SToby Isaac 
18134c274da1SToby Isaac   PetscFunctionBegin;
18144c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18154c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18164c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
18174c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18187711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18194c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18204c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
18214c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
18224c274da1SToby Isaac       if (dof) {
18234c274da1SToby Isaac         PetscInt d;
18244c274da1SToby Isaac         PetscScalar *vals;
18254c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
18264c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
18274c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
18284c274da1SToby Isaac          * we just extracted */
18294c274da1SToby Isaac         for (d = 0; d < dof; d++) {
18304c274da1SToby Isaac           vals[d] = 0.;
18314c274da1SToby Isaac         }
18324c274da1SToby Isaac       }
18334c274da1SToby Isaac     }
18344c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
18354c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
18364c274da1SToby Isaac   }
18374c274da1SToby Isaac   PetscFunctionReturn(0);
18384c274da1SToby Isaac }
18394c274da1SToby Isaac 
18404c274da1SToby Isaac #undef __FUNCT__
18419a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
184247c6ae99SBarry Smith /*@
18439a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
18449a42bb27SBarry Smith 
18459a42bb27SBarry Smith     Neighbor-wise Collective on DM
18469a42bb27SBarry Smith 
18479a42bb27SBarry Smith     Input Parameters:
18489a42bb27SBarry Smith +   dm - the DM object
1849f6813fd5SJed Brown .   l - the local vector
18501eb28f2eSBarry 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 base point.
18511eb28f2eSBarry Smith -   g - the global vector
18529a42bb27SBarry Smith 
1853d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
185484330215SMatthew G. Knepley            INSERT_VALUES is not supported for DMDA, in that case simply compute the values directly into a global vector instead of a local one.
18559a42bb27SBarry Smith 
18569a42bb27SBarry Smith     Level: beginner
18579a42bb27SBarry Smith 
1858e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
18599a42bb27SBarry Smith 
18609a42bb27SBarry Smith @*/
18617087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
18629a42bb27SBarry Smith {
18637128ae9fSMatthew G Knepley   PetscSF                 sf;
186484330215SMatthew G. Knepley   PetscSection            s, gs;
1865d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
1866ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
1867ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
186884330215SMatthew G. Knepley   PetscBool               isInsert;
186984330215SMatthew G. Knepley   PetscErrorCode          ierr;
18709a42bb27SBarry Smith 
18719a42bb27SBarry Smith   PetscFunctionBegin;
1872171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1873d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1874d4d07f1eSToby Isaac     if (link->beginhook) {
1875d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1876d4d07f1eSToby Isaac     }
1877d4d07f1eSToby Isaac   }
18784c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
18797128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
188084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
18817128ae9fSMatthew G Knepley   switch (mode) {
18827128ae9fSMatthew G Knepley   case INSERT_VALUES:
18837128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
188484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
18857128ae9fSMatthew G Knepley   case ADD_VALUES:
18867128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
188784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
18887128ae9fSMatthew G Knepley   default:
188982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18907128ae9fSMatthew G Knepley   }
189184330215SMatthew G. Knepley   if (sf && !isInsert) {
1892ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
18937128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
189484330215SMatthew G. Knepley     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPI_SUM);CHKERRQ(ierr);
1895ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
189684330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
189784330215SMatthew G. Knepley   } else if (s && isInsert) {
189884330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
189984330215SMatthew G. Knepley 
190084330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
190184330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
190284330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
1903ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
190484330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
190584330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
1906b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
190784330215SMatthew G. Knepley 
190884330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
190903442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
191084330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
1911b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
191284330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
191384330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
1914b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
191503442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
1916b3b16f48SMatthew G. Knepley       if (dof != gdof) SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
1917b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
1918b3b16f48SMatthew G. Knepley       if (!gcdof) {
191984330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
1920b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
1921b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
192284330215SMatthew G. Knepley         const PetscInt *cdofs;
192384330215SMatthew G. Knepley         PetscInt        cind = 0;
192484330215SMatthew G. Knepley 
192584330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
1926b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
192784330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
1928b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
192984330215SMatthew G. Knepley         }
1930b3b16f48SMatthew G. Knepley       } else SETERRQ5(PETSC_COMM_SELF, PETSC_ERR_ARG_SIZ, "Inconsistent sizes, p: %d dof: %d gdof: %d cdof: %d gcdof: %d", p, dof, gdof, cdof, gcdof);
193184330215SMatthew G. Knepley     }
1932ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
19337128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
19347128ae9fSMatthew G Knepley   } else {
1935843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
19367128ae9fSMatthew G Knepley   }
19379a42bb27SBarry Smith   PetscFunctionReturn(0);
19389a42bb27SBarry Smith }
19399a42bb27SBarry Smith 
19409a42bb27SBarry Smith #undef __FUNCT__
19419a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
19429a42bb27SBarry Smith /*@
19439a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
194447c6ae99SBarry Smith 
194547c6ae99SBarry Smith     Neighbor-wise Collective on DM
194647c6ae99SBarry Smith 
194747c6ae99SBarry Smith     Input Parameters:
194847c6ae99SBarry Smith +   dm - the DM object
1949f6813fd5SJed Brown .   l - the local vector
195047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
1951f6813fd5SJed Brown -   g - the global vector
195247c6ae99SBarry Smith 
195347c6ae99SBarry Smith 
195447c6ae99SBarry Smith     Level: beginner
195547c6ae99SBarry Smith 
1956e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
195747c6ae99SBarry Smith 
195847c6ae99SBarry Smith @*/
19597087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
196047c6ae99SBarry Smith {
19617128ae9fSMatthew G Knepley   PetscSF                 sf;
196284330215SMatthew G. Knepley   PetscSection            s;
1963d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
196484330215SMatthew G. Knepley   PetscBool               isInsert;
196584330215SMatthew G. Knepley   PetscErrorCode          ierr;
196647c6ae99SBarry Smith 
196747c6ae99SBarry Smith   PetscFunctionBegin;
1968171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
19697128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
197084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19717128ae9fSMatthew G Knepley   switch (mode) {
19727128ae9fSMatthew G Knepley   case INSERT_VALUES:
19737128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
197484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
19757128ae9fSMatthew G Knepley   case ADD_VALUES:
19767128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
197784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
19787128ae9fSMatthew G Knepley   default:
197982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19807128ae9fSMatthew G Knepley   }
198184330215SMatthew G. Knepley   if (sf && !isInsert) {
1982ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
1983ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
198484330215SMatthew G. Knepley 
1985ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19867128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
198784330215SMatthew G. Knepley     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPI_SUM);CHKERRQ(ierr);
1988ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
19897128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
199084330215SMatthew G. Knepley   } else if (s && isInsert) {
19917128ae9fSMatthew G Knepley   } else {
1992843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
19937128ae9fSMatthew G Knepley   }
1994d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1995d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1996d4d07f1eSToby Isaac   }
199747c6ae99SBarry Smith   PetscFunctionReturn(0);
199847c6ae99SBarry Smith }
199947c6ae99SBarry Smith 
200047c6ae99SBarry Smith #undef __FUNCT__
2001f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2002f089877aSRichard Tran Mills /*@
2003bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2004bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2005d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2006f089877aSRichard Tran Mills 
2007bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2008f089877aSRichard Tran Mills 
2009f089877aSRichard Tran Mills    Input Parameters:
2010f089877aSRichard Tran Mills +  dm - the DM object
2011bc0a1609SRichard Tran Mills .  g - the original local vector
2012bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2013f089877aSRichard Tran Mills 
2014bc0a1609SRichard Tran Mills    Output Parameter:
2015bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2016f089877aSRichard Tran Mills 
2017f089877aSRichard Tran Mills    Level: intermediate
2018f089877aSRichard Tran Mills 
2019bc0a1609SRichard Tran Mills    Notes:
2020bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2021bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2022bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2023bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2024bc0a1609SRichard Tran Mills 
2025bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2026bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2027f089877aSRichard Tran Mills 
2028f089877aSRichard Tran Mills @*/
2029f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2030f089877aSRichard Tran Mills {
2031f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2032f089877aSRichard Tran Mills 
2033f089877aSRichard Tran Mills   PetscFunctionBegin;
2034f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2035f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2036f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2037f089877aSRichard Tran Mills }
2038f089877aSRichard Tran Mills 
2039f089877aSRichard Tran Mills #undef __FUNCT__
2040f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2041f089877aSRichard Tran Mills /*@
2042bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2043bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2044d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2045f089877aSRichard Tran Mills 
2046bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2047f089877aSRichard Tran Mills 
2048f089877aSRichard Tran Mills    Input Parameters:
2049bc0a1609SRichard Tran Mills +  da - the DM object
2050bc0a1609SRichard Tran Mills .  g - the original local vector
2051bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2052f089877aSRichard Tran Mills 
2053bc0a1609SRichard Tran Mills    Output Parameter:
2054bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2055f089877aSRichard Tran Mills 
2056f089877aSRichard Tran Mills    Level: intermediate
2057f089877aSRichard Tran Mills 
2058bc0a1609SRichard Tran Mills    Notes:
2059bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2060bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2061bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2062bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2063bc0a1609SRichard Tran Mills 
2064bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2065bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2066f089877aSRichard Tran Mills 
2067f089877aSRichard Tran Mills @*/
2068f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2069f089877aSRichard Tran Mills {
2070f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2071f089877aSRichard Tran Mills 
2072f089877aSRichard Tran Mills   PetscFunctionBegin;
2073f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2074f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2075f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2076f089877aSRichard Tran Mills }
2077f089877aSRichard Tran Mills 
2078f089877aSRichard Tran Mills 
2079f089877aSRichard Tran Mills #undef __FUNCT__
208047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
208147c6ae99SBarry Smith /*@
208247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
208347c6ae99SBarry Smith 
208447c6ae99SBarry Smith     Collective on DM
208547c6ae99SBarry Smith 
208647c6ae99SBarry Smith     Input Parameter:
208747c6ae99SBarry Smith +   dm - the DM object
208891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith     Output Parameter:
209147c6ae99SBarry Smith .   dmc - the coarsened DM
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith     Level: developer
209447c6ae99SBarry Smith 
2095e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
209647c6ae99SBarry Smith 
209747c6ae99SBarry Smith @*/
20987087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
209947c6ae99SBarry Smith {
210047c6ae99SBarry Smith   PetscErrorCode    ierr;
2101b17ce1afSJed Brown   DMCoarsenHookLink link;
210247c6ae99SBarry Smith 
210347c6ae99SBarry Smith   PetscFunctionBegin;
2104171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
210547c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2106*8892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
210743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
21088cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2109644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
21100598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2111656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2112e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2113b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2114b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2115b17ce1afSJed Brown   }
2116b17ce1afSJed Brown   PetscFunctionReturn(0);
2117b17ce1afSJed Brown }
2118b17ce1afSJed Brown 
2119b17ce1afSJed Brown #undef __FUNCT__
2120b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2121bb9467b5SJed Brown /*@C
2122b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2123b17ce1afSJed Brown 
2124b17ce1afSJed Brown    Logically Collective
2125b17ce1afSJed Brown 
2126b17ce1afSJed Brown    Input Arguments:
2127b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2128b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2129b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
21300298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2131b17ce1afSJed Brown 
2132b17ce1afSJed Brown    Calling sequence of coarsenhook:
2133b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2134b17ce1afSJed Brown 
2135b17ce1afSJed Brown +  fine - fine level DM
2136b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2137b17ce1afSJed Brown -  ctx - optional user-defined function context
2138b17ce1afSJed Brown 
2139b17ce1afSJed Brown    Calling sequence for restricthook:
2140c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2141b17ce1afSJed Brown 
2142b17ce1afSJed Brown +  fine - fine level DM
2143b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2144c833c3b5SJed Brown .  rscale - scaling vector for restriction
2145c833c3b5SJed Brown .  inject - matrix restricting by injection
2146b17ce1afSJed Brown .  coarse - coarse level DM to update
2147b17ce1afSJed Brown -  ctx - optional user-defined function context
2148b17ce1afSJed Brown 
2149b17ce1afSJed Brown    Level: advanced
2150b17ce1afSJed Brown 
2151b17ce1afSJed Brown    Notes:
2152b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2153b17ce1afSJed Brown 
2154b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2155b17ce1afSJed Brown 
2156b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2157b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2158b17ce1afSJed Brown 
2159bb9467b5SJed Brown    This function is currently not available from Fortran.
2160bb9467b5SJed Brown 
2161c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2162b17ce1afSJed Brown @*/
2163b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2164b17ce1afSJed Brown {
2165b17ce1afSJed Brown   PetscErrorCode    ierr;
2166b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2167b17ce1afSJed Brown 
2168b17ce1afSJed Brown   PetscFunctionBegin;
2169b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
21706bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2171b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2172b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2173b17ce1afSJed Brown   link->restricthook = restricthook;
2174b17ce1afSJed Brown   link->ctx          = ctx;
21750298fd71SBarry Smith   link->next         = NULL;
2176b17ce1afSJed Brown   *p                 = link;
2177b17ce1afSJed Brown   PetscFunctionReturn(0);
2178b17ce1afSJed Brown }
2179b17ce1afSJed Brown 
2180b17ce1afSJed Brown #undef __FUNCT__
2181b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2182b17ce1afSJed Brown /*@
2183b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2184b17ce1afSJed Brown 
2185b17ce1afSJed Brown    Collective if any hooks are
2186b17ce1afSJed Brown 
2187b17ce1afSJed Brown    Input Arguments:
2188b17ce1afSJed Brown +  fine - finer DM to use as a base
2189b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2190b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2191b17ce1afSJed Brown -  coarse - coarer DM to update
2192b17ce1afSJed Brown 
2193b17ce1afSJed Brown    Level: developer
2194b17ce1afSJed Brown 
2195b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2196b17ce1afSJed Brown @*/
2197b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2198b17ce1afSJed Brown {
2199b17ce1afSJed Brown   PetscErrorCode    ierr;
2200b17ce1afSJed Brown   DMCoarsenHookLink link;
2201b17ce1afSJed Brown 
2202b17ce1afSJed Brown   PetscFunctionBegin;
2203b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
22048865f1eaSKarl Rupp     if (link->restricthook) {
22058865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
22068865f1eaSKarl Rupp     }
2207b17ce1afSJed Brown   }
220847c6ae99SBarry Smith   PetscFunctionReturn(0);
220947c6ae99SBarry Smith }
221047c6ae99SBarry Smith 
221147c6ae99SBarry Smith #undef __FUNCT__
2212be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2213bb9467b5SJed Brown /*@C
2214be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
22155dbd56e3SPeter Brune 
22165dbd56e3SPeter Brune    Logically Collective
22175dbd56e3SPeter Brune 
22185dbd56e3SPeter Brune    Input Arguments:
22195dbd56e3SPeter Brune +  global - global DM
2220ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
22215dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
22220298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
22235dbd56e3SPeter Brune 
2224ec4806b8SPeter Brune 
2225ec4806b8SPeter Brune    Calling sequence for ddhook:
2226ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2227ec4806b8SPeter Brune 
2228ec4806b8SPeter Brune +  global - global DM
2229ec4806b8SPeter Brune .  block  - block DM
2230ec4806b8SPeter Brune -  ctx - optional user-defined function context
2231ec4806b8SPeter Brune 
22325dbd56e3SPeter Brune    Calling sequence for restricthook:
2233ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
22345dbd56e3SPeter Brune 
22355dbd56e3SPeter Brune +  global - global DM
22365dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
22375dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2238ec4806b8SPeter Brune .  block  - block DM
22395dbd56e3SPeter Brune -  ctx - optional user-defined function context
22405dbd56e3SPeter Brune 
22415dbd56e3SPeter Brune    Level: advanced
22425dbd56e3SPeter Brune 
22435dbd56e3SPeter Brune    Notes:
2244ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
22455dbd56e3SPeter Brune 
22465dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
22475dbd56e3SPeter Brune 
22485dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2249ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
22505dbd56e3SPeter Brune 
2251bb9467b5SJed Brown    This function is currently not available from Fortran.
2252bb9467b5SJed Brown 
22535dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
22545dbd56e3SPeter Brune @*/
2255be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
22565dbd56e3SPeter Brune {
22575dbd56e3SPeter Brune   PetscErrorCode      ierr;
2258be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
22595dbd56e3SPeter Brune 
22605dbd56e3SPeter Brune   PetscFunctionBegin;
22615dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2262be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2263be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
22645dbd56e3SPeter Brune   link->restricthook = restricthook;
2265be081cd6SPeter Brune   link->ddhook       = ddhook;
22665dbd56e3SPeter Brune   link->ctx          = ctx;
22670298fd71SBarry Smith   link->next         = NULL;
22685dbd56e3SPeter Brune   *p                 = link;
22695dbd56e3SPeter Brune   PetscFunctionReturn(0);
22705dbd56e3SPeter Brune }
22715dbd56e3SPeter Brune 
22725dbd56e3SPeter Brune #undef __FUNCT__
2273be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
22745dbd56e3SPeter Brune /*@
2275be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
22765dbd56e3SPeter Brune 
22775dbd56e3SPeter Brune    Collective if any hooks are
22785dbd56e3SPeter Brune 
22795dbd56e3SPeter Brune    Input Arguments:
22805dbd56e3SPeter Brune +  fine - finer DM to use as a base
2281be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2282be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
22835dbd56e3SPeter Brune -  coarse - coarer DM to update
22845dbd56e3SPeter Brune 
22855dbd56e3SPeter Brune    Level: developer
22865dbd56e3SPeter Brune 
22875dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
22885dbd56e3SPeter Brune @*/
2289be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
22905dbd56e3SPeter Brune {
22915dbd56e3SPeter Brune   PetscErrorCode      ierr;
2292be081cd6SPeter Brune   DMSubDomainHookLink link;
22935dbd56e3SPeter Brune 
22945dbd56e3SPeter Brune   PetscFunctionBegin;
2295be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
22968865f1eaSKarl Rupp     if (link->restricthook) {
22978865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
22988865f1eaSKarl Rupp     }
22995dbd56e3SPeter Brune   }
23005dbd56e3SPeter Brune   PetscFunctionReturn(0);
23015dbd56e3SPeter Brune }
23025dbd56e3SPeter Brune 
23035dbd56e3SPeter Brune #undef __FUNCT__
23045fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
23055fe1f584SPeter Brune /*@
23066a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
23075fe1f584SPeter Brune 
23085fe1f584SPeter Brune     Not Collective
23095fe1f584SPeter Brune 
23105fe1f584SPeter Brune     Input Parameter:
23115fe1f584SPeter Brune .   dm - the DM object
23125fe1f584SPeter Brune 
23135fe1f584SPeter Brune     Output Parameter:
23146a7d9d85SPeter Brune .   level - number of coarsenings
23155fe1f584SPeter Brune 
23165fe1f584SPeter Brune     Level: developer
23175fe1f584SPeter Brune 
23186a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
23195fe1f584SPeter Brune 
23205fe1f584SPeter Brune @*/
23215fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
23225fe1f584SPeter Brune {
23235fe1f584SPeter Brune   PetscFunctionBegin;
23245fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23255fe1f584SPeter Brune   *level = dm->leveldown;
23265fe1f584SPeter Brune   PetscFunctionReturn(0);
23275fe1f584SPeter Brune }
23285fe1f584SPeter Brune 
23295fe1f584SPeter Brune 
23305fe1f584SPeter Brune 
23315fe1f584SPeter Brune #undef __FUNCT__
233247c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
233347c6ae99SBarry Smith /*@C
233447c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
233547c6ae99SBarry Smith 
233647c6ae99SBarry Smith     Collective on DM
233747c6ae99SBarry Smith 
233847c6ae99SBarry Smith     Input Parameter:
233947c6ae99SBarry Smith +   dm - the DM object
234047c6ae99SBarry Smith -   nlevels - the number of levels of refinement
234147c6ae99SBarry Smith 
234247c6ae99SBarry Smith     Output Parameter:
234347c6ae99SBarry Smith .   dmf - the refined DM hierarchy
234447c6ae99SBarry Smith 
234547c6ae99SBarry Smith     Level: developer
234647c6ae99SBarry Smith 
2347e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
234847c6ae99SBarry Smith 
234947c6ae99SBarry Smith @*/
23507087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
235147c6ae99SBarry Smith {
235247c6ae99SBarry Smith   PetscErrorCode ierr;
235347c6ae99SBarry Smith 
235447c6ae99SBarry Smith   PetscFunctionBegin;
2355171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2356ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
235747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
235847c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
235947c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
236047c6ae99SBarry Smith   } else if (dm->ops->refine) {
236147c6ae99SBarry Smith     PetscInt i;
236247c6ae99SBarry Smith 
2363ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
236447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2365ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
236647c6ae99SBarry Smith     }
2367ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
236847c6ae99SBarry Smith   PetscFunctionReturn(0);
236947c6ae99SBarry Smith }
237047c6ae99SBarry Smith 
237147c6ae99SBarry Smith #undef __FUNCT__
237247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
237347c6ae99SBarry Smith /*@C
237447c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
237547c6ae99SBarry Smith 
237647c6ae99SBarry Smith     Collective on DM
237747c6ae99SBarry Smith 
237847c6ae99SBarry Smith     Input Parameter:
237947c6ae99SBarry Smith +   dm - the DM object
238047c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
238147c6ae99SBarry Smith 
238247c6ae99SBarry Smith     Output Parameter:
238347c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
238447c6ae99SBarry Smith 
238547c6ae99SBarry Smith     Level: developer
238647c6ae99SBarry Smith 
2387e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
238847c6ae99SBarry Smith 
238947c6ae99SBarry Smith @*/
23907087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
239147c6ae99SBarry Smith {
239247c6ae99SBarry Smith   PetscErrorCode ierr;
239347c6ae99SBarry Smith 
239447c6ae99SBarry Smith   PetscFunctionBegin;
2395171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2396ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
239747c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
239847c6ae99SBarry Smith   PetscValidPointer(dmc,3);
239947c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
240047c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
240147c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
240247c6ae99SBarry Smith     PetscInt i;
240347c6ae99SBarry Smith 
2404ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
240547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2406ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
240747c6ae99SBarry Smith     }
2408ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
240947c6ae99SBarry Smith   PetscFunctionReturn(0);
241047c6ae99SBarry Smith }
241147c6ae99SBarry Smith 
241247c6ae99SBarry Smith #undef __FUNCT__
2413e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
241447c6ae99SBarry Smith /*@
2415e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
241647c6ae99SBarry Smith    grids associated with two DMs.
241747c6ae99SBarry Smith 
241847c6ae99SBarry Smith    Collective on DM
241947c6ae99SBarry Smith 
242047c6ae99SBarry Smith    Input Parameters:
242147c6ae99SBarry Smith +  dmc - the coarse grid DM
242247c6ae99SBarry Smith -  dmf - the fine grid DM
242347c6ae99SBarry Smith 
242447c6ae99SBarry Smith    Output Parameters:
242547c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
242647c6ae99SBarry Smith 
242747c6ae99SBarry Smith    Level: intermediate
242847c6ae99SBarry Smith 
242947c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
243047c6ae99SBarry Smith 
2431e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
243247c6ae99SBarry Smith @*/
2433e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
243447c6ae99SBarry Smith {
243547c6ae99SBarry Smith   PetscErrorCode ierr;
243647c6ae99SBarry Smith 
243747c6ae99SBarry Smith   PetscFunctionBegin;
2438171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2439171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
244047c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
244147c6ae99SBarry Smith   PetscFunctionReturn(0);
244247c6ae99SBarry Smith }
244347c6ae99SBarry Smith 
244447c6ae99SBarry Smith #undef __FUNCT__
24451a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
24461a266240SBarry Smith /*@C
24471a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
24481a266240SBarry Smith 
24491a266240SBarry Smith     Not Collective
24501a266240SBarry Smith 
24511a266240SBarry Smith     Input Parameters:
24521a266240SBarry Smith +   dm - the DM object
24531a266240SBarry Smith -   destroy - the destroy function
24541a266240SBarry Smith 
24551a266240SBarry Smith     Level: intermediate
24561a266240SBarry Smith 
2457e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
24581a266240SBarry Smith 
2459f07f9ceaSJed Brown @*/
24601a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
24611a266240SBarry Smith {
24621a266240SBarry Smith   PetscFunctionBegin;
2463171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24641a266240SBarry Smith   dm->ctxdestroy = destroy;
24651a266240SBarry Smith   PetscFunctionReturn(0);
24661a266240SBarry Smith }
24671a266240SBarry Smith 
24681a266240SBarry Smith #undef __FUNCT__
24691b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2470b07ff414SBarry Smith /*@
24711b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
247247c6ae99SBarry Smith 
247347c6ae99SBarry Smith     Not Collective
247447c6ae99SBarry Smith 
247547c6ae99SBarry Smith     Input Parameters:
247647c6ae99SBarry Smith +   dm - the DM object
247747c6ae99SBarry Smith -   ctx - the user context
247847c6ae99SBarry Smith 
247947c6ae99SBarry Smith     Level: intermediate
248047c6ae99SBarry Smith 
2481e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
248247c6ae99SBarry Smith 
248347c6ae99SBarry Smith @*/
24841b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
248547c6ae99SBarry Smith {
248647c6ae99SBarry Smith   PetscFunctionBegin;
2487171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
248847c6ae99SBarry Smith   dm->ctx = ctx;
248947c6ae99SBarry Smith   PetscFunctionReturn(0);
249047c6ae99SBarry Smith }
249147c6ae99SBarry Smith 
249247c6ae99SBarry Smith #undef __FUNCT__
24931b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
249447c6ae99SBarry Smith /*@
24951b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
249647c6ae99SBarry Smith 
249747c6ae99SBarry Smith     Not Collective
249847c6ae99SBarry Smith 
249947c6ae99SBarry Smith     Input Parameter:
250047c6ae99SBarry Smith .   dm - the DM object
250147c6ae99SBarry Smith 
250247c6ae99SBarry Smith     Output Parameter:
250347c6ae99SBarry Smith .   ctx - the user context
250447c6ae99SBarry Smith 
250547c6ae99SBarry Smith     Level: intermediate
250647c6ae99SBarry Smith 
2507e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
250847c6ae99SBarry Smith 
250947c6ae99SBarry Smith @*/
25101b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
251147c6ae99SBarry Smith {
251247c6ae99SBarry Smith   PetscFunctionBegin;
2513171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25141b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
251547c6ae99SBarry Smith   PetscFunctionReturn(0);
251647c6ae99SBarry Smith }
251747c6ae99SBarry Smith 
251847c6ae99SBarry Smith #undef __FUNCT__
251908da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
252008da532bSDmitry Karpeev /*@C
2521df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
252208da532bSDmitry Karpeev 
252308da532bSDmitry Karpeev     Logically Collective on DM
252408da532bSDmitry Karpeev 
252508da532bSDmitry Karpeev     Input Parameter:
252608da532bSDmitry Karpeev +   dm - the DM object
25270298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
252808da532bSDmitry Karpeev 
252908da532bSDmitry Karpeev     Level: intermediate
253008da532bSDmitry Karpeev 
2531835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
253208da532bSDmitry Karpeev          DMSetJacobian()
253308da532bSDmitry Karpeev 
253408da532bSDmitry Karpeev @*/
253508da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
253608da532bSDmitry Karpeev {
253708da532bSDmitry Karpeev   PetscFunctionBegin;
253808da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
253908da532bSDmitry Karpeev   PetscFunctionReturn(0);
254008da532bSDmitry Karpeev }
254108da532bSDmitry Karpeev 
254208da532bSDmitry Karpeev #undef __FUNCT__
254308da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
254408da532bSDmitry Karpeev /*@
254508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
254608da532bSDmitry Karpeev 
254708da532bSDmitry Karpeev     Not Collective
254808da532bSDmitry Karpeev 
254908da532bSDmitry Karpeev     Input Parameter:
255008da532bSDmitry Karpeev .   dm - the DM object to destroy
255108da532bSDmitry Karpeev 
255208da532bSDmitry Karpeev     Output Parameter:
255308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
255408da532bSDmitry Karpeev 
255508da532bSDmitry Karpeev     Level: developer
255608da532bSDmitry Karpeev 
255774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
255808da532bSDmitry Karpeev 
255908da532bSDmitry Karpeev @*/
256008da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
256108da532bSDmitry Karpeev {
256208da532bSDmitry Karpeev   PetscFunctionBegin;
256308da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
256408da532bSDmitry Karpeev   PetscFunctionReturn(0);
256508da532bSDmitry Karpeev }
256608da532bSDmitry Karpeev 
256708da532bSDmitry Karpeev #undef __FUNCT__
256808da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
256908da532bSDmitry Karpeev /*@C
257008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
257108da532bSDmitry Karpeev 
257208da532bSDmitry Karpeev     Logically Collective on DM
257308da532bSDmitry Karpeev 
257408da532bSDmitry Karpeev     Input Parameters:
2575907376e6SBarry Smith .   dm - the DM object
257608da532bSDmitry Karpeev 
257708da532bSDmitry Karpeev     Output parameters:
257808da532bSDmitry Karpeev +   xl - lower bound
257908da532bSDmitry Karpeev -   xu - upper bound
258008da532bSDmitry Karpeev 
2581907376e6SBarry Smith     Level: advanced
2582907376e6SBarry Smith 
2583907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
258408da532bSDmitry Karpeev 
258574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
258608da532bSDmitry Karpeev 
258708da532bSDmitry Karpeev @*/
258808da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
258908da532bSDmitry Karpeev {
259008da532bSDmitry Karpeev   PetscErrorCode ierr;
25915fd66863SKarl Rupp 
259208da532bSDmitry Karpeev   PetscFunctionBegin;
259308da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
259408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
259508da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
259608da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
25978865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
259808da532bSDmitry Karpeev   PetscFunctionReturn(0);
259908da532bSDmitry Karpeev }
260008da532bSDmitry Karpeev 
260108da532bSDmitry Karpeev #undef __FUNCT__
2602b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2603b0ae01b7SPeter Brune /*@
2604b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2605b0ae01b7SPeter Brune 
2606b0ae01b7SPeter Brune     Not Collective
2607b0ae01b7SPeter Brune 
2608b0ae01b7SPeter Brune     Input Parameter:
2609b0ae01b7SPeter Brune .   dm - the DM object
2610b0ae01b7SPeter Brune 
2611b0ae01b7SPeter Brune     Output Parameter:
2612b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2613b0ae01b7SPeter Brune 
2614b0ae01b7SPeter Brune     Level: developer
2615b0ae01b7SPeter Brune 
2616b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2617b0ae01b7SPeter Brune 
2618b0ae01b7SPeter Brune @*/
2619b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2620b0ae01b7SPeter Brune {
2621b0ae01b7SPeter Brune   PetscFunctionBegin;
2622b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2623b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2624b0ae01b7SPeter Brune }
2625b0ae01b7SPeter Brune 
2626b0ae01b7SPeter Brune #undef  __FUNCT__
262708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2628748fac09SDmitry Karpeev /*@C
262908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
263008da532bSDmitry Karpeev 
263108da532bSDmitry Karpeev     Collective on DM
263208da532bSDmitry Karpeev 
263308da532bSDmitry Karpeev     Input Parameter:
263408da532bSDmitry Karpeev +   dm - the DM object
26350298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
263608da532bSDmitry Karpeev 
263708da532bSDmitry Karpeev     Level: developer
263808da532bSDmitry Karpeev 
263974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
264008da532bSDmitry Karpeev 
264108da532bSDmitry Karpeev @*/
264208da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
264308da532bSDmitry Karpeev {
264408da532bSDmitry Karpeev   PetscErrorCode ierr;
26455fd66863SKarl Rupp 
264608da532bSDmitry Karpeev   PetscFunctionBegin;
264708da532bSDmitry Karpeev   if (x) {
264808da532bSDmitry Karpeev     if (!dm->x) {
264908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
265008da532bSDmitry Karpeev     }
265108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
26528865f1eaSKarl Rupp   } else if (dm->x) {
265308da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
265408da532bSDmitry Karpeev   }
265508da532bSDmitry Karpeev   PetscFunctionReturn(0);
265608da532bSDmitry Karpeev }
265708da532bSDmitry Karpeev 
26580298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2659264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2660264ace61SBarry Smith 
2661264ace61SBarry Smith #undef __FUNCT__
2662264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2663264ace61SBarry Smith /*@C
2664264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2665264ace61SBarry Smith 
2666264ace61SBarry Smith   Collective on DM
2667264ace61SBarry Smith 
2668264ace61SBarry Smith   Input Parameters:
2669264ace61SBarry Smith + dm     - The DM object
2670264ace61SBarry Smith - method - The name of the DM type
2671264ace61SBarry Smith 
2672264ace61SBarry Smith   Options Database Key:
2673264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2674264ace61SBarry Smith 
2675264ace61SBarry Smith   Notes:
2676e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2677264ace61SBarry Smith 
2678264ace61SBarry Smith   Level: intermediate
2679264ace61SBarry Smith 
2680264ace61SBarry Smith .keywords: DM, set, type
2681264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2682264ace61SBarry Smith @*/
268319fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2684264ace61SBarry Smith {
2685264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2686264ace61SBarry Smith   PetscBool      match;
2687264ace61SBarry Smith   PetscErrorCode ierr;
2688264ace61SBarry Smith 
2689264ace61SBarry Smith   PetscFunctionBegin;
2690264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2691251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2692264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2693264ace61SBarry Smith 
26940f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
26951c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2696ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2697264ace61SBarry Smith 
2698264ace61SBarry Smith   if (dm->ops->destroy) {
2699264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
27000298fd71SBarry Smith     dm->ops->destroy = NULL;
2701264ace61SBarry Smith   }
2702264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2703264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2704264ace61SBarry Smith   PetscFunctionReturn(0);
2705264ace61SBarry Smith }
2706264ace61SBarry Smith 
2707264ace61SBarry Smith #undef __FUNCT__
2708264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2709264ace61SBarry Smith /*@C
2710264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2711264ace61SBarry Smith 
2712264ace61SBarry Smith   Not Collective
2713264ace61SBarry Smith 
2714264ace61SBarry Smith   Input Parameter:
2715264ace61SBarry Smith . dm  - The DM
2716264ace61SBarry Smith 
2717264ace61SBarry Smith   Output Parameter:
2718264ace61SBarry Smith . type - The DM type name
2719264ace61SBarry Smith 
2720264ace61SBarry Smith   Level: intermediate
2721264ace61SBarry Smith 
2722264ace61SBarry Smith .keywords: DM, get, type, name
2723264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2724264ace61SBarry Smith @*/
272519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2726264ace61SBarry Smith {
2727264ace61SBarry Smith   PetscErrorCode ierr;
2728264ace61SBarry Smith 
2729264ace61SBarry Smith   PetscFunctionBegin;
2730264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2731c959eef4SJed Brown   PetscValidPointer(type,2);
2732607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2733264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2734264ace61SBarry Smith   PetscFunctionReturn(0);
2735264ace61SBarry Smith }
2736264ace61SBarry Smith 
273767a56275SMatthew G Knepley #undef __FUNCT__
273867a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
273967a56275SMatthew G Knepley /*@C
274067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
274167a56275SMatthew G Knepley 
274267a56275SMatthew G Knepley   Collective on DM
274367a56275SMatthew G Knepley 
274467a56275SMatthew G Knepley   Input Parameters:
274567a56275SMatthew G Knepley + dm - the DM
274667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
274767a56275SMatthew G Knepley 
274867a56275SMatthew G Knepley   Output Parameter:
274967a56275SMatthew G Knepley . M - pointer to new DM
275067a56275SMatthew G Knepley 
275167a56275SMatthew G Knepley   Notes:
275267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
275367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
275467a56275SMatthew G Knepley   of the input DM.
275567a56275SMatthew G Knepley 
275667a56275SMatthew G Knepley   Level: intermediate
275767a56275SMatthew G Knepley 
275867a56275SMatthew G Knepley .seealso: DMCreate()
275967a56275SMatthew G Knepley @*/
276019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
276167a56275SMatthew G Knepley {
276267a56275SMatthew G Knepley   DM             B;
276367a56275SMatthew G Knepley   char           convname[256];
276467a56275SMatthew G Knepley   PetscBool      sametype, issame;
276567a56275SMatthew G Knepley   PetscErrorCode ierr;
276667a56275SMatthew G Knepley 
276767a56275SMatthew G Knepley   PetscFunctionBegin;
276867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
276967a56275SMatthew G Knepley   PetscValidType(dm,1);
277067a56275SMatthew G Knepley   PetscValidPointer(M,3);
2771251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
277267a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
277367a56275SMatthew G Knepley   {
27740298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
277567a56275SMatthew G Knepley 
277667a56275SMatthew G Knepley     /*
277767a56275SMatthew G Knepley        Order of precedence:
277867a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
277967a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
278067a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
278167a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
278267a56275SMatthew G Knepley        5) Use a really basic converter.
278367a56275SMatthew G Knepley     */
278467a56275SMatthew G Knepley 
278567a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
278667a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
278767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
278867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
278967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
279067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
27910005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
279267a56275SMatthew G Knepley     if (conv) goto foundconv;
279367a56275SMatthew G Knepley 
279467a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
279582f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
279667a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
279767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
279867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
279967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
280067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
280167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28020005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
280367a56275SMatthew G Knepley     if (conv) {
2804fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
280567a56275SMatthew G Knepley       goto foundconv;
280667a56275SMatthew G Knepley     }
280767a56275SMatthew G Knepley 
280867a56275SMatthew G Knepley #if 0
280967a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
281067a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2811fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
281267a56275SMatthew G Knepley     if (conv) goto foundconv;
281367a56275SMatthew G Knepley 
281467a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
281567a56275SMatthew G Knepley     if (dm->ops->convert) {
281667a56275SMatthew G Knepley       conv = dm->ops->convert;
281767a56275SMatthew G Knepley     }
281867a56275SMatthew G Knepley     if (conv) goto foundconv;
281967a56275SMatthew G Knepley #endif
282067a56275SMatthew G Knepley 
282167a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
282282f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
282367a56275SMatthew G Knepley 
282467a56275SMatthew G Knepley foundconv:
282567a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
282667a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
282767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
282867a56275SMatthew G Knepley   }
282967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
283067a56275SMatthew G Knepley   PetscFunctionReturn(0);
283167a56275SMatthew G Knepley }
2832264ace61SBarry Smith 
2833264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2834264ace61SBarry Smith 
2835264ace61SBarry Smith #undef __FUNCT__
2836264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2837264ace61SBarry Smith /*@C
28381c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
28391c84c290SBarry Smith 
28401c84c290SBarry Smith   Not Collective
28411c84c290SBarry Smith 
28421c84c290SBarry Smith   Input Parameters:
28431c84c290SBarry Smith + name        - The name of a new user-defined creation routine
28441c84c290SBarry Smith - create_func - The creation routine itself
28451c84c290SBarry Smith 
28461c84c290SBarry Smith   Notes:
28471c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
28481c84c290SBarry Smith 
28491c84c290SBarry Smith 
28501c84c290SBarry Smith   Sample usage:
28511c84c290SBarry Smith .vb
2852bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
28531c84c290SBarry Smith .ve
28541c84c290SBarry Smith 
28551c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
28561c84c290SBarry Smith .vb
28571c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
28581c84c290SBarry Smith     DMSetType(DM,"my_da");
28591c84c290SBarry Smith .ve
28601c84c290SBarry Smith    or at runtime via the option
28611c84c290SBarry Smith .vb
28621c84c290SBarry Smith     -da_type my_da
28631c84c290SBarry Smith .ve
2864264ace61SBarry Smith 
2865264ace61SBarry Smith   Level: advanced
28661c84c290SBarry Smith 
28671c84c290SBarry Smith .keywords: DM, register
2868bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
28691c84c290SBarry Smith 
2870264ace61SBarry Smith @*/
2871bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2872264ace61SBarry Smith {
2873264ace61SBarry Smith   PetscErrorCode ierr;
2874264ace61SBarry Smith 
2875264ace61SBarry Smith   PetscFunctionBegin;
2876a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2877264ace61SBarry Smith   PetscFunctionReturn(0);
2878264ace61SBarry Smith }
2879264ace61SBarry Smith 
2880b859378eSBarry Smith #undef __FUNCT__
2881b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2882b859378eSBarry Smith /*@C
288355849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2884b859378eSBarry Smith 
2885b859378eSBarry Smith   Collective on PetscViewer
2886b859378eSBarry Smith 
2887b859378eSBarry Smith   Input Parameters:
2888b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2889b859378eSBarry Smith            some related function before a call to DMLoad().
2890b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2891b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2892b859378eSBarry Smith 
2893b859378eSBarry Smith    Level: intermediate
2894b859378eSBarry Smith 
2895b859378eSBarry Smith   Notes:
289655849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2897b859378eSBarry Smith 
2898b859378eSBarry Smith   Notes for advanced users:
2899b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2900b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2901b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2902b859378eSBarry Smith   format is
2903b859378eSBarry Smith .vb
2904b859378eSBarry Smith      has not yet been determined
2905b859378eSBarry Smith .ve
2906b859378eSBarry Smith 
2907b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2908b859378eSBarry Smith @*/
2909b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2910b859378eSBarry Smith {
29119331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2912b859378eSBarry Smith   PetscErrorCode ierr;
2913b859378eSBarry Smith 
2914b859378eSBarry Smith   PetscFunctionBegin;
2915b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2916b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
291732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
29189331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
29199331c7a4SMatthew G. Knepley   if (isbinary) {
29209331c7a4SMatthew G. Knepley     PetscInt classid;
29219331c7a4SMatthew G. Knepley     char     type[256];
2922b859378eSBarry Smith 
292332c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,&classid,1,PETSC_INT);CHKERRQ(ierr);
29249200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
292532c0f0efSBarry Smith     ierr = PetscViewerBinaryRead(viewer,type,256,PETSC_CHAR);CHKERRQ(ierr);
292632c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
29279331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
29289331c7a4SMatthew G. Knepley   } else if (ishdf5) {
29299331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
29309331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
2931b859378eSBarry Smith   PetscFunctionReturn(0);
2932b859378eSBarry Smith }
2933b859378eSBarry Smith 
29347da65231SMatthew G Knepley /******************************** FEM Support **********************************/
29357da65231SMatthew G Knepley 
29367da65231SMatthew G Knepley #undef __FUNCT__
29377da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
2938a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
2939a6dfd86eSKarl Rupp {
29401d47ebbbSSatish Balay   PetscInt       f;
29411b30c384SMatthew G Knepley   PetscErrorCode ierr;
29421b30c384SMatthew G Knepley 
29437da65231SMatthew G Knepley   PetscFunctionBegin;
294474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29451d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
294657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
29477da65231SMatthew G Knepley   }
29487da65231SMatthew G Knepley   PetscFunctionReturn(0);
29497da65231SMatthew G Knepley }
29507da65231SMatthew G Knepley 
29517da65231SMatthew G Knepley #undef __FUNCT__
29527da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
2953a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
2954a6dfd86eSKarl Rupp {
29551b30c384SMatthew G Knepley   PetscInt       f, g;
29567da65231SMatthew G Knepley   PetscErrorCode ierr;
29577da65231SMatthew G Knepley 
29587da65231SMatthew G Knepley   PetscFunctionBegin;
295974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
29601d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
296174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
29621d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
2963e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
29647da65231SMatthew G Knepley     }
296574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
29667da65231SMatthew G Knepley   }
29677da65231SMatthew G Knepley   PetscFunctionReturn(0);
29687da65231SMatthew G Knepley }
2969e7c4fc90SDmitry Karpeev 
2970970e74d5SMatthew G Knepley #undef __FUNCT__
2971e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
29726113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
2973e759306cSMatthew G. Knepley {
2974e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
2975e759306cSMatthew G. Knepley   PetscInt       p;
2976e759306cSMatthew G. Knepley   PetscErrorCode ierr;
2977e759306cSMatthew G. Knepley 
2978e759306cSMatthew G. Knepley   PetscFunctionBegin;
2979e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
2980e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
2981e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
2982e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
2983e759306cSMatthew G. Knepley     if (p == rank) {
2984e759306cSMatthew G. Knepley       Vec x;
2985e759306cSMatthew G. Knepley 
2986e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
2987e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
29886113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
2989e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2990e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
2991e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
2992e759306cSMatthew G. Knepley     }
2993e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
2994e759306cSMatthew G. Knepley   }
2995e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
2996e759306cSMatthew G. Knepley }
2997e759306cSMatthew G. Knepley 
2998e759306cSMatthew G. Knepley #undef __FUNCT__
299988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
300088ed4aceSMatthew G Knepley /*@
300188ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
300288ed4aceSMatthew G Knepley 
300388ed4aceSMatthew G Knepley   Input Parameter:
300488ed4aceSMatthew G Knepley . dm - The DM
300588ed4aceSMatthew G Knepley 
300688ed4aceSMatthew G Knepley   Output Parameter:
300788ed4aceSMatthew G Knepley . section - The PetscSection
300888ed4aceSMatthew G Knepley 
300988ed4aceSMatthew G Knepley   Level: intermediate
301088ed4aceSMatthew G Knepley 
301188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
301288ed4aceSMatthew G Knepley 
301388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
301488ed4aceSMatthew G Knepley @*/
30150adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
30160adebc6cSBarry Smith {
3017fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3018fd59a867SMatthew G. Knepley 
301988ed4aceSMatthew G Knepley   PetscFunctionBegin;
302088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
302188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
30222f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
30232f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
30242f0f8703SMatthew G. Knepley     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, "dm_", "-petscsection_view");CHKERRQ(ierr);
30252f0f8703SMatthew G. Knepley   }
302688ed4aceSMatthew G Knepley   *section = dm->defaultSection;
302788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
302888ed4aceSMatthew G Knepley }
302988ed4aceSMatthew G Knepley 
303088ed4aceSMatthew G Knepley #undef __FUNCT__
303188ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
303288ed4aceSMatthew G Knepley /*@
303388ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
303488ed4aceSMatthew G Knepley 
303588ed4aceSMatthew G Knepley   Input Parameters:
303688ed4aceSMatthew G Knepley + dm - The DM
303788ed4aceSMatthew G Knepley - section - The PetscSection
303888ed4aceSMatthew G Knepley 
303988ed4aceSMatthew G Knepley   Level: intermediate
304088ed4aceSMatthew G Knepley 
304188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
304288ed4aceSMatthew G Knepley 
304388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
304488ed4aceSMatthew G Knepley @*/
30450adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
30460adebc6cSBarry Smith {
3047af122d2aSMatthew G Knepley   PetscInt       numFields;
3048af122d2aSMatthew G Knepley   PetscInt       f;
304988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
305088ed4aceSMatthew G Knepley 
305188ed4aceSMatthew G Knepley   PetscFunctionBegin;
305288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
30531d799100SJed Brown   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
30541d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
305588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
305688ed4aceSMatthew G Knepley   dm->defaultSection = section;
3057af122d2aSMatthew G Knepley   ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);
3058af122d2aSMatthew G Knepley   if (numFields) {
3059af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3060af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
30610f21e855SMatthew G. Knepley       PetscObject disc;
3062af122d2aSMatthew G Knepley       const char *name;
3063af122d2aSMatthew G Knepley 
3064af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
30650f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
30660f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3067af122d2aSMatthew G Knepley     }
3068af122d2aSMatthew G Knepley   }
30691d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
30701d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
307188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
307288ed4aceSMatthew G Knepley }
307388ed4aceSMatthew G Knepley 
307488ed4aceSMatthew G Knepley #undef __FUNCT__
30759435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
30769435951eSToby Isaac /*@
30779435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
30789435951eSToby Isaac 
3079e228b242SToby Isaac   not collective
3080e228b242SToby Isaac 
30819435951eSToby Isaac   Input Parameter:
30829435951eSToby Isaac . dm - The DM
30839435951eSToby Isaac 
30849435951eSToby Isaac   Output Parameter:
30859435951eSToby Isaac + section - The PetscSection describing the range of the constraint matrix: relates rows of the constraint matrix to dofs of the default section.  Returns NULL if there are no local constraints.
30869435951eSToby Isaac - mat - The Mat that interpolates local constraints: its width should be the layout size of the default section.  Returns NULL if there are no local constraints.
30879435951eSToby Isaac 
30889435951eSToby Isaac   Level: advanced
30899435951eSToby Isaac 
30909435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
30919435951eSToby Isaac 
30929435951eSToby Isaac .seealso: DMSetDefaultConstraints()
30939435951eSToby Isaac @*/
30949435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
30959435951eSToby Isaac {
30969435951eSToby Isaac   PetscErrorCode ierr;
30979435951eSToby Isaac 
30989435951eSToby Isaac   PetscFunctionBegin;
30999435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31009435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
310145a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
310245a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
31039435951eSToby Isaac   PetscFunctionReturn(0);
31049435951eSToby Isaac }
31059435951eSToby Isaac 
31069435951eSToby Isaac #undef __FUNCT__
31079435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
31089435951eSToby Isaac /*@
31099435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
31109435951eSToby Isaac 
31119435951eSToby Isaac   If a constraint matrix is specified, then it is applied during DMGlobalToLocalEnd() when mode is INSERT_VALUES, INSERT_BC_VALUES, or INSERT_ALL_VALUES.  Without a constraint matrix, the local vector l returned by DMGlobalToLocalEnd() contains values that have been scattered from a global vector without modification; with a constraint matrix A, l is modified by computing c = A * l, l[s[i]] = c[i], where the scatter s is defined by the PetscSection returned by DMGetDefaultConstraintMatrix().
31129435951eSToby Isaac 
31139435951eSToby Isaac   If a constraint matrix is specified, then its adjoint is applied during DMLocalToGlobalBegin() when mode is ADD_VALUES, ADD_BC_VALUES, or ADD_ALL_VALUES.  Without a constraint matrix, the local vector l is accumulated into a global vector without modification; with a constraint matrix A, l is first modified by computing c[i] = l[s[i]], l[s[i]] = 0, l = l + A'*c, which is the adjoint of the operation described above.
31149435951eSToby Isaac 
3115e228b242SToby Isaac   collective on dm
3116e228b242SToby Isaac 
31179435951eSToby Isaac   Input Parameters:
31189435951eSToby Isaac + dm - The DM
3119e228b242SToby Isaac + section - The PetscSection describing the range of the constraint matrix: relates rows of the constraint matrix to dofs of the default section.  Must have a local communicator (PETSC_COMM_SELF or derivative).
3120e228b242SToby Isaac - mat - The Mat that interpolates local constraints: its width should be the layout size of the default section:  NULL indicates no constraints.  Must have a local communicator (PETSC_COMM_SELF or derivative).
31219435951eSToby Isaac 
31229435951eSToby Isaac   Level: advanced
31239435951eSToby Isaac 
31249435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
31259435951eSToby Isaac 
31269435951eSToby Isaac .seealso: DMGetDefaultConstraints()
31279435951eSToby Isaac @*/
31289435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
31299435951eSToby Isaac {
3130e228b242SToby Isaac   PetscMPIInt result;
31319435951eSToby Isaac   PetscErrorCode ierr;
31329435951eSToby Isaac 
31339435951eSToby Isaac   PetscFunctionBegin;
31349435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3135e228b242SToby Isaac   if (section) {
3136e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3137e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3138e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3139e228b242SToby Isaac   }
3140e228b242SToby Isaac   if (mat) {
3141e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3142e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3143e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3144e228b242SToby Isaac   }
31459435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
31469435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
31479435951eSToby Isaac   dm->defaultConstraintSection = section;
31489435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
31499435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
31509435951eSToby Isaac   dm->defaultConstraintMat = mat;
31519435951eSToby Isaac   PetscFunctionReturn(0);
31529435951eSToby Isaac }
31539435951eSToby Isaac 
31549435951eSToby Isaac #undef __FUNCT__
315588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
315688ed4aceSMatthew G Knepley /*@
315788ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
315888ed4aceSMatthew G Knepley 
31598b1ab98fSJed Brown   Collective on DM
31608b1ab98fSJed Brown 
316188ed4aceSMatthew G Knepley   Input Parameter:
316288ed4aceSMatthew G Knepley . dm - The DM
316388ed4aceSMatthew G Knepley 
316488ed4aceSMatthew G Knepley   Output Parameter:
316588ed4aceSMatthew G Knepley . section - The PetscSection
316688ed4aceSMatthew G Knepley 
316788ed4aceSMatthew G Knepley   Level: intermediate
316888ed4aceSMatthew G Knepley 
316988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
317088ed4aceSMatthew G Knepley 
317188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
317288ed4aceSMatthew G Knepley @*/
31730adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
31740adebc6cSBarry Smith {
317588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
317688ed4aceSMatthew G Knepley 
317788ed4aceSMatthew G Knepley   PetscFunctionBegin;
317888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
317988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
318088ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3181fd59a867SMatthew G. Knepley     PetscSection s;
3182fd59a867SMatthew G. Knepley 
3183fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3184fd59a867SMatthew G. Knepley     if (!s)  SETERRQ(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSection in order to create a global PetscSection");
3185fd59a867SMatthew G. Knepley     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSF in order to create a global PetscSection");
3186fd59a867SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3187cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3188ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
318988ed4aceSMatthew G Knepley   }
319088ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
319188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
319288ed4aceSMatthew G Knepley }
319388ed4aceSMatthew G Knepley 
319488ed4aceSMatthew G Knepley #undef __FUNCT__
3195b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3196b21d0597SMatthew G Knepley /*@
3197b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3198b21d0597SMatthew G Knepley 
3199b21d0597SMatthew G Knepley   Input Parameters:
3200b21d0597SMatthew G Knepley + dm - The DM
32015080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3202b21d0597SMatthew G Knepley 
3203b21d0597SMatthew G Knepley   Level: intermediate
3204b21d0597SMatthew G Knepley 
3205b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3206b21d0597SMatthew G Knepley 
3207b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3208b21d0597SMatthew G Knepley @*/
32090adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
32100adebc6cSBarry Smith {
3211b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3212b21d0597SMatthew G Knepley 
3213b21d0597SMatthew G Knepley   PetscFunctionBegin;
3214b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
32155080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
32161d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3217b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3218b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3219b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3220b21d0597SMatthew G Knepley }
3221b21d0597SMatthew G Knepley 
3222b21d0597SMatthew G Knepley #undef __FUNCT__
322388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
322488ed4aceSMatthew G Knepley /*@
322588ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
322688ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
322788ed4aceSMatthew G Knepley 
322888ed4aceSMatthew G Knepley   Input Parameter:
322988ed4aceSMatthew G Knepley . dm - The DM
323088ed4aceSMatthew G Knepley 
323188ed4aceSMatthew G Knepley   Output Parameter:
323288ed4aceSMatthew G Knepley . sf - The PetscSF
323388ed4aceSMatthew G Knepley 
323488ed4aceSMatthew G Knepley   Level: intermediate
323588ed4aceSMatthew G Knepley 
323688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
323788ed4aceSMatthew G Knepley 
323888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
323988ed4aceSMatthew G Knepley @*/
32400adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
32410adebc6cSBarry Smith {
324288ed4aceSMatthew G Knepley   PetscInt       nroots;
324388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
324488ed4aceSMatthew G Knepley 
324588ed4aceSMatthew G Knepley   PetscFunctionBegin;
324688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
324788ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
32480298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
324988ed4aceSMatthew G Knepley   if (nroots < 0) {
325088ed4aceSMatthew G Knepley     PetscSection section, gSection;
325188ed4aceSMatthew G Knepley 
325288ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
325331ea6d37SMatthew G Knepley     if (section) {
325488ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
325588ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
325631ea6d37SMatthew G Knepley     } else {
32570298fd71SBarry Smith       *sf = NULL;
325831ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
325931ea6d37SMatthew G Knepley     }
326088ed4aceSMatthew G Knepley   }
326188ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
326288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
326388ed4aceSMatthew G Knepley }
326488ed4aceSMatthew G Knepley 
326588ed4aceSMatthew G Knepley #undef __FUNCT__
326688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
326788ed4aceSMatthew G Knepley /*@
326888ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
326988ed4aceSMatthew G Knepley 
327088ed4aceSMatthew G Knepley   Input Parameters:
327188ed4aceSMatthew G Knepley + dm - The DM
327288ed4aceSMatthew G Knepley - sf - The PetscSF
327388ed4aceSMatthew G Knepley 
327488ed4aceSMatthew G Knepley   Level: intermediate
327588ed4aceSMatthew G Knepley 
327688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
327788ed4aceSMatthew G Knepley 
327888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
327988ed4aceSMatthew G Knepley @*/
32800adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
32810adebc6cSBarry Smith {
328288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
328388ed4aceSMatthew G Knepley 
328488ed4aceSMatthew G Knepley   PetscFunctionBegin;
328588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
328688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
328788ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
328888ed4aceSMatthew G Knepley   dm->defaultSF = sf;
328988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
329088ed4aceSMatthew G Knepley }
329188ed4aceSMatthew G Knepley 
329288ed4aceSMatthew G Knepley #undef __FUNCT__
329388ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
329488ed4aceSMatthew G Knepley /*@C
329588ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
329688ed4aceSMatthew G Knepley   describing the data layout.
329788ed4aceSMatthew G Knepley 
329888ed4aceSMatthew G Knepley   Input Parameters:
329988ed4aceSMatthew G Knepley + dm - The DM
330088ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
330188ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
330288ed4aceSMatthew G Knepley 
330388ed4aceSMatthew G Knepley   Level: intermediate
330488ed4aceSMatthew G Knepley 
330588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
330688ed4aceSMatthew G Knepley @*/
330788ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
330888ed4aceSMatthew G Knepley {
330982f516ccSBarry Smith   MPI_Comm       comm;
331088ed4aceSMatthew G Knepley   PetscLayout    layout;
331188ed4aceSMatthew G Knepley   const PetscInt *ranges;
331288ed4aceSMatthew G Knepley   PetscInt       *local;
331388ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3314ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
331588ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
331688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
331788ed4aceSMatthew G Knepley 
331888ed4aceSMatthew G Knepley   PetscFunctionBegin;
331982f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
332088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
332188ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
332288ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
332388ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
332488ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
332588ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
332688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
332788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
332888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
332988ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3330ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
33316636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
333288ed4aceSMatthew G Knepley 
33336636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
33346636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3335235fbf56SMatthew G. Knepley     if (gcdof > (gdof < 0 ? -(gdof+1) : gdof)) SETERRQ3(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d has %d constraints > %d dof", p, gcdof, (gdof < 0 ? -(gdof+1) : gdof));
33366636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
333788ed4aceSMatthew G Knepley   }
3338785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3339785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
334088ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
33411f588964SMatthew G Knepley     const PetscInt *cind;
33426636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
334388ed4aceSMatthew G Knepley 
334488ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
334588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
334688ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
334788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
334888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
33496636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
335088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
33516636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
33526636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
33536636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3354057b4bcdSMatthew 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);
33556636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
33566636e97aSMatthew G Knepley     }
335788ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
335888ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
335988ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
336088ed4aceSMatthew G Knepley     }
336188ed4aceSMatthew G Knepley     if (gdof < 0) {
33626636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
336388ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
336488ed4aceSMatthew G Knepley 
336505376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
336631d3f06eSJed Brown         if (r < 0) r = -(r+2);
336705376888SMatthew G. Knepley         if ((r < 0) || (r >= size)) SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Point %d mapped to invalid process %d (%d, %d)", p, r, gdof, goff);
336888ed4aceSMatthew G Knepley         remote[l].rank  = r;
336988ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
337088ed4aceSMatthew G Knepley       }
337188ed4aceSMatthew G Knepley     } else {
33726636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
337388ed4aceSMatthew G Knepley         remote[l].rank  = rank;
337488ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
337588ed4aceSMatthew G Knepley       }
337688ed4aceSMatthew G Knepley     }
337788ed4aceSMatthew G Knepley   }
33786636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
337988ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
338088ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
338188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
338288ed4aceSMatthew G Knepley }
3383af122d2aSMatthew G Knepley 
3384af122d2aSMatthew G Knepley #undef __FUNCT__
3385b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3386b21d0597SMatthew G Knepley /*@
3387b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3388b21d0597SMatthew G Knepley 
3389b21d0597SMatthew G Knepley   Input Parameter:
3390b21d0597SMatthew G Knepley . dm - The DM
3391b21d0597SMatthew G Knepley 
3392b21d0597SMatthew G Knepley   Output Parameter:
3393b21d0597SMatthew G Knepley . sf - The PetscSF
3394b21d0597SMatthew G Knepley 
3395b21d0597SMatthew G Knepley   Level: intermediate
3396b21d0597SMatthew G Knepley 
3397b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3398b21d0597SMatthew G Knepley 
3399057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3400b21d0597SMatthew G Knepley @*/
34010adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
34020adebc6cSBarry Smith {
3403b21d0597SMatthew G Knepley   PetscFunctionBegin;
3404b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3405b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3406b21d0597SMatthew G Knepley   *sf = dm->sf;
3407b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3408b21d0597SMatthew G Knepley }
3409b21d0597SMatthew G Knepley 
3410b21d0597SMatthew G Knepley #undef __FUNCT__
3411057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3412057b4bcdSMatthew G Knepley /*@
3413057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3414057b4bcdSMatthew G Knepley 
3415057b4bcdSMatthew G Knepley   Input Parameters:
3416057b4bcdSMatthew G Knepley + dm - The DM
3417057b4bcdSMatthew G Knepley - sf - The PetscSF
3418057b4bcdSMatthew G Knepley 
3419057b4bcdSMatthew G Knepley   Level: intermediate
3420057b4bcdSMatthew G Knepley 
3421057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3422057b4bcdSMatthew G Knepley @*/
34230adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
34240adebc6cSBarry Smith {
3425057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3426057b4bcdSMatthew G Knepley 
3427057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3428057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3429057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3430057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3431057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3432057b4bcdSMatthew G Knepley   dm->sf = sf;
3433057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3434057b4bcdSMatthew G Knepley }
3435057b4bcdSMatthew G Knepley 
3436057b4bcdSMatthew G Knepley #undef __FUNCT__
34372764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
34382764a2aaSMatthew G. Knepley /*@
34392764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
34402764a2aaSMatthew G. Knepley 
34412764a2aaSMatthew G. Knepley   Input Parameter:
34422764a2aaSMatthew G. Knepley . dm - The DM
34432764a2aaSMatthew G. Knepley 
34442764a2aaSMatthew G. Knepley   Output Parameter:
34452764a2aaSMatthew G. Knepley . prob - The PetscDS
34462764a2aaSMatthew G. Knepley 
34472764a2aaSMatthew G. Knepley   Level: developer
34482764a2aaSMatthew G. Knepley 
34492764a2aaSMatthew G. Knepley .seealso: DMSetDS()
34502764a2aaSMatthew G. Knepley @*/
34512764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3452af122d2aSMatthew G Knepley {
3453af122d2aSMatthew G Knepley   PetscFunctionBegin;
3454af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34550f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
34560f21e855SMatthew G. Knepley   *prob = dm->prob;
34570f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
34580f21e855SMatthew G. Knepley }
34590f21e855SMatthew G. Knepley 
34600f21e855SMatthew G. Knepley #undef __FUNCT__
34612764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
34622764a2aaSMatthew G. Knepley /*@
34632764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
34642764a2aaSMatthew G. Knepley 
34652764a2aaSMatthew G. Knepley   Input Parameters:
34662764a2aaSMatthew G. Knepley + dm - The DM
34672764a2aaSMatthew G. Knepley - prob - The PetscDS
34682764a2aaSMatthew G. Knepley 
34692764a2aaSMatthew G. Knepley   Level: developer
34702764a2aaSMatthew G. Knepley 
34712764a2aaSMatthew G. Knepley .seealso: DMGetDS()
34722764a2aaSMatthew G. Knepley @*/
34732764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
34740f21e855SMatthew G. Knepley {
34750f21e855SMatthew G. Knepley   PetscErrorCode ierr;
34760f21e855SMatthew G. Knepley 
34770f21e855SMatthew G. Knepley   PetscFunctionBegin;
34780f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34792764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
34802764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
34810f21e855SMatthew G. Knepley   dm->prob = prob;
34820f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
34830f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
34840f21e855SMatthew G. Knepley }
34850f21e855SMatthew G. Knepley 
34860f21e855SMatthew G. Knepley #undef __FUNCT__
34870f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
34880f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
34890f21e855SMatthew G. Knepley {
34900f21e855SMatthew G. Knepley   PetscErrorCode ierr;
34910f21e855SMatthew G. Knepley 
34920f21e855SMatthew G. Knepley   PetscFunctionBegin;
34930f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34942764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3495af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3496af122d2aSMatthew G Knepley }
3497af122d2aSMatthew G Knepley 
3498af122d2aSMatthew G Knepley #undef __FUNCT__
3499af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3500af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3501af122d2aSMatthew G Knepley {
35020f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3503af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3504af122d2aSMatthew G Knepley 
3505af122d2aSMatthew G Knepley   PetscFunctionBegin;
3506af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35072764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
35080f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
35090f21e855SMatthew G. Knepley     PetscContainer obj;
35100f21e855SMatthew G. Knepley 
35110f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
35122764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
35130f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3514af122d2aSMatthew G Knepley   }
3515af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3516af122d2aSMatthew G Knepley }
3517af122d2aSMatthew G Knepley 
3518af122d2aSMatthew G Knepley #undef __FUNCT__
3519af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3520c1929be8SMatthew G. Knepley /*@
3521c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3522c1929be8SMatthew G. Knepley 
3523c1929be8SMatthew G. Knepley   Not collective
3524c1929be8SMatthew G. Knepley 
3525c1929be8SMatthew G. Knepley   Input Parameters:
3526c1929be8SMatthew G. Knepley + dm - The DM
3527c1929be8SMatthew G. Knepley - f  - The field number
3528c1929be8SMatthew G. Knepley 
3529c1929be8SMatthew G. Knepley   Output Parameter:
3530c1929be8SMatthew G. Knepley . field - The discretization object
3531c1929be8SMatthew G. Knepley 
3532c1929be8SMatthew G. Knepley   Level: developer
3533c1929be8SMatthew G. Knepley 
3534c1929be8SMatthew G. Knepley .seealso: DMSetField()
3535c1929be8SMatthew G. Knepley @*/
3536af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3537af122d2aSMatthew G Knepley {
35380f21e855SMatthew G. Knepley   PetscErrorCode ierr;
35390f21e855SMatthew G. Knepley 
3540af122d2aSMatthew G Knepley   PetscFunctionBegin;
3541af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35422764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3543decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3544decb47aaSMatthew G. Knepley }
3545decb47aaSMatthew G. Knepley 
3546decb47aaSMatthew G. Knepley #undef __FUNCT__
3547decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3548c1929be8SMatthew G. Knepley /*@
3549c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3550c1929be8SMatthew G. Knepley 
3551c1929be8SMatthew G. Knepley   Logically collective on DM
3552c1929be8SMatthew G. Knepley 
3553c1929be8SMatthew G. Knepley   Input Parameters:
3554c1929be8SMatthew G. Knepley + dm - The DM
3555c1929be8SMatthew G. Knepley . f  - The field number
3556c1929be8SMatthew G. Knepley - field - The discretization object
3557c1929be8SMatthew G. Knepley 
3558c1929be8SMatthew G. Knepley   Level: developer
3559c1929be8SMatthew G. Knepley 
3560c1929be8SMatthew G. Knepley .seealso: DMGetField()
3561c1929be8SMatthew G. Knepley @*/
3562decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3563decb47aaSMatthew G. Knepley {
3564decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3565decb47aaSMatthew G. Knepley 
3566decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3567decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
35682764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3569af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3570af122d2aSMatthew G Knepley }
35716636e97aSMatthew G Knepley 
35726636e97aSMatthew G Knepley #undef __FUNCT__
3573b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3574b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3575b64e0483SPeter Brune {
3576b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3577b64e0483SPeter Brune   PetscErrorCode ierr;
3578b64e0483SPeter Brune   Vec coords,ccoords;
35796dbf9973SLawrence Mitchell   Mat inject;
3580b64e0483SPeter Brune   PetscFunctionBegin;
3581b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3582b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3583b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3584b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3585b64e0483SPeter Brune   if (coords && !ccoords) {
3586b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
35876dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
35882adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
35896dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3590b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3591b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3592b64e0483SPeter Brune   }
3593b64e0483SPeter Brune   PetscFunctionReturn(0);
3594b64e0483SPeter Brune }
3595b64e0483SPeter Brune 
3596b64e0483SPeter Brune #undef __FUNCT__
359703dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
359803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
359903dadc2fSPeter Brune {
360003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
360103dadc2fSPeter Brune   PetscErrorCode ierr;
360203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
360303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
360403dadc2fSPeter Brune   PetscFunctionBegin;
360503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
360603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
360703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
360803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
360903dadc2fSPeter Brune   if (coords && !ccoords) {
361003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
361103dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
361203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
361303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
361403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
361503dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
361603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
361703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
361803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
361903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
362003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
362103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
362203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
362303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
362403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
362503dadc2fSPeter Brune   }
362603dadc2fSPeter Brune   PetscFunctionReturn(0);
362703dadc2fSPeter Brune }
362803dadc2fSPeter Brune 
362903dadc2fSPeter Brune #undef __FUNCT__
3630c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3631c73cfb54SMatthew G. Knepley /*@
3632c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3633c73cfb54SMatthew G. Knepley 
3634c73cfb54SMatthew G. Knepley   Not collective
3635c73cfb54SMatthew G. Knepley 
3636c73cfb54SMatthew G. Knepley   Input Parameter:
3637c73cfb54SMatthew G. Knepley . dm - The DM
3638c73cfb54SMatthew G. Knepley 
3639c73cfb54SMatthew G. Knepley   Output Parameter:
3640c73cfb54SMatthew G. Knepley . dim - The topological dimension
3641c73cfb54SMatthew G. Knepley 
3642c73cfb54SMatthew G. Knepley   Level: beginner
3643c73cfb54SMatthew G. Knepley 
3644c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3645c73cfb54SMatthew G. Knepley @*/
3646c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3647c73cfb54SMatthew G. Knepley {
3648c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3649c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3650c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3651c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3652c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3653c73cfb54SMatthew G. Knepley }
3654c73cfb54SMatthew G. Knepley 
3655c73cfb54SMatthew G. Knepley #undef __FUNCT__
3656c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3657c73cfb54SMatthew G. Knepley /*@
3658c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3659c73cfb54SMatthew G. Knepley 
3660c73cfb54SMatthew G. Knepley   Collective on dm
3661c73cfb54SMatthew G. Knepley 
3662c73cfb54SMatthew G. Knepley   Input Parameters:
3663c73cfb54SMatthew G. Knepley + dm - The DM
3664c73cfb54SMatthew G. Knepley - dim - The topological dimension
3665c73cfb54SMatthew G. Knepley 
3666c73cfb54SMatthew G. Knepley   Level: beginner
3667c73cfb54SMatthew G. Knepley 
3668c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3669c73cfb54SMatthew G. Knepley @*/
3670c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3671c73cfb54SMatthew G. Knepley {
3672c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3673c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3674c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3675c73cfb54SMatthew G. Knepley   dm->dim = dim;
3676c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3677c73cfb54SMatthew G. Knepley }
3678c73cfb54SMatthew G. Knepley 
3679793f3fe5SMatthew G. Knepley #undef __FUNCT__
3680793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3681793f3fe5SMatthew G. Knepley /*@
3682793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3683793f3fe5SMatthew G. Knepley 
3684793f3fe5SMatthew G. Knepley   Collective on DM
3685793f3fe5SMatthew G. Knepley 
3686793f3fe5SMatthew G. Knepley   Input Parameters:
3687793f3fe5SMatthew G. Knepley + dm - the DM
3688793f3fe5SMatthew G. Knepley - dim - the dimension
3689793f3fe5SMatthew G. Knepley 
3690793f3fe5SMatthew G. Knepley   Output Parameters:
3691793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3692793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3693793f3fe5SMatthew G. Knepley 
3694793f3fe5SMatthew G. Knepley   Note:
3695793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3696793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3697793f3fe5SMatthew G. Knepley   then the interval is empty.
3698793f3fe5SMatthew G. Knepley 
3699793f3fe5SMatthew G. Knepley   Level: intermediate
3700793f3fe5SMatthew G. Knepley 
3701793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3702793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3703793f3fe5SMatthew G. Knepley @*/
3704793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3705793f3fe5SMatthew G. Knepley {
3706793f3fe5SMatthew G. Knepley   PetscInt       d;
3707793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3708793f3fe5SMatthew G. Knepley 
3709793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3710793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3711793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3712793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3713793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3714793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3715793f3fe5SMatthew G. Knepley }
3716793f3fe5SMatthew G. Knepley 
3717793f3fe5SMatthew G. Knepley #undef __FUNCT__
37186636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
37196636e97aSMatthew G Knepley /*@
37206636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
37216636e97aSMatthew G Knepley 
37226636e97aSMatthew G Knepley   Collective on DM
37236636e97aSMatthew G Knepley 
37246636e97aSMatthew G Knepley   Input Parameters:
37256636e97aSMatthew G Knepley + dm - the DM
37266636e97aSMatthew G Knepley - c - coordinate vector
37276636e97aSMatthew G Knepley 
37286636e97aSMatthew G Knepley   Note:
37296636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
37306636e97aSMatthew G Knepley 
37316636e97aSMatthew G Knepley   Level: intermediate
37326636e97aSMatthew G Knepley 
37336636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
37346636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
37356636e97aSMatthew G Knepley @*/
37366636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
37376636e97aSMatthew G Knepley {
37386636e97aSMatthew G Knepley   PetscErrorCode ierr;
37396636e97aSMatthew G Knepley 
37406636e97aSMatthew G Knepley   PetscFunctionBegin;
37416636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
37426636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
37436636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
37446636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
37456636e97aSMatthew G Knepley   dm->coordinates = c;
37466636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3747b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
374803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
37496636e97aSMatthew G Knepley   PetscFunctionReturn(0);
37506636e97aSMatthew G Knepley }
37516636e97aSMatthew G Knepley 
37526636e97aSMatthew G Knepley #undef __FUNCT__
37536636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
37546636e97aSMatthew G Knepley /*@
37556636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
37566636e97aSMatthew G Knepley 
37576636e97aSMatthew G Knepley   Collective on DM
37586636e97aSMatthew G Knepley 
37596636e97aSMatthew G Knepley    Input Parameters:
37606636e97aSMatthew G Knepley +  dm - the DM
37616636e97aSMatthew G Knepley -  c - coordinate vector
37626636e97aSMatthew G Knepley 
37636636e97aSMatthew G Knepley   Note:
37646636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
37656636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
37666636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
37676636e97aSMatthew G Knepley 
37686636e97aSMatthew G Knepley   Level: intermediate
37696636e97aSMatthew G Knepley 
37706636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
37716636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
37726636e97aSMatthew G Knepley @*/
37736636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
37746636e97aSMatthew G Knepley {
37756636e97aSMatthew G Knepley   PetscErrorCode ierr;
37766636e97aSMatthew G Knepley 
37776636e97aSMatthew G Knepley   PetscFunctionBegin;
37786636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
37796636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
37806636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
37816636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
37828865f1eaSKarl Rupp 
37836636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
37848865f1eaSKarl Rupp 
37856636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
37866636e97aSMatthew G Knepley   PetscFunctionReturn(0);
37876636e97aSMatthew G Knepley }
37886636e97aSMatthew G Knepley 
37896636e97aSMatthew G Knepley #undef __FUNCT__
37906636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
37916636e97aSMatthew G Knepley /*@
37926636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
37936636e97aSMatthew G Knepley 
37946636e97aSMatthew G Knepley   Not Collective
37956636e97aSMatthew G Knepley 
37966636e97aSMatthew G Knepley   Input Parameter:
37976636e97aSMatthew G Knepley . dm - the DM
37986636e97aSMatthew G Knepley 
37996636e97aSMatthew G Knepley   Output Parameter:
38006636e97aSMatthew G Knepley . c - global coordinate vector
38016636e97aSMatthew G Knepley 
38026636e97aSMatthew G Knepley   Note:
38036636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
38046636e97aSMatthew G Knepley 
38056636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
38066636e97aSMatthew G Knepley 
38076636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
38086636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
38096636e97aSMatthew G Knepley 
38106636e97aSMatthew G Knepley   Level: intermediate
38116636e97aSMatthew G Knepley 
38126636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38136636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
38146636e97aSMatthew G Knepley @*/
38156636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
38166636e97aSMatthew G Knepley {
38176636e97aSMatthew G Knepley   PetscErrorCode ierr;
38186636e97aSMatthew G Knepley 
38196636e97aSMatthew G Knepley   PetscFunctionBegin;
38206636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38216636e97aSMatthew G Knepley   PetscValidPointer(c,2);
38221f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
38230298fd71SBarry Smith     DM cdm = NULL;
38246636e97aSMatthew G Knepley 
38256636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
38266636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
38276636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
38286636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
38296636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
38306636e97aSMatthew G Knepley   }
38316636e97aSMatthew G Knepley   *c = dm->coordinates;
38326636e97aSMatthew G Knepley   PetscFunctionReturn(0);
38336636e97aSMatthew G Knepley }
38346636e97aSMatthew G Knepley 
38356636e97aSMatthew G Knepley #undef __FUNCT__
38366636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
38376636e97aSMatthew G Knepley /*@
38386636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
38396636e97aSMatthew G Knepley 
38406636e97aSMatthew G Knepley   Collective on DM
38416636e97aSMatthew G Knepley 
38426636e97aSMatthew G Knepley   Input Parameter:
38436636e97aSMatthew G Knepley . dm - the DM
38446636e97aSMatthew G Knepley 
38456636e97aSMatthew G Knepley   Output Parameter:
38466636e97aSMatthew G Knepley . c - coordinate vector
38476636e97aSMatthew G Knepley 
38486636e97aSMatthew G Knepley   Note:
38496636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
38506636e97aSMatthew G Knepley 
38516636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
38526636e97aSMatthew G Knepley 
38536636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
38546636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
38556636e97aSMatthew G Knepley 
38566636e97aSMatthew G Knepley   Level: intermediate
38576636e97aSMatthew G Knepley 
38586636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38596636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
38606636e97aSMatthew G Knepley @*/
38616636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
38626636e97aSMatthew G Knepley {
38636636e97aSMatthew G Knepley   PetscErrorCode ierr;
38646636e97aSMatthew G Knepley 
38656636e97aSMatthew G Knepley   PetscFunctionBegin;
38666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38676636e97aSMatthew G Knepley   PetscValidPointer(c,2);
38681f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
38690298fd71SBarry Smith     DM cdm = NULL;
38706636e97aSMatthew G Knepley 
38716636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
38726636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
38736636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
38746636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
38756636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
38766636e97aSMatthew G Knepley   }
38776636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
38786636e97aSMatthew G Knepley   PetscFunctionReturn(0);
38796636e97aSMatthew G Knepley }
38806636e97aSMatthew G Knepley 
38816636e97aSMatthew G Knepley #undef __FUNCT__
38826636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
38836636e97aSMatthew G Knepley /*@
38841cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
38856636e97aSMatthew G Knepley 
38866636e97aSMatthew G Knepley   Collective on DM
38876636e97aSMatthew G Knepley 
38886636e97aSMatthew G Knepley   Input Parameter:
38896636e97aSMatthew G Knepley . dm - the DM
38906636e97aSMatthew G Knepley 
38916636e97aSMatthew G Knepley   Output Parameter:
38926636e97aSMatthew G Knepley . cdm - coordinate DM
38936636e97aSMatthew G Knepley 
38946636e97aSMatthew G Knepley   Level: intermediate
38956636e97aSMatthew G Knepley 
38966636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38971cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
38986636e97aSMatthew G Knepley @*/
38996636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
39006636e97aSMatthew G Knepley {
39016636e97aSMatthew G Knepley   PetscErrorCode ierr;
39026636e97aSMatthew G Knepley 
39036636e97aSMatthew G Knepley   PetscFunctionBegin;
39046636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39056636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
39066636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
390782f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
39086636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
39096636e97aSMatthew G Knepley   }
39106636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
39116636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39126636e97aSMatthew G Knepley }
3913e87bb0d3SMatthew G Knepley 
3914e87bb0d3SMatthew G Knepley #undef __FUNCT__
39151cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
39161cfe2091SMatthew G. Knepley /*@
39171cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
39181cfe2091SMatthew G. Knepley 
39191cfe2091SMatthew G. Knepley   Logically Collective on DM
39201cfe2091SMatthew G. Knepley 
39211cfe2091SMatthew G. Knepley   Input Parameters:
39221cfe2091SMatthew G. Knepley + dm - the DM
39231cfe2091SMatthew G. Knepley - cdm - coordinate DM
39241cfe2091SMatthew G. Knepley 
39251cfe2091SMatthew G. Knepley   Level: intermediate
39261cfe2091SMatthew G. Knepley 
39271cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39281cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
39291cfe2091SMatthew G. Knepley @*/
39301cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
39311cfe2091SMatthew G. Knepley {
39321cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
39331cfe2091SMatthew G. Knepley 
39341cfe2091SMatthew G. Knepley   PetscFunctionBegin;
39351cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39361cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
39371cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
39381cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
39391cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
39401cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
39411cfe2091SMatthew G. Knepley }
39421cfe2091SMatthew G. Knepley 
39431cfe2091SMatthew G. Knepley #undef __FUNCT__
394446e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
394546e270d4SMatthew G. Knepley /*@
394646e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
394746e270d4SMatthew G. Knepley 
394846e270d4SMatthew G. Knepley   Not Collective
394946e270d4SMatthew G. Knepley 
395046e270d4SMatthew G. Knepley   Input Parameter:
395146e270d4SMatthew G. Knepley . dm - The DM object
395246e270d4SMatthew G. Knepley 
395346e270d4SMatthew G. Knepley   Output Parameter:
395446e270d4SMatthew G. Knepley . dim - The embedding dimension
395546e270d4SMatthew G. Knepley 
395646e270d4SMatthew G. Knepley   Level: intermediate
395746e270d4SMatthew G. Knepley 
395846e270d4SMatthew G. Knepley .keywords: mesh, coordinates
395946e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
396046e270d4SMatthew G. Knepley @*/
396146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
396246e270d4SMatthew G. Knepley {
396346e270d4SMatthew G. Knepley   PetscFunctionBegin;
396446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
396546e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
39669a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
39679a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
39689a9a41abSToby Isaac   }
396946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
397046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
397146e270d4SMatthew G. Knepley }
397246e270d4SMatthew G. Knepley 
397346e270d4SMatthew G. Knepley #undef __FUNCT__
397446e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
397546e270d4SMatthew G. Knepley /*@
397646e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
397746e270d4SMatthew G. Knepley 
397846e270d4SMatthew G. Knepley   Not Collective
397946e270d4SMatthew G. Knepley 
398046e270d4SMatthew G. Knepley   Input Parameters:
398146e270d4SMatthew G. Knepley + dm  - The DM object
398246e270d4SMatthew G. Knepley - dim - The embedding dimension
398346e270d4SMatthew G. Knepley 
398446e270d4SMatthew G. Knepley   Level: intermediate
398546e270d4SMatthew G. Knepley 
398646e270d4SMatthew G. Knepley .keywords: mesh, coordinates
398746e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
398846e270d4SMatthew G. Knepley @*/
398946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
399046e270d4SMatthew G. Knepley {
399146e270d4SMatthew G. Knepley   PetscFunctionBegin;
399246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
399346e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
399446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
399546e270d4SMatthew G. Knepley }
399646e270d4SMatthew G. Knepley 
399746e270d4SMatthew G. Knepley #undef __FUNCT__
3998e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
3999e8abe2deSMatthew G. Knepley /*@
4000e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4001e8abe2deSMatthew G. Knepley 
4002e8abe2deSMatthew G. Knepley   Not Collective
4003e8abe2deSMatthew G. Knepley 
4004e8abe2deSMatthew G. Knepley   Input Parameter:
4005e8abe2deSMatthew G. Knepley . dm - The DM object
4006e8abe2deSMatthew G. Knepley 
4007e8abe2deSMatthew G. Knepley   Output Parameter:
4008e8abe2deSMatthew G. Knepley . section - The PetscSection object
4009e8abe2deSMatthew G. Knepley 
4010e8abe2deSMatthew G. Knepley   Level: intermediate
4011e8abe2deSMatthew G. Knepley 
4012e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4013e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4014e8abe2deSMatthew G. Knepley @*/
4015e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4016e8abe2deSMatthew G. Knepley {
4017e8abe2deSMatthew G. Knepley   DM             cdm;
4018e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4019e8abe2deSMatthew G. Knepley 
4020e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4021e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4022e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4023e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4024e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4025e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4026e8abe2deSMatthew G. Knepley }
4027e8abe2deSMatthew G. Knepley 
4028e8abe2deSMatthew G. Knepley #undef __FUNCT__
4029e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4030e8abe2deSMatthew G. Knepley /*@
4031e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4032e8abe2deSMatthew G. Knepley 
4033e8abe2deSMatthew G. Knepley   Not Collective
4034e8abe2deSMatthew G. Knepley 
4035e8abe2deSMatthew G. Knepley   Input Parameters:
4036e8abe2deSMatthew G. Knepley + dm      - The DM object
403746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4038e8abe2deSMatthew G. Knepley - section - The PetscSection object
4039e8abe2deSMatthew G. Knepley 
4040e8abe2deSMatthew G. Knepley   Level: intermediate
4041e8abe2deSMatthew G. Knepley 
4042e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4043e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4044e8abe2deSMatthew G. Knepley @*/
404546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4046e8abe2deSMatthew G. Knepley {
4047e8abe2deSMatthew G. Knepley   DM             cdm;
4048e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4049e8abe2deSMatthew G. Knepley 
4050e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4051e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
405246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4053e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4054e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
405546e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
405646e270d4SMatthew G. Knepley     PetscInt d = dim;
405746e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
405846e270d4SMatthew G. Knepley 
405946e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
406046e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
406146e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
406246e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
406346e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
406446e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
406546e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
406646e270d4SMatthew G. Knepley     }
406746e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
406846e270d4SMatthew G. Knepley   }
4069e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4070e8abe2deSMatthew G. Knepley }
4071e8abe2deSMatthew G. Knepley 
4072e8abe2deSMatthew G. Knepley #undef __FUNCT__
4073c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
40745dc8c3f7SMatthew G. Knepley /*@C
40755dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
40765dc8c3f7SMatthew G. Knepley 
40775dc8c3f7SMatthew G. Knepley   Input Parameters:
40785dc8c3f7SMatthew G. Knepley + dm      - The DM object
40795dc8c3f7SMatthew G. Knepley . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates
40805dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
40815dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
40825dc8c3f7SMatthew G. Knepley 
40835dc8c3f7SMatthew G. Knepley   Level: developer
40845dc8c3f7SMatthew G. Knepley 
40855dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
40865dc8c3f7SMatthew G. Knepley @*/
40875dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4088c6b900c6SMatthew G. Knepley {
4089c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4090c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4091c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4092c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
40935dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4094c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4095c6b900c6SMatthew G. Knepley }
4096c6b900c6SMatthew G. Knepley 
4097c6b900c6SMatthew G. Knepley #undef __FUNCT__
4098c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
40995dc8c3f7SMatthew G. Knepley /*@C
41005dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
41015dc8c3f7SMatthew G. Knepley 
41025dc8c3f7SMatthew G. Knepley   Input Parameters:
41035dc8c3f7SMatthew G. Knepley + dm      - The DM object
41045dc8c3f7SMatthew G. Knepley . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates
41055dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
41065dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
41075dc8c3f7SMatthew G. Knepley 
41085dc8c3f7SMatthew G. Knepley   Level: developer
41095dc8c3f7SMatthew G. Knepley 
41105dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
41115dc8c3f7SMatthew G. Knepley @*/
41125dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4113c6b900c6SMatthew G. Knepley {
4114c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4115c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4116c6b900c6SMatthew G. Knepley 
4117c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4118c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41195dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
41205dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
41215dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
41225dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
41235dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4124c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4125c6b900c6SMatthew G. Knepley }
4126c6b900c6SMatthew G. Knepley 
4127c6b900c6SMatthew G. Knepley #undef __FUNCT__
4128e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4129e87bb0d3SMatthew G Knepley /*@
4130e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4131e87bb0d3SMatthew G Knepley 
4132e87bb0d3SMatthew G Knepley   Not collective
4133e87bb0d3SMatthew G Knepley 
4134e87bb0d3SMatthew G Knepley   Input Parameters:
4135e87bb0d3SMatthew G Knepley + dm - The DM
4136e87bb0d3SMatthew G Knepley - v - The Vec of points
4137e87bb0d3SMatthew G Knepley 
413861e3bb9bSMatthew G Knepley   Output Parameter:
4139e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4140e87bb0d3SMatthew G Knepley 
4141e87bb0d3SMatthew G Knepley   Level: developer
414261e3bb9bSMatthew G Knepley 
414361e3bb9bSMatthew G Knepley .keywords: point location, mesh
414461e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
414561e3bb9bSMatthew G Knepley @*/
4146e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4147e87bb0d3SMatthew G Knepley {
4148735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4149735aa83eSMatthew G Knepley 
4150e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4151e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4152e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4153e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
4154735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4155735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
415682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
4157e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4158e87bb0d3SMatthew G Knepley }
415914f150ffSMatthew G. Knepley 
416014f150ffSMatthew G. Knepley #undef __FUNCT__
416114f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4162f4d763aaSMatthew G. Knepley /*@
4163f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4164f4d763aaSMatthew G. Knepley 
4165f4d763aaSMatthew G. Knepley   Input Parameter:
4166f4d763aaSMatthew G. Knepley . dm - The original DM
4167f4d763aaSMatthew G. Knepley 
4168f4d763aaSMatthew G. Knepley   Output Parameter:
4169f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4170f4d763aaSMatthew G. Knepley 
4171f4d763aaSMatthew G. Knepley   Level: intermediate
4172f4d763aaSMatthew G. Knepley 
4173f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4174f4d763aaSMatthew G. Knepley @*/
417514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
417614f150ffSMatthew G. Knepley {
4177c26acbdeSMatthew G. Knepley   PetscSection   section;
4178c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
417914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
418014f150ffSMatthew G. Knepley 
418114f150ffSMatthew G. Knepley   PetscFunctionBegin;
418214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
418314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4184c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4185c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4186c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4187c26acbdeSMatthew G. Knepley     *odm = dm;
4188c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4189c26acbdeSMatthew G. Knepley   }
419014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4191c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
419214f150ffSMatthew G. Knepley     PetscSF      sf;
419314f150ffSMatthew G. Knepley 
419414f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
419514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
419614f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
419714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
419814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
419914f150ffSMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, &gsection);CHKERRQ(ierr);
420014f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
420114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
420214f150ffSMatthew G. Knepley   }
420314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
420414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
420514f150ffSMatthew G. Knepley }
4206f4d763aaSMatthew G. Knepley 
4207f4d763aaSMatthew G. Knepley #undef __FUNCT__
4208f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4209f4d763aaSMatthew G. Knepley /*@
4210cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4211f4d763aaSMatthew G. Knepley 
4212f4d763aaSMatthew G. Knepley   Input Parameter:
4213f4d763aaSMatthew G. Knepley . dm - The original DM
4214f4d763aaSMatthew G. Knepley 
4215cdb7a50dSMatthew G. Knepley   Output Parameters:
4216cdb7a50dSMatthew G. Knepley + num - The output sequence number
4217cdb7a50dSMatthew G. Knepley - val - The output sequence value
4218f4d763aaSMatthew G. Knepley 
4219f4d763aaSMatthew G. Knepley   Level: intermediate
4220f4d763aaSMatthew G. Knepley 
4221f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4222f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4223f4d763aaSMatthew G. Knepley 
4224f4d763aaSMatthew G. Knepley .seealso: VecView()
4225f4d763aaSMatthew G. Knepley @*/
4226cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4227f4d763aaSMatthew G. Knepley {
4228f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4229f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4230cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4231cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4232f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4233f4d763aaSMatthew G. Knepley }
4234f4d763aaSMatthew G. Knepley 
4235f4d763aaSMatthew G. Knepley #undef __FUNCT__
4236f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4237f4d763aaSMatthew G. Knepley /*@
4238cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4239f4d763aaSMatthew G. Knepley 
4240f4d763aaSMatthew G. Knepley   Input Parameters:
4241f4d763aaSMatthew G. Knepley + dm - The original DM
4242cdb7a50dSMatthew G. Knepley . num - The output sequence number
4243cdb7a50dSMatthew G. Knepley - val - The output sequence value
4244f4d763aaSMatthew G. Knepley 
4245f4d763aaSMatthew G. Knepley   Level: intermediate
4246f4d763aaSMatthew G. Knepley 
4247f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4248f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4249f4d763aaSMatthew G. Knepley 
4250f4d763aaSMatthew G. Knepley .seealso: VecView()
4251f4d763aaSMatthew G. Knepley @*/
4252cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4253f4d763aaSMatthew G. Knepley {
4254f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4255f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4256f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4257cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4258cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4259cdb7a50dSMatthew G. Knepley }
4260cdb7a50dSMatthew G. Knepley 
4261cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4262cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4263cdb7a50dSMatthew G. Knepley /*@C
4264cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4265cdb7a50dSMatthew G. Knepley 
4266cdb7a50dSMatthew G. Knepley   Input Parameters:
4267cdb7a50dSMatthew G. Knepley + dm   - The original DM
4268cdb7a50dSMatthew G. Knepley . name - The sequence name
4269cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4270cdb7a50dSMatthew G. Knepley 
4271cdb7a50dSMatthew G. Knepley   Output Parameter:
4272cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4273cdb7a50dSMatthew G. Knepley 
4274cdb7a50dSMatthew G. Knepley   Level: intermediate
4275cdb7a50dSMatthew G. Knepley 
4276cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4277cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4278cdb7a50dSMatthew G. Knepley 
4279cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4280cdb7a50dSMatthew G. Knepley @*/
4281cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4282cdb7a50dSMatthew G. Knepley {
4283cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4284cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4285cdb7a50dSMatthew G. Knepley 
4286cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4287cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4288cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4289cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4290cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4291cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4292cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4293cdb7a50dSMatthew G. Knepley     PetscScalar value;
4294cdb7a50dSMatthew G. Knepley 
4295cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
42964aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4297cdb7a50dSMatthew G. Knepley #endif
4298cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4299f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4300f4d763aaSMatthew G. Knepley }
4301