xref: /petsc/src/dm/interface/dm.c (revision 13bffc4c19c2998a938fbad31aa63d4d9e91fb7b)
1af0996ceSBarry Smith #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;
6cea54e9dSPatrick Farrell PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_CreateInterpolation;
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 
288472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
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 
4373107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
44e7c4fc90SDmitry Karpeev 
450298fd71SBarry Smith   v->ltogmap                  = NULL;
461411c6eeSJed Brown   v->bs                       = 1;
47171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4888ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
500298fd71SBarry Smith   v->defaultSection           = NULL;
510298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
52fba222abSToby Isaac   v->defaultConstraintSection = NULL;
53fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
54c6b900c6SMatthew G. Knepley   v->L                        = NULL;
55c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
565dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
579a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
58435a35e8SMatthew G Knepley   {
59435a35e8SMatthew G Knepley     PetscInt i;
60435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
610298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
62435a35e8SMatthew G Knepley     }
63435a35e8SMatthew G Knepley   }
642764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6514f150ffSMatthew G. Knepley   v->dmBC = NULL;
66f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
67cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
68c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
69b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
701411c6eeSJed Brown   *dm = v;
71a4121054SBarry Smith   PetscFunctionReturn(0);
72a4121054SBarry Smith }
73a4121054SBarry Smith 
74a4121054SBarry Smith #undef __FUNCT__
7538221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
7638221697SMatthew G. Knepley /*@
7738221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
7838221697SMatthew G. Knepley 
7938221697SMatthew G. Knepley   Collective on MPI_Comm
8038221697SMatthew G. Knepley 
8138221697SMatthew G. Knepley   Input Parameter:
8238221697SMatthew G. Knepley . dm - The original DM object
8338221697SMatthew G. Knepley 
8438221697SMatthew G. Knepley   Output Parameter:
8538221697SMatthew G. Knepley . newdm  - The new DM object
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley   Level: beginner
8838221697SMatthew G. Knepley 
8938221697SMatthew G. Knepley .keywords: DM, topology, create
9038221697SMatthew G. Knepley @*/
9138221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9238221697SMatthew G. Knepley {
9338221697SMatthew G. Knepley   PetscSF        sf;
9438221697SMatthew G. Knepley   Vec            coords;
9538221697SMatthew G. Knepley   void          *ctx;
961de53e9aSMatthew G. Knepley   PetscInt       dim;
9738221697SMatthew G. Knepley   PetscErrorCode ierr;
9838221697SMatthew G. Knepley 
9938221697SMatthew G. Knepley   PetscFunctionBegin;
10038221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10138221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10238221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
1031de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1041de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
10538221697SMatthew G. Knepley   if (dm->ops->clone) {
10638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10738221697SMatthew G. Knepley   }
108cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
10938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
11138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
11238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
113be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
114be4c1c3eSMatthew G. Knepley     DM           ncdm;
115be4c1c3eSMatthew G. Knepley     PetscSection cs;
116be4c1c3eSMatthew G. Knepley     PetscInt     pEnd = -1;
117be4c1c3eSMatthew G. Knepley 
118be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
119be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
120be4c1c3eSMatthew G. Knepley     if (pEnd >= 0) {
121be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
122be4c1c3eSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
123*13bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
124be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
125be4c1c3eSMatthew G. Knepley     }
126be4c1c3eSMatthew G. Knepley   }
12738221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
12838221697SMatthew G. Knepley   if (coords) {
12938221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
13038221697SMatthew G. Knepley   } else {
13138221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
13238221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
13338221697SMatthew G. Knepley   }
134c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
135c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1365dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1375dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1385dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
139c6b900c6SMatthew G. Knepley   }
14038221697SMatthew G. Knepley   PetscFunctionReturn(0);
14138221697SMatthew G. Knepley }
14238221697SMatthew G. Knepley 
14338221697SMatthew G. Knepley #undef __FUNCT__
1449a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1459a42bb27SBarry Smith /*@C
146564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1479a42bb27SBarry Smith 
1488472ad0fSDave May    Logically Collective on DM
1499a42bb27SBarry Smith 
1509a42bb27SBarry Smith    Input Parameter:
1519a42bb27SBarry Smith +  da - initial distributed array
1528154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1539a42bb27SBarry Smith 
1549a42bb27SBarry Smith    Options Database:
155dd85299cSBarry Smith .   -dm_vec_type ctype
1569a42bb27SBarry Smith 
1579a42bb27SBarry Smith    Level: intermediate
1589a42bb27SBarry Smith 
1598472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1609a42bb27SBarry Smith @*/
16119fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1629a42bb27SBarry Smith {
1639a42bb27SBarry Smith   PetscErrorCode ierr;
1649a42bb27SBarry Smith 
1659a42bb27SBarry Smith   PetscFunctionBegin;
1669a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1679a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
16819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1699a42bb27SBarry Smith   PetscFunctionReturn(0);
1709a42bb27SBarry Smith }
1719a42bb27SBarry Smith 
1729a42bb27SBarry Smith #undef __FUNCT__
173c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
174c0dedaeaSBarry Smith /*@C
175c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
176c0dedaeaSBarry Smith 
1778472ad0fSDave May    Logically Collective on DM
178c0dedaeaSBarry Smith 
179c0dedaeaSBarry Smith    Input Parameter:
180c0dedaeaSBarry Smith .  da - initial distributed array
181c0dedaeaSBarry Smith 
182c0dedaeaSBarry Smith    Output Parameter:
183c0dedaeaSBarry Smith .  ctype - the vector type
184c0dedaeaSBarry Smith 
185c0dedaeaSBarry Smith    Level: intermediate
186c0dedaeaSBarry Smith 
1878472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
188c0dedaeaSBarry Smith @*/
189c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
190c0dedaeaSBarry Smith {
191c0dedaeaSBarry Smith   PetscFunctionBegin;
192c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
193c0dedaeaSBarry Smith   *ctype = da->vectype;
194c0dedaeaSBarry Smith   PetscFunctionReturn(0);
195c0dedaeaSBarry Smith }
196c0dedaeaSBarry Smith 
197c0dedaeaSBarry Smith #undef __FUNCT__
1985f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1995f1ad066SMatthew G Knepley /*@
20034f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2015f1ad066SMatthew G Knepley 
2025f1ad066SMatthew G Knepley   Not collective
2035f1ad066SMatthew G Knepley 
2045f1ad066SMatthew G Knepley   Input Parameter:
2055f1ad066SMatthew G Knepley . v - The Vec
2065f1ad066SMatthew G Knepley 
2075f1ad066SMatthew G Knepley   Output Parameter:
2085f1ad066SMatthew G Knepley . dm - The DM
2095f1ad066SMatthew G Knepley 
2105f1ad066SMatthew G Knepley   Level: intermediate
2115f1ad066SMatthew G Knepley 
2125f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2135f1ad066SMatthew G Knepley @*/
2145f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2155f1ad066SMatthew G Knepley {
2165f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley   PetscFunctionBegin;
2195f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2205f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2215f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2225f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2235f1ad066SMatthew G Knepley }
2245f1ad066SMatthew G Knepley 
2255f1ad066SMatthew G Knepley #undef __FUNCT__
2265f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2275f1ad066SMatthew G Knepley /*@
228d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   Not collective
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley   Input Parameters:
2335f1ad066SMatthew G Knepley + v - The Vec
2345f1ad066SMatthew G Knepley - dm - The DM
2355f1ad066SMatthew G Knepley 
236d9805387SMatthew G. Knepley   Note: This is NOT the same as DMCreateGlobalVector() since it does not change the view methods or perform other customization, but merely sets the DM member.
237d9805387SMatthew G. Knepley 
2385f1ad066SMatthew G Knepley   Level: intermediate
2395f1ad066SMatthew G Knepley 
2405f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2415f1ad066SMatthew G Knepley @*/
2425f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2435f1ad066SMatthew G Knepley {
2445f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   PetscFunctionBegin;
2475f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
248d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2495f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2505f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2515f1ad066SMatthew G Knepley }
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley #undef __FUNCT__
254521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
255521d9a4cSLisandro Dalcin /*@C
256521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
257521d9a4cSLisandro Dalcin 
258521d9a4cSLisandro Dalcin    Logically Collective on DM
259521d9a4cSLisandro Dalcin 
260521d9a4cSLisandro Dalcin    Input Parameter:
261521d9a4cSLisandro Dalcin +  dm - the DM context
262521d9a4cSLisandro Dalcin .  ctype - the matrix type
263521d9a4cSLisandro Dalcin 
264521d9a4cSLisandro Dalcin    Options Database:
265521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
266521d9a4cSLisandro Dalcin 
267521d9a4cSLisandro Dalcin    Level: intermediate
268521d9a4cSLisandro Dalcin 
269c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
270521d9a4cSLisandro Dalcin @*/
27119fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
272521d9a4cSLisandro Dalcin {
273521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27488f0584fSBarry Smith 
275521d9a4cSLisandro Dalcin   PetscFunctionBegin;
276521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
277521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
27819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
279521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
280521d9a4cSLisandro Dalcin }
281521d9a4cSLisandro Dalcin 
282521d9a4cSLisandro Dalcin #undef __FUNCT__
283c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
284c0dedaeaSBarry Smith /*@C
285c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
286c0dedaeaSBarry Smith 
287c0dedaeaSBarry Smith    Logically Collective on DM
288c0dedaeaSBarry Smith 
289c0dedaeaSBarry Smith    Input Parameter:
290c0dedaeaSBarry Smith .  dm - the DM context
291c0dedaeaSBarry Smith 
292c0dedaeaSBarry Smith    Output Parameter:
293c0dedaeaSBarry Smith .  ctype - the matrix type
294c0dedaeaSBarry Smith 
295c0dedaeaSBarry Smith    Options Database:
296c0dedaeaSBarry Smith .   -dm_mat_type ctype
297c0dedaeaSBarry Smith 
298c0dedaeaSBarry Smith    Level: intermediate
299c0dedaeaSBarry Smith 
300c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
301c0dedaeaSBarry Smith @*/
302c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
303c0dedaeaSBarry Smith {
304c0dedaeaSBarry Smith   PetscFunctionBegin;
305c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
306c0dedaeaSBarry Smith   *ctype = dm->mattype;
307c0dedaeaSBarry Smith   PetscFunctionReturn(0);
308c0dedaeaSBarry Smith }
309c0dedaeaSBarry Smith 
310c0dedaeaSBarry Smith #undef __FUNCT__
311c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
312c688c046SMatthew G Knepley /*@
31334f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
314c688c046SMatthew G Knepley 
315c688c046SMatthew G Knepley   Not collective
316c688c046SMatthew G Knepley 
317c688c046SMatthew G Knepley   Input Parameter:
318c688c046SMatthew G Knepley . A - The Mat
319c688c046SMatthew G Knepley 
320c688c046SMatthew G Knepley   Output Parameter:
321c688c046SMatthew G Knepley . dm - The DM
322c688c046SMatthew G Knepley 
323c688c046SMatthew G Knepley   Level: intermediate
324c688c046SMatthew G Knepley 
325c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
326c688c046SMatthew G Knepley @*/
327c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
328c688c046SMatthew G Knepley {
329c688c046SMatthew G Knepley   PetscErrorCode ierr;
330c688c046SMatthew G Knepley 
331c688c046SMatthew G Knepley   PetscFunctionBegin;
332c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
333c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
334c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
335c688c046SMatthew G Knepley   PetscFunctionReturn(0);
336c688c046SMatthew G Knepley }
337c688c046SMatthew G Knepley 
338c688c046SMatthew G Knepley #undef __FUNCT__
339c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
340c688c046SMatthew G Knepley /*@
341c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
342c688c046SMatthew G Knepley 
343c688c046SMatthew G Knepley   Not collective
344c688c046SMatthew G Knepley 
345c688c046SMatthew G Knepley   Input Parameters:
346c688c046SMatthew G Knepley + A - The Mat
347c688c046SMatthew G Knepley - dm - The DM
348c688c046SMatthew G Knepley 
349c688c046SMatthew G Knepley   Level: intermediate
350c688c046SMatthew G Knepley 
351c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
352c688c046SMatthew G Knepley @*/
353c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
354c688c046SMatthew G Knepley {
355c688c046SMatthew G Knepley   PetscErrorCode ierr;
356c688c046SMatthew G Knepley 
357c688c046SMatthew G Knepley   PetscFunctionBegin;
358c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3598865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
360c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
361c688c046SMatthew G Knepley   PetscFunctionReturn(0);
362c688c046SMatthew G Knepley }
363c688c046SMatthew G Knepley 
364c688c046SMatthew G Knepley #undef __FUNCT__
3659a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3669a42bb27SBarry Smith /*@C
3679a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3686757b960SDave May    DM options in the database.
3699a42bb27SBarry Smith 
3708353ddbbSDave May    Logically Collective on DM
3719a42bb27SBarry Smith 
3729a42bb27SBarry Smith    Input Parameter:
3738353ddbbSDave May +  da - the DM context
3749a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3759a42bb27SBarry Smith 
3769a42bb27SBarry Smith    Notes:
3779a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3789a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3799a42bb27SBarry Smith 
3809a42bb27SBarry Smith    Level: advanced
3819a42bb27SBarry Smith 
3828353ddbbSDave May .keywords: DM, set, options, prefix, database
3839a42bb27SBarry Smith 
3849a42bb27SBarry Smith .seealso: DMSetFromOptions()
3859a42bb27SBarry Smith @*/
3867087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3879a42bb27SBarry Smith {
3889a42bb27SBarry Smith   PetscErrorCode ierr;
3899a42bb27SBarry Smith 
3909a42bb27SBarry Smith   PetscFunctionBegin;
3919a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3929a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
393691be533SLawrence Mitchell   if (dm->sf) {
394691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
395691be533SLawrence Mitchell   }
396691be533SLawrence Mitchell   if (dm->defaultSF) {
397691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
398691be533SLawrence Mitchell   }
3999a42bb27SBarry Smith   PetscFunctionReturn(0);
4009a42bb27SBarry Smith }
4019a42bb27SBarry Smith 
4029a42bb27SBarry Smith #undef __FUNCT__
40331697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
40431697293SDave May /*@C
40531697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
40631697293SDave May    DM options in the database.
40731697293SDave May 
40831697293SDave May    Logically Collective on DM
40931697293SDave May 
41031697293SDave May    Input Parameters:
41131697293SDave May +  dm - the DM context
41231697293SDave May -  prefix - the prefix string to prepend to all DM option requests
41331697293SDave May 
41431697293SDave May    Notes:
41531697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
41631697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
41731697293SDave May 
41831697293SDave May    Level: advanced
41931697293SDave May 
42031697293SDave May .keywords: DM, append, options, prefix, database
42131697293SDave May 
42231697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
42331697293SDave May @*/
42431697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
42531697293SDave May {
42631697293SDave May   PetscErrorCode ierr;
42731697293SDave May 
42831697293SDave May   PetscFunctionBegin;
42931697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43031697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
43131697293SDave May   PetscFunctionReturn(0);
43231697293SDave May }
43331697293SDave May 
43431697293SDave May #undef __FUNCT__
43531697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
43631697293SDave May /*@C
43731697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
43831697293SDave May    DM options in the database.
43931697293SDave May 
44031697293SDave May    Not Collective
44131697293SDave May 
44231697293SDave May    Input Parameters:
44331697293SDave May .  dm - the DM context
44431697293SDave May 
44531697293SDave May    Output Parameters:
44631697293SDave May .  prefix - pointer to the prefix string used is returned
44731697293SDave May 
44831697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
44931697293SDave May    sufficient length to hold the prefix.
45031697293SDave May 
45131697293SDave May    Level: advanced
45231697293SDave May 
45331697293SDave May .keywords: DM, set, options, prefix, database
45431697293SDave May 
45531697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
45631697293SDave May @*/
45731697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
45831697293SDave May {
45931697293SDave May   PetscErrorCode ierr;
46031697293SDave May 
46131697293SDave May   PetscFunctionBegin;
46231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46331697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
46431697293SDave May   PetscFunctionReturn(0);
46531697293SDave May }
46631697293SDave May 
46731697293SDave May #undef __FUNCT__
46847c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
46947c6ae99SBarry Smith /*@
4708472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
47147c6ae99SBarry Smith 
47247c6ae99SBarry Smith     Collective on DM
47347c6ae99SBarry Smith 
47447c6ae99SBarry Smith     Input Parameter:
47547c6ae99SBarry Smith .   dm - the DM object to destroy
47647c6ae99SBarry Smith 
47747c6ae99SBarry Smith     Level: developer
47847c6ae99SBarry Smith 
479e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
48047c6ae99SBarry Smith 
48147c6ae99SBarry Smith @*/
482fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
48347c6ae99SBarry Smith {
484c83e3748SMatthew G. Knepley   PetscInt       i, cnt = 0;
485dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
48647c6ae99SBarry Smith   PetscErrorCode ierr;
48747c6ae99SBarry Smith 
48847c6ae99SBarry Smith   PetscFunctionBegin;
4896bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4906bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
49187e657c6SBarry Smith 
49287e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
493732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4948865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4958865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
496732e2eb9SMatthew G Knepley   }
497dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4982348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4992348bcf4SPeter Brune   if ((*dm)->x) {
5002348bcf4SPeter Brune     DM obj;
5012348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
5022348bcf4SPeter Brune     if (obj == *dm) cnt++;
5032348bcf4SPeter Brune   }
504732e2eb9SMatthew G Knepley 
5056bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
506732e2eb9SMatthew G Knepley   /*
507732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
508732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
509732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
510732e2eb9SMatthew G Knepley   */
5116bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5126bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
513732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5146bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5156bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
516732e2eb9SMatthew G Knepley   }
517f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5180298fd71SBarry Smith   (*dm)->namedglobal = NULL;
519f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5202348bcf4SPeter Brune     nnext = nlink->next;
5212348bcf4SPeter 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);
5222348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5232348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5242348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5252348bcf4SPeter Brune   }
526f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5270298fd71SBarry Smith   (*dm)->namedlocal = NULL;
528f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
529f490541aSPeter Brune     nnext = nlink->next;
530f490541aSPeter 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);
531f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
532f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
533f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
534f490541aSPeter Brune   }
5352348bcf4SPeter Brune 
536b17ce1afSJed Brown   /* Destroy the list of hooks */
537c833c3b5SJed Brown   {
538c833c3b5SJed Brown     DMCoarsenHookLink link,next;
539b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
540b17ce1afSJed Brown       next = link->next;
541b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
542b17ce1afSJed Brown     }
5430298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
544c833c3b5SJed Brown   }
545c833c3b5SJed Brown   {
546c833c3b5SJed Brown     DMRefineHookLink link,next;
547c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
548c833c3b5SJed Brown       next = link->next;
549c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
550c833c3b5SJed Brown     }
5510298fd71SBarry Smith     (*dm)->refinehook = NULL;
552c833c3b5SJed Brown   }
553be081cd6SPeter Brune   {
554be081cd6SPeter Brune     DMSubDomainHookLink link,next;
555be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
556be081cd6SPeter Brune       next = link->next;
557be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
558be081cd6SPeter Brune     }
5590298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
560be081cd6SPeter Brune   }
561baf369e7SPeter Brune   {
562baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
563baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
564baf369e7SPeter Brune       next = link->next;
565baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
566baf369e7SPeter Brune     }
5670298fd71SBarry Smith     (*dm)->gtolhook = NULL;
568baf369e7SPeter Brune   }
569d4d07f1eSToby Isaac   {
570d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
571d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
572d4d07f1eSToby Isaac       next = link->next;
573d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
574d4d07f1eSToby Isaac     }
575d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
576d4d07f1eSToby Isaac   }
577aa1993deSMatthew G Knepley   /* Destroy the work arrays */
578aa1993deSMatthew G Knepley   {
579aa1993deSMatthew G Knepley     DMWorkLink link,next;
580aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
581aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
582aa1993deSMatthew G Knepley       next = link->next;
583aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
584aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
585aa1993deSMatthew G Knepley     }
5860298fd71SBarry Smith     (*dm)->workin = NULL;
587aa1993deSMatthew G Knepley   }
588b17ce1afSJed Brown 
58952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
59052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
59152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
59252536dc3SBarry Smith 
5931a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5941a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5951a266240SBarry Smith   }
59687e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
59771cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5984dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5996bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6006bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
601073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
60288ed4aceSMatthew G Knepley 
60388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
60488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6058b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
606fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
607fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
60888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
60988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6108e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
611af122d2aSMatthew G Knepley 
6126636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6136636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6146636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6155dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6166636e97aSMatthew G Knepley 
6172764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
61814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
619e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
620e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
621732e2eb9SMatthew G Knepley 
6226bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
623435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
624732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
62547c6ae99SBarry Smith   PetscFunctionReturn(0);
62647c6ae99SBarry Smith }
62747c6ae99SBarry Smith 
62847c6ae99SBarry Smith #undef __FUNCT__
629d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
630d7bf68aeSBarry Smith /*@
631d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
632d7bf68aeSBarry Smith 
633d7bf68aeSBarry Smith     Collective on DM
634d7bf68aeSBarry Smith 
635d7bf68aeSBarry Smith     Input Parameter:
636d7bf68aeSBarry Smith .   dm - the DM object to setup
637d7bf68aeSBarry Smith 
638d7bf68aeSBarry Smith     Level: developer
639d7bf68aeSBarry Smith 
640e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
641d7bf68aeSBarry Smith 
642d7bf68aeSBarry Smith @*/
6437087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
644d7bf68aeSBarry Smith {
645d7bf68aeSBarry Smith   PetscErrorCode ierr;
646d7bf68aeSBarry Smith 
647d7bf68aeSBarry Smith   PetscFunctionBegin;
648171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6498387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
650d7bf68aeSBarry Smith   if (dm->ops->setup) {
651d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
652d7bf68aeSBarry Smith   }
6538387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
654d7bf68aeSBarry Smith   PetscFunctionReturn(0);
655d7bf68aeSBarry Smith }
656d7bf68aeSBarry Smith 
657d7bf68aeSBarry Smith #undef __FUNCT__
658d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
659d7bf68aeSBarry Smith /*@
660d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
661d7bf68aeSBarry Smith 
662d7bf68aeSBarry Smith     Collective on DM
663d7bf68aeSBarry Smith 
664d7bf68aeSBarry Smith     Input Parameter:
665d7bf68aeSBarry Smith .   dm - the DM object to set options for
666d7bf68aeSBarry Smith 
667732e2eb9SMatthew G Knepley     Options Database:
6684164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
6694164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
6704164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
6714164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
672732e2eb9SMatthew G Knepley 
673d7bf68aeSBarry Smith     Level: developer
674d7bf68aeSBarry Smith 
675e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
676d7bf68aeSBarry Smith 
677d7bf68aeSBarry Smith @*/
6787087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
679d7bf68aeSBarry Smith {
6807781c08eSBarry Smith   char           typeName[256];
681ca266f36SBarry Smith   PetscBool      flg;
682d7bf68aeSBarry Smith   PetscErrorCode ierr;
683d7bf68aeSBarry Smith 
684d7bf68aeSBarry Smith   PetscFunctionBegin;
685171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
686691be533SLawrence Mitchell   if (dm->sf) {
687691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
688691be533SLawrence Mitchell   }
689691be533SLawrence Mitchell   if (dm->defaultSF) {
690691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
691691be533SLawrence Mitchell   }
6923194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
6930298fd71SBarry 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);
694a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
695f9ba7244SBarry Smith   if (flg) {
696f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
697f9ba7244SBarry Smith   }
698a264d7a6SBarry 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);
699073dac72SJed Brown   if (flg) {
700521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
701073dac72SJed Brown   }
7020298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
703f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
704e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
705f9ba7244SBarry Smith   }
706f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7070633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
70882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
709d7bf68aeSBarry Smith   PetscFunctionReturn(0);
710d7bf68aeSBarry Smith }
711d7bf68aeSBarry Smith 
712d7bf68aeSBarry Smith #undef __FUNCT__
71347c6ae99SBarry Smith #define __FUNCT__ "DMView"
714fc9bc008SSatish Balay /*@C
715224748a4SBarry Smith     DMView - Views a DM
71647c6ae99SBarry Smith 
71747c6ae99SBarry Smith     Collective on DM
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith     Input Parameter:
72047c6ae99SBarry Smith +   dm - the DM object to view
72147c6ae99SBarry Smith -   v - the viewer
72247c6ae99SBarry Smith 
723224748a4SBarry Smith     Level: beginner
72447c6ae99SBarry Smith 
725e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
72647c6ae99SBarry Smith 
72747c6ae99SBarry Smith @*/
7287087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
72947c6ae99SBarry Smith {
73047c6ae99SBarry Smith   PetscErrorCode ierr;
73132c0f0efSBarry Smith   PetscBool      isbinary;
73247c6ae99SBarry Smith 
73347c6ae99SBarry Smith   PetscFunctionBegin;
734171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7353014e516SBarry Smith   if (!v) {
736ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
7373014e516SBarry Smith   }
73898c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
73932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
74032c0f0efSBarry Smith   if (isbinary) {
74155849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
74232c0f0efSBarry Smith     char     type[256];
74332c0f0efSBarry Smith 
74432c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
74532c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
74632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
74732c0f0efSBarry Smith   }
7480c010503SBarry Smith   if (dm->ops->view) {
7490c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
75047c6ae99SBarry Smith   }
75147c6ae99SBarry Smith   PetscFunctionReturn(0);
75247c6ae99SBarry Smith }
75347c6ae99SBarry Smith 
75447c6ae99SBarry Smith #undef __FUNCT__
75547c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
75647c6ae99SBarry Smith /*@
7578472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
75847c6ae99SBarry Smith 
75947c6ae99SBarry Smith     Collective on DM
76047c6ae99SBarry Smith 
76147c6ae99SBarry Smith     Input Parameter:
76247c6ae99SBarry Smith .   dm - the DM object
76347c6ae99SBarry Smith 
76447c6ae99SBarry Smith     Output Parameter:
76547c6ae99SBarry Smith .   vec - the global vector
76647c6ae99SBarry Smith 
767073dac72SJed Brown     Level: beginner
76847c6ae99SBarry Smith 
769e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
77047c6ae99SBarry Smith 
77147c6ae99SBarry Smith @*/
7727087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
77347c6ae99SBarry Smith {
77447c6ae99SBarry Smith   PetscErrorCode ierr;
77547c6ae99SBarry Smith 
77647c6ae99SBarry Smith   PetscFunctionBegin;
777171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77847c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
77947c6ae99SBarry Smith   PetscFunctionReturn(0);
78047c6ae99SBarry Smith }
78147c6ae99SBarry Smith 
78247c6ae99SBarry Smith #undef __FUNCT__
78347c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
78447c6ae99SBarry Smith /*@
7858472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
78647c6ae99SBarry Smith 
78747c6ae99SBarry Smith     Not Collective
78847c6ae99SBarry Smith 
78947c6ae99SBarry Smith     Input Parameter:
79047c6ae99SBarry Smith .   dm - the DM object
79147c6ae99SBarry Smith 
79247c6ae99SBarry Smith     Output Parameter:
79347c6ae99SBarry Smith .   vec - the local vector
79447c6ae99SBarry Smith 
795073dac72SJed Brown     Level: beginner
79647c6ae99SBarry Smith 
797e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
79847c6ae99SBarry Smith 
79947c6ae99SBarry Smith @*/
8007087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
80147c6ae99SBarry Smith {
80247c6ae99SBarry Smith   PetscErrorCode ierr;
80347c6ae99SBarry Smith 
80447c6ae99SBarry Smith   PetscFunctionBegin;
805171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80647c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
80747c6ae99SBarry Smith   PetscFunctionReturn(0);
80847c6ae99SBarry Smith }
80947c6ae99SBarry Smith 
81047c6ae99SBarry Smith #undef __FUNCT__
8111411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
8121411c6eeSJed Brown /*@
8131411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8141411c6eeSJed Brown 
8151411c6eeSJed Brown    Collective on DM
8161411c6eeSJed Brown 
8171411c6eeSJed Brown    Input Parameter:
8181411c6eeSJed Brown .  dm - the DM that provides the mapping
8191411c6eeSJed Brown 
8201411c6eeSJed Brown    Output Parameter:
8211411c6eeSJed Brown .  ltog - the mapping
8221411c6eeSJed Brown 
8231411c6eeSJed Brown    Level: intermediate
8241411c6eeSJed Brown 
8251411c6eeSJed Brown    Notes:
8261411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8271411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8281411c6eeSJed Brown 
829fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8301411c6eeSJed Brown @*/
8317087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8321411c6eeSJed Brown {
8331411c6eeSJed Brown   PetscErrorCode ierr;
8341411c6eeSJed Brown 
8351411c6eeSJed Brown   PetscFunctionBegin;
8361411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8371411c6eeSJed Brown   PetscValidPointer(ltog,2);
8381411c6eeSJed Brown   if (!dm->ltogmap) {
83937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
84037d0c07bSMatthew G Knepley 
84137d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
84237d0c07bSMatthew G Knepley     if (section) {
84337d0c07bSMatthew G Knepley       PetscInt *ltog;
84437d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
84537d0c07bSMatthew G Knepley 
84637d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
84737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
84837d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
849785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
85037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
85137d0c07bSMatthew G Knepley         PetscInt dof, off, c;
85237d0c07bSMatthew G Knepley 
85337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
85437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
85537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
85637d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
85737d0c07bSMatthew G Knepley           ltog[l] = off+c;
85837d0c07bSMatthew G Knepley         }
85937d0c07bSMatthew G Knepley       }
860f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
8613bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
86237d0c07bSMatthew G Knepley     } else {
863184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
864184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
8651411c6eeSJed Brown     }
86637d0c07bSMatthew G Knepley   }
8671411c6eeSJed Brown   *ltog = dm->ltogmap;
8681411c6eeSJed Brown   PetscFunctionReturn(0);
8691411c6eeSJed Brown }
8701411c6eeSJed Brown 
8711411c6eeSJed Brown #undef __FUNCT__
8721411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
8731411c6eeSJed Brown /*@
8741411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
8751411c6eeSJed Brown 
8761411c6eeSJed Brown    Not Collective
8771411c6eeSJed Brown 
8781411c6eeSJed Brown    Input Parameter:
8791411c6eeSJed Brown .  dm - the DM with block structure
8801411c6eeSJed Brown 
8811411c6eeSJed Brown    Output Parameter:
8821411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
8831411c6eeSJed Brown 
8841411c6eeSJed Brown    Level: intermediate
8851411c6eeSJed Brown 
886fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
8871411c6eeSJed Brown @*/
8887087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8891411c6eeSJed Brown {
8901411c6eeSJed Brown   PetscFunctionBegin;
8911411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8921411c6eeSJed Brown   PetscValidPointer(bs,2);
8931411c6eeSJed 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");
8941411c6eeSJed Brown   *bs = dm->bs;
8951411c6eeSJed Brown   PetscFunctionReturn(0);
8961411c6eeSJed Brown }
8971411c6eeSJed Brown 
8981411c6eeSJed Brown #undef __FUNCT__
899e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
90047c6ae99SBarry Smith /*@
9018472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
90247c6ae99SBarry Smith 
90347c6ae99SBarry Smith     Collective on DM
90447c6ae99SBarry Smith 
90547c6ae99SBarry Smith     Input Parameter:
90647c6ae99SBarry Smith +   dm1 - the DM object
90747c6ae99SBarry Smith -   dm2 - the second, finer DM object
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith     Output Parameter:
91047c6ae99SBarry Smith +  mat - the interpolation
91147c6ae99SBarry Smith -  vec - the scaling (optional)
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith     Level: developer
91447c6ae99SBarry Smith 
91585afcc9aSBarry 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
91685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
917d52bd9f3SBarry Smith 
9181f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
919d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
92085afcc9aSBarry Smith 
92185afcc9aSBarry Smith 
922e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith @*/
925e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
92647c6ae99SBarry Smith {
92747c6ae99SBarry Smith   PetscErrorCode ierr;
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith   PetscFunctionBegin;
930171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
931171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
932cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
93325296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
934cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
93547c6ae99SBarry Smith   PetscFunctionReturn(0);
93647c6ae99SBarry Smith }
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith #undef __FUNCT__
939e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
94047c6ae99SBarry Smith /*@
9418472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith     Collective on DM
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith     Input Parameter:
94647c6ae99SBarry Smith +   dm1 - the DM object
94747c6ae99SBarry Smith -   dm2 - the second, finer DM object
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith     Output Parameter:
9506dbf9973SLawrence Mitchell .   mat - the injection
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith     Level: developer
95347c6ae99SBarry Smith 
95485afcc9aSBarry 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
95585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
95685afcc9aSBarry Smith 
957e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
95847c6ae99SBarry Smith 
95947c6ae99SBarry Smith @*/
9606dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
96147c6ae99SBarry Smith {
96247c6ae99SBarry Smith   PetscErrorCode ierr;
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith   PetscFunctionBegin;
965171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
966171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
9676dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
96847c6ae99SBarry Smith   PetscFunctionReturn(0);
96947c6ae99SBarry Smith }
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith #undef __FUNCT__
972e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
973b412c318SBarry Smith /*@
974b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
97547c6ae99SBarry Smith 
97647c6ae99SBarry Smith     Collective on DM
97747c6ae99SBarry Smith 
97847c6ae99SBarry Smith     Input Parameter:
97947c6ae99SBarry Smith +   dm - the DM object
980b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
98147c6ae99SBarry Smith 
98247c6ae99SBarry Smith     Output Parameter:
98347c6ae99SBarry Smith .   coloring - the coloring
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith     Level: developer
98647c6ae99SBarry Smith 
987b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
98847c6ae99SBarry Smith 
989aab9d709SJed Brown @*/
990b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
99147c6ae99SBarry Smith {
99247c6ae99SBarry Smith   PetscErrorCode ierr;
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith   PetscFunctionBegin;
995171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
996ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
997b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
99847c6ae99SBarry Smith   PetscFunctionReturn(0);
99947c6ae99SBarry Smith }
100047c6ae99SBarry Smith 
100147c6ae99SBarry Smith #undef __FUNCT__
1002950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1003b412c318SBarry Smith /*@
10048472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
100547c6ae99SBarry Smith 
100647c6ae99SBarry Smith     Collective on DM
100747c6ae99SBarry Smith 
100847c6ae99SBarry Smith     Input Parameter:
1009b412c318SBarry Smith .   dm - the DM object
101047c6ae99SBarry Smith 
101147c6ae99SBarry Smith     Output Parameter:
101247c6ae99SBarry Smith .   mat - the empty Jacobian
101347c6ae99SBarry Smith 
1014073dac72SJed Brown     Level: beginner
101547c6ae99SBarry Smith 
101694013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
101794013140SBarry Smith        do not need to do it yourself.
101894013140SBarry Smith 
101994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1020aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
102194013140SBarry Smith 
102294013140SBarry 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
102394013140SBarry Smith        internally by PETSc.
102494013140SBarry Smith 
102594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1026aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
102794013140SBarry Smith 
1028b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
102947c6ae99SBarry Smith 
1030aab9d709SJed Brown @*/
1031b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
103247c6ae99SBarry Smith {
103347c6ae99SBarry Smith   PetscErrorCode ierr;
103447c6ae99SBarry Smith 
103547c6ae99SBarry Smith   PetscFunctionBegin;
1036171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1037607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1038c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1039c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1040b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
104147c6ae99SBarry Smith   PetscFunctionReturn(0);
104247c6ae99SBarry Smith }
104347c6ae99SBarry Smith 
104447c6ae99SBarry Smith #undef __FUNCT__
1045732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1046732e2eb9SMatthew G Knepley /*@
1047950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1048732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1049732e2eb9SMatthew G Knepley 
10508472ad0fSDave May   Logically Collective on DM
1051732e2eb9SMatthew G Knepley 
1052732e2eb9SMatthew G Knepley   Input Parameter:
1053732e2eb9SMatthew G Knepley + dm - the DM
1054732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1055732e2eb9SMatthew G Knepley 
1056732e2eb9SMatthew G Knepley   Level: developer
1057950540a4SJed Brown .seealso DMCreateMatrix()
1058732e2eb9SMatthew G Knepley @*/
1059732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1060732e2eb9SMatthew G Knepley {
1061732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1062732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1063732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1064732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1065732e2eb9SMatthew G Knepley }
1066732e2eb9SMatthew G Knepley 
1067732e2eb9SMatthew G Knepley #undef __FUNCT__
1068a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1069a89ea682SMatthew G Knepley /*@C
1070aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1071a89ea682SMatthew G Knepley 
1072a89ea682SMatthew G Knepley   Not Collective
1073a89ea682SMatthew G Knepley 
1074a89ea682SMatthew G Knepley   Input Parameters:
1075a89ea682SMatthew G Knepley + dm - the DM object
1076aa1993deSMatthew G Knepley . count - The minium size
1077aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1078a89ea682SMatthew G Knepley 
1079a89ea682SMatthew G Knepley   Output Parameter:
1080a89ea682SMatthew G Knepley . array - the work array
1081a89ea682SMatthew G Knepley 
1082a89ea682SMatthew G Knepley   Level: developer
1083a89ea682SMatthew G Knepley 
1084a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1085a89ea682SMatthew G Knepley @*/
1086aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1087a89ea682SMatthew G Knepley {
1088a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1089aa1993deSMatthew G Knepley   DMWorkLink     link;
1090854ce69bSBarry Smith   size_t         dsize;
1091a89ea682SMatthew G Knepley 
1092a89ea682SMatthew G Knepley   PetscFunctionBegin;
1093a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1094aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1095aa1993deSMatthew G Knepley   if (dm->workin) {
1096aa1993deSMatthew G Knepley     link       = dm->workin;
1097aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1098aa1993deSMatthew G Knepley   } else {
1099b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1100a89ea682SMatthew G Knepley   }
1101854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1102854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1103aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1104854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1105854ce69bSBarry Smith     link->bytes = dsize*count;
1106aa1993deSMatthew G Knepley   }
1107aa1993deSMatthew G Knepley   link->next   = dm->workout;
1108aa1993deSMatthew G Knepley   dm->workout  = link;
1109aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1110a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1111a89ea682SMatthew G Knepley }
1112a89ea682SMatthew G Knepley 
1113aa1993deSMatthew G Knepley #undef __FUNCT__
1114aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1115aa1993deSMatthew G Knepley /*@C
1116aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1117aa1993deSMatthew G Knepley 
1118aa1993deSMatthew G Knepley   Not Collective
1119aa1993deSMatthew G Knepley 
1120aa1993deSMatthew G Knepley   Input Parameters:
1121aa1993deSMatthew G Knepley + dm - the DM object
1122aa1993deSMatthew G Knepley . count - The minium size
1123aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1124aa1993deSMatthew G Knepley 
1125aa1993deSMatthew G Knepley   Output Parameter:
1126aa1993deSMatthew G Knepley . array - the work array
1127aa1993deSMatthew G Knepley 
1128aa1993deSMatthew G Knepley   Level: developer
1129aa1993deSMatthew G Knepley 
1130aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1131aa1993deSMatthew G Knepley @*/
1132aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1133aa1993deSMatthew G Knepley {
1134aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1135aa1993deSMatthew G Knepley 
1136aa1993deSMatthew G Knepley   PetscFunctionBegin;
1137aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1138aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1139aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1140aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1141aa1993deSMatthew G Knepley       *p           = link->next;
1142aa1993deSMatthew G Knepley       link->next   = dm->workin;
1143aa1993deSMatthew G Knepley       dm->workin   = link;
11440298fd71SBarry Smith       *(void**)mem = NULL;
1145aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1146aa1993deSMatthew G Knepley     }
1147aa1993deSMatthew G Knepley   }
1148aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1149aa1993deSMatthew G Knepley }
1150e7c4fc90SDmitry Karpeev 
1151e7c4fc90SDmitry Karpeev #undef __FUNCT__
1152435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1153435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1154435a35e8SMatthew G Knepley {
1155435a35e8SMatthew G Knepley   PetscFunctionBegin;
1156435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
115782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1158435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1159435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1160435a35e8SMatthew G Knepley }
1161435a35e8SMatthew G Knepley 
1162435a35e8SMatthew G Knepley #undef __FUNCT__
11634d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
11644f3b5142SJed Brown /*@C
11654d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
11664d343eeaSMatthew G Knepley 
11674d343eeaSMatthew G Knepley   Not collective
11684d343eeaSMatthew G Knepley 
11694d343eeaSMatthew G Knepley   Input Parameter:
11704d343eeaSMatthew G Knepley . dm - the DM object
11714d343eeaSMatthew G Knepley 
11724d343eeaSMatthew G Knepley   Output Parameters:
11730298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
11740298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
11750298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
11764d343eeaSMatthew G Knepley 
11774d343eeaSMatthew G Knepley   Level: intermediate
11784d343eeaSMatthew G Knepley 
117921c9b008SJed Brown   Notes:
118021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
118121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
118221c9b008SJed Brown   PetscFree().
118321c9b008SJed Brown 
11844d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11854d343eeaSMatthew G Knepley @*/
118637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11874d343eeaSMatthew G Knepley {
118837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11894d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11904d343eeaSMatthew G Knepley 
11914d343eeaSMatthew G Knepley   PetscFunctionBegin;
11924d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
119369ca1f37SDmitry Karpeev   if (numFields) {
119469ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
119569ca1f37SDmitry Karpeev     *numFields = 0;
119669ca1f37SDmitry Karpeev   }
119737d0c07bSMatthew G Knepley   if (fieldNames) {
119837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11990298fd71SBarry Smith     *fieldNames = NULL;
120069ca1f37SDmitry Karpeev   }
120169ca1f37SDmitry Karpeev   if (fields) {
120269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
12030298fd71SBarry Smith     *fields = NULL;
120469ca1f37SDmitry Karpeev   }
120537d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
120637d0c07bSMatthew G Knepley   if (section) {
120737d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
120837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
120937d0c07bSMatthew G Knepley 
121037d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
121137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1212dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
121337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
121437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
121537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
121637d0c07bSMatthew G Knepley     }
121737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
121837d0c07bSMatthew G Knepley       PetscInt gdof;
121937d0c07bSMatthew G Knepley 
122037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
122137d0c07bSMatthew G Knepley       if (gdof > 0) {
122237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
122337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
122437d0c07bSMatthew G Knepley 
122537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
122637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
122737d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
122837d0c07bSMatthew G Knepley         }
122937d0c07bSMatthew G Knepley       }
123037d0c07bSMatthew G Knepley     }
123137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1232785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
123337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
123437d0c07bSMatthew G Knepley     }
123537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
123637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
123737d0c07bSMatthew G Knepley 
123837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
123937d0c07bSMatthew G Knepley       if (gdof > 0) {
124037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
124137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
124237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
124337d0c07bSMatthew G Knepley 
124437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
124537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
124637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
124737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
124837d0c07bSMatthew G Knepley           }
124937d0c07bSMatthew G Knepley         }
125037d0c07bSMatthew G Knepley       }
125137d0c07bSMatthew G Knepley     }
12528865f1eaSKarl Rupp     if (numFields) *numFields = nF;
125337d0c07bSMatthew G Knepley     if (fieldNames) {
1254785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
125537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
125637d0c07bSMatthew G Knepley         const char *fieldName;
125737d0c07bSMatthew G Knepley 
125837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
125937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
126037d0c07bSMatthew G Knepley       }
126137d0c07bSMatthew G Knepley     }
126237d0c07bSMatthew G Knepley     if (fields) {
1263785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
126437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
126582f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
126637d0c07bSMatthew G Knepley       }
126737d0c07bSMatthew G Knepley     }
126837d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
12698865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
12708865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
127169ca1f37SDmitry Karpeev   }
12724d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
12734d343eeaSMatthew G Knepley }
12744d343eeaSMatthew G Knepley 
127516621825SDmitry Karpeev 
127616621825SDmitry Karpeev #undef __FUNCT__
127716621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
127816621825SDmitry Karpeev /*@C
127916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
128016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
128116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1282e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1283e7c4fc90SDmitry Karpeev 
1284e7c4fc90SDmitry Karpeev   Not collective
1285e7c4fc90SDmitry Karpeev 
1286e7c4fc90SDmitry Karpeev   Input Parameter:
1287e7c4fc90SDmitry Karpeev . dm - the DM object
1288e7c4fc90SDmitry Karpeev 
1289e7c4fc90SDmitry Karpeev   Output Parameters:
12900298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12910298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12920298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12930298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1294e7c4fc90SDmitry Karpeev 
1295e7c4fc90SDmitry Karpeev   Level: intermediate
1296e7c4fc90SDmitry Karpeev 
1297e7c4fc90SDmitry Karpeev   Notes:
1298e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1299e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1300e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1301e7c4fc90SDmitry Karpeev 
1302e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1303e7c4fc90SDmitry Karpeev @*/
130416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1305e7c4fc90SDmitry Karpeev {
1306e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1307e7c4fc90SDmitry Karpeev 
1308e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1309e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13108865f1eaSKarl Rupp   if (len) {
13118865f1eaSKarl Rupp     PetscValidPointer(len,2);
13128865f1eaSKarl Rupp     *len = 0;
13138865f1eaSKarl Rupp   }
13148865f1eaSKarl Rupp   if (namelist) {
13158865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13168865f1eaSKarl Rupp     *namelist = 0;
13178865f1eaSKarl Rupp   }
13188865f1eaSKarl Rupp   if (islist) {
13198865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13208865f1eaSKarl Rupp     *islist = 0;
13218865f1eaSKarl Rupp   }
13228865f1eaSKarl Rupp   if (dmlist) {
13238865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13248865f1eaSKarl Rupp     *dmlist = 0;
13258865f1eaSKarl Rupp   }
1326f3f0edfdSDmitry Karpeev   /*
1327f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1328f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1329f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1330f3f0edfdSDmitry Karpeev    */
1331ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
133216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1333435a35e8SMatthew G Knepley     PetscSection section;
1334435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1335435a35e8SMatthew G Knepley 
1336435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1337435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1338435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1339435a35e8SMatthew G Knepley       *len = numFields;
134003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
134103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
134203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1343435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1344435a35e8SMatthew G Knepley         const char *fieldName;
1345435a35e8SMatthew G Knepley 
134603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
134703dc3394SMatthew G. Knepley         if (namelist) {
1348435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1349435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1350435a35e8SMatthew G Knepley         }
135103dc3394SMatthew G. Knepley       }
1352435a35e8SMatthew G Knepley     } else {
135369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1354e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
13550298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1356e7c4fc90SDmitry Karpeev     }
13578865f1eaSKarl Rupp   } else {
135816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
135916621825SDmitry Karpeev   }
136016621825SDmitry Karpeev   PetscFunctionReturn(0);
136116621825SDmitry Karpeev }
136216621825SDmitry Karpeev 
136316621825SDmitry Karpeev #undef __FUNCT__
1364435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1365564cec59SMatthew G. Knepley /*@
1366435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1367435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1368435a35e8SMatthew G Knepley 
1369435a35e8SMatthew G Knepley   Not collective
1370435a35e8SMatthew G Knepley 
1371435a35e8SMatthew G Knepley   Input Parameters:
1372435a35e8SMatthew G Knepley + dm - the DM object
1373435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
13740298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1375435a35e8SMatthew G Knepley 
1376435a35e8SMatthew G Knepley   Output Parameters:
1377435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1378435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1379435a35e8SMatthew G Knepley 
1380435a35e8SMatthew G Knepley   Level: intermediate
1381435a35e8SMatthew G Knepley 
1382435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1383435a35e8SMatthew G Knepley @*/
1384435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1385435a35e8SMatthew G Knepley {
1386435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1387435a35e8SMatthew G Knepley 
1388435a35e8SMatthew G Knepley   PetscFunctionBegin;
1389435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1390435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13918865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13928865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1393435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1394435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
139582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1396435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1397435a35e8SMatthew G Knepley }
1398435a35e8SMatthew G Knepley 
139916621825SDmitry Karpeev 
140016621825SDmitry Karpeev #undef __FUNCT__
140116621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
140216621825SDmitry Karpeev /*@C
14038d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14048d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14058d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14068d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14078d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
140816621825SDmitry Karpeev 
140916621825SDmitry Karpeev   Not collective
141016621825SDmitry Karpeev 
141116621825SDmitry Karpeev   Input Parameter:
141216621825SDmitry Karpeev . dm - the DM object
141316621825SDmitry Karpeev 
141416621825SDmitry Karpeev   Output Parameters:
14150298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14160298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14170298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14180298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14190298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
142016621825SDmitry Karpeev 
142116621825SDmitry Karpeev   Level: intermediate
142216621825SDmitry Karpeev 
142316621825SDmitry Karpeev   Notes:
142416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
142516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
142616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
142716621825SDmitry Karpeev 
14288d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
142916621825SDmitry Karpeev @*/
14308d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
143116621825SDmitry Karpeev {
143216621825SDmitry Karpeev   PetscErrorCode      ierr;
1433be081cd6SPeter Brune   DMSubDomainHookLink link;
1434be081cd6SPeter Brune   PetscInt            i,l;
143516621825SDmitry Karpeev 
143616621825SDmitry Karpeev   PetscFunctionBegin;
143716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143814a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14390298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14400298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14410298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14420298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1443f3f0edfdSDmitry Karpeev   /*
1444f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1445f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1446f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1447f3f0edfdSDmitry Karpeev    */
1448ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
144916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1450be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
145114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
145214a18fd3SPeter Brune     if (dmlist) {
1453be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1454be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1455be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1456be081cd6SPeter Brune         }
1457d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1458e7c4fc90SDmitry Karpeev       }
145914a18fd3SPeter Brune     }
146014a18fd3SPeter Brune     if (len) *len = l;
146114a18fd3SPeter Brune   }
1462e30e807fSPeter Brune   PetscFunctionReturn(0);
1463e30e807fSPeter Brune }
1464e30e807fSPeter Brune 
1465e30e807fSPeter Brune 
1466e30e807fSPeter Brune #undef __FUNCT__
1467e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1468e30e807fSPeter Brune /*@C
1469e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1470e30e807fSPeter Brune 
1471e30e807fSPeter Brune   Not collective
1472e30e807fSPeter Brune 
1473e30e807fSPeter Brune   Input Parameters:
1474e30e807fSPeter Brune + dm - the DM object
1475e30e807fSPeter Brune . n  - the number of subdomain scatters
1476e30e807fSPeter Brune - subdms - the local subdomains
1477e30e807fSPeter Brune 
1478e30e807fSPeter Brune   Output Parameters:
1479e30e807fSPeter Brune + n     - the number of scatters returned
1480e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1481e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1482e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1483e30e807fSPeter Brune 
1484e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1485e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1486e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1487e30e807fSPeter Brune   solution and residual data.
1488e30e807fSPeter Brune 
1489e30e807fSPeter Brune   Level: developer
1490e30e807fSPeter Brune 
1491e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1492e30e807fSPeter Brune @*/
1493e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1494e30e807fSPeter Brune {
1495e30e807fSPeter Brune   PetscErrorCode ierr;
1496e30e807fSPeter Brune 
1497e30e807fSPeter Brune   PetscFunctionBegin;
1498e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1499e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1500e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1501e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
150282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1503e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1504e7c4fc90SDmitry Karpeev }
1505e7c4fc90SDmitry Karpeev 
1506731c8d9eSDmitry Karpeev #undef __FUNCT__
150747c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
150847c6ae99SBarry Smith /*@
150947c6ae99SBarry Smith   DMRefine - Refines a DM object
151047c6ae99SBarry Smith 
151147c6ae99SBarry Smith   Collective on DM
151247c6ae99SBarry Smith 
151347c6ae99SBarry Smith   Input Parameter:
151447c6ae99SBarry Smith + dm   - the DM object
151591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
151647c6ae99SBarry Smith 
151747c6ae99SBarry Smith   Output Parameter:
15180298fd71SBarry Smith . dmf - the refined DM, or NULL
1519ae0a1c52SMatthew G Knepley 
15200298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
152147c6ae99SBarry Smith 
152247c6ae99SBarry Smith   Level: developer
152347c6ae99SBarry Smith 
1524e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
152547c6ae99SBarry Smith @*/
15267087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
152747c6ae99SBarry Smith {
152847c6ae99SBarry Smith   PetscErrorCode   ierr;
1529c833c3b5SJed Brown   DMRefineHookLink link;
153047c6ae99SBarry Smith 
153147c6ae99SBarry Smith   PetscFunctionBegin;
1532732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
153347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15344057135bSMatthew G Knepley   if (*dmf) {
153543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15368865f1eaSKarl Rupp 
15378cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15388865f1eaSKarl Rupp 
1539644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15400598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1541656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15428865f1eaSKarl Rupp 
1543e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1544c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15458865f1eaSKarl Rupp       if (link->refinehook) {
15468865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
15478865f1eaSKarl Rupp       }
1548c833c3b5SJed Brown     }
1549c833c3b5SJed Brown   }
1550c833c3b5SJed Brown   PetscFunctionReturn(0);
1551c833c3b5SJed Brown }
1552c833c3b5SJed Brown 
1553c833c3b5SJed Brown #undef __FUNCT__
1554c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1555bb9467b5SJed Brown /*@C
1556c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1557c833c3b5SJed Brown 
1558c833c3b5SJed Brown    Logically Collective
1559c833c3b5SJed Brown 
1560c833c3b5SJed Brown    Input Arguments:
1561c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1562c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1563c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
15640298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1565c833c3b5SJed Brown 
1566c833c3b5SJed Brown    Calling sequence of refinehook:
1567c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1568c833c3b5SJed Brown 
1569c833c3b5SJed Brown +  coarse - coarse level DM
1570c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1571c833c3b5SJed Brown -  ctx - optional user-defined function context
1572c833c3b5SJed Brown 
1573c833c3b5SJed Brown    Calling sequence for interphook:
1574c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1575c833c3b5SJed Brown 
1576c833c3b5SJed Brown +  coarse - coarse level DM
1577c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1578c833c3b5SJed Brown .  fine - fine level DM to update
1579c833c3b5SJed Brown -  ctx - optional user-defined function context
1580c833c3b5SJed Brown 
1581c833c3b5SJed Brown    Level: advanced
1582c833c3b5SJed Brown 
1583c833c3b5SJed Brown    Notes:
1584c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1585c833c3b5SJed Brown 
1586c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1587c833c3b5SJed Brown 
1588bb9467b5SJed Brown    This function is currently not available from Fortran.
1589bb9467b5SJed Brown 
1590c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1591c833c3b5SJed Brown @*/
1592c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1593c833c3b5SJed Brown {
1594c833c3b5SJed Brown   PetscErrorCode   ierr;
1595c833c3b5SJed Brown   DMRefineHookLink link,*p;
1596c833c3b5SJed Brown 
1597c833c3b5SJed Brown   PetscFunctionBegin;
1598c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1599c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1600c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1601c833c3b5SJed Brown   link->refinehook = refinehook;
1602c833c3b5SJed Brown   link->interphook = interphook;
1603c833c3b5SJed Brown   link->ctx        = ctx;
16040298fd71SBarry Smith   link->next       = NULL;
1605c833c3b5SJed Brown   *p               = link;
1606c833c3b5SJed Brown   PetscFunctionReturn(0);
1607c833c3b5SJed Brown }
1608c833c3b5SJed Brown 
1609c833c3b5SJed Brown #undef __FUNCT__
1610c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1611c833c3b5SJed Brown /*@
1612c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1613c833c3b5SJed Brown 
1614c833c3b5SJed Brown    Collective if any hooks are
1615c833c3b5SJed Brown 
1616c833c3b5SJed Brown    Input Arguments:
1617c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1618c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1619c833c3b5SJed Brown -  fine - finer DM to update
1620c833c3b5SJed Brown 
1621c833c3b5SJed Brown    Level: developer
1622c833c3b5SJed Brown 
1623c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1624c833c3b5SJed Brown @*/
1625c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1626c833c3b5SJed Brown {
1627c833c3b5SJed Brown   PetscErrorCode   ierr;
1628c833c3b5SJed Brown   DMRefineHookLink link;
1629c833c3b5SJed Brown 
1630c833c3b5SJed Brown   PetscFunctionBegin;
1631c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16328865f1eaSKarl Rupp     if (link->interphook) {
16338865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16348865f1eaSKarl Rupp     }
16354057135bSMatthew G Knepley   }
163647c6ae99SBarry Smith   PetscFunctionReturn(0);
163747c6ae99SBarry Smith }
163847c6ae99SBarry Smith 
163947c6ae99SBarry Smith #undef __FUNCT__
1640eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1641eb3f98d2SBarry Smith /*@
1642eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1643eb3f98d2SBarry Smith 
1644eb3f98d2SBarry Smith     Not Collective
1645eb3f98d2SBarry Smith 
1646eb3f98d2SBarry Smith     Input Parameter:
1647eb3f98d2SBarry Smith .   dm - the DM object
1648eb3f98d2SBarry Smith 
1649eb3f98d2SBarry Smith     Output Parameter:
1650eb3f98d2SBarry Smith .   level - number of refinements
1651eb3f98d2SBarry Smith 
1652eb3f98d2SBarry Smith     Level: developer
1653eb3f98d2SBarry Smith 
16546a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1655eb3f98d2SBarry Smith 
1656eb3f98d2SBarry Smith @*/
1657eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1658eb3f98d2SBarry Smith {
1659eb3f98d2SBarry Smith   PetscFunctionBegin;
1660eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1661eb3f98d2SBarry Smith   *level = dm->levelup;
1662eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1663eb3f98d2SBarry Smith }
1664eb3f98d2SBarry Smith 
1665eb3f98d2SBarry Smith #undef __FUNCT__
1666baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1667bb9467b5SJed Brown /*@C
1668baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1669baf369e7SPeter Brune 
1670baf369e7SPeter Brune    Logically Collective
1671baf369e7SPeter Brune 
1672baf369e7SPeter Brune    Input Arguments:
1673baf369e7SPeter Brune +  dm - the DM
1674baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1675baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
16760298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1677baf369e7SPeter Brune 
1678baf369e7SPeter Brune    Calling sequence for beginhook:
1679baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1680baf369e7SPeter Brune 
1681baf369e7SPeter Brune +  dm - global DM
1682baf369e7SPeter Brune .  g - global vector
1683baf369e7SPeter Brune .  mode - mode
1684baf369e7SPeter Brune .  l - local vector
1685baf369e7SPeter Brune -  ctx - optional user-defined function context
1686baf369e7SPeter Brune 
1687baf369e7SPeter Brune 
1688baf369e7SPeter Brune    Calling sequence for endhook:
1689ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1690baf369e7SPeter Brune 
1691baf369e7SPeter Brune +  global - global DM
1692baf369e7SPeter Brune -  ctx - optional user-defined function context
1693baf369e7SPeter Brune 
1694baf369e7SPeter Brune    Level: advanced
1695baf369e7SPeter Brune 
1696baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1697baf369e7SPeter Brune @*/
1698baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1699baf369e7SPeter Brune {
1700baf369e7SPeter Brune   PetscErrorCode          ierr;
1701baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1702baf369e7SPeter Brune 
1703baf369e7SPeter Brune   PetscFunctionBegin;
1704baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1705baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1706baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1707baf369e7SPeter Brune   link->beginhook = beginhook;
1708baf369e7SPeter Brune   link->endhook   = endhook;
1709baf369e7SPeter Brune   link->ctx       = ctx;
17100298fd71SBarry Smith   link->next      = NULL;
1711baf369e7SPeter Brune   *p              = link;
1712baf369e7SPeter Brune   PetscFunctionReturn(0);
1713baf369e7SPeter Brune }
1714baf369e7SPeter Brune 
1715baf369e7SPeter Brune #undef __FUNCT__
17164c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17174c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17184c274da1SToby Isaac {
17194c274da1SToby Isaac   Mat cMat;
17204c274da1SToby Isaac   Vec cVec;
17214c274da1SToby Isaac   PetscSection section, cSec;
17224c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17234c274da1SToby Isaac   PetscErrorCode ierr;
17244c274da1SToby Isaac 
17254c274da1SToby Isaac   PetscFunctionBegin;
17264c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17274c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17284c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17294c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17307711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17314c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17324c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17334c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17344c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17354c274da1SToby Isaac       if (dof) {
17364c274da1SToby Isaac         PetscScalar *vals;
17374c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17384c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17394c274da1SToby Isaac       }
17404c274da1SToby Isaac     }
17414c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17424c274da1SToby Isaac   }
17434c274da1SToby Isaac   PetscFunctionReturn(0);
17444c274da1SToby Isaac }
17454c274da1SToby Isaac 
17464c274da1SToby Isaac #undef __FUNCT__
174747c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
174847c6ae99SBarry Smith /*@
174947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
175047c6ae99SBarry Smith 
175147c6ae99SBarry Smith     Neighbor-wise Collective on DM
175247c6ae99SBarry Smith 
175347c6ae99SBarry Smith     Input Parameters:
175447c6ae99SBarry Smith +   dm - the DM object
175547c6ae99SBarry Smith .   g - the global vector
175647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
175747c6ae99SBarry Smith -   l - the local vector
175847c6ae99SBarry Smith 
175947c6ae99SBarry Smith 
176047c6ae99SBarry Smith     Level: beginner
176147c6ae99SBarry Smith 
1762e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
176347c6ae99SBarry Smith 
176447c6ae99SBarry Smith @*/
17657087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
176647c6ae99SBarry Smith {
17677128ae9fSMatthew G Knepley   PetscSF                 sf;
176847c6ae99SBarry Smith   PetscErrorCode          ierr;
1769baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
177047c6ae99SBarry Smith 
177147c6ae99SBarry Smith   PetscFunctionBegin;
1772171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1773baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
17748865f1eaSKarl Rupp     if (link->beginhook) {
17758865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
17768865f1eaSKarl Rupp     }
1777baf369e7SPeter Brune   }
17787128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17797128ae9fSMatthew G Knepley   if (sf) {
1780ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1781ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
17827128ae9fSMatthew G Knepley 
178382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17847128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1785ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17867128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17877128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1788ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17897128ae9fSMatthew G Knepley   } else {
1790843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17917128ae9fSMatthew G Knepley   }
179247c6ae99SBarry Smith   PetscFunctionReturn(0);
179347c6ae99SBarry Smith }
179447c6ae99SBarry Smith 
179547c6ae99SBarry Smith #undef __FUNCT__
179647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
179747c6ae99SBarry Smith /*@
179847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
179947c6ae99SBarry Smith 
180047c6ae99SBarry Smith     Neighbor-wise Collective on DM
180147c6ae99SBarry Smith 
180247c6ae99SBarry Smith     Input Parameters:
180347c6ae99SBarry Smith +   dm - the DM object
180447c6ae99SBarry Smith .   g - the global vector
180547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
180647c6ae99SBarry Smith -   l - the local vector
180747c6ae99SBarry Smith 
180847c6ae99SBarry Smith 
180947c6ae99SBarry Smith     Level: beginner
181047c6ae99SBarry Smith 
1811e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
181247c6ae99SBarry Smith 
181347c6ae99SBarry Smith @*/
18147087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
181547c6ae99SBarry Smith {
18167128ae9fSMatthew G Knepley   PetscSF                 sf;
181747c6ae99SBarry Smith   PetscErrorCode          ierr;
1818ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1819ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1820baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
182147c6ae99SBarry Smith 
182247c6ae99SBarry Smith   PetscFunctionBegin;
1823171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18247128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18257128ae9fSMatthew G Knepley   if (sf) {
182682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18277128ae9fSMatthew G Knepley 
18287128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1829ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18307128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18317128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1832ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18337128ae9fSMatthew G Knepley   } else {
1834843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18357128ae9fSMatthew G Knepley   }
18364c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1837baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1838baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1839baf369e7SPeter Brune   }
184047c6ae99SBarry Smith   PetscFunctionReturn(0);
184147c6ae99SBarry Smith }
184247c6ae99SBarry Smith 
184347c6ae99SBarry Smith #undef __FUNCT__
1844d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1845d4d07f1eSToby Isaac /*@C
1846d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1847d4d07f1eSToby Isaac 
1848d4d07f1eSToby Isaac    Logically Collective
1849d4d07f1eSToby Isaac 
1850d4d07f1eSToby Isaac    Input Arguments:
1851d4d07f1eSToby Isaac +  dm - the DM
1852d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1853d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1854d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1855d4d07f1eSToby Isaac 
1856d4d07f1eSToby Isaac    Calling sequence for beginhook:
1857d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1858d4d07f1eSToby Isaac 
1859d4d07f1eSToby Isaac +  dm - global DM
1860d4d07f1eSToby Isaac .  l - local vector
1861d4d07f1eSToby Isaac .  mode - mode
1862d4d07f1eSToby Isaac .  g - global vector
1863d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1864d4d07f1eSToby Isaac 
1865d4d07f1eSToby Isaac 
1866d4d07f1eSToby Isaac    Calling sequence for endhook:
1867d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1868d4d07f1eSToby Isaac 
1869d4d07f1eSToby Isaac +  global - global DM
1870d4d07f1eSToby Isaac .  l - local vector
1871d4d07f1eSToby Isaac .  mode - mode
1872d4d07f1eSToby Isaac .  g - global vector
1873d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1874d4d07f1eSToby Isaac 
1875d4d07f1eSToby Isaac    Level: advanced
1876d4d07f1eSToby Isaac 
1877d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1878d4d07f1eSToby Isaac @*/
1879d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1880d4d07f1eSToby Isaac {
1881d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1882d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1883d4d07f1eSToby Isaac 
1884d4d07f1eSToby Isaac   PetscFunctionBegin;
1885d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1886d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1887d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1888d4d07f1eSToby Isaac   link->beginhook = beginhook;
1889d4d07f1eSToby Isaac   link->endhook   = endhook;
1890d4d07f1eSToby Isaac   link->ctx       = ctx;
1891d4d07f1eSToby Isaac   link->next      = NULL;
1892d4d07f1eSToby Isaac   *p              = link;
1893d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1894d4d07f1eSToby Isaac }
1895d4d07f1eSToby Isaac 
1896d4d07f1eSToby Isaac #undef __FUNCT__
18974c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
18984c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
18994c274da1SToby Isaac {
19004c274da1SToby Isaac   Mat cMat;
19014c274da1SToby Isaac   Vec cVec;
19024c274da1SToby Isaac   PetscSection section, cSec;
19034c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19044c274da1SToby Isaac   PetscErrorCode ierr;
19054c274da1SToby Isaac 
19064c274da1SToby Isaac   PetscFunctionBegin;
19074c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19084c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19094c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
19104c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19117711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19124c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19134c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19144c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19154c274da1SToby Isaac       if (dof) {
19164c274da1SToby Isaac         PetscInt d;
19174c274da1SToby Isaac         PetscScalar *vals;
19184c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19194c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19204c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19214c274da1SToby Isaac          * we just extracted */
19224c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19234c274da1SToby Isaac           vals[d] = 0.;
19244c274da1SToby Isaac         }
19254c274da1SToby Isaac       }
19264c274da1SToby Isaac     }
19274c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19284c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19294c274da1SToby Isaac   }
19304c274da1SToby Isaac   PetscFunctionReturn(0);
19314c274da1SToby Isaac }
19324c274da1SToby Isaac 
19334c274da1SToby Isaac #undef __FUNCT__
19349a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
193547c6ae99SBarry Smith /*@
19369a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19379a42bb27SBarry Smith 
19389a42bb27SBarry Smith     Neighbor-wise Collective on DM
19399a42bb27SBarry Smith 
19409a42bb27SBarry Smith     Input Parameters:
19419a42bb27SBarry Smith +   dm - the DM object
1942f6813fd5SJed Brown .   l - the local vector
19431eb28f2eSBarry 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.
19441eb28f2eSBarry Smith -   g - the global vector
19459a42bb27SBarry Smith 
1946d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
194784330215SMatthew 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.
19489a42bb27SBarry Smith 
19499a42bb27SBarry Smith     Level: beginner
19509a42bb27SBarry Smith 
1951e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
19529a42bb27SBarry Smith 
19539a42bb27SBarry Smith @*/
19547087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
19559a42bb27SBarry Smith {
19567128ae9fSMatthew G Knepley   PetscSF                 sf;
195784330215SMatthew G. Knepley   PetscSection            s, gs;
1958d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
1959ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
1960ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
196184330215SMatthew G. Knepley   PetscBool               isInsert;
196284330215SMatthew G. Knepley   PetscErrorCode          ierr;
19639a42bb27SBarry Smith 
19649a42bb27SBarry Smith   PetscFunctionBegin;
1965171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1966d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1967d4d07f1eSToby Isaac     if (link->beginhook) {
1968d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1969d4d07f1eSToby Isaac     }
1970d4d07f1eSToby Isaac   }
19714c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
19727128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
197384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19747128ae9fSMatthew G Knepley   switch (mode) {
19757128ae9fSMatthew G Knepley   case INSERT_VALUES:
19767128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
197784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
19787128ae9fSMatthew G Knepley   case ADD_VALUES:
19797128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
198084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
19817128ae9fSMatthew G Knepley   default:
198282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19837128ae9fSMatthew G Knepley   }
198484330215SMatthew G. Knepley   if (sf && !isInsert) {
1985ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19867128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
1987a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
1988ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
198984330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
199084330215SMatthew G. Knepley   } else if (s && isInsert) {
199184330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
199284330215SMatthew G. Knepley 
199384330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
199484330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
199584330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
1996ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
199784330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
199884330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
1999b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
200084330215SMatthew G. Knepley 
200184330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
200203442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
200384330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2004b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
200584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
200684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2007b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
200803442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2009b3b16f48SMatthew 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);
2010b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2011b3b16f48SMatthew G. Knepley       if (!gcdof) {
201284330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2013b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2014b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
201584330215SMatthew G. Knepley         const PetscInt *cdofs;
201684330215SMatthew G. Knepley         PetscInt        cind = 0;
201784330215SMatthew G. Knepley 
201884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2019b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
202084330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2021b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
202284330215SMatthew G. Knepley         }
2023b3b16f48SMatthew 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);
202484330215SMatthew G. Knepley     }
2025ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20267128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20277128ae9fSMatthew G Knepley   } else {
2028843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20297128ae9fSMatthew G Knepley   }
20309a42bb27SBarry Smith   PetscFunctionReturn(0);
20319a42bb27SBarry Smith }
20329a42bb27SBarry Smith 
20339a42bb27SBarry Smith #undef __FUNCT__
20349a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20359a42bb27SBarry Smith /*@
20369a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
203747c6ae99SBarry Smith 
203847c6ae99SBarry Smith     Neighbor-wise Collective on DM
203947c6ae99SBarry Smith 
204047c6ae99SBarry Smith     Input Parameters:
204147c6ae99SBarry Smith +   dm - the DM object
2042f6813fd5SJed Brown .   l - the local vector
204347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2044f6813fd5SJed Brown -   g - the global vector
204547c6ae99SBarry Smith 
204647c6ae99SBarry Smith 
204747c6ae99SBarry Smith     Level: beginner
204847c6ae99SBarry Smith 
2049e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
205047c6ae99SBarry Smith 
205147c6ae99SBarry Smith @*/
20527087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
205347c6ae99SBarry Smith {
20547128ae9fSMatthew G Knepley   PetscSF                 sf;
205584330215SMatthew G. Knepley   PetscSection            s;
2056d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
205784330215SMatthew G. Knepley   PetscBool               isInsert;
205884330215SMatthew G. Knepley   PetscErrorCode          ierr;
205947c6ae99SBarry Smith 
206047c6ae99SBarry Smith   PetscFunctionBegin;
2061171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20627128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
206384330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20647128ae9fSMatthew G Knepley   switch (mode) {
20657128ae9fSMatthew G Knepley   case INSERT_VALUES:
20667128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
206784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20687128ae9fSMatthew G Knepley   case ADD_VALUES:
20697128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
207084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20717128ae9fSMatthew G Knepley   default:
207282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20737128ae9fSMatthew G Knepley   }
207484330215SMatthew G. Knepley   if (sf && !isInsert) {
2075ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2076ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
207784330215SMatthew G. Knepley 
2078ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20797128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2080a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2081ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20827128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
208384330215SMatthew G. Knepley   } else if (s && isInsert) {
20847128ae9fSMatthew G Knepley   } else {
2085843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20867128ae9fSMatthew G Knepley   }
2087d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2088d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2089d4d07f1eSToby Isaac   }
209047c6ae99SBarry Smith   PetscFunctionReturn(0);
209147c6ae99SBarry Smith }
209247c6ae99SBarry Smith 
209347c6ae99SBarry Smith #undef __FUNCT__
2094f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2095f089877aSRichard Tran Mills /*@
2096bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2097bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2098d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2099f089877aSRichard Tran Mills 
2100bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2101f089877aSRichard Tran Mills 
2102f089877aSRichard Tran Mills    Input Parameters:
2103f089877aSRichard Tran Mills +  dm - the DM object
2104bc0a1609SRichard Tran Mills .  g - the original local vector
2105bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2106f089877aSRichard Tran Mills 
2107bc0a1609SRichard Tran Mills    Output Parameter:
2108bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2109f089877aSRichard Tran Mills 
2110f089877aSRichard Tran Mills    Level: intermediate
2111f089877aSRichard Tran Mills 
2112bc0a1609SRichard Tran Mills    Notes:
2113bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2114bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2115bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2116bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2117bc0a1609SRichard Tran Mills 
2118bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2119bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2120f089877aSRichard Tran Mills 
2121f089877aSRichard Tran Mills @*/
2122f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2123f089877aSRichard Tran Mills {
2124f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2125f089877aSRichard Tran Mills 
2126f089877aSRichard Tran Mills   PetscFunctionBegin;
2127f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2128f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2129f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2130f089877aSRichard Tran Mills }
2131f089877aSRichard Tran Mills 
2132f089877aSRichard Tran Mills #undef __FUNCT__
2133f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2134f089877aSRichard Tran Mills /*@
2135bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2136bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2137d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2138f089877aSRichard Tran Mills 
2139bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2140f089877aSRichard Tran Mills 
2141f089877aSRichard Tran Mills    Input Parameters:
2142bc0a1609SRichard Tran Mills +  da - the DM object
2143bc0a1609SRichard Tran Mills .  g - the original local vector
2144bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2145f089877aSRichard Tran Mills 
2146bc0a1609SRichard Tran Mills    Output Parameter:
2147bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2148f089877aSRichard Tran Mills 
2149f089877aSRichard Tran Mills    Level: intermediate
2150f089877aSRichard Tran Mills 
2151bc0a1609SRichard Tran Mills    Notes:
2152bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2153bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2154bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2155bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2156bc0a1609SRichard Tran Mills 
2157bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2158bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2159f089877aSRichard Tran Mills 
2160f089877aSRichard Tran Mills @*/
2161f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2162f089877aSRichard Tran Mills {
2163f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2164f089877aSRichard Tran Mills 
2165f089877aSRichard Tran Mills   PetscFunctionBegin;
2166f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2167f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2168f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2169f089877aSRichard Tran Mills }
2170f089877aSRichard Tran Mills 
2171f089877aSRichard Tran Mills 
2172f089877aSRichard Tran Mills #undef __FUNCT__
217347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
217447c6ae99SBarry Smith /*@
217547c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
217647c6ae99SBarry Smith 
217747c6ae99SBarry Smith     Collective on DM
217847c6ae99SBarry Smith 
217947c6ae99SBarry Smith     Input Parameter:
218047c6ae99SBarry Smith +   dm - the DM object
218191d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
218247c6ae99SBarry Smith 
218347c6ae99SBarry Smith     Output Parameter:
218447c6ae99SBarry Smith .   dmc - the coarsened DM
218547c6ae99SBarry Smith 
218647c6ae99SBarry Smith     Level: developer
218747c6ae99SBarry Smith 
2188e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
218947c6ae99SBarry Smith 
219047c6ae99SBarry Smith @*/
21917087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
219247c6ae99SBarry Smith {
219347c6ae99SBarry Smith   PetscErrorCode    ierr;
2194b17ce1afSJed Brown   DMCoarsenHookLink link;
219547c6ae99SBarry Smith 
219647c6ae99SBarry Smith   PetscFunctionBegin;
2197171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
219847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
219947c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
22008892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
220143842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
22028cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2203644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
22040598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2205656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2206e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2207b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2208b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2209b17ce1afSJed Brown   }
221047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2211b17ce1afSJed Brown   PetscFunctionReturn(0);
2212b17ce1afSJed Brown }
2213b17ce1afSJed Brown 
2214b17ce1afSJed Brown #undef __FUNCT__
2215b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2216bb9467b5SJed Brown /*@C
2217b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2218b17ce1afSJed Brown 
2219b17ce1afSJed Brown    Logically Collective
2220b17ce1afSJed Brown 
2221b17ce1afSJed Brown    Input Arguments:
2222b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2223b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2224b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22250298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2226b17ce1afSJed Brown 
2227b17ce1afSJed Brown    Calling sequence of coarsenhook:
2228b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2229b17ce1afSJed Brown 
2230b17ce1afSJed Brown +  fine - fine level DM
2231b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2232b17ce1afSJed Brown -  ctx - optional user-defined function context
2233b17ce1afSJed Brown 
2234b17ce1afSJed Brown    Calling sequence for restricthook:
2235c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2236b17ce1afSJed Brown 
2237b17ce1afSJed Brown +  fine - fine level DM
2238b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2239c833c3b5SJed Brown .  rscale - scaling vector for restriction
2240c833c3b5SJed Brown .  inject - matrix restricting by injection
2241b17ce1afSJed Brown .  coarse - coarse level DM to update
2242b17ce1afSJed Brown -  ctx - optional user-defined function context
2243b17ce1afSJed Brown 
2244b17ce1afSJed Brown    Level: advanced
2245b17ce1afSJed Brown 
2246b17ce1afSJed Brown    Notes:
2247b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2248b17ce1afSJed Brown 
2249b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2250b17ce1afSJed Brown 
2251b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2252b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2253b17ce1afSJed Brown 
2254bb9467b5SJed Brown    This function is currently not available from Fortran.
2255bb9467b5SJed Brown 
2256c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2257b17ce1afSJed Brown @*/
2258b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2259b17ce1afSJed Brown {
2260b17ce1afSJed Brown   PetscErrorCode    ierr;
2261b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2262b17ce1afSJed Brown 
2263b17ce1afSJed Brown   PetscFunctionBegin;
2264b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
22656bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2266b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2267b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2268b17ce1afSJed Brown   link->restricthook = restricthook;
2269b17ce1afSJed Brown   link->ctx          = ctx;
22700298fd71SBarry Smith   link->next         = NULL;
2271b17ce1afSJed Brown   *p                 = link;
2272b17ce1afSJed Brown   PetscFunctionReturn(0);
2273b17ce1afSJed Brown }
2274b17ce1afSJed Brown 
2275b17ce1afSJed Brown #undef __FUNCT__
2276b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2277b17ce1afSJed Brown /*@
2278b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2279b17ce1afSJed Brown 
2280b17ce1afSJed Brown    Collective if any hooks are
2281b17ce1afSJed Brown 
2282b17ce1afSJed Brown    Input Arguments:
2283b17ce1afSJed Brown +  fine - finer DM to use as a base
2284b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2285b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2286b17ce1afSJed Brown -  coarse - coarer DM to update
2287b17ce1afSJed Brown 
2288b17ce1afSJed Brown    Level: developer
2289b17ce1afSJed Brown 
2290b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2291b17ce1afSJed Brown @*/
2292b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2293b17ce1afSJed Brown {
2294b17ce1afSJed Brown   PetscErrorCode    ierr;
2295b17ce1afSJed Brown   DMCoarsenHookLink link;
2296b17ce1afSJed Brown 
2297b17ce1afSJed Brown   PetscFunctionBegin;
2298b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
22998865f1eaSKarl Rupp     if (link->restricthook) {
23008865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23018865f1eaSKarl Rupp     }
2302b17ce1afSJed Brown   }
230347c6ae99SBarry Smith   PetscFunctionReturn(0);
230447c6ae99SBarry Smith }
230547c6ae99SBarry Smith 
230647c6ae99SBarry Smith #undef __FUNCT__
2307be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2308bb9467b5SJed Brown /*@C
2309be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
23105dbd56e3SPeter Brune 
23115dbd56e3SPeter Brune    Logically Collective
23125dbd56e3SPeter Brune 
23135dbd56e3SPeter Brune    Input Arguments:
23145dbd56e3SPeter Brune +  global - global DM
2315ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
23165dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
23170298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23185dbd56e3SPeter Brune 
2319ec4806b8SPeter Brune 
2320ec4806b8SPeter Brune    Calling sequence for ddhook:
2321ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2322ec4806b8SPeter Brune 
2323ec4806b8SPeter Brune +  global - global DM
2324ec4806b8SPeter Brune .  block  - block DM
2325ec4806b8SPeter Brune -  ctx - optional user-defined function context
2326ec4806b8SPeter Brune 
23275dbd56e3SPeter Brune    Calling sequence for restricthook:
2328ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23295dbd56e3SPeter Brune 
23305dbd56e3SPeter Brune +  global - global DM
23315dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23325dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2333ec4806b8SPeter Brune .  block  - block DM
23345dbd56e3SPeter Brune -  ctx - optional user-defined function context
23355dbd56e3SPeter Brune 
23365dbd56e3SPeter Brune    Level: advanced
23375dbd56e3SPeter Brune 
23385dbd56e3SPeter Brune    Notes:
2339ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23405dbd56e3SPeter Brune 
23415dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23425dbd56e3SPeter Brune 
23435dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2344ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
23455dbd56e3SPeter Brune 
2346bb9467b5SJed Brown    This function is currently not available from Fortran.
2347bb9467b5SJed Brown 
23485dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
23495dbd56e3SPeter Brune @*/
2350be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
23515dbd56e3SPeter Brune {
23525dbd56e3SPeter Brune   PetscErrorCode      ierr;
2353be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
23545dbd56e3SPeter Brune 
23555dbd56e3SPeter Brune   PetscFunctionBegin;
23565dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2357be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2358be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
23595dbd56e3SPeter Brune   link->restricthook = restricthook;
2360be081cd6SPeter Brune   link->ddhook       = ddhook;
23615dbd56e3SPeter Brune   link->ctx          = ctx;
23620298fd71SBarry Smith   link->next         = NULL;
23635dbd56e3SPeter Brune   *p                 = link;
23645dbd56e3SPeter Brune   PetscFunctionReturn(0);
23655dbd56e3SPeter Brune }
23665dbd56e3SPeter Brune 
23675dbd56e3SPeter Brune #undef __FUNCT__
2368be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
23695dbd56e3SPeter Brune /*@
2370be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
23715dbd56e3SPeter Brune 
23725dbd56e3SPeter Brune    Collective if any hooks are
23735dbd56e3SPeter Brune 
23745dbd56e3SPeter Brune    Input Arguments:
23755dbd56e3SPeter Brune +  fine - finer DM to use as a base
2376be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2377be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
23785dbd56e3SPeter Brune -  coarse - coarer DM to update
23795dbd56e3SPeter Brune 
23805dbd56e3SPeter Brune    Level: developer
23815dbd56e3SPeter Brune 
23825dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
23835dbd56e3SPeter Brune @*/
2384be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
23855dbd56e3SPeter Brune {
23865dbd56e3SPeter Brune   PetscErrorCode      ierr;
2387be081cd6SPeter Brune   DMSubDomainHookLink link;
23885dbd56e3SPeter Brune 
23895dbd56e3SPeter Brune   PetscFunctionBegin;
2390be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
23918865f1eaSKarl Rupp     if (link->restricthook) {
23928865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
23938865f1eaSKarl Rupp     }
23945dbd56e3SPeter Brune   }
23955dbd56e3SPeter Brune   PetscFunctionReturn(0);
23965dbd56e3SPeter Brune }
23975dbd56e3SPeter Brune 
23985dbd56e3SPeter Brune #undef __FUNCT__
23995fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
24005fe1f584SPeter Brune /*@
24016a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24025fe1f584SPeter Brune 
24035fe1f584SPeter Brune     Not Collective
24045fe1f584SPeter Brune 
24055fe1f584SPeter Brune     Input Parameter:
24065fe1f584SPeter Brune .   dm - the DM object
24075fe1f584SPeter Brune 
24085fe1f584SPeter Brune     Output Parameter:
24096a7d9d85SPeter Brune .   level - number of coarsenings
24105fe1f584SPeter Brune 
24115fe1f584SPeter Brune     Level: developer
24125fe1f584SPeter Brune 
24136a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
24145fe1f584SPeter Brune 
24155fe1f584SPeter Brune @*/
24165fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
24175fe1f584SPeter Brune {
24185fe1f584SPeter Brune   PetscFunctionBegin;
24195fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24205fe1f584SPeter Brune   *level = dm->leveldown;
24215fe1f584SPeter Brune   PetscFunctionReturn(0);
24225fe1f584SPeter Brune }
24235fe1f584SPeter Brune 
24245fe1f584SPeter Brune 
24255fe1f584SPeter Brune 
24265fe1f584SPeter Brune #undef __FUNCT__
242747c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
242847c6ae99SBarry Smith /*@C
242947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
243047c6ae99SBarry Smith 
243147c6ae99SBarry Smith     Collective on DM
243247c6ae99SBarry Smith 
243347c6ae99SBarry Smith     Input Parameter:
243447c6ae99SBarry Smith +   dm - the DM object
243547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
243647c6ae99SBarry Smith 
243747c6ae99SBarry Smith     Output Parameter:
243847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
243947c6ae99SBarry Smith 
244047c6ae99SBarry Smith     Level: developer
244147c6ae99SBarry Smith 
2442e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
244347c6ae99SBarry Smith 
244447c6ae99SBarry Smith @*/
24457087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
244647c6ae99SBarry Smith {
244747c6ae99SBarry Smith   PetscErrorCode ierr;
244847c6ae99SBarry Smith 
244947c6ae99SBarry Smith   PetscFunctionBegin;
2450171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2451ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
245247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
245347c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
245447c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
245547c6ae99SBarry Smith   } else if (dm->ops->refine) {
245647c6ae99SBarry Smith     PetscInt i;
245747c6ae99SBarry Smith 
2458ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
245947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2460ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
246147c6ae99SBarry Smith     }
2462ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
246347c6ae99SBarry Smith   PetscFunctionReturn(0);
246447c6ae99SBarry Smith }
246547c6ae99SBarry Smith 
246647c6ae99SBarry Smith #undef __FUNCT__
246747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
246847c6ae99SBarry Smith /*@C
246947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
247047c6ae99SBarry Smith 
247147c6ae99SBarry Smith     Collective on DM
247247c6ae99SBarry Smith 
247347c6ae99SBarry Smith     Input Parameter:
247447c6ae99SBarry Smith +   dm - the DM object
247547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
247647c6ae99SBarry Smith 
247747c6ae99SBarry Smith     Output Parameter:
247847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
247947c6ae99SBarry Smith 
248047c6ae99SBarry Smith     Level: developer
248147c6ae99SBarry Smith 
2482e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
248347c6ae99SBarry Smith 
248447c6ae99SBarry Smith @*/
24857087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
248647c6ae99SBarry Smith {
248747c6ae99SBarry Smith   PetscErrorCode ierr;
248847c6ae99SBarry Smith 
248947c6ae99SBarry Smith   PetscFunctionBegin;
2490171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2491ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
249247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
249347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
249447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
249547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
249647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
249747c6ae99SBarry Smith     PetscInt i;
249847c6ae99SBarry Smith 
2499ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
250047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2501ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
250247c6ae99SBarry Smith     }
2503ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
250447c6ae99SBarry Smith   PetscFunctionReturn(0);
250547c6ae99SBarry Smith }
250647c6ae99SBarry Smith 
250747c6ae99SBarry Smith #undef __FUNCT__
2508e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
250947c6ae99SBarry Smith /*@
2510e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
251147c6ae99SBarry Smith    grids associated with two DMs.
251247c6ae99SBarry Smith 
251347c6ae99SBarry Smith    Collective on DM
251447c6ae99SBarry Smith 
251547c6ae99SBarry Smith    Input Parameters:
251647c6ae99SBarry Smith +  dmc - the coarse grid DM
251747c6ae99SBarry Smith -  dmf - the fine grid DM
251847c6ae99SBarry Smith 
251947c6ae99SBarry Smith    Output Parameters:
252047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
252147c6ae99SBarry Smith 
252247c6ae99SBarry Smith    Level: intermediate
252347c6ae99SBarry Smith 
252447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
252547c6ae99SBarry Smith 
2526e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
252747c6ae99SBarry Smith @*/
2528e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
252947c6ae99SBarry Smith {
253047c6ae99SBarry Smith   PetscErrorCode ierr;
253147c6ae99SBarry Smith 
253247c6ae99SBarry Smith   PetscFunctionBegin;
2533171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2534171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
253547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
253647c6ae99SBarry Smith   PetscFunctionReturn(0);
253747c6ae99SBarry Smith }
253847c6ae99SBarry Smith 
253947c6ae99SBarry Smith #undef __FUNCT__
25401a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25411a266240SBarry Smith /*@C
25421a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25431a266240SBarry Smith 
25441a266240SBarry Smith     Not Collective
25451a266240SBarry Smith 
25461a266240SBarry Smith     Input Parameters:
25471a266240SBarry Smith +   dm - the DM object
25481a266240SBarry Smith -   destroy - the destroy function
25491a266240SBarry Smith 
25501a266240SBarry Smith     Level: intermediate
25511a266240SBarry Smith 
2552e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
25531a266240SBarry Smith 
2554f07f9ceaSJed Brown @*/
25551a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
25561a266240SBarry Smith {
25571a266240SBarry Smith   PetscFunctionBegin;
2558171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25591a266240SBarry Smith   dm->ctxdestroy = destroy;
25601a266240SBarry Smith   PetscFunctionReturn(0);
25611a266240SBarry Smith }
25621a266240SBarry Smith 
25631a266240SBarry Smith #undef __FUNCT__
25641b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2565b07ff414SBarry Smith /*@
25661b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
256747c6ae99SBarry Smith 
256847c6ae99SBarry Smith     Not Collective
256947c6ae99SBarry Smith 
257047c6ae99SBarry Smith     Input Parameters:
257147c6ae99SBarry Smith +   dm - the DM object
257247c6ae99SBarry Smith -   ctx - the user context
257347c6ae99SBarry Smith 
257447c6ae99SBarry Smith     Level: intermediate
257547c6ae99SBarry Smith 
2576e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
257747c6ae99SBarry Smith 
257847c6ae99SBarry Smith @*/
25791b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
258047c6ae99SBarry Smith {
258147c6ae99SBarry Smith   PetscFunctionBegin;
2582171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258347c6ae99SBarry Smith   dm->ctx = ctx;
258447c6ae99SBarry Smith   PetscFunctionReturn(0);
258547c6ae99SBarry Smith }
258647c6ae99SBarry Smith 
258747c6ae99SBarry Smith #undef __FUNCT__
25881b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
258947c6ae99SBarry Smith /*@
25901b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
259147c6ae99SBarry Smith 
259247c6ae99SBarry Smith     Not Collective
259347c6ae99SBarry Smith 
259447c6ae99SBarry Smith     Input Parameter:
259547c6ae99SBarry Smith .   dm - the DM object
259647c6ae99SBarry Smith 
259747c6ae99SBarry Smith     Output Parameter:
259847c6ae99SBarry Smith .   ctx - the user context
259947c6ae99SBarry Smith 
260047c6ae99SBarry Smith     Level: intermediate
260147c6ae99SBarry Smith 
2602e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
260347c6ae99SBarry Smith 
260447c6ae99SBarry Smith @*/
26051b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
260647c6ae99SBarry Smith {
260747c6ae99SBarry Smith   PetscFunctionBegin;
2608171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26091b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
261047c6ae99SBarry Smith   PetscFunctionReturn(0);
261147c6ae99SBarry Smith }
261247c6ae99SBarry Smith 
261347c6ae99SBarry Smith #undef __FUNCT__
261408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
261508da532bSDmitry Karpeev /*@C
2616df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
261708da532bSDmitry Karpeev 
261808da532bSDmitry Karpeev     Logically Collective on DM
261908da532bSDmitry Karpeev 
262008da532bSDmitry Karpeev     Input Parameter:
262108da532bSDmitry Karpeev +   dm - the DM object
26220298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
262308da532bSDmitry Karpeev 
262408da532bSDmitry Karpeev     Level: intermediate
262508da532bSDmitry Karpeev 
2626835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
262708da532bSDmitry Karpeev          DMSetJacobian()
262808da532bSDmitry Karpeev 
262908da532bSDmitry Karpeev @*/
263008da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
263108da532bSDmitry Karpeev {
263208da532bSDmitry Karpeev   PetscFunctionBegin;
263308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
263408da532bSDmitry Karpeev   PetscFunctionReturn(0);
263508da532bSDmitry Karpeev }
263608da532bSDmitry Karpeev 
263708da532bSDmitry Karpeev #undef __FUNCT__
263808da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
263908da532bSDmitry Karpeev /*@
264008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
264108da532bSDmitry Karpeev 
264208da532bSDmitry Karpeev     Not Collective
264308da532bSDmitry Karpeev 
264408da532bSDmitry Karpeev     Input Parameter:
264508da532bSDmitry Karpeev .   dm - the DM object to destroy
264608da532bSDmitry Karpeev 
264708da532bSDmitry Karpeev     Output Parameter:
264808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
264908da532bSDmitry Karpeev 
265008da532bSDmitry Karpeev     Level: developer
265108da532bSDmitry Karpeev 
265274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
265308da532bSDmitry Karpeev 
265408da532bSDmitry Karpeev @*/
265508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
265608da532bSDmitry Karpeev {
265708da532bSDmitry Karpeev   PetscFunctionBegin;
265808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
265908da532bSDmitry Karpeev   PetscFunctionReturn(0);
266008da532bSDmitry Karpeev }
266108da532bSDmitry Karpeev 
266208da532bSDmitry Karpeev #undef __FUNCT__
266308da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
266408da532bSDmitry Karpeev /*@C
266508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
266608da532bSDmitry Karpeev 
266708da532bSDmitry Karpeev     Logically Collective on DM
266808da532bSDmitry Karpeev 
266908da532bSDmitry Karpeev     Input Parameters:
2670907376e6SBarry Smith .   dm - the DM object
267108da532bSDmitry Karpeev 
267208da532bSDmitry Karpeev     Output parameters:
267308da532bSDmitry Karpeev +   xl - lower bound
267408da532bSDmitry Karpeev -   xu - upper bound
267508da532bSDmitry Karpeev 
2676907376e6SBarry Smith     Level: advanced
2677907376e6SBarry Smith 
2678907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
267908da532bSDmitry Karpeev 
268074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
268108da532bSDmitry Karpeev 
268208da532bSDmitry Karpeev @*/
268308da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
268408da532bSDmitry Karpeev {
268508da532bSDmitry Karpeev   PetscErrorCode ierr;
26865fd66863SKarl Rupp 
268708da532bSDmitry Karpeev   PetscFunctionBegin;
268808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
268908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
269008da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
269108da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
26928865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
269308da532bSDmitry Karpeev   PetscFunctionReturn(0);
269408da532bSDmitry Karpeev }
269508da532bSDmitry Karpeev 
269608da532bSDmitry Karpeev #undef __FUNCT__
2697b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2698b0ae01b7SPeter Brune /*@
2699b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2700b0ae01b7SPeter Brune 
2701b0ae01b7SPeter Brune     Not Collective
2702b0ae01b7SPeter Brune 
2703b0ae01b7SPeter Brune     Input Parameter:
2704b0ae01b7SPeter Brune .   dm - the DM object
2705b0ae01b7SPeter Brune 
2706b0ae01b7SPeter Brune     Output Parameter:
2707b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2708b0ae01b7SPeter Brune 
2709b0ae01b7SPeter Brune     Level: developer
2710b0ae01b7SPeter Brune 
2711b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2712b0ae01b7SPeter Brune 
2713b0ae01b7SPeter Brune @*/
2714b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2715b0ae01b7SPeter Brune {
2716b0ae01b7SPeter Brune   PetscFunctionBegin;
2717b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2718b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2719b0ae01b7SPeter Brune }
2720b0ae01b7SPeter Brune 
2721b0ae01b7SPeter Brune #undef  __FUNCT__
272208da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2723748fac09SDmitry Karpeev /*@C
272408da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
272508da532bSDmitry Karpeev 
272608da532bSDmitry Karpeev     Collective on DM
272708da532bSDmitry Karpeev 
272808da532bSDmitry Karpeev     Input Parameter:
272908da532bSDmitry Karpeev +   dm - the DM object
27300298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
273108da532bSDmitry Karpeev 
273208da532bSDmitry Karpeev     Level: developer
273308da532bSDmitry Karpeev 
273474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
273508da532bSDmitry Karpeev 
273608da532bSDmitry Karpeev @*/
273708da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
273808da532bSDmitry Karpeev {
273908da532bSDmitry Karpeev   PetscErrorCode ierr;
27405fd66863SKarl Rupp 
274108da532bSDmitry Karpeev   PetscFunctionBegin;
274208da532bSDmitry Karpeev   if (x) {
274308da532bSDmitry Karpeev     if (!dm->x) {
274408da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
274508da532bSDmitry Karpeev     }
274608da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
27478865f1eaSKarl Rupp   } else if (dm->x) {
274808da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
274908da532bSDmitry Karpeev   }
275008da532bSDmitry Karpeev   PetscFunctionReturn(0);
275108da532bSDmitry Karpeev }
275208da532bSDmitry Karpeev 
27530298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2754264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2755264ace61SBarry Smith 
2756264ace61SBarry Smith #undef __FUNCT__
2757264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2758264ace61SBarry Smith /*@C
2759264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2760264ace61SBarry Smith 
2761264ace61SBarry Smith   Collective on DM
2762264ace61SBarry Smith 
2763264ace61SBarry Smith   Input Parameters:
2764264ace61SBarry Smith + dm     - The DM object
2765264ace61SBarry Smith - method - The name of the DM type
2766264ace61SBarry Smith 
2767264ace61SBarry Smith   Options Database Key:
2768264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2769264ace61SBarry Smith 
2770264ace61SBarry Smith   Notes:
2771e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2772264ace61SBarry Smith 
2773264ace61SBarry Smith   Level: intermediate
2774264ace61SBarry Smith 
2775264ace61SBarry Smith .keywords: DM, set, type
2776264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2777264ace61SBarry Smith @*/
277819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2779264ace61SBarry Smith {
2780264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2781264ace61SBarry Smith   PetscBool      match;
2782264ace61SBarry Smith   PetscErrorCode ierr;
2783264ace61SBarry Smith 
2784264ace61SBarry Smith   PetscFunctionBegin;
2785264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2786251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2787264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2788264ace61SBarry Smith 
27890f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
27901c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2791ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2792264ace61SBarry Smith 
2793264ace61SBarry Smith   if (dm->ops->destroy) {
2794264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
27950298fd71SBarry Smith     dm->ops->destroy = NULL;
2796264ace61SBarry Smith   }
2797264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2798264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2799264ace61SBarry Smith   PetscFunctionReturn(0);
2800264ace61SBarry Smith }
2801264ace61SBarry Smith 
2802264ace61SBarry Smith #undef __FUNCT__
2803264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2804264ace61SBarry Smith /*@C
2805264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2806264ace61SBarry Smith 
2807264ace61SBarry Smith   Not Collective
2808264ace61SBarry Smith 
2809264ace61SBarry Smith   Input Parameter:
2810264ace61SBarry Smith . dm  - The DM
2811264ace61SBarry Smith 
2812264ace61SBarry Smith   Output Parameter:
2813264ace61SBarry Smith . type - The DM type name
2814264ace61SBarry Smith 
2815264ace61SBarry Smith   Level: intermediate
2816264ace61SBarry Smith 
2817264ace61SBarry Smith .keywords: DM, get, type, name
2818264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2819264ace61SBarry Smith @*/
282019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2821264ace61SBarry Smith {
2822264ace61SBarry Smith   PetscErrorCode ierr;
2823264ace61SBarry Smith 
2824264ace61SBarry Smith   PetscFunctionBegin;
2825264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2826c959eef4SJed Brown   PetscValidPointer(type,2);
2827607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2828264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2829264ace61SBarry Smith   PetscFunctionReturn(0);
2830264ace61SBarry Smith }
2831264ace61SBarry Smith 
283267a56275SMatthew G Knepley #undef __FUNCT__
283367a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
283467a56275SMatthew G Knepley /*@C
283567a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
283667a56275SMatthew G Knepley 
283767a56275SMatthew G Knepley   Collective on DM
283867a56275SMatthew G Knepley 
283967a56275SMatthew G Knepley   Input Parameters:
284067a56275SMatthew G Knepley + dm - the DM
284167a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
284267a56275SMatthew G Knepley 
284367a56275SMatthew G Knepley   Output Parameter:
284467a56275SMatthew G Knepley . M - pointer to new DM
284567a56275SMatthew G Knepley 
284667a56275SMatthew G Knepley   Notes:
284767a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
284867a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
284967a56275SMatthew G Knepley   of the input DM.
285067a56275SMatthew G Knepley 
285167a56275SMatthew G Knepley   Level: intermediate
285267a56275SMatthew G Knepley 
285367a56275SMatthew G Knepley .seealso: DMCreate()
285467a56275SMatthew G Knepley @*/
285519fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
285667a56275SMatthew G Knepley {
285767a56275SMatthew G Knepley   DM             B;
285867a56275SMatthew G Knepley   char           convname[256];
285967a56275SMatthew G Knepley   PetscBool      sametype, issame;
286067a56275SMatthew G Knepley   PetscErrorCode ierr;
286167a56275SMatthew G Knepley 
286267a56275SMatthew G Knepley   PetscFunctionBegin;
286367a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
286467a56275SMatthew G Knepley   PetscValidType(dm,1);
286567a56275SMatthew G Knepley   PetscValidPointer(M,3);
2866251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
286767a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
286867a56275SMatthew G Knepley   {
28690298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
287067a56275SMatthew G Knepley 
287167a56275SMatthew G Knepley     /*
287267a56275SMatthew G Knepley        Order of precedence:
287367a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
287467a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
287567a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
287667a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
287767a56275SMatthew G Knepley        5) Use a really basic converter.
287867a56275SMatthew G Knepley     */
287967a56275SMatthew G Knepley 
288067a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
288167a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
288267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
288367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
288467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
288567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28860005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
288767a56275SMatthew G Knepley     if (conv) goto foundconv;
288867a56275SMatthew G Knepley 
288967a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
289082f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
289167a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
289267a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
289367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
289467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
289567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
289667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28970005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
289867a56275SMatthew G Knepley     if (conv) {
2899fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
290067a56275SMatthew G Knepley       goto foundconv;
290167a56275SMatthew G Knepley     }
290267a56275SMatthew G Knepley 
290367a56275SMatthew G Knepley #if 0
290467a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
290567a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2906fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
290767a56275SMatthew G Knepley     if (conv) goto foundconv;
290867a56275SMatthew G Knepley 
290967a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
291067a56275SMatthew G Knepley     if (dm->ops->convert) {
291167a56275SMatthew G Knepley       conv = dm->ops->convert;
291267a56275SMatthew G Knepley     }
291367a56275SMatthew G Knepley     if (conv) goto foundconv;
291467a56275SMatthew G Knepley #endif
291567a56275SMatthew G Knepley 
291667a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
291782f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
291867a56275SMatthew G Knepley 
291967a56275SMatthew G Knepley foundconv:
292067a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
292167a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
292267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
292367a56275SMatthew G Knepley   }
292467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
292567a56275SMatthew G Knepley   PetscFunctionReturn(0);
292667a56275SMatthew G Knepley }
2927264ace61SBarry Smith 
2928264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2929264ace61SBarry Smith 
2930264ace61SBarry Smith #undef __FUNCT__
2931264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2932264ace61SBarry Smith /*@C
29331c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29341c84c290SBarry Smith 
29351c84c290SBarry Smith   Not Collective
29361c84c290SBarry Smith 
29371c84c290SBarry Smith   Input Parameters:
29381c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29391c84c290SBarry Smith - create_func - The creation routine itself
29401c84c290SBarry Smith 
29411c84c290SBarry Smith   Notes:
29421c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29431c84c290SBarry Smith 
29441c84c290SBarry Smith 
29451c84c290SBarry Smith   Sample usage:
29461c84c290SBarry Smith .vb
2947bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
29481c84c290SBarry Smith .ve
29491c84c290SBarry Smith 
29501c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
29511c84c290SBarry Smith .vb
29521c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
29531c84c290SBarry Smith     DMSetType(DM,"my_da");
29541c84c290SBarry Smith .ve
29551c84c290SBarry Smith    or at runtime via the option
29561c84c290SBarry Smith .vb
29571c84c290SBarry Smith     -da_type my_da
29581c84c290SBarry Smith .ve
2959264ace61SBarry Smith 
2960264ace61SBarry Smith   Level: advanced
29611c84c290SBarry Smith 
29621c84c290SBarry Smith .keywords: DM, register
2963bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
29641c84c290SBarry Smith 
2965264ace61SBarry Smith @*/
2966bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2967264ace61SBarry Smith {
2968264ace61SBarry Smith   PetscErrorCode ierr;
2969264ace61SBarry Smith 
2970264ace61SBarry Smith   PetscFunctionBegin;
2971a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2972264ace61SBarry Smith   PetscFunctionReturn(0);
2973264ace61SBarry Smith }
2974264ace61SBarry Smith 
2975b859378eSBarry Smith #undef __FUNCT__
2976b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2977b859378eSBarry Smith /*@C
297855849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2979b859378eSBarry Smith 
2980b859378eSBarry Smith   Collective on PetscViewer
2981b859378eSBarry Smith 
2982b859378eSBarry Smith   Input Parameters:
2983b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2984b859378eSBarry Smith            some related function before a call to DMLoad().
2985b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2986b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2987b859378eSBarry Smith 
2988b859378eSBarry Smith    Level: intermediate
2989b859378eSBarry Smith 
2990b859378eSBarry Smith   Notes:
299155849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2992b859378eSBarry Smith 
2993b859378eSBarry Smith   Notes for advanced users:
2994b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2995b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2996b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2997b859378eSBarry Smith   format is
2998b859378eSBarry Smith .vb
2999b859378eSBarry Smith      has not yet been determined
3000b859378eSBarry Smith .ve
3001b859378eSBarry Smith 
3002b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3003b859378eSBarry Smith @*/
3004b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3005b859378eSBarry Smith {
30069331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3007b859378eSBarry Smith   PetscErrorCode ierr;
3008b859378eSBarry Smith 
3009b859378eSBarry Smith   PetscFunctionBegin;
3010b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3011b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
301232c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
30139331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
30149331c7a4SMatthew G. Knepley   if (isbinary) {
30159331c7a4SMatthew G. Knepley     PetscInt classid;
30169331c7a4SMatthew G. Knepley     char     type[256];
3017b859378eSBarry Smith 
3018060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30199200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3020060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
302132c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30229331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30239331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30249331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30259331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3026b859378eSBarry Smith   PetscFunctionReturn(0);
3027b859378eSBarry Smith }
3028b859378eSBarry Smith 
30297da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30307da65231SMatthew G Knepley 
30317da65231SMatthew G Knepley #undef __FUNCT__
30327da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3033a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3034a6dfd86eSKarl Rupp {
30351d47ebbbSSatish Balay   PetscInt       f;
30361b30c384SMatthew G Knepley   PetscErrorCode ierr;
30371b30c384SMatthew G Knepley 
30387da65231SMatthew G Knepley   PetscFunctionBegin;
303974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30401d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
304157622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30427da65231SMatthew G Knepley   }
30437da65231SMatthew G Knepley   PetscFunctionReturn(0);
30447da65231SMatthew G Knepley }
30457da65231SMatthew G Knepley 
30467da65231SMatthew G Knepley #undef __FUNCT__
30477da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3048a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3049a6dfd86eSKarl Rupp {
30501b30c384SMatthew G Knepley   PetscInt       f, g;
30517da65231SMatthew G Knepley   PetscErrorCode ierr;
30527da65231SMatthew G Knepley 
30537da65231SMatthew G Knepley   PetscFunctionBegin;
305474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30551d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
305674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
30571d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3058e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
30597da65231SMatthew G Knepley     }
306074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30617da65231SMatthew G Knepley   }
30627da65231SMatthew G Knepley   PetscFunctionReturn(0);
30637da65231SMatthew G Knepley }
3064e7c4fc90SDmitry Karpeev 
3065970e74d5SMatthew G Knepley #undef __FUNCT__
3066e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
30676113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3068e759306cSMatthew G. Knepley {
3069e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3070e759306cSMatthew G. Knepley   PetscInt       p;
3071e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3072e759306cSMatthew G. Knepley 
3073e759306cSMatthew G. Knepley   PetscFunctionBegin;
3074e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3075e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3076e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3077e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3078e759306cSMatthew G. Knepley     if (p == rank) {
3079e759306cSMatthew G. Knepley       Vec x;
3080e759306cSMatthew G. Knepley 
3081e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3082e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
30836113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3084e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3085e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3086e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3087e759306cSMatthew G. Knepley     }
3088e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3089e759306cSMatthew G. Knepley   }
3090e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3091e759306cSMatthew G. Knepley }
3092e759306cSMatthew G. Knepley 
3093e759306cSMatthew G. Knepley #undef __FUNCT__
309488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
309588ed4aceSMatthew G Knepley /*@
309688ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
309788ed4aceSMatthew G Knepley 
309888ed4aceSMatthew G Knepley   Input Parameter:
309988ed4aceSMatthew G Knepley . dm - The DM
310088ed4aceSMatthew G Knepley 
310188ed4aceSMatthew G Knepley   Output Parameter:
310288ed4aceSMatthew G Knepley . section - The PetscSection
310388ed4aceSMatthew G Knepley 
310488ed4aceSMatthew G Knepley   Level: intermediate
310588ed4aceSMatthew G Knepley 
310688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
310788ed4aceSMatthew G Knepley 
310888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
310988ed4aceSMatthew G Knepley @*/
31100adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
31110adebc6cSBarry Smith {
3112fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3113fd59a867SMatthew G. Knepley 
311488ed4aceSMatthew G Knepley   PetscFunctionBegin;
311588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
311688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
31172f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31182f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3119ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
31202f0f8703SMatthew G. Knepley   }
312188ed4aceSMatthew G Knepley   *section = dm->defaultSection;
312288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
312388ed4aceSMatthew G Knepley }
312488ed4aceSMatthew G Knepley 
312588ed4aceSMatthew G Knepley #undef __FUNCT__
312688ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
312788ed4aceSMatthew G Knepley /*@
312888ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
312988ed4aceSMatthew G Knepley 
313088ed4aceSMatthew G Knepley   Input Parameters:
313188ed4aceSMatthew G Knepley + dm - The DM
313288ed4aceSMatthew G Knepley - section - The PetscSection
313388ed4aceSMatthew G Knepley 
313488ed4aceSMatthew G Knepley   Level: intermediate
313588ed4aceSMatthew G Knepley 
313688ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
313788ed4aceSMatthew G Knepley 
313888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
313988ed4aceSMatthew G Knepley @*/
31400adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31410adebc6cSBarry Smith {
3142c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3143af122d2aSMatthew G Knepley   PetscInt       f;
314488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
314588ed4aceSMatthew G Knepley 
314688ed4aceSMatthew G Knepley   PetscFunctionBegin;
314788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3148c473ab19SMatthew G. Knepley   if (section) {
31491d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
31501d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3151c473ab19SMatthew G. Knepley   }
315288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
315388ed4aceSMatthew G Knepley   dm->defaultSection = section;
3154c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3155af122d2aSMatthew G Knepley   if (numFields) {
3156af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3157af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
31580f21e855SMatthew G. Knepley       PetscObject disc;
3159af122d2aSMatthew G Knepley       const char *name;
3160af122d2aSMatthew G Knepley 
3161af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
31620f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
31630f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3164af122d2aSMatthew G Knepley     }
3165af122d2aSMatthew G Knepley   }
31661d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
31671d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
316888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
316988ed4aceSMatthew G Knepley }
317088ed4aceSMatthew G Knepley 
317188ed4aceSMatthew G Knepley #undef __FUNCT__
31729435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
31739435951eSToby Isaac /*@
31749435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
31759435951eSToby Isaac 
3176e228b242SToby Isaac   not collective
3177e228b242SToby Isaac 
31789435951eSToby Isaac   Input Parameter:
31799435951eSToby Isaac . dm - The DM
31809435951eSToby Isaac 
31819435951eSToby Isaac   Output Parameter:
31829435951eSToby 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.
31839435951eSToby 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.
31849435951eSToby Isaac 
31859435951eSToby Isaac   Level: advanced
31869435951eSToby Isaac 
31879435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
31889435951eSToby Isaac 
31899435951eSToby Isaac .seealso: DMSetDefaultConstraints()
31909435951eSToby Isaac @*/
31919435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
31929435951eSToby Isaac {
31939435951eSToby Isaac   PetscErrorCode ierr;
31949435951eSToby Isaac 
31959435951eSToby Isaac   PetscFunctionBegin;
31969435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31979435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
319845a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
319945a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
32009435951eSToby Isaac   PetscFunctionReturn(0);
32019435951eSToby Isaac }
32029435951eSToby Isaac 
32039435951eSToby Isaac #undef __FUNCT__
32049435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
32059435951eSToby Isaac /*@
32069435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32079435951eSToby Isaac 
32089435951eSToby 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().
32099435951eSToby Isaac 
32109435951eSToby 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.
32119435951eSToby Isaac 
3212e228b242SToby Isaac   collective on dm
3213e228b242SToby Isaac 
32149435951eSToby Isaac   Input Parameters:
32159435951eSToby Isaac + dm - The DM
3216e228b242SToby 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).
3217e228b242SToby 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).
32189435951eSToby Isaac 
32199435951eSToby Isaac   Level: advanced
32209435951eSToby Isaac 
32219435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32229435951eSToby Isaac 
32239435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32249435951eSToby Isaac @*/
32259435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32269435951eSToby Isaac {
3227e228b242SToby Isaac   PetscMPIInt result;
32289435951eSToby Isaac   PetscErrorCode ierr;
32299435951eSToby Isaac 
32309435951eSToby Isaac   PetscFunctionBegin;
32319435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3232e228b242SToby Isaac   if (section) {
3233e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3234e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3235e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3236e228b242SToby Isaac   }
3237e228b242SToby Isaac   if (mat) {
3238e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3239e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3240e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3241e228b242SToby Isaac   }
32429435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32439435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
32449435951eSToby Isaac   dm->defaultConstraintSection = section;
32459435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
32469435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
32479435951eSToby Isaac   dm->defaultConstraintMat = mat;
32489435951eSToby Isaac   PetscFunctionReturn(0);
32499435951eSToby Isaac }
32509435951eSToby Isaac 
3251f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
32529435951eSToby Isaac #undef __FUNCT__
3253284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3254507e4973SMatthew G. Knepley /*
3255507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3256507e4973SMatthew G. Knepley 
3257507e4973SMatthew G. Knepley   Input Parameters:
3258507e4973SMatthew G. Knepley + dm - The DM
3259507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3260507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3261507e4973SMatthew G. Knepley 
3262507e4973SMatthew G. Knepley   Level: intermediate
3263507e4973SMatthew G. Knepley 
3264507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3265507e4973SMatthew G. Knepley */
3266f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3267507e4973SMatthew G. Knepley {
3268507e4973SMatthew G. Knepley   MPI_Comm        comm;
3269507e4973SMatthew G. Knepley   PetscLayout     layout;
3270507e4973SMatthew G. Knepley   const PetscInt *ranges;
3271507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3272507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3273507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3274507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3275507e4973SMatthew G. Knepley 
3276507e4973SMatthew G. Knepley   PetscFunctionBegin;
3277507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3278507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3279507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3280507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3281507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3282507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3283507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3284507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3285507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3286507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3287507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3288507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3289f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3290507e4973SMatthew G. Knepley 
3291507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3292507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3293507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3294507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3295507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3296507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3297507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3298507e4973SMatthew G. Knepley     if ((gdof < 0 ? -(gdof+1) : gdof) != dof) {ierr = PetscSynchronizedPrintf(comm, "[%d]Global dof %d for point %d not equal to local dof %d\n", rank, gdof, p, dof);CHKERRQ(ierr); valid = PETSC_FALSE;}
3299507e4973SMatthew G. Knepley     if (gcdof && (gcdof != cdof)) {ierr = PetscSynchronizedPrintf(comm, "[%d]Global constraints %d for point %d not equal to local constraints %d\n", rank, gcdof, p, cdof);CHKERRQ(ierr); valid = PETSC_FALSE;}
3300507e4973SMatthew G. Knepley     if (gdof < 0) {
3301507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3302507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3303507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3304507e4973SMatthew G. Knepley 
3305507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3306507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3307507e4973SMatthew G. Knepley         if ((r < 0) || (r >= size)) {ierr = PetscSynchronizedPrintf(comm, "[%d]Point %d mapped to invalid process %d (%d, %d)\n", rank, p, r, gdof, goff);CHKERRQ(ierr); valid = PETSC_FALSE;break;}
3308507e4973SMatthew G. Knepley       }
3309507e4973SMatthew G. Knepley     }
3310507e4973SMatthew G. Knepley   }
3311507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3312507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3313b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3314507e4973SMatthew G. Knepley   if (!gvalid) {
3315507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3316507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3317507e4973SMatthew G. Knepley   }
3318507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3319507e4973SMatthew G. Knepley }
3320f741bcd2SMatthew G. Knepley #endif
3321507e4973SMatthew G. Knepley 
3322507e4973SMatthew G. Knepley #undef __FUNCT__
332388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
332488ed4aceSMatthew G Knepley /*@
332588ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
332688ed4aceSMatthew G Knepley 
33278b1ab98fSJed Brown   Collective on DM
33288b1ab98fSJed Brown 
332988ed4aceSMatthew G Knepley   Input Parameter:
333088ed4aceSMatthew G Knepley . dm - The DM
333188ed4aceSMatthew G Knepley 
333288ed4aceSMatthew G Knepley   Output Parameter:
333388ed4aceSMatthew G Knepley . section - The PetscSection
333488ed4aceSMatthew G Knepley 
333588ed4aceSMatthew G Knepley   Level: intermediate
333688ed4aceSMatthew G Knepley 
333788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
333888ed4aceSMatthew G Knepley 
333988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
334088ed4aceSMatthew G Knepley @*/
33410adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33420adebc6cSBarry Smith {
334388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
334488ed4aceSMatthew G Knepley 
334588ed4aceSMatthew G Knepley   PetscFunctionBegin;
334688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
334788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
334888ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3349fd59a867SMatthew G. Knepley     PetscSection s;
3350fd59a867SMatthew G. Knepley 
3351fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3352fd59a867SMatthew 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");
3353fd59a867SMatthew 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");
335415b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3355cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3356ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3357685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
335888ed4aceSMatthew G Knepley   }
335988ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
336088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
336188ed4aceSMatthew G Knepley }
336288ed4aceSMatthew G Knepley 
336388ed4aceSMatthew G Knepley #undef __FUNCT__
3364b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3365b21d0597SMatthew G Knepley /*@
3366b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3367b21d0597SMatthew G Knepley 
3368b21d0597SMatthew G Knepley   Input Parameters:
3369b21d0597SMatthew G Knepley + dm - The DM
33705080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3371b21d0597SMatthew G Knepley 
3372b21d0597SMatthew G Knepley   Level: intermediate
3373b21d0597SMatthew G Knepley 
3374b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3375b21d0597SMatthew G Knepley 
3376b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3377b21d0597SMatthew G Knepley @*/
33780adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
33790adebc6cSBarry Smith {
3380b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3381b21d0597SMatthew G Knepley 
3382b21d0597SMatthew G Knepley   PetscFunctionBegin;
3383b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33845080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33851d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3386b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3387b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3388507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3389f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3390507e4973SMatthew G. Knepley #endif
3391b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3392b21d0597SMatthew G Knepley }
3393b21d0597SMatthew G Knepley 
3394b21d0597SMatthew G Knepley #undef __FUNCT__
339588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
339688ed4aceSMatthew G Knepley /*@
339788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
339888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
339988ed4aceSMatthew G Knepley 
340088ed4aceSMatthew G Knepley   Input Parameter:
340188ed4aceSMatthew G Knepley . dm - The DM
340288ed4aceSMatthew G Knepley 
340388ed4aceSMatthew G Knepley   Output Parameter:
340488ed4aceSMatthew G Knepley . sf - The PetscSF
340588ed4aceSMatthew G Knepley 
340688ed4aceSMatthew G Knepley   Level: intermediate
340788ed4aceSMatthew G Knepley 
340888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
340988ed4aceSMatthew G Knepley 
341088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
341188ed4aceSMatthew G Knepley @*/
34120adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
34130adebc6cSBarry Smith {
341488ed4aceSMatthew G Knepley   PetscInt       nroots;
341588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
341688ed4aceSMatthew G Knepley 
341788ed4aceSMatthew G Knepley   PetscFunctionBegin;
341888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
341988ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34200298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
342188ed4aceSMatthew G Knepley   if (nroots < 0) {
342288ed4aceSMatthew G Knepley     PetscSection section, gSection;
342388ed4aceSMatthew G Knepley 
342488ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
342531ea6d37SMatthew G Knepley     if (section) {
342688ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
342788ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
342831ea6d37SMatthew G Knepley     } else {
34290298fd71SBarry Smith       *sf = NULL;
343031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
343131ea6d37SMatthew G Knepley     }
343288ed4aceSMatthew G Knepley   }
343388ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
343488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
343588ed4aceSMatthew G Knepley }
343688ed4aceSMatthew G Knepley 
343788ed4aceSMatthew G Knepley #undef __FUNCT__
343888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
343988ed4aceSMatthew G Knepley /*@
344088ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
344188ed4aceSMatthew G Knepley 
344288ed4aceSMatthew G Knepley   Input Parameters:
344388ed4aceSMatthew G Knepley + dm - The DM
344488ed4aceSMatthew G Knepley - sf - The PetscSF
344588ed4aceSMatthew G Knepley 
344688ed4aceSMatthew G Knepley   Level: intermediate
344788ed4aceSMatthew G Knepley 
344888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
344988ed4aceSMatthew G Knepley 
345088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
345188ed4aceSMatthew G Knepley @*/
34520adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
34530adebc6cSBarry Smith {
345488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
345588ed4aceSMatthew G Knepley 
345688ed4aceSMatthew G Knepley   PetscFunctionBegin;
345788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
345888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
345988ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
346088ed4aceSMatthew G Knepley   dm->defaultSF = sf;
346188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
346288ed4aceSMatthew G Knepley }
346388ed4aceSMatthew G Knepley 
346488ed4aceSMatthew G Knepley #undef __FUNCT__
346588ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
346688ed4aceSMatthew G Knepley /*@C
346788ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
346888ed4aceSMatthew G Knepley   describing the data layout.
346988ed4aceSMatthew G Knepley 
347088ed4aceSMatthew G Knepley   Input Parameters:
347188ed4aceSMatthew G Knepley + dm - The DM
347288ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
347388ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
347488ed4aceSMatthew G Knepley 
347588ed4aceSMatthew G Knepley   Level: intermediate
347688ed4aceSMatthew G Knepley 
347788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
347888ed4aceSMatthew G Knepley @*/
347988ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
348088ed4aceSMatthew G Knepley {
348182f516ccSBarry Smith   MPI_Comm       comm;
348288ed4aceSMatthew G Knepley   PetscLayout    layout;
348388ed4aceSMatthew G Knepley   const PetscInt *ranges;
348488ed4aceSMatthew G Knepley   PetscInt       *local;
348588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3486ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
348788ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
348888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
348988ed4aceSMatthew G Knepley 
349088ed4aceSMatthew G Knepley   PetscFunctionBegin;
349182f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
349288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
349388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
349488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
349588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
349688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
349788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
349888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
349988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
350088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
350188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3502ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35036636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
350488ed4aceSMatthew G Knepley 
35056636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35066636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3507235fbf56SMatthew 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));
35086636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
350988ed4aceSMatthew G Knepley   }
3510785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3511785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
351288ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
35131f588964SMatthew G Knepley     const PetscInt *cind;
35146636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
351588ed4aceSMatthew G Knepley 
351688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
351788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
351888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
351988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
352088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35216636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
352288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35236636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35246636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35256636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3526057b4bcdSMatthew 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);
35276636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35286636e97aSMatthew G Knepley     }
352988ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
353088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
353188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
353288ed4aceSMatthew G Knepley     }
353388ed4aceSMatthew G Knepley     if (gdof < 0) {
35346636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
353588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
353688ed4aceSMatthew G Knepley 
353705376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
353831d3f06eSJed Brown         if (r < 0) r = -(r+2);
353905376888SMatthew 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);
354088ed4aceSMatthew G Knepley         remote[l].rank  = r;
354188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
354288ed4aceSMatthew G Knepley       }
354388ed4aceSMatthew G Knepley     } else {
35446636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
354588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
354688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
354788ed4aceSMatthew G Knepley       }
354888ed4aceSMatthew G Knepley     }
354988ed4aceSMatthew G Knepley   }
35506636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
355188ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
355288ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
355388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
355488ed4aceSMatthew G Knepley }
3555af122d2aSMatthew G Knepley 
3556af122d2aSMatthew G Knepley #undef __FUNCT__
3557b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3558b21d0597SMatthew G Knepley /*@
3559b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3560b21d0597SMatthew G Knepley 
3561b21d0597SMatthew G Knepley   Input Parameter:
3562b21d0597SMatthew G Knepley . dm - The DM
3563b21d0597SMatthew G Knepley 
3564b21d0597SMatthew G Knepley   Output Parameter:
3565b21d0597SMatthew G Knepley . sf - The PetscSF
3566b21d0597SMatthew G Knepley 
3567b21d0597SMatthew G Knepley   Level: intermediate
3568b21d0597SMatthew G Knepley 
3569b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3570b21d0597SMatthew G Knepley 
3571057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3572b21d0597SMatthew G Knepley @*/
35730adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
35740adebc6cSBarry Smith {
3575b21d0597SMatthew G Knepley   PetscFunctionBegin;
3576b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3577b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3578b21d0597SMatthew G Knepley   *sf = dm->sf;
3579b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3580b21d0597SMatthew G Knepley }
3581b21d0597SMatthew G Knepley 
3582b21d0597SMatthew G Knepley #undef __FUNCT__
3583057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3584057b4bcdSMatthew G Knepley /*@
3585057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3586057b4bcdSMatthew G Knepley 
3587057b4bcdSMatthew G Knepley   Input Parameters:
3588057b4bcdSMatthew G Knepley + dm - The DM
3589057b4bcdSMatthew G Knepley - sf - The PetscSF
3590057b4bcdSMatthew G Knepley 
3591057b4bcdSMatthew G Knepley   Level: intermediate
3592057b4bcdSMatthew G Knepley 
3593057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3594057b4bcdSMatthew G Knepley @*/
35950adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
35960adebc6cSBarry Smith {
3597057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3598057b4bcdSMatthew G Knepley 
3599057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3600057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3601057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3602057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3603057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3604057b4bcdSMatthew G Knepley   dm->sf = sf;
3605057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3606057b4bcdSMatthew G Knepley }
3607057b4bcdSMatthew G Knepley 
3608057b4bcdSMatthew G Knepley #undef __FUNCT__
36092764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
36102764a2aaSMatthew G. Knepley /*@
36112764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
36122764a2aaSMatthew G. Knepley 
36132764a2aaSMatthew G. Knepley   Input Parameter:
36142764a2aaSMatthew G. Knepley . dm - The DM
36152764a2aaSMatthew G. Knepley 
36162764a2aaSMatthew G. Knepley   Output Parameter:
36172764a2aaSMatthew G. Knepley . prob - The PetscDS
36182764a2aaSMatthew G. Knepley 
36192764a2aaSMatthew G. Knepley   Level: developer
36202764a2aaSMatthew G. Knepley 
36212764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36222764a2aaSMatthew G. Knepley @*/
36232764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3624af122d2aSMatthew G Knepley {
3625af122d2aSMatthew G Knepley   PetscFunctionBegin;
3626af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36270f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36280f21e855SMatthew G. Knepley   *prob = dm->prob;
36290f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36300f21e855SMatthew G. Knepley }
36310f21e855SMatthew G. Knepley 
36320f21e855SMatthew G. Knepley #undef __FUNCT__
36332764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36342764a2aaSMatthew G. Knepley /*@
36352764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36362764a2aaSMatthew G. Knepley 
36372764a2aaSMatthew G. Knepley   Input Parameters:
36382764a2aaSMatthew G. Knepley + dm - The DM
36392764a2aaSMatthew G. Knepley - prob - The PetscDS
36402764a2aaSMatthew G. Knepley 
36412764a2aaSMatthew G. Knepley   Level: developer
36422764a2aaSMatthew G. Knepley 
36432764a2aaSMatthew G. Knepley .seealso: DMGetDS()
36442764a2aaSMatthew G. Knepley @*/
36452764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
36460f21e855SMatthew G. Knepley {
36470f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36480f21e855SMatthew G. Knepley 
36490f21e855SMatthew G. Knepley   PetscFunctionBegin;
36500f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36512764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
36522764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
36530f21e855SMatthew G. Knepley   dm->prob = prob;
36540f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
36550f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36560f21e855SMatthew G. Knepley }
36570f21e855SMatthew G. Knepley 
36580f21e855SMatthew G. Knepley #undef __FUNCT__
36590f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
36600f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
36610f21e855SMatthew G. Knepley {
36620f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36630f21e855SMatthew G. Knepley 
36640f21e855SMatthew G. Knepley   PetscFunctionBegin;
36650f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36662764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3667af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3668af122d2aSMatthew G Knepley }
3669af122d2aSMatthew G Knepley 
3670af122d2aSMatthew G Knepley #undef __FUNCT__
3671af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3672af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3673af122d2aSMatthew G Knepley {
36740f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3675af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3676af122d2aSMatthew G Knepley 
3677af122d2aSMatthew G Knepley   PetscFunctionBegin;
3678af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36792764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
36800f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
36810f21e855SMatthew G. Knepley     PetscContainer obj;
36820f21e855SMatthew G. Knepley 
36830f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
36842764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
36850f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3686af122d2aSMatthew G Knepley   }
3687af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3688af122d2aSMatthew G Knepley }
3689af122d2aSMatthew G Knepley 
3690af122d2aSMatthew G Knepley #undef __FUNCT__
3691af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3692c1929be8SMatthew G. Knepley /*@
3693c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3694c1929be8SMatthew G. Knepley 
3695c1929be8SMatthew G. Knepley   Not collective
3696c1929be8SMatthew G. Knepley 
3697c1929be8SMatthew G. Knepley   Input Parameters:
3698c1929be8SMatthew G. Knepley + dm - The DM
3699c1929be8SMatthew G. Knepley - f  - The field number
3700c1929be8SMatthew G. Knepley 
3701c1929be8SMatthew G. Knepley   Output Parameter:
3702c1929be8SMatthew G. Knepley . field - The discretization object
3703c1929be8SMatthew G. Knepley 
3704c1929be8SMatthew G. Knepley   Level: developer
3705c1929be8SMatthew G. Knepley 
3706c1929be8SMatthew G. Knepley .seealso: DMSetField()
3707c1929be8SMatthew G. Knepley @*/
3708af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3709af122d2aSMatthew G Knepley {
37100f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37110f21e855SMatthew G. Knepley 
3712af122d2aSMatthew G Knepley   PetscFunctionBegin;
3713af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37142764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3715decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3716decb47aaSMatthew G. Knepley }
3717decb47aaSMatthew G. Knepley 
3718decb47aaSMatthew G. Knepley #undef __FUNCT__
3719decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3720c1929be8SMatthew G. Knepley /*@
3721c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3722c1929be8SMatthew G. Knepley 
3723c1929be8SMatthew G. Knepley   Logically collective on DM
3724c1929be8SMatthew G. Knepley 
3725c1929be8SMatthew G. Knepley   Input Parameters:
3726c1929be8SMatthew G. Knepley + dm - The DM
3727c1929be8SMatthew G. Knepley . f  - The field number
3728c1929be8SMatthew G. Knepley - field - The discretization object
3729c1929be8SMatthew G. Knepley 
3730c1929be8SMatthew G. Knepley   Level: developer
3731c1929be8SMatthew G. Knepley 
3732c1929be8SMatthew G. Knepley .seealso: DMGetField()
3733c1929be8SMatthew G. Knepley @*/
3734decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3735decb47aaSMatthew G. Knepley {
3736decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3737decb47aaSMatthew G. Knepley 
3738decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3739decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37402764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3741af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3742af122d2aSMatthew G Knepley }
37436636e97aSMatthew G Knepley 
37446636e97aSMatthew G Knepley #undef __FUNCT__
3745b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3746b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3747b64e0483SPeter Brune {
3748b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3749b64e0483SPeter Brune   PetscErrorCode ierr;
3750b64e0483SPeter Brune   Vec coords,ccoords;
37516dbf9973SLawrence Mitchell   Mat inject;
3752b64e0483SPeter Brune   PetscFunctionBegin;
3753b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3754b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3755b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3756b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3757b64e0483SPeter Brune   if (coords && !ccoords) {
3758b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
37596dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
37602adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
37616dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3762b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3763b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3764b64e0483SPeter Brune   }
3765b64e0483SPeter Brune   PetscFunctionReturn(0);
3766b64e0483SPeter Brune }
3767b64e0483SPeter Brune 
3768b64e0483SPeter Brune #undef __FUNCT__
376903dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
377003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
377103dadc2fSPeter Brune {
377203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
377303dadc2fSPeter Brune   PetscErrorCode ierr;
377403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
377503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
377603dadc2fSPeter Brune   PetscFunctionBegin;
377703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
377803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
377903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
378003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
378103dadc2fSPeter Brune   if (coords && !ccoords) {
378203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
378303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
378403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
378503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
379003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
379103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
379203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
379303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
379403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
379503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
379603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
379703dadc2fSPeter Brune   }
379803dadc2fSPeter Brune   PetscFunctionReturn(0);
379903dadc2fSPeter Brune }
380003dadc2fSPeter Brune 
380103dadc2fSPeter Brune #undef __FUNCT__
3802c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3803c73cfb54SMatthew G. Knepley /*@
3804c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3805c73cfb54SMatthew G. Knepley 
3806c73cfb54SMatthew G. Knepley   Not collective
3807c73cfb54SMatthew G. Knepley 
3808c73cfb54SMatthew G. Knepley   Input Parameter:
3809c73cfb54SMatthew G. Knepley . dm - The DM
3810c73cfb54SMatthew G. Knepley 
3811c73cfb54SMatthew G. Knepley   Output Parameter:
3812c73cfb54SMatthew G. Knepley . dim - The topological dimension
3813c73cfb54SMatthew G. Knepley 
3814c73cfb54SMatthew G. Knepley   Level: beginner
3815c73cfb54SMatthew G. Knepley 
3816c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3817c73cfb54SMatthew G. Knepley @*/
3818c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3819c73cfb54SMatthew G. Knepley {
3820c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3821c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3822c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3823c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3824c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3825c73cfb54SMatthew G. Knepley }
3826c73cfb54SMatthew G. Knepley 
3827c73cfb54SMatthew G. Knepley #undef __FUNCT__
3828c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3829c73cfb54SMatthew G. Knepley /*@
3830c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3831c73cfb54SMatthew G. Knepley 
3832c73cfb54SMatthew G. Knepley   Collective on dm
3833c73cfb54SMatthew G. Knepley 
3834c73cfb54SMatthew G. Knepley   Input Parameters:
3835c73cfb54SMatthew G. Knepley + dm - The DM
3836c73cfb54SMatthew G. Knepley - dim - The topological dimension
3837c73cfb54SMatthew G. Knepley 
3838c73cfb54SMatthew G. Knepley   Level: beginner
3839c73cfb54SMatthew G. Knepley 
3840c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3841c73cfb54SMatthew G. Knepley @*/
3842c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3843c73cfb54SMatthew G. Knepley {
3844c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3845c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3846c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3847c73cfb54SMatthew G. Knepley   dm->dim = dim;
3848c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3849c73cfb54SMatthew G. Knepley }
3850c73cfb54SMatthew G. Knepley 
3851793f3fe5SMatthew G. Knepley #undef __FUNCT__
3852793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3853793f3fe5SMatthew G. Knepley /*@
3854793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3855793f3fe5SMatthew G. Knepley 
3856793f3fe5SMatthew G. Knepley   Collective on DM
3857793f3fe5SMatthew G. Knepley 
3858793f3fe5SMatthew G. Knepley   Input Parameters:
3859793f3fe5SMatthew G. Knepley + dm - the DM
3860793f3fe5SMatthew G. Knepley - dim - the dimension
3861793f3fe5SMatthew G. Knepley 
3862793f3fe5SMatthew G. Knepley   Output Parameters:
3863793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3864793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3865793f3fe5SMatthew G. Knepley 
3866793f3fe5SMatthew G. Knepley   Note:
3867793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3868793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3869793f3fe5SMatthew G. Knepley   then the interval is empty.
3870793f3fe5SMatthew G. Knepley 
3871793f3fe5SMatthew G. Knepley   Level: intermediate
3872793f3fe5SMatthew G. Knepley 
3873793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3874793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3875793f3fe5SMatthew G. Knepley @*/
3876793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3877793f3fe5SMatthew G. Knepley {
3878793f3fe5SMatthew G. Knepley   PetscInt       d;
3879793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3880793f3fe5SMatthew G. Knepley 
3881793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3882793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3883793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3884793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3885793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3886793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3887793f3fe5SMatthew G. Knepley }
3888793f3fe5SMatthew G. Knepley 
3889793f3fe5SMatthew G. Knepley #undef __FUNCT__
38906636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
38916636e97aSMatthew G Knepley /*@
38926636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
38936636e97aSMatthew G Knepley 
38946636e97aSMatthew G Knepley   Collective on DM
38956636e97aSMatthew G Knepley 
38966636e97aSMatthew G Knepley   Input Parameters:
38976636e97aSMatthew G Knepley + dm - the DM
38986636e97aSMatthew G Knepley - c - coordinate vector
38996636e97aSMatthew G Knepley 
39006636e97aSMatthew G Knepley   Note:
39016636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39026636e97aSMatthew G Knepley 
39036636e97aSMatthew G Knepley   Level: intermediate
39046636e97aSMatthew G Knepley 
39056636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39066636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
39076636e97aSMatthew G Knepley @*/
39086636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
39096636e97aSMatthew G Knepley {
39106636e97aSMatthew G Knepley   PetscErrorCode ierr;
39116636e97aSMatthew G Knepley 
39126636e97aSMatthew G Knepley   PetscFunctionBegin;
39136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39146636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39156636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39166636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39176636e97aSMatthew G Knepley   dm->coordinates = c;
39186636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3919b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
392003dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39216636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39226636e97aSMatthew G Knepley }
39236636e97aSMatthew G Knepley 
39246636e97aSMatthew G Knepley #undef __FUNCT__
39256636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39266636e97aSMatthew G Knepley /*@
39276636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39286636e97aSMatthew G Knepley 
39296636e97aSMatthew G Knepley   Collective on DM
39306636e97aSMatthew G Knepley 
39316636e97aSMatthew G Knepley    Input Parameters:
39326636e97aSMatthew G Knepley +  dm - the DM
39336636e97aSMatthew G Knepley -  c - coordinate vector
39346636e97aSMatthew G Knepley 
39356636e97aSMatthew G Knepley   Note:
39366636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39376636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39386636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39396636e97aSMatthew G Knepley 
39406636e97aSMatthew G Knepley   Level: intermediate
39416636e97aSMatthew G Knepley 
39426636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39436636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39446636e97aSMatthew G Knepley @*/
39456636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
39466636e97aSMatthew G Knepley {
39476636e97aSMatthew G Knepley   PetscErrorCode ierr;
39486636e97aSMatthew G Knepley 
39496636e97aSMatthew G Knepley   PetscFunctionBegin;
39506636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39516636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39526636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39536636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
39548865f1eaSKarl Rupp 
39556636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
39568865f1eaSKarl Rupp 
39576636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39586636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39596636e97aSMatthew G Knepley }
39606636e97aSMatthew G Knepley 
39616636e97aSMatthew G Knepley #undef __FUNCT__
39626636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
39636636e97aSMatthew G Knepley /*@
39646636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
39656636e97aSMatthew G Knepley 
39666636e97aSMatthew G Knepley   Not Collective
39676636e97aSMatthew G Knepley 
39686636e97aSMatthew G Knepley   Input Parameter:
39696636e97aSMatthew G Knepley . dm - the DM
39706636e97aSMatthew G Knepley 
39716636e97aSMatthew G Knepley   Output Parameter:
39726636e97aSMatthew G Knepley . c - global coordinate vector
39736636e97aSMatthew G Knepley 
39746636e97aSMatthew G Knepley   Note:
39756636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
39766636e97aSMatthew G Knepley 
39776636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
39786636e97aSMatthew G Knepley 
39796636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
39806636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
39816636e97aSMatthew G Knepley 
39826636e97aSMatthew G Knepley   Level: intermediate
39836636e97aSMatthew G Knepley 
39846636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39856636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
39866636e97aSMatthew G Knepley @*/
39876636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
39886636e97aSMatthew G Knepley {
39896636e97aSMatthew G Knepley   PetscErrorCode ierr;
39906636e97aSMatthew G Knepley 
39916636e97aSMatthew G Knepley   PetscFunctionBegin;
39926636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39936636e97aSMatthew G Knepley   PetscValidPointer(c,2);
39941f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
39950298fd71SBarry Smith     DM cdm = NULL;
39966636e97aSMatthew G Knepley 
39976636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
39986636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
39996636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
40006636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40016636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40026636e97aSMatthew G Knepley   }
40036636e97aSMatthew G Knepley   *c = dm->coordinates;
40046636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40056636e97aSMatthew G Knepley }
40066636e97aSMatthew G Knepley 
40076636e97aSMatthew G Knepley #undef __FUNCT__
40086636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
40096636e97aSMatthew G Knepley /*@
40106636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
40116636e97aSMatthew G Knepley 
40126636e97aSMatthew G Knepley   Collective on DM
40136636e97aSMatthew G Knepley 
40146636e97aSMatthew G Knepley   Input Parameter:
40156636e97aSMatthew G Knepley . dm - the DM
40166636e97aSMatthew G Knepley 
40176636e97aSMatthew G Knepley   Output Parameter:
40186636e97aSMatthew G Knepley . c - coordinate vector
40196636e97aSMatthew G Knepley 
40206636e97aSMatthew G Knepley   Note:
40216636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40226636e97aSMatthew G Knepley 
40236636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40246636e97aSMatthew G Knepley 
40256636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40266636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40276636e97aSMatthew G Knepley 
40286636e97aSMatthew G Knepley   Level: intermediate
40296636e97aSMatthew G Knepley 
40306636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40316636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40326636e97aSMatthew G Knepley @*/
40336636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40346636e97aSMatthew G Knepley {
40356636e97aSMatthew G Knepley   PetscErrorCode ierr;
40366636e97aSMatthew G Knepley 
40376636e97aSMatthew G Knepley   PetscFunctionBegin;
40386636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40396636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40401f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40410298fd71SBarry Smith     DM cdm = NULL;
40426636e97aSMatthew G Knepley 
40436636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40446636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
40456636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
40466636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40476636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40486636e97aSMatthew G Knepley   }
40496636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
40506636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40516636e97aSMatthew G Knepley }
40526636e97aSMatthew G Knepley 
40536636e97aSMatthew G Knepley #undef __FUNCT__
40546636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
40556636e97aSMatthew G Knepley /*@
40561cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
40576636e97aSMatthew G Knepley 
40586636e97aSMatthew G Knepley   Collective on DM
40596636e97aSMatthew G Knepley 
40606636e97aSMatthew G Knepley   Input Parameter:
40616636e97aSMatthew G Knepley . dm - the DM
40626636e97aSMatthew G Knepley 
40636636e97aSMatthew G Knepley   Output Parameter:
40646636e97aSMatthew G Knepley . cdm - coordinate DM
40656636e97aSMatthew G Knepley 
40666636e97aSMatthew G Knepley   Level: intermediate
40676636e97aSMatthew G Knepley 
40686636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40691cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40706636e97aSMatthew G Knepley @*/
40716636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
40726636e97aSMatthew G Knepley {
40736636e97aSMatthew G Knepley   PetscErrorCode ierr;
40746636e97aSMatthew G Knepley 
40756636e97aSMatthew G Knepley   PetscFunctionBegin;
40766636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40776636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
40786636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
407982f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
40806636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
40816636e97aSMatthew G Knepley   }
40826636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
40836636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40846636e97aSMatthew G Knepley }
4085e87bb0d3SMatthew G Knepley 
4086e87bb0d3SMatthew G Knepley #undef __FUNCT__
40871cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
40881cfe2091SMatthew G. Knepley /*@
40891cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
40901cfe2091SMatthew G. Knepley 
40911cfe2091SMatthew G. Knepley   Logically Collective on DM
40921cfe2091SMatthew G. Knepley 
40931cfe2091SMatthew G. Knepley   Input Parameters:
40941cfe2091SMatthew G. Knepley + dm - the DM
40951cfe2091SMatthew G. Knepley - cdm - coordinate DM
40961cfe2091SMatthew G. Knepley 
40971cfe2091SMatthew G. Knepley   Level: intermediate
40981cfe2091SMatthew G. Knepley 
40991cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41001cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41011cfe2091SMatthew G. Knepley @*/
41021cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41031cfe2091SMatthew G. Knepley {
41041cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41051cfe2091SMatthew G. Knepley 
41061cfe2091SMatthew G. Knepley   PetscFunctionBegin;
41071cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41081cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
41091cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
41101cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
41111cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
41121cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
41131cfe2091SMatthew G. Knepley }
41141cfe2091SMatthew G. Knepley 
41151cfe2091SMatthew G. Knepley #undef __FUNCT__
411646e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
411746e270d4SMatthew G. Knepley /*@
411846e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
411946e270d4SMatthew G. Knepley 
412046e270d4SMatthew G. Knepley   Not Collective
412146e270d4SMatthew G. Knepley 
412246e270d4SMatthew G. Knepley   Input Parameter:
412346e270d4SMatthew G. Knepley . dm - The DM object
412446e270d4SMatthew G. Knepley 
412546e270d4SMatthew G. Knepley   Output Parameter:
412646e270d4SMatthew G. Knepley . dim - The embedding dimension
412746e270d4SMatthew G. Knepley 
412846e270d4SMatthew G. Knepley   Level: intermediate
412946e270d4SMatthew G. Knepley 
413046e270d4SMatthew G. Knepley .keywords: mesh, coordinates
413146e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
413246e270d4SMatthew G. Knepley @*/
413346e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
413446e270d4SMatthew G. Knepley {
413546e270d4SMatthew G. Knepley   PetscFunctionBegin;
413646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413746e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41389a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41399a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41409a9a41abSToby Isaac   }
414146e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
414246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
414346e270d4SMatthew G. Knepley }
414446e270d4SMatthew G. Knepley 
414546e270d4SMatthew G. Knepley #undef __FUNCT__
414646e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
414746e270d4SMatthew G. Knepley /*@
414846e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
414946e270d4SMatthew G. Knepley 
415046e270d4SMatthew G. Knepley   Not Collective
415146e270d4SMatthew G. Knepley 
415246e270d4SMatthew G. Knepley   Input Parameters:
415346e270d4SMatthew G. Knepley + dm  - The DM object
415446e270d4SMatthew G. Knepley - dim - The embedding dimension
415546e270d4SMatthew G. Knepley 
415646e270d4SMatthew G. Knepley   Level: intermediate
415746e270d4SMatthew G. Knepley 
415846e270d4SMatthew G. Knepley .keywords: mesh, coordinates
415946e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
416046e270d4SMatthew G. Knepley @*/
416146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
416246e270d4SMatthew G. Knepley {
416346e270d4SMatthew G. Knepley   PetscFunctionBegin;
416446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
416546e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
416646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
416746e270d4SMatthew G. Knepley }
416846e270d4SMatthew G. Knepley 
416946e270d4SMatthew G. Knepley #undef __FUNCT__
4170e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4171e8abe2deSMatthew G. Knepley /*@
4172e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4173e8abe2deSMatthew G. Knepley 
4174e8abe2deSMatthew G. Knepley   Not Collective
4175e8abe2deSMatthew G. Knepley 
4176e8abe2deSMatthew G. Knepley   Input Parameter:
4177e8abe2deSMatthew G. Knepley . dm - The DM object
4178e8abe2deSMatthew G. Knepley 
4179e8abe2deSMatthew G. Knepley   Output Parameter:
4180e8abe2deSMatthew G. Knepley . section - The PetscSection object
4181e8abe2deSMatthew G. Knepley 
4182e8abe2deSMatthew G. Knepley   Level: intermediate
4183e8abe2deSMatthew G. Knepley 
4184e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4185e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4186e8abe2deSMatthew G. Knepley @*/
4187e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4188e8abe2deSMatthew G. Knepley {
4189e8abe2deSMatthew G. Knepley   DM             cdm;
4190e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4191e8abe2deSMatthew G. Knepley 
4192e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4193e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4194e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4195e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4196e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4197e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4198e8abe2deSMatthew G. Knepley }
4199e8abe2deSMatthew G. Knepley 
4200e8abe2deSMatthew G. Knepley #undef __FUNCT__
4201e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4202e8abe2deSMatthew G. Knepley /*@
4203e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4204e8abe2deSMatthew G. Knepley 
4205e8abe2deSMatthew G. Knepley   Not Collective
4206e8abe2deSMatthew G. Knepley 
4207e8abe2deSMatthew G. Knepley   Input Parameters:
4208e8abe2deSMatthew G. Knepley + dm      - The DM object
420946e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4210e8abe2deSMatthew G. Knepley - section - The PetscSection object
4211e8abe2deSMatthew G. Knepley 
4212e8abe2deSMatthew G. Knepley   Level: intermediate
4213e8abe2deSMatthew G. Knepley 
4214e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4215e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4216e8abe2deSMatthew G. Knepley @*/
421746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4218e8abe2deSMatthew G. Knepley {
4219e8abe2deSMatthew G. Knepley   DM             cdm;
4220e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4221e8abe2deSMatthew G. Knepley 
4222e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4223e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
422446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4225e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4226e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
422746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
422846e270d4SMatthew G. Knepley     PetscInt d = dim;
422946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
423046e270d4SMatthew G. Knepley 
423146e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
423246e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
423346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
423446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
423546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
423646e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
423746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
423846e270d4SMatthew G. Knepley     }
423946e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
424046e270d4SMatthew G. Knepley   }
4241e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4242e8abe2deSMatthew G. Knepley }
4243e8abe2deSMatthew G. Knepley 
4244e8abe2deSMatthew G. Knepley #undef __FUNCT__
4245c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
42465dc8c3f7SMatthew G. Knepley /*@C
42475dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42485dc8c3f7SMatthew G. Knepley 
42495dc8c3f7SMatthew G. Knepley   Input Parameters:
42505dc8c3f7SMatthew G. Knepley + dm      - The DM object
42515dc8c3f7SMatthew 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
42525dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42535dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42545dc8c3f7SMatthew G. Knepley 
42555dc8c3f7SMatthew G. Knepley   Level: developer
42565dc8c3f7SMatthew G. Knepley 
42575dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42585dc8c3f7SMatthew G. Knepley @*/
42595dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4260c6b900c6SMatthew G. Knepley {
4261c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4262c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4263c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4264c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
42655dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4266c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4267c6b900c6SMatthew G. Knepley }
4268c6b900c6SMatthew G. Knepley 
4269c6b900c6SMatthew G. Knepley #undef __FUNCT__
4270c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
42715dc8c3f7SMatthew G. Knepley /*@C
42725dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42735dc8c3f7SMatthew G. Knepley 
42745dc8c3f7SMatthew G. Knepley   Input Parameters:
42755dc8c3f7SMatthew G. Knepley + dm      - The DM object
42765dc8c3f7SMatthew 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
42775dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42785dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42795dc8c3f7SMatthew G. Knepley 
42805dc8c3f7SMatthew G. Knepley   Level: developer
42815dc8c3f7SMatthew G. Knepley 
42825dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42835dc8c3f7SMatthew G. Knepley @*/
42845dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4285c6b900c6SMatthew G. Knepley {
4286c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4287c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4288c6b900c6SMatthew G. Knepley 
4289c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4290c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42915dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
42925dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
42935dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
42945dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
42955dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4296c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4297c6b900c6SMatthew G. Knepley }
4298c6b900c6SMatthew G. Knepley 
4299c6b900c6SMatthew G. Knepley #undef __FUNCT__
4300e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4301e87bb0d3SMatthew G Knepley /*@
4302e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4303e87bb0d3SMatthew G Knepley 
4304e87bb0d3SMatthew G Knepley   Not collective
4305e87bb0d3SMatthew G Knepley 
4306e87bb0d3SMatthew G Knepley   Input Parameters:
4307e87bb0d3SMatthew G Knepley + dm - The DM
4308e87bb0d3SMatthew G Knepley - v - The Vec of points
4309e87bb0d3SMatthew G Knepley 
431061e3bb9bSMatthew G Knepley   Output Parameter:
4311e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4312e87bb0d3SMatthew G Knepley 
4313e87bb0d3SMatthew G Knepley   Level: developer
431461e3bb9bSMatthew G Knepley 
431561e3bb9bSMatthew G Knepley .keywords: point location, mesh
431661e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
431761e3bb9bSMatthew G Knepley @*/
4318e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4319e87bb0d3SMatthew G Knepley {
4320735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4321735aa83eSMatthew G Knepley 
4322e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4323e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4324e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4325e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
432647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4327735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4328735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
432982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
433047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4331e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4332e87bb0d3SMatthew G Knepley }
433314f150ffSMatthew G. Knepley 
433414f150ffSMatthew G. Knepley #undef __FUNCT__
433514f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4336f4d763aaSMatthew G. Knepley /*@
4337f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4338f4d763aaSMatthew G. Knepley 
4339f4d763aaSMatthew G. Knepley   Input Parameter:
4340f4d763aaSMatthew G. Knepley . dm - The original DM
4341f4d763aaSMatthew G. Knepley 
4342f4d763aaSMatthew G. Knepley   Output Parameter:
4343f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4344f4d763aaSMatthew G. Knepley 
4345f4d763aaSMatthew G. Knepley   Level: intermediate
4346f4d763aaSMatthew G. Knepley 
4347f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4348f4d763aaSMatthew G. Knepley @*/
434914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
435014f150ffSMatthew G. Knepley {
4351c26acbdeSMatthew G. Knepley   PetscSection   section;
4352c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
435314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
435414f150ffSMatthew G. Knepley 
435514f150ffSMatthew G. Knepley   PetscFunctionBegin;
435614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
435714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4358c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4359c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4360c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4361c26acbdeSMatthew G. Knepley     *odm = dm;
4362c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4363c26acbdeSMatthew G. Knepley   }
436414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4365c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
436614f150ffSMatthew G. Knepley     PetscSF      sf;
436714f150ffSMatthew G. Knepley 
436814f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
436914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
437014f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
437114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
437214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
437315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
437414f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
437514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
437614f150ffSMatthew G. Knepley   }
437714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
437814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
437914f150ffSMatthew G. Knepley }
4380f4d763aaSMatthew G. Knepley 
4381f4d763aaSMatthew G. Knepley #undef __FUNCT__
4382f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4383f4d763aaSMatthew G. Knepley /*@
4384cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4385f4d763aaSMatthew G. Knepley 
4386f4d763aaSMatthew G. Knepley   Input Parameter:
4387f4d763aaSMatthew G. Knepley . dm - The original DM
4388f4d763aaSMatthew G. Knepley 
4389cdb7a50dSMatthew G. Knepley   Output Parameters:
4390cdb7a50dSMatthew G. Knepley + num - The output sequence number
4391cdb7a50dSMatthew G. Knepley - val - The output sequence value
4392f4d763aaSMatthew G. Knepley 
4393f4d763aaSMatthew G. Knepley   Level: intermediate
4394f4d763aaSMatthew G. Knepley 
4395f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4396f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4397f4d763aaSMatthew G. Knepley 
4398f4d763aaSMatthew G. Knepley .seealso: VecView()
4399f4d763aaSMatthew G. Knepley @*/
4400cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4401f4d763aaSMatthew G. Knepley {
4402f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4403f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4404cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4405cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4406f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4407f4d763aaSMatthew G. Knepley }
4408f4d763aaSMatthew G. Knepley 
4409f4d763aaSMatthew G. Knepley #undef __FUNCT__
4410f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4411f4d763aaSMatthew G. Knepley /*@
4412cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4413f4d763aaSMatthew G. Knepley 
4414f4d763aaSMatthew G. Knepley   Input Parameters:
4415f4d763aaSMatthew G. Knepley + dm - The original DM
4416cdb7a50dSMatthew G. Knepley . num - The output sequence number
4417cdb7a50dSMatthew G. Knepley - val - The output sequence value
4418f4d763aaSMatthew G. Knepley 
4419f4d763aaSMatthew G. Knepley   Level: intermediate
4420f4d763aaSMatthew G. Knepley 
4421f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4422f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4423f4d763aaSMatthew G. Knepley 
4424f4d763aaSMatthew G. Knepley .seealso: VecView()
4425f4d763aaSMatthew G. Knepley @*/
4426cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4427f4d763aaSMatthew G. Knepley {
4428f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4429f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4430f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4431cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4432cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4433cdb7a50dSMatthew G. Knepley }
4434cdb7a50dSMatthew G. Knepley 
4435cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4436cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4437cdb7a50dSMatthew G. Knepley /*@C
4438cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4439cdb7a50dSMatthew G. Knepley 
4440cdb7a50dSMatthew G. Knepley   Input Parameters:
4441cdb7a50dSMatthew G. Knepley + dm   - The original DM
4442cdb7a50dSMatthew G. Knepley . name - The sequence name
4443cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4444cdb7a50dSMatthew G. Knepley 
4445cdb7a50dSMatthew G. Knepley   Output Parameter:
4446cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4447cdb7a50dSMatthew G. Knepley 
4448cdb7a50dSMatthew G. Knepley   Level: intermediate
4449cdb7a50dSMatthew G. Knepley 
4450cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4451cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4452cdb7a50dSMatthew G. Knepley 
4453cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4454cdb7a50dSMatthew G. Knepley @*/
4455cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4456cdb7a50dSMatthew G. Knepley {
4457cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4458cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4459cdb7a50dSMatthew G. Knepley 
4460cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4461cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4462cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4463cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4464cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4465cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4466cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4467cdb7a50dSMatthew G. Knepley     PetscScalar value;
4468cdb7a50dSMatthew G. Knepley 
4469cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
44704aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4471cdb7a50dSMatthew G. Knepley #endif
4472cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4473f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4474f4d763aaSMatthew G. Knepley }
44758e4ac7eaSMatthew G. Knepley 
44768e4ac7eaSMatthew G. Knepley #undef __FUNCT__
44778e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
44788e4ac7eaSMatthew G. Knepley /*@
44798e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
44808e4ac7eaSMatthew G. Knepley 
44818e4ac7eaSMatthew G. Knepley   Not collective
44828e4ac7eaSMatthew G. Knepley 
44838e4ac7eaSMatthew G. Knepley   Input Parameter:
44848e4ac7eaSMatthew G. Knepley . dm - The DM
44858e4ac7eaSMatthew G. Knepley 
44868e4ac7eaSMatthew G. Knepley   Output Parameter:
44878e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
44888e4ac7eaSMatthew G. Knepley 
44898e4ac7eaSMatthew G. Knepley   Level: beginner
44908e4ac7eaSMatthew G. Knepley 
44918e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
44928e4ac7eaSMatthew G. Knepley @*/
44938e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
44948e4ac7eaSMatthew G. Knepley {
44958e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
44968e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44978e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
44988e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
44998e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45008e4ac7eaSMatthew G. Knepley }
45018e4ac7eaSMatthew G. Knepley 
45028e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45038e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
45048e4ac7eaSMatthew G. Knepley /*@
45058e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
45068e4ac7eaSMatthew G. Knepley 
45078e4ac7eaSMatthew G. Knepley   Collective on dm
45088e4ac7eaSMatthew G. Knepley 
45098e4ac7eaSMatthew G. Knepley   Input Parameters:
45108e4ac7eaSMatthew G. Knepley + dm - The DM
45118e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
45128e4ac7eaSMatthew G. Knepley 
45138e4ac7eaSMatthew G. Knepley   Level: beginner
45148e4ac7eaSMatthew G. Knepley 
45158e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
45168e4ac7eaSMatthew G. Knepley @*/
45178e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
45188e4ac7eaSMatthew G. Knepley {
45198e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45208e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45218e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45228e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45238e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45248e4ac7eaSMatthew G. Knepley }
4525