xref: /petsc/src/dm/interface/dm.c (revision b2566f29c2b6470df769aa9f7deb9e2726b0959e)
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);
11338221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
11438221697SMatthew G. Knepley   if (coords) {
11538221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
11638221697SMatthew G. Knepley   } else {
11738221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
11838221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
11938221697SMatthew G. Knepley   }
120c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
121c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1225dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1235dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1245dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
125c6b900c6SMatthew G. Knepley   }
12638221697SMatthew G. Knepley   PetscFunctionReturn(0);
12738221697SMatthew G. Knepley }
12838221697SMatthew G. Knepley 
12938221697SMatthew G. Knepley #undef __FUNCT__
1309a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1319a42bb27SBarry Smith /*@C
132564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1339a42bb27SBarry Smith 
1348472ad0fSDave May    Logically Collective on DM
1359a42bb27SBarry Smith 
1369a42bb27SBarry Smith    Input Parameter:
1379a42bb27SBarry Smith +  da - initial distributed array
1388154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1399a42bb27SBarry Smith 
1409a42bb27SBarry Smith    Options Database:
141dd85299cSBarry Smith .   -dm_vec_type ctype
1429a42bb27SBarry Smith 
1439a42bb27SBarry Smith    Level: intermediate
1449a42bb27SBarry Smith 
1458472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1469a42bb27SBarry Smith @*/
14719fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1489a42bb27SBarry Smith {
1499a42bb27SBarry Smith   PetscErrorCode ierr;
1509a42bb27SBarry Smith 
1519a42bb27SBarry Smith   PetscFunctionBegin;
1529a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1539a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
15419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1559a42bb27SBarry Smith   PetscFunctionReturn(0);
1569a42bb27SBarry Smith }
1579a42bb27SBarry Smith 
1589a42bb27SBarry Smith #undef __FUNCT__
159c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
160c0dedaeaSBarry Smith /*@C
161c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
162c0dedaeaSBarry Smith 
1638472ad0fSDave May    Logically Collective on DM
164c0dedaeaSBarry Smith 
165c0dedaeaSBarry Smith    Input Parameter:
166c0dedaeaSBarry Smith .  da - initial distributed array
167c0dedaeaSBarry Smith 
168c0dedaeaSBarry Smith    Output Parameter:
169c0dedaeaSBarry Smith .  ctype - the vector type
170c0dedaeaSBarry Smith 
171c0dedaeaSBarry Smith    Level: intermediate
172c0dedaeaSBarry Smith 
1738472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
174c0dedaeaSBarry Smith @*/
175c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
176c0dedaeaSBarry Smith {
177c0dedaeaSBarry Smith   PetscFunctionBegin;
178c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
179c0dedaeaSBarry Smith   *ctype = da->vectype;
180c0dedaeaSBarry Smith   PetscFunctionReturn(0);
181c0dedaeaSBarry Smith }
182c0dedaeaSBarry Smith 
183c0dedaeaSBarry Smith #undef __FUNCT__
1845f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1855f1ad066SMatthew G Knepley /*@
18634f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
1875f1ad066SMatthew G Knepley 
1885f1ad066SMatthew G Knepley   Not collective
1895f1ad066SMatthew G Knepley 
1905f1ad066SMatthew G Knepley   Input Parameter:
1915f1ad066SMatthew G Knepley . v - The Vec
1925f1ad066SMatthew G Knepley 
1935f1ad066SMatthew G Knepley   Output Parameter:
1945f1ad066SMatthew G Knepley . dm - The DM
1955f1ad066SMatthew G Knepley 
1965f1ad066SMatthew G Knepley   Level: intermediate
1975f1ad066SMatthew G Knepley 
1985f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
1995f1ad066SMatthew G Knepley @*/
2005f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2015f1ad066SMatthew G Knepley {
2025f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2035f1ad066SMatthew G Knepley 
2045f1ad066SMatthew G Knepley   PetscFunctionBegin;
2055f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2065f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2075f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2085f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2095f1ad066SMatthew G Knepley }
2105f1ad066SMatthew G Knepley 
2115f1ad066SMatthew G Knepley #undef __FUNCT__
2125f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2135f1ad066SMatthew G Knepley /*@
214d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2155f1ad066SMatthew G Knepley 
2165f1ad066SMatthew G Knepley   Not collective
2175f1ad066SMatthew G Knepley 
2185f1ad066SMatthew G Knepley   Input Parameters:
2195f1ad066SMatthew G Knepley + v - The Vec
2205f1ad066SMatthew G Knepley - dm - The DM
2215f1ad066SMatthew G Knepley 
222d9805387SMatthew 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.
223d9805387SMatthew G. Knepley 
2245f1ad066SMatthew G Knepley   Level: intermediate
2255f1ad066SMatthew G Knepley 
2265f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2275f1ad066SMatthew G Knepley @*/
2285f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2295f1ad066SMatthew G Knepley {
2305f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley   PetscFunctionBegin;
2335f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
234d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2355f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2365f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2375f1ad066SMatthew G Knepley }
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley #undef __FUNCT__
240521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
241521d9a4cSLisandro Dalcin /*@C
242521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
243521d9a4cSLisandro Dalcin 
244521d9a4cSLisandro Dalcin    Logically Collective on DM
245521d9a4cSLisandro Dalcin 
246521d9a4cSLisandro Dalcin    Input Parameter:
247521d9a4cSLisandro Dalcin +  dm - the DM context
248521d9a4cSLisandro Dalcin .  ctype - the matrix type
249521d9a4cSLisandro Dalcin 
250521d9a4cSLisandro Dalcin    Options Database:
251521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
252521d9a4cSLisandro Dalcin 
253521d9a4cSLisandro Dalcin    Level: intermediate
254521d9a4cSLisandro Dalcin 
255c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
256521d9a4cSLisandro Dalcin @*/
25719fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
258521d9a4cSLisandro Dalcin {
259521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
26088f0584fSBarry Smith 
261521d9a4cSLisandro Dalcin   PetscFunctionBegin;
262521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
263521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
26419fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
265521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
266521d9a4cSLisandro Dalcin }
267521d9a4cSLisandro Dalcin 
268521d9a4cSLisandro Dalcin #undef __FUNCT__
269c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
270c0dedaeaSBarry Smith /*@C
271c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
272c0dedaeaSBarry Smith 
273c0dedaeaSBarry Smith    Logically Collective on DM
274c0dedaeaSBarry Smith 
275c0dedaeaSBarry Smith    Input Parameter:
276c0dedaeaSBarry Smith .  dm - the DM context
277c0dedaeaSBarry Smith 
278c0dedaeaSBarry Smith    Output Parameter:
279c0dedaeaSBarry Smith .  ctype - the matrix type
280c0dedaeaSBarry Smith 
281c0dedaeaSBarry Smith    Options Database:
282c0dedaeaSBarry Smith .   -dm_mat_type ctype
283c0dedaeaSBarry Smith 
284c0dedaeaSBarry Smith    Level: intermediate
285c0dedaeaSBarry Smith 
286c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
287c0dedaeaSBarry Smith @*/
288c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
289c0dedaeaSBarry Smith {
290c0dedaeaSBarry Smith   PetscFunctionBegin;
291c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
292c0dedaeaSBarry Smith   *ctype = dm->mattype;
293c0dedaeaSBarry Smith   PetscFunctionReturn(0);
294c0dedaeaSBarry Smith }
295c0dedaeaSBarry Smith 
296c0dedaeaSBarry Smith #undef __FUNCT__
297c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
298c688c046SMatthew G Knepley /*@
29934f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
300c688c046SMatthew G Knepley 
301c688c046SMatthew G Knepley   Not collective
302c688c046SMatthew G Knepley 
303c688c046SMatthew G Knepley   Input Parameter:
304c688c046SMatthew G Knepley . A - The Mat
305c688c046SMatthew G Knepley 
306c688c046SMatthew G Knepley   Output Parameter:
307c688c046SMatthew G Knepley . dm - The DM
308c688c046SMatthew G Knepley 
309c688c046SMatthew G Knepley   Level: intermediate
310c688c046SMatthew G Knepley 
311c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
312c688c046SMatthew G Knepley @*/
313c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
314c688c046SMatthew G Knepley {
315c688c046SMatthew G Knepley   PetscErrorCode ierr;
316c688c046SMatthew G Knepley 
317c688c046SMatthew G Knepley   PetscFunctionBegin;
318c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
319c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
320c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
321c688c046SMatthew G Knepley   PetscFunctionReturn(0);
322c688c046SMatthew G Knepley }
323c688c046SMatthew G Knepley 
324c688c046SMatthew G Knepley #undef __FUNCT__
325c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
326c688c046SMatthew G Knepley /*@
327c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
328c688c046SMatthew G Knepley 
329c688c046SMatthew G Knepley   Not collective
330c688c046SMatthew G Knepley 
331c688c046SMatthew G Knepley   Input Parameters:
332c688c046SMatthew G Knepley + A - The Mat
333c688c046SMatthew G Knepley - dm - The DM
334c688c046SMatthew G Knepley 
335c688c046SMatthew G Knepley   Level: intermediate
336c688c046SMatthew G Knepley 
337c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
338c688c046SMatthew G Knepley @*/
339c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
340c688c046SMatthew G Knepley {
341c688c046SMatthew G Knepley   PetscErrorCode ierr;
342c688c046SMatthew G Knepley 
343c688c046SMatthew G Knepley   PetscFunctionBegin;
344c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3458865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
346c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
347c688c046SMatthew G Knepley   PetscFunctionReturn(0);
348c688c046SMatthew G Knepley }
349c688c046SMatthew G Knepley 
350c688c046SMatthew G Knepley #undef __FUNCT__
3519a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3529a42bb27SBarry Smith /*@C
3539a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3546757b960SDave May    DM options in the database.
3559a42bb27SBarry Smith 
3568353ddbbSDave May    Logically Collective on DM
3579a42bb27SBarry Smith 
3589a42bb27SBarry Smith    Input Parameter:
3598353ddbbSDave May +  da - the DM context
3609a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3619a42bb27SBarry Smith 
3629a42bb27SBarry Smith    Notes:
3639a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3649a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3659a42bb27SBarry Smith 
3669a42bb27SBarry Smith    Level: advanced
3679a42bb27SBarry Smith 
3688353ddbbSDave May .keywords: DM, set, options, prefix, database
3699a42bb27SBarry Smith 
3709a42bb27SBarry Smith .seealso: DMSetFromOptions()
3719a42bb27SBarry Smith @*/
3727087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3739a42bb27SBarry Smith {
3749a42bb27SBarry Smith   PetscErrorCode ierr;
3759a42bb27SBarry Smith 
3769a42bb27SBarry Smith   PetscFunctionBegin;
3779a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3789a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
379691be533SLawrence Mitchell   if (dm->sf) {
380691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
381691be533SLawrence Mitchell   }
382691be533SLawrence Mitchell   if (dm->defaultSF) {
383691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
384691be533SLawrence Mitchell   }
3859a42bb27SBarry Smith   PetscFunctionReturn(0);
3869a42bb27SBarry Smith }
3879a42bb27SBarry Smith 
3889a42bb27SBarry Smith #undef __FUNCT__
38931697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
39031697293SDave May /*@C
39131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
39231697293SDave May    DM options in the database.
39331697293SDave May 
39431697293SDave May    Logically Collective on DM
39531697293SDave May 
39631697293SDave May    Input Parameters:
39731697293SDave May +  dm - the DM context
39831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
39931697293SDave May 
40031697293SDave May    Notes:
40131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
40231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
40331697293SDave May 
40431697293SDave May    Level: advanced
40531697293SDave May 
40631697293SDave May .keywords: DM, append, options, prefix, database
40731697293SDave May 
40831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
40931697293SDave May @*/
41031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
41131697293SDave May {
41231697293SDave May   PetscErrorCode ierr;
41331697293SDave May 
41431697293SDave May   PetscFunctionBegin;
41531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
41731697293SDave May   PetscFunctionReturn(0);
41831697293SDave May }
41931697293SDave May 
42031697293SDave May #undef __FUNCT__
42131697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
42231697293SDave May /*@C
42331697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
42431697293SDave May    DM options in the database.
42531697293SDave May 
42631697293SDave May    Not Collective
42731697293SDave May 
42831697293SDave May    Input Parameters:
42931697293SDave May .  dm - the DM context
43031697293SDave May 
43131697293SDave May    Output Parameters:
43231697293SDave May .  prefix - pointer to the prefix string used is returned
43331697293SDave May 
43431697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
43531697293SDave May    sufficient length to hold the prefix.
43631697293SDave May 
43731697293SDave May    Level: advanced
43831697293SDave May 
43931697293SDave May .keywords: DM, set, options, prefix, database
44031697293SDave May 
44131697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
44231697293SDave May @*/
44331697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
44431697293SDave May {
44531697293SDave May   PetscErrorCode ierr;
44631697293SDave May 
44731697293SDave May   PetscFunctionBegin;
44831697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44931697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
45031697293SDave May   PetscFunctionReturn(0);
45131697293SDave May }
45231697293SDave May 
45331697293SDave May #undef __FUNCT__
45447c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
45547c6ae99SBarry Smith /*@
4568472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
45747c6ae99SBarry Smith 
45847c6ae99SBarry Smith     Collective on DM
45947c6ae99SBarry Smith 
46047c6ae99SBarry Smith     Input Parameter:
46147c6ae99SBarry Smith .   dm - the DM object to destroy
46247c6ae99SBarry Smith 
46347c6ae99SBarry Smith     Level: developer
46447c6ae99SBarry Smith 
465e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
46647c6ae99SBarry Smith 
46747c6ae99SBarry Smith @*/
468fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
46947c6ae99SBarry Smith {
470c83e3748SMatthew G. Knepley   PetscInt       i, cnt = 0;
471dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
47247c6ae99SBarry Smith   PetscErrorCode ierr;
47347c6ae99SBarry Smith 
47447c6ae99SBarry Smith   PetscFunctionBegin;
4756bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4766bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
47787e657c6SBarry Smith 
47887e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
479732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4808865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4818865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
482732e2eb9SMatthew G Knepley   }
483dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4842348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4852348bcf4SPeter Brune   if ((*dm)->x) {
4862348bcf4SPeter Brune     DM obj;
4872348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
4882348bcf4SPeter Brune     if (obj == *dm) cnt++;
4892348bcf4SPeter Brune   }
490732e2eb9SMatthew G Knepley 
4916bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
492732e2eb9SMatthew G Knepley   /*
493732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
494732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
495732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
496732e2eb9SMatthew G Knepley   */
4976bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
4986bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
499732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5006bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5016bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
502732e2eb9SMatthew G Knepley   }
503f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5040298fd71SBarry Smith   (*dm)->namedglobal = NULL;
505f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5062348bcf4SPeter Brune     nnext = nlink->next;
5072348bcf4SPeter 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);
5082348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5092348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5102348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5112348bcf4SPeter Brune   }
512f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5130298fd71SBarry Smith   (*dm)->namedlocal = NULL;
514f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
515f490541aSPeter Brune     nnext = nlink->next;
516f490541aSPeter 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);
517f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
518f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
519f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
520f490541aSPeter Brune   }
5212348bcf4SPeter Brune 
522b17ce1afSJed Brown   /* Destroy the list of hooks */
523c833c3b5SJed Brown   {
524c833c3b5SJed Brown     DMCoarsenHookLink link,next;
525b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
526b17ce1afSJed Brown       next = link->next;
527b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
528b17ce1afSJed Brown     }
5290298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
530c833c3b5SJed Brown   }
531c833c3b5SJed Brown   {
532c833c3b5SJed Brown     DMRefineHookLink link,next;
533c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
534c833c3b5SJed Brown       next = link->next;
535c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
536c833c3b5SJed Brown     }
5370298fd71SBarry Smith     (*dm)->refinehook = NULL;
538c833c3b5SJed Brown   }
539be081cd6SPeter Brune   {
540be081cd6SPeter Brune     DMSubDomainHookLink link,next;
541be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
542be081cd6SPeter Brune       next = link->next;
543be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
544be081cd6SPeter Brune     }
5450298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
546be081cd6SPeter Brune   }
547baf369e7SPeter Brune   {
548baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
549baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
550baf369e7SPeter Brune       next = link->next;
551baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
552baf369e7SPeter Brune     }
5530298fd71SBarry Smith     (*dm)->gtolhook = NULL;
554baf369e7SPeter Brune   }
555d4d07f1eSToby Isaac   {
556d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
557d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
558d4d07f1eSToby Isaac       next = link->next;
559d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
560d4d07f1eSToby Isaac     }
561d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
562d4d07f1eSToby Isaac   }
563aa1993deSMatthew G Knepley   /* Destroy the work arrays */
564aa1993deSMatthew G Knepley   {
565aa1993deSMatthew G Knepley     DMWorkLink link,next;
566aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
567aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
568aa1993deSMatthew G Knepley       next = link->next;
569aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
570aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
571aa1993deSMatthew G Knepley     }
5720298fd71SBarry Smith     (*dm)->workin = NULL;
573aa1993deSMatthew G Knepley   }
574b17ce1afSJed Brown 
57552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
57652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
57752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
57852536dc3SBarry Smith 
5791a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5801a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5811a266240SBarry Smith   }
58287e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
58371cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5844dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5856bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5866bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
587073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
58888ed4aceSMatthew G Knepley 
58988ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
59088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
5918b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
592fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
593fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
59488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
59588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
5968e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
597af122d2aSMatthew G Knepley 
5986636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
5996636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6006636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6015dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6026636e97aSMatthew G Knepley 
6032764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
60414f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
605e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
606e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
607732e2eb9SMatthew G Knepley 
6086bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
609435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
610732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
61147c6ae99SBarry Smith   PetscFunctionReturn(0);
61247c6ae99SBarry Smith }
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith #undef __FUNCT__
615d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
616d7bf68aeSBarry Smith /*@
617d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
618d7bf68aeSBarry Smith 
619d7bf68aeSBarry Smith     Collective on DM
620d7bf68aeSBarry Smith 
621d7bf68aeSBarry Smith     Input Parameter:
622d7bf68aeSBarry Smith .   dm - the DM object to setup
623d7bf68aeSBarry Smith 
624d7bf68aeSBarry Smith     Level: developer
625d7bf68aeSBarry Smith 
626e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
627d7bf68aeSBarry Smith 
628d7bf68aeSBarry Smith @*/
6297087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
630d7bf68aeSBarry Smith {
631d7bf68aeSBarry Smith   PetscErrorCode ierr;
632d7bf68aeSBarry Smith 
633d7bf68aeSBarry Smith   PetscFunctionBegin;
634171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6358387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
636d7bf68aeSBarry Smith   if (dm->ops->setup) {
637d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
638d7bf68aeSBarry Smith   }
6398387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
640d7bf68aeSBarry Smith   PetscFunctionReturn(0);
641d7bf68aeSBarry Smith }
642d7bf68aeSBarry Smith 
643d7bf68aeSBarry Smith #undef __FUNCT__
644d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
645d7bf68aeSBarry Smith /*@
646d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
647d7bf68aeSBarry Smith 
648d7bf68aeSBarry Smith     Collective on DM
649d7bf68aeSBarry Smith 
650d7bf68aeSBarry Smith     Input Parameter:
651d7bf68aeSBarry Smith .   dm - the DM object to set options for
652d7bf68aeSBarry Smith 
653732e2eb9SMatthew G Knepley     Options Database:
6544164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
6554164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
6564164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
6574164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
658732e2eb9SMatthew G Knepley 
659d7bf68aeSBarry Smith     Level: developer
660d7bf68aeSBarry Smith 
661e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
662d7bf68aeSBarry Smith 
663d7bf68aeSBarry Smith @*/
6647087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
665d7bf68aeSBarry Smith {
6667781c08eSBarry Smith   char           typeName[256];
667ca266f36SBarry Smith   PetscBool      flg;
668d7bf68aeSBarry Smith   PetscErrorCode ierr;
669d7bf68aeSBarry Smith 
670d7bf68aeSBarry Smith   PetscFunctionBegin;
671171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
672691be533SLawrence Mitchell   if (dm->sf) {
673691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
674691be533SLawrence Mitchell   }
675691be533SLawrence Mitchell   if (dm->defaultSF) {
676691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
677691be533SLawrence Mitchell   }
6783194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
6790298fd71SBarry 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);
680a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
681f9ba7244SBarry Smith   if (flg) {
682f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
683f9ba7244SBarry Smith   }
684a264d7a6SBarry 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);
685073dac72SJed Brown   if (flg) {
686521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
687073dac72SJed Brown   }
6880298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
689f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
690e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
691f9ba7244SBarry Smith   }
692f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
693f9ba7244SBarry Smith   ierr = PetscObjectProcessOptionsHandlers((PetscObject) dm);CHKERRQ(ierr);
69482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
695d7bf68aeSBarry Smith   PetscFunctionReturn(0);
696d7bf68aeSBarry Smith }
697d7bf68aeSBarry Smith 
698d7bf68aeSBarry Smith #undef __FUNCT__
69947c6ae99SBarry Smith #define __FUNCT__ "DMView"
700fc9bc008SSatish Balay /*@C
701224748a4SBarry Smith     DMView - Views a DM
70247c6ae99SBarry Smith 
70347c6ae99SBarry Smith     Collective on DM
70447c6ae99SBarry Smith 
70547c6ae99SBarry Smith     Input Parameter:
70647c6ae99SBarry Smith +   dm - the DM object to view
70747c6ae99SBarry Smith -   v - the viewer
70847c6ae99SBarry Smith 
709224748a4SBarry Smith     Level: beginner
71047c6ae99SBarry Smith 
711e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
71247c6ae99SBarry Smith 
71347c6ae99SBarry Smith @*/
7147087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
71547c6ae99SBarry Smith {
71647c6ae99SBarry Smith   PetscErrorCode ierr;
71732c0f0efSBarry Smith   PetscBool      isbinary;
71847c6ae99SBarry Smith 
71947c6ae99SBarry Smith   PetscFunctionBegin;
720171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7213014e516SBarry Smith   if (!v) {
722ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
7233014e516SBarry Smith   }
72498c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
72532c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
72632c0f0efSBarry Smith   if (isbinary) {
72755849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
72832c0f0efSBarry Smith     char     type[256];
72932c0f0efSBarry Smith 
73032c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
73132c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
73232c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
73332c0f0efSBarry Smith   }
7340c010503SBarry Smith   if (dm->ops->view) {
7350c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
73647c6ae99SBarry Smith   }
73747c6ae99SBarry Smith   PetscFunctionReturn(0);
73847c6ae99SBarry Smith }
73947c6ae99SBarry Smith 
74047c6ae99SBarry Smith #undef __FUNCT__
74147c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
74247c6ae99SBarry Smith /*@
7438472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
74447c6ae99SBarry Smith 
74547c6ae99SBarry Smith     Collective on DM
74647c6ae99SBarry Smith 
74747c6ae99SBarry Smith     Input Parameter:
74847c6ae99SBarry Smith .   dm - the DM object
74947c6ae99SBarry Smith 
75047c6ae99SBarry Smith     Output Parameter:
75147c6ae99SBarry Smith .   vec - the global vector
75247c6ae99SBarry Smith 
753073dac72SJed Brown     Level: beginner
75447c6ae99SBarry Smith 
755e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
75647c6ae99SBarry Smith 
75747c6ae99SBarry Smith @*/
7587087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
75947c6ae99SBarry Smith {
76047c6ae99SBarry Smith   PetscErrorCode ierr;
76147c6ae99SBarry Smith 
76247c6ae99SBarry Smith   PetscFunctionBegin;
763171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76447c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
76547c6ae99SBarry Smith   PetscFunctionReturn(0);
76647c6ae99SBarry Smith }
76747c6ae99SBarry Smith 
76847c6ae99SBarry Smith #undef __FUNCT__
76947c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
77047c6ae99SBarry Smith /*@
7718472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
77247c6ae99SBarry Smith 
77347c6ae99SBarry Smith     Not Collective
77447c6ae99SBarry Smith 
77547c6ae99SBarry Smith     Input Parameter:
77647c6ae99SBarry Smith .   dm - the DM object
77747c6ae99SBarry Smith 
77847c6ae99SBarry Smith     Output Parameter:
77947c6ae99SBarry Smith .   vec - the local vector
78047c6ae99SBarry Smith 
781073dac72SJed Brown     Level: beginner
78247c6ae99SBarry Smith 
783e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
78447c6ae99SBarry Smith 
78547c6ae99SBarry Smith @*/
7867087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
78747c6ae99SBarry Smith {
78847c6ae99SBarry Smith   PetscErrorCode ierr;
78947c6ae99SBarry Smith 
79047c6ae99SBarry Smith   PetscFunctionBegin;
791171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79247c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
79347c6ae99SBarry Smith   PetscFunctionReturn(0);
79447c6ae99SBarry Smith }
79547c6ae99SBarry Smith 
79647c6ae99SBarry Smith #undef __FUNCT__
7971411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
7981411c6eeSJed Brown /*@
7991411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8001411c6eeSJed Brown 
8011411c6eeSJed Brown    Collective on DM
8021411c6eeSJed Brown 
8031411c6eeSJed Brown    Input Parameter:
8041411c6eeSJed Brown .  dm - the DM that provides the mapping
8051411c6eeSJed Brown 
8061411c6eeSJed Brown    Output Parameter:
8071411c6eeSJed Brown .  ltog - the mapping
8081411c6eeSJed Brown 
8091411c6eeSJed Brown    Level: intermediate
8101411c6eeSJed Brown 
8111411c6eeSJed Brown    Notes:
8121411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8131411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8141411c6eeSJed Brown 
815fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8161411c6eeSJed Brown @*/
8177087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8181411c6eeSJed Brown {
8191411c6eeSJed Brown   PetscErrorCode ierr;
8201411c6eeSJed Brown 
8211411c6eeSJed Brown   PetscFunctionBegin;
8221411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8231411c6eeSJed Brown   PetscValidPointer(ltog,2);
8241411c6eeSJed Brown   if (!dm->ltogmap) {
82537d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
82637d0c07bSMatthew G Knepley 
82737d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
82837d0c07bSMatthew G Knepley     if (section) {
82937d0c07bSMatthew G Knepley       PetscInt *ltog;
83037d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
83137d0c07bSMatthew G Knepley 
83237d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
83337d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
83437d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
835785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
83637d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
83737d0c07bSMatthew G Knepley         PetscInt dof, off, c;
83837d0c07bSMatthew G Knepley 
83937d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
84037d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
84137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
84237d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
84337d0c07bSMatthew G Knepley           ltog[l] = off+c;
84437d0c07bSMatthew G Knepley         }
84537d0c07bSMatthew G Knepley       }
846f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
8473bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
84837d0c07bSMatthew G Knepley     } else {
849184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
850184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
8511411c6eeSJed Brown     }
85237d0c07bSMatthew G Knepley   }
8531411c6eeSJed Brown   *ltog = dm->ltogmap;
8541411c6eeSJed Brown   PetscFunctionReturn(0);
8551411c6eeSJed Brown }
8561411c6eeSJed Brown 
8571411c6eeSJed Brown #undef __FUNCT__
8581411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
8591411c6eeSJed Brown /*@
8601411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
8611411c6eeSJed Brown 
8621411c6eeSJed Brown    Not Collective
8631411c6eeSJed Brown 
8641411c6eeSJed Brown    Input Parameter:
8651411c6eeSJed Brown .  dm - the DM with block structure
8661411c6eeSJed Brown 
8671411c6eeSJed Brown    Output Parameter:
8681411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
8691411c6eeSJed Brown 
8701411c6eeSJed Brown    Level: intermediate
8711411c6eeSJed Brown 
872fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
8731411c6eeSJed Brown @*/
8747087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8751411c6eeSJed Brown {
8761411c6eeSJed Brown   PetscFunctionBegin;
8771411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8781411c6eeSJed Brown   PetscValidPointer(bs,2);
8791411c6eeSJed 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");
8801411c6eeSJed Brown   *bs = dm->bs;
8811411c6eeSJed Brown   PetscFunctionReturn(0);
8821411c6eeSJed Brown }
8831411c6eeSJed Brown 
8841411c6eeSJed Brown #undef __FUNCT__
885e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
88647c6ae99SBarry Smith /*@
8878472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith     Collective on DM
89047c6ae99SBarry Smith 
89147c6ae99SBarry Smith     Input Parameter:
89247c6ae99SBarry Smith +   dm1 - the DM object
89347c6ae99SBarry Smith -   dm2 - the second, finer DM object
89447c6ae99SBarry Smith 
89547c6ae99SBarry Smith     Output Parameter:
89647c6ae99SBarry Smith +  mat - the interpolation
89747c6ae99SBarry Smith -  vec - the scaling (optional)
89847c6ae99SBarry Smith 
89947c6ae99SBarry Smith     Level: developer
90047c6ae99SBarry Smith 
90185afcc9aSBarry 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
90285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
903d52bd9f3SBarry Smith 
9041f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
905d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
90685afcc9aSBarry Smith 
90785afcc9aSBarry Smith 
908e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
90947c6ae99SBarry Smith 
91047c6ae99SBarry Smith @*/
911e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
91247c6ae99SBarry Smith {
91347c6ae99SBarry Smith   PetscErrorCode ierr;
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith   PetscFunctionBegin;
916171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
917171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
918cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
91925296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
920cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
92147c6ae99SBarry Smith   PetscFunctionReturn(0);
92247c6ae99SBarry Smith }
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith #undef __FUNCT__
925e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
92647c6ae99SBarry Smith /*@
9278472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith     Collective on DM
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith     Input Parameter:
93247c6ae99SBarry Smith +   dm1 - the DM object
93347c6ae99SBarry Smith -   dm2 - the second, finer DM object
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith     Output Parameter:
9366dbf9973SLawrence Mitchell .   mat - the injection
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith     Level: developer
93947c6ae99SBarry Smith 
94085afcc9aSBarry 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
94185afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
94285afcc9aSBarry Smith 
943e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
94447c6ae99SBarry Smith 
94547c6ae99SBarry Smith @*/
9466dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
94747c6ae99SBarry Smith {
94847c6ae99SBarry Smith   PetscErrorCode ierr;
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith   PetscFunctionBegin;
951171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
952171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
9536dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
95447c6ae99SBarry Smith   PetscFunctionReturn(0);
95547c6ae99SBarry Smith }
95647c6ae99SBarry Smith 
95747c6ae99SBarry Smith #undef __FUNCT__
958e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
959b412c318SBarry Smith /*@
960b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith     Collective on DM
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith     Input Parameter:
96547c6ae99SBarry Smith +   dm - the DM object
966b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith     Output Parameter:
96947c6ae99SBarry Smith .   coloring - the coloring
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith     Level: developer
97247c6ae99SBarry Smith 
973b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
97447c6ae99SBarry Smith 
975aab9d709SJed Brown @*/
976b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
97747c6ae99SBarry Smith {
97847c6ae99SBarry Smith   PetscErrorCode ierr;
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith   PetscFunctionBegin;
981171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
982ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
983b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
98447c6ae99SBarry Smith   PetscFunctionReturn(0);
98547c6ae99SBarry Smith }
98647c6ae99SBarry Smith 
98747c6ae99SBarry Smith #undef __FUNCT__
988950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
989b412c318SBarry Smith /*@
9908472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith     Collective on DM
99347c6ae99SBarry Smith 
99447c6ae99SBarry Smith     Input Parameter:
995b412c318SBarry Smith .   dm - the DM object
99647c6ae99SBarry Smith 
99747c6ae99SBarry Smith     Output Parameter:
99847c6ae99SBarry Smith .   mat - the empty Jacobian
99947c6ae99SBarry Smith 
1000073dac72SJed Brown     Level: beginner
100147c6ae99SBarry Smith 
100294013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
100394013140SBarry Smith        do not need to do it yourself.
100494013140SBarry Smith 
100594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1006aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
100794013140SBarry Smith 
100894013140SBarry 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
100994013140SBarry Smith        internally by PETSc.
101094013140SBarry Smith 
101194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1012aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
101394013140SBarry Smith 
1014b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
101547c6ae99SBarry Smith 
1016aab9d709SJed Brown @*/
1017b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
101847c6ae99SBarry Smith {
101947c6ae99SBarry Smith   PetscErrorCode ierr;
102047c6ae99SBarry Smith 
102147c6ae99SBarry Smith   PetscFunctionBegin;
1022171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1023607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1024c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1025c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1026b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
102747c6ae99SBarry Smith   PetscFunctionReturn(0);
102847c6ae99SBarry Smith }
102947c6ae99SBarry Smith 
103047c6ae99SBarry Smith #undef __FUNCT__
1031732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1032732e2eb9SMatthew G Knepley /*@
1033950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1034732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1035732e2eb9SMatthew G Knepley 
10368472ad0fSDave May   Logically Collective on DM
1037732e2eb9SMatthew G Knepley 
1038732e2eb9SMatthew G Knepley   Input Parameter:
1039732e2eb9SMatthew G Knepley + dm - the DM
1040732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1041732e2eb9SMatthew G Knepley 
1042732e2eb9SMatthew G Knepley   Level: developer
1043950540a4SJed Brown .seealso DMCreateMatrix()
1044732e2eb9SMatthew G Knepley @*/
1045732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1046732e2eb9SMatthew G Knepley {
1047732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1048732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1049732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1050732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1051732e2eb9SMatthew G Knepley }
1052732e2eb9SMatthew G Knepley 
1053732e2eb9SMatthew G Knepley #undef __FUNCT__
1054a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1055a89ea682SMatthew G Knepley /*@C
1056aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1057a89ea682SMatthew G Knepley 
1058a89ea682SMatthew G Knepley   Not Collective
1059a89ea682SMatthew G Knepley 
1060a89ea682SMatthew G Knepley   Input Parameters:
1061a89ea682SMatthew G Knepley + dm - the DM object
1062aa1993deSMatthew G Knepley . count - The minium size
1063aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1064a89ea682SMatthew G Knepley 
1065a89ea682SMatthew G Knepley   Output Parameter:
1066a89ea682SMatthew G Knepley . array - the work array
1067a89ea682SMatthew G Knepley 
1068a89ea682SMatthew G Knepley   Level: developer
1069a89ea682SMatthew G Knepley 
1070a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1071a89ea682SMatthew G Knepley @*/
1072aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1073a89ea682SMatthew G Knepley {
1074a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1075aa1993deSMatthew G Knepley   DMWorkLink     link;
1076854ce69bSBarry Smith   size_t         dsize;
1077a89ea682SMatthew G Knepley 
1078a89ea682SMatthew G Knepley   PetscFunctionBegin;
1079a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1080aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1081aa1993deSMatthew G Knepley   if (dm->workin) {
1082aa1993deSMatthew G Knepley     link       = dm->workin;
1083aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1084aa1993deSMatthew G Knepley   } else {
1085b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1086a89ea682SMatthew G Knepley   }
1087854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1088854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1089aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1090854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1091854ce69bSBarry Smith     link->bytes = dsize*count;
1092aa1993deSMatthew G Knepley   }
1093aa1993deSMatthew G Knepley   link->next   = dm->workout;
1094aa1993deSMatthew G Knepley   dm->workout  = link;
1095aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1096a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1097a89ea682SMatthew G Knepley }
1098a89ea682SMatthew G Knepley 
1099aa1993deSMatthew G Knepley #undef __FUNCT__
1100aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1101aa1993deSMatthew G Knepley /*@C
1102aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1103aa1993deSMatthew G Knepley 
1104aa1993deSMatthew G Knepley   Not Collective
1105aa1993deSMatthew G Knepley 
1106aa1993deSMatthew G Knepley   Input Parameters:
1107aa1993deSMatthew G Knepley + dm - the DM object
1108aa1993deSMatthew G Knepley . count - The minium size
1109aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1110aa1993deSMatthew G Knepley 
1111aa1993deSMatthew G Knepley   Output Parameter:
1112aa1993deSMatthew G Knepley . array - the work array
1113aa1993deSMatthew G Knepley 
1114aa1993deSMatthew G Knepley   Level: developer
1115aa1993deSMatthew G Knepley 
1116aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1117aa1993deSMatthew G Knepley @*/
1118aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1119aa1993deSMatthew G Knepley {
1120aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1121aa1993deSMatthew G Knepley 
1122aa1993deSMatthew G Knepley   PetscFunctionBegin;
1123aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1124aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1125aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1126aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1127aa1993deSMatthew G Knepley       *p           = link->next;
1128aa1993deSMatthew G Knepley       link->next   = dm->workin;
1129aa1993deSMatthew G Knepley       dm->workin   = link;
11300298fd71SBarry Smith       *(void**)mem = NULL;
1131aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1132aa1993deSMatthew G Knepley     }
1133aa1993deSMatthew G Knepley   }
1134aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1135aa1993deSMatthew G Knepley }
1136e7c4fc90SDmitry Karpeev 
1137e7c4fc90SDmitry Karpeev #undef __FUNCT__
1138435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1139435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1140435a35e8SMatthew G Knepley {
1141435a35e8SMatthew G Knepley   PetscFunctionBegin;
1142435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
114382f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1144435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1145435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1146435a35e8SMatthew G Knepley }
1147435a35e8SMatthew G Knepley 
1148435a35e8SMatthew G Knepley #undef __FUNCT__
11494d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
11504f3b5142SJed Brown /*@C
11514d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
11524d343eeaSMatthew G Knepley 
11534d343eeaSMatthew G Knepley   Not collective
11544d343eeaSMatthew G Knepley 
11554d343eeaSMatthew G Knepley   Input Parameter:
11564d343eeaSMatthew G Knepley . dm - the DM object
11574d343eeaSMatthew G Knepley 
11584d343eeaSMatthew G Knepley   Output Parameters:
11590298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
11600298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
11610298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
11624d343eeaSMatthew G Knepley 
11634d343eeaSMatthew G Knepley   Level: intermediate
11644d343eeaSMatthew G Knepley 
116521c9b008SJed Brown   Notes:
116621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
116721c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
116821c9b008SJed Brown   PetscFree().
116921c9b008SJed Brown 
11704d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11714d343eeaSMatthew G Knepley @*/
117237d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11734d343eeaSMatthew G Knepley {
117437d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11754d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11764d343eeaSMatthew G Knepley 
11774d343eeaSMatthew G Knepley   PetscFunctionBegin;
11784d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
117969ca1f37SDmitry Karpeev   if (numFields) {
118069ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
118169ca1f37SDmitry Karpeev     *numFields = 0;
118269ca1f37SDmitry Karpeev   }
118337d0c07bSMatthew G Knepley   if (fieldNames) {
118437d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11850298fd71SBarry Smith     *fieldNames = NULL;
118669ca1f37SDmitry Karpeev   }
118769ca1f37SDmitry Karpeev   if (fields) {
118869ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11890298fd71SBarry Smith     *fields = NULL;
119069ca1f37SDmitry Karpeev   }
119137d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
119237d0c07bSMatthew G Knepley   if (section) {
119337d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
119437d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
119537d0c07bSMatthew G Knepley 
119637d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
119737d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1198dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
119937d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
120037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
120137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
120237d0c07bSMatthew G Knepley     }
120337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
120437d0c07bSMatthew G Knepley       PetscInt gdof;
120537d0c07bSMatthew G Knepley 
120637d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
120737d0c07bSMatthew G Knepley       if (gdof > 0) {
120837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
120937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
121037d0c07bSMatthew G Knepley 
121137d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
121237d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
121337d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
121437d0c07bSMatthew G Knepley         }
121537d0c07bSMatthew G Knepley       }
121637d0c07bSMatthew G Knepley     }
121737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1218785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
121937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
122037d0c07bSMatthew G Knepley     }
122137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
122237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
122337d0c07bSMatthew G Knepley 
122437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
122537d0c07bSMatthew G Knepley       if (gdof > 0) {
122637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
122737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
122837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
122937d0c07bSMatthew G Knepley 
123037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
123137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
123237d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
123337d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
123437d0c07bSMatthew G Knepley           }
123537d0c07bSMatthew G Knepley         }
123637d0c07bSMatthew G Knepley       }
123737d0c07bSMatthew G Knepley     }
12388865f1eaSKarl Rupp     if (numFields) *numFields = nF;
123937d0c07bSMatthew G Knepley     if (fieldNames) {
1240785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
124137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
124237d0c07bSMatthew G Knepley         const char *fieldName;
124337d0c07bSMatthew G Knepley 
124437d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
124537d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
124637d0c07bSMatthew G Knepley       }
124737d0c07bSMatthew G Knepley     }
124837d0c07bSMatthew G Knepley     if (fields) {
1249785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
125037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
125182f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
125237d0c07bSMatthew G Knepley       }
125337d0c07bSMatthew G Knepley     }
125437d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
12558865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
12568865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
125769ca1f37SDmitry Karpeev   }
12584d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
12594d343eeaSMatthew G Knepley }
12604d343eeaSMatthew G Knepley 
126116621825SDmitry Karpeev 
126216621825SDmitry Karpeev #undef __FUNCT__
126316621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
126416621825SDmitry Karpeev /*@C
126516621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
126616621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
126716621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1268e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1269e7c4fc90SDmitry Karpeev 
1270e7c4fc90SDmitry Karpeev   Not collective
1271e7c4fc90SDmitry Karpeev 
1272e7c4fc90SDmitry Karpeev   Input Parameter:
1273e7c4fc90SDmitry Karpeev . dm - the DM object
1274e7c4fc90SDmitry Karpeev 
1275e7c4fc90SDmitry Karpeev   Output Parameters:
12760298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12770298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12780298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12790298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1280e7c4fc90SDmitry Karpeev 
1281e7c4fc90SDmitry Karpeev   Level: intermediate
1282e7c4fc90SDmitry Karpeev 
1283e7c4fc90SDmitry Karpeev   Notes:
1284e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1285e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1286e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1287e7c4fc90SDmitry Karpeev 
1288e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1289e7c4fc90SDmitry Karpeev @*/
129016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1291e7c4fc90SDmitry Karpeev {
1292e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1293e7c4fc90SDmitry Karpeev 
1294e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1295e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12968865f1eaSKarl Rupp   if (len) {
12978865f1eaSKarl Rupp     PetscValidPointer(len,2);
12988865f1eaSKarl Rupp     *len = 0;
12998865f1eaSKarl Rupp   }
13008865f1eaSKarl Rupp   if (namelist) {
13018865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13028865f1eaSKarl Rupp     *namelist = 0;
13038865f1eaSKarl Rupp   }
13048865f1eaSKarl Rupp   if (islist) {
13058865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13068865f1eaSKarl Rupp     *islist = 0;
13078865f1eaSKarl Rupp   }
13088865f1eaSKarl Rupp   if (dmlist) {
13098865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13108865f1eaSKarl Rupp     *dmlist = 0;
13118865f1eaSKarl Rupp   }
1312f3f0edfdSDmitry Karpeev   /*
1313f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1314f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1315f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1316f3f0edfdSDmitry Karpeev    */
1317ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
131816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1319435a35e8SMatthew G Knepley     PetscSection section;
1320435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1321435a35e8SMatthew G Knepley 
1322435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1323435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1324435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1325435a35e8SMatthew G Knepley       *len = numFields;
132603dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
132703dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
132803dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1329435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1330435a35e8SMatthew G Knepley         const char *fieldName;
1331435a35e8SMatthew G Knepley 
133203dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
133303dc3394SMatthew G. Knepley         if (namelist) {
1334435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1335435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1336435a35e8SMatthew G Knepley         }
133703dc3394SMatthew G. Knepley       }
1338435a35e8SMatthew G Knepley     } else {
133969ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1340e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
13410298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1342e7c4fc90SDmitry Karpeev     }
13438865f1eaSKarl Rupp   } else {
134416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
134516621825SDmitry Karpeev   }
134616621825SDmitry Karpeev   PetscFunctionReturn(0);
134716621825SDmitry Karpeev }
134816621825SDmitry Karpeev 
134916621825SDmitry Karpeev #undef __FUNCT__
1350435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1351564cec59SMatthew G. Knepley /*@
1352435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1353435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1354435a35e8SMatthew G Knepley 
1355435a35e8SMatthew G Knepley   Not collective
1356435a35e8SMatthew G Knepley 
1357435a35e8SMatthew G Knepley   Input Parameters:
1358435a35e8SMatthew G Knepley + dm - the DM object
1359435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
13600298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1361435a35e8SMatthew G Knepley 
1362435a35e8SMatthew G Knepley   Output Parameters:
1363435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1364435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1365435a35e8SMatthew G Knepley 
1366435a35e8SMatthew G Knepley   Level: intermediate
1367435a35e8SMatthew G Knepley 
1368435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1369435a35e8SMatthew G Knepley @*/
1370435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1371435a35e8SMatthew G Knepley {
1372435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1373435a35e8SMatthew G Knepley 
1374435a35e8SMatthew G Knepley   PetscFunctionBegin;
1375435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1376435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13778865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13788865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1379435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1380435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
138182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1382435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1383435a35e8SMatthew G Knepley }
1384435a35e8SMatthew G Knepley 
138516621825SDmitry Karpeev 
138616621825SDmitry Karpeev #undef __FUNCT__
138716621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
138816621825SDmitry Karpeev /*@C
13898d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13908d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13918d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13928d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13938d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
139416621825SDmitry Karpeev 
139516621825SDmitry Karpeev   Not collective
139616621825SDmitry Karpeev 
139716621825SDmitry Karpeev   Input Parameter:
139816621825SDmitry Karpeev . dm - the DM object
139916621825SDmitry Karpeev 
140016621825SDmitry Karpeev   Output Parameters:
14010298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14020298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14030298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14040298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14050298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
140616621825SDmitry Karpeev 
140716621825SDmitry Karpeev   Level: intermediate
140816621825SDmitry Karpeev 
140916621825SDmitry Karpeev   Notes:
141016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
141116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
141216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
141316621825SDmitry Karpeev 
14148d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
141516621825SDmitry Karpeev @*/
14168d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
141716621825SDmitry Karpeev {
141816621825SDmitry Karpeev   PetscErrorCode      ierr;
1419be081cd6SPeter Brune   DMSubDomainHookLink link;
1420be081cd6SPeter Brune   PetscInt            i,l;
142116621825SDmitry Karpeev 
142216621825SDmitry Karpeev   PetscFunctionBegin;
142316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
142414a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14250298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14260298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14270298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14280298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1429f3f0edfdSDmitry Karpeev   /*
1430f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1431f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1432f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1433f3f0edfdSDmitry Karpeev    */
1434ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
143516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1436be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
143714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
143814a18fd3SPeter Brune     if (dmlist) {
1439be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1440be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1441be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1442be081cd6SPeter Brune         }
1443d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1444e7c4fc90SDmitry Karpeev       }
144514a18fd3SPeter Brune     }
144614a18fd3SPeter Brune     if (len) *len = l;
144714a18fd3SPeter Brune   }
1448e30e807fSPeter Brune   PetscFunctionReturn(0);
1449e30e807fSPeter Brune }
1450e30e807fSPeter Brune 
1451e30e807fSPeter Brune 
1452e30e807fSPeter Brune #undef __FUNCT__
1453e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1454e30e807fSPeter Brune /*@C
1455e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1456e30e807fSPeter Brune 
1457e30e807fSPeter Brune   Not collective
1458e30e807fSPeter Brune 
1459e30e807fSPeter Brune   Input Parameters:
1460e30e807fSPeter Brune + dm - the DM object
1461e30e807fSPeter Brune . n  - the number of subdomain scatters
1462e30e807fSPeter Brune - subdms - the local subdomains
1463e30e807fSPeter Brune 
1464e30e807fSPeter Brune   Output Parameters:
1465e30e807fSPeter Brune + n     - the number of scatters returned
1466e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1467e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1468e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1469e30e807fSPeter Brune 
1470e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1471e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1472e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1473e30e807fSPeter Brune   solution and residual data.
1474e30e807fSPeter Brune 
1475e30e807fSPeter Brune   Level: developer
1476e30e807fSPeter Brune 
1477e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1478e30e807fSPeter Brune @*/
1479e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1480e30e807fSPeter Brune {
1481e30e807fSPeter Brune   PetscErrorCode ierr;
1482e30e807fSPeter Brune 
1483e30e807fSPeter Brune   PetscFunctionBegin;
1484e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1485e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1486e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1487e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
148882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1489e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1490e7c4fc90SDmitry Karpeev }
1491e7c4fc90SDmitry Karpeev 
1492731c8d9eSDmitry Karpeev #undef __FUNCT__
149347c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
149447c6ae99SBarry Smith /*@
149547c6ae99SBarry Smith   DMRefine - Refines a DM object
149647c6ae99SBarry Smith 
149747c6ae99SBarry Smith   Collective on DM
149847c6ae99SBarry Smith 
149947c6ae99SBarry Smith   Input Parameter:
150047c6ae99SBarry Smith + dm   - the DM object
150191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
150247c6ae99SBarry Smith 
150347c6ae99SBarry Smith   Output Parameter:
15040298fd71SBarry Smith . dmf - the refined DM, or NULL
1505ae0a1c52SMatthew G Knepley 
15060298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
150747c6ae99SBarry Smith 
150847c6ae99SBarry Smith   Level: developer
150947c6ae99SBarry Smith 
1510e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
151147c6ae99SBarry Smith @*/
15127087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
151347c6ae99SBarry Smith {
151447c6ae99SBarry Smith   PetscErrorCode   ierr;
1515c833c3b5SJed Brown   DMRefineHookLink link;
151647c6ae99SBarry Smith 
151747c6ae99SBarry Smith   PetscFunctionBegin;
1518732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15204057135bSMatthew G Knepley   if (*dmf) {
152143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15228865f1eaSKarl Rupp 
15238cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15248865f1eaSKarl Rupp 
1525644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15260598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1527656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15288865f1eaSKarl Rupp 
1529e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1530c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15318865f1eaSKarl Rupp       if (link->refinehook) {
15328865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
15338865f1eaSKarl Rupp       }
1534c833c3b5SJed Brown     }
1535c833c3b5SJed Brown   }
1536c833c3b5SJed Brown   PetscFunctionReturn(0);
1537c833c3b5SJed Brown }
1538c833c3b5SJed Brown 
1539c833c3b5SJed Brown #undef __FUNCT__
1540c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1541bb9467b5SJed Brown /*@C
1542c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1543c833c3b5SJed Brown 
1544c833c3b5SJed Brown    Logically Collective
1545c833c3b5SJed Brown 
1546c833c3b5SJed Brown    Input Arguments:
1547c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1548c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1549c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
15500298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1551c833c3b5SJed Brown 
1552c833c3b5SJed Brown    Calling sequence of refinehook:
1553c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1554c833c3b5SJed Brown 
1555c833c3b5SJed Brown +  coarse - coarse level DM
1556c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1557c833c3b5SJed Brown -  ctx - optional user-defined function context
1558c833c3b5SJed Brown 
1559c833c3b5SJed Brown    Calling sequence for interphook:
1560c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1561c833c3b5SJed Brown 
1562c833c3b5SJed Brown +  coarse - coarse level DM
1563c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1564c833c3b5SJed Brown .  fine - fine level DM to update
1565c833c3b5SJed Brown -  ctx - optional user-defined function context
1566c833c3b5SJed Brown 
1567c833c3b5SJed Brown    Level: advanced
1568c833c3b5SJed Brown 
1569c833c3b5SJed Brown    Notes:
1570c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1571c833c3b5SJed Brown 
1572c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1573c833c3b5SJed Brown 
1574bb9467b5SJed Brown    This function is currently not available from Fortran.
1575bb9467b5SJed Brown 
1576c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1577c833c3b5SJed Brown @*/
1578c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1579c833c3b5SJed Brown {
1580c833c3b5SJed Brown   PetscErrorCode   ierr;
1581c833c3b5SJed Brown   DMRefineHookLink link,*p;
1582c833c3b5SJed Brown 
1583c833c3b5SJed Brown   PetscFunctionBegin;
1584c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1585c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1586c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1587c833c3b5SJed Brown   link->refinehook = refinehook;
1588c833c3b5SJed Brown   link->interphook = interphook;
1589c833c3b5SJed Brown   link->ctx        = ctx;
15900298fd71SBarry Smith   link->next       = NULL;
1591c833c3b5SJed Brown   *p               = link;
1592c833c3b5SJed Brown   PetscFunctionReturn(0);
1593c833c3b5SJed Brown }
1594c833c3b5SJed Brown 
1595c833c3b5SJed Brown #undef __FUNCT__
1596c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1597c833c3b5SJed Brown /*@
1598c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1599c833c3b5SJed Brown 
1600c833c3b5SJed Brown    Collective if any hooks are
1601c833c3b5SJed Brown 
1602c833c3b5SJed Brown    Input Arguments:
1603c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1604c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1605c833c3b5SJed Brown -  fine - finer DM to update
1606c833c3b5SJed Brown 
1607c833c3b5SJed Brown    Level: developer
1608c833c3b5SJed Brown 
1609c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1610c833c3b5SJed Brown @*/
1611c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1612c833c3b5SJed Brown {
1613c833c3b5SJed Brown   PetscErrorCode   ierr;
1614c833c3b5SJed Brown   DMRefineHookLink link;
1615c833c3b5SJed Brown 
1616c833c3b5SJed Brown   PetscFunctionBegin;
1617c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16188865f1eaSKarl Rupp     if (link->interphook) {
16198865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16208865f1eaSKarl Rupp     }
16214057135bSMatthew G Knepley   }
162247c6ae99SBarry Smith   PetscFunctionReturn(0);
162347c6ae99SBarry Smith }
162447c6ae99SBarry Smith 
162547c6ae99SBarry Smith #undef __FUNCT__
1626eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1627eb3f98d2SBarry Smith /*@
1628eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1629eb3f98d2SBarry Smith 
1630eb3f98d2SBarry Smith     Not Collective
1631eb3f98d2SBarry Smith 
1632eb3f98d2SBarry Smith     Input Parameter:
1633eb3f98d2SBarry Smith .   dm - the DM object
1634eb3f98d2SBarry Smith 
1635eb3f98d2SBarry Smith     Output Parameter:
1636eb3f98d2SBarry Smith .   level - number of refinements
1637eb3f98d2SBarry Smith 
1638eb3f98d2SBarry Smith     Level: developer
1639eb3f98d2SBarry Smith 
16406a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1641eb3f98d2SBarry Smith 
1642eb3f98d2SBarry Smith @*/
1643eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1644eb3f98d2SBarry Smith {
1645eb3f98d2SBarry Smith   PetscFunctionBegin;
1646eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1647eb3f98d2SBarry Smith   *level = dm->levelup;
1648eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1649eb3f98d2SBarry Smith }
1650eb3f98d2SBarry Smith 
1651eb3f98d2SBarry Smith #undef __FUNCT__
1652baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1653bb9467b5SJed Brown /*@C
1654baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1655baf369e7SPeter Brune 
1656baf369e7SPeter Brune    Logically Collective
1657baf369e7SPeter Brune 
1658baf369e7SPeter Brune    Input Arguments:
1659baf369e7SPeter Brune +  dm - the DM
1660baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1661baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
16620298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1663baf369e7SPeter Brune 
1664baf369e7SPeter Brune    Calling sequence for beginhook:
1665baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1666baf369e7SPeter Brune 
1667baf369e7SPeter Brune +  dm - global DM
1668baf369e7SPeter Brune .  g - global vector
1669baf369e7SPeter Brune .  mode - mode
1670baf369e7SPeter Brune .  l - local vector
1671baf369e7SPeter Brune -  ctx - optional user-defined function context
1672baf369e7SPeter Brune 
1673baf369e7SPeter Brune 
1674baf369e7SPeter Brune    Calling sequence for endhook:
1675ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1676baf369e7SPeter Brune 
1677baf369e7SPeter Brune +  global - global DM
1678baf369e7SPeter Brune -  ctx - optional user-defined function context
1679baf369e7SPeter Brune 
1680baf369e7SPeter Brune    Level: advanced
1681baf369e7SPeter Brune 
1682baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1683baf369e7SPeter Brune @*/
1684baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1685baf369e7SPeter Brune {
1686baf369e7SPeter Brune   PetscErrorCode          ierr;
1687baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1688baf369e7SPeter Brune 
1689baf369e7SPeter Brune   PetscFunctionBegin;
1690baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1691baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1692baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1693baf369e7SPeter Brune   link->beginhook = beginhook;
1694baf369e7SPeter Brune   link->endhook   = endhook;
1695baf369e7SPeter Brune   link->ctx       = ctx;
16960298fd71SBarry Smith   link->next      = NULL;
1697baf369e7SPeter Brune   *p              = link;
1698baf369e7SPeter Brune   PetscFunctionReturn(0);
1699baf369e7SPeter Brune }
1700baf369e7SPeter Brune 
1701baf369e7SPeter Brune #undef __FUNCT__
17024c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17034c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17044c274da1SToby Isaac {
17054c274da1SToby Isaac   Mat cMat;
17064c274da1SToby Isaac   Vec cVec;
17074c274da1SToby Isaac   PetscSection section, cSec;
17084c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17094c274da1SToby Isaac   PetscErrorCode ierr;
17104c274da1SToby Isaac 
17114c274da1SToby Isaac   PetscFunctionBegin;
17124c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17134c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17144c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17154c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17167711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17174c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17184c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17194c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17204c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17214c274da1SToby Isaac       if (dof) {
17224c274da1SToby Isaac         PetscScalar *vals;
17234c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17244c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17254c274da1SToby Isaac       }
17264c274da1SToby Isaac     }
17274c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17284c274da1SToby Isaac   }
17294c274da1SToby Isaac   PetscFunctionReturn(0);
17304c274da1SToby Isaac }
17314c274da1SToby Isaac 
17324c274da1SToby Isaac #undef __FUNCT__
173347c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
173447c6ae99SBarry Smith /*@
173547c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
173647c6ae99SBarry Smith 
173747c6ae99SBarry Smith     Neighbor-wise Collective on DM
173847c6ae99SBarry Smith 
173947c6ae99SBarry Smith     Input Parameters:
174047c6ae99SBarry Smith +   dm - the DM object
174147c6ae99SBarry Smith .   g - the global vector
174247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
174347c6ae99SBarry Smith -   l - the local vector
174447c6ae99SBarry Smith 
174547c6ae99SBarry Smith 
174647c6ae99SBarry Smith     Level: beginner
174747c6ae99SBarry Smith 
1748e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
174947c6ae99SBarry Smith 
175047c6ae99SBarry Smith @*/
17517087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
175247c6ae99SBarry Smith {
17537128ae9fSMatthew G Knepley   PetscSF                 sf;
175447c6ae99SBarry Smith   PetscErrorCode          ierr;
1755baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
175647c6ae99SBarry Smith 
175747c6ae99SBarry Smith   PetscFunctionBegin;
1758171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1759baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
17608865f1eaSKarl Rupp     if (link->beginhook) {
17618865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
17628865f1eaSKarl Rupp     }
1763baf369e7SPeter Brune   }
17647128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17657128ae9fSMatthew G Knepley   if (sf) {
1766ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1767ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
17687128ae9fSMatthew G Knepley 
176982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17707128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1771ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17727128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17737128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1774ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17757128ae9fSMatthew G Knepley   } else {
1776843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17777128ae9fSMatthew G Knepley   }
177847c6ae99SBarry Smith   PetscFunctionReturn(0);
177947c6ae99SBarry Smith }
178047c6ae99SBarry Smith 
178147c6ae99SBarry Smith #undef __FUNCT__
178247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
178347c6ae99SBarry Smith /*@
178447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
178547c6ae99SBarry Smith 
178647c6ae99SBarry Smith     Neighbor-wise Collective on DM
178747c6ae99SBarry Smith 
178847c6ae99SBarry Smith     Input Parameters:
178947c6ae99SBarry Smith +   dm - the DM object
179047c6ae99SBarry Smith .   g - the global vector
179147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
179247c6ae99SBarry Smith -   l - the local vector
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith 
179547c6ae99SBarry Smith     Level: beginner
179647c6ae99SBarry Smith 
1797e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
179847c6ae99SBarry Smith 
179947c6ae99SBarry Smith @*/
18007087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
180147c6ae99SBarry Smith {
18027128ae9fSMatthew G Knepley   PetscSF                 sf;
180347c6ae99SBarry Smith   PetscErrorCode          ierr;
1804ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1805ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1806baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
180747c6ae99SBarry Smith 
180847c6ae99SBarry Smith   PetscFunctionBegin;
1809171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18107128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18117128ae9fSMatthew G Knepley   if (sf) {
181282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18137128ae9fSMatthew G Knepley 
18147128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1815ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18167128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18177128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1818ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18197128ae9fSMatthew G Knepley   } else {
1820843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18217128ae9fSMatthew G Knepley   }
18224c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1823baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1824baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1825baf369e7SPeter Brune   }
182647c6ae99SBarry Smith   PetscFunctionReturn(0);
182747c6ae99SBarry Smith }
182847c6ae99SBarry Smith 
182947c6ae99SBarry Smith #undef __FUNCT__
1830d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1831d4d07f1eSToby Isaac /*@C
1832d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1833d4d07f1eSToby Isaac 
1834d4d07f1eSToby Isaac    Logically Collective
1835d4d07f1eSToby Isaac 
1836d4d07f1eSToby Isaac    Input Arguments:
1837d4d07f1eSToby Isaac +  dm - the DM
1838d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1839d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1840d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1841d4d07f1eSToby Isaac 
1842d4d07f1eSToby Isaac    Calling sequence for beginhook:
1843d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1844d4d07f1eSToby Isaac 
1845d4d07f1eSToby Isaac +  dm - global DM
1846d4d07f1eSToby Isaac .  l - local vector
1847d4d07f1eSToby Isaac .  mode - mode
1848d4d07f1eSToby Isaac .  g - global vector
1849d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1850d4d07f1eSToby Isaac 
1851d4d07f1eSToby Isaac 
1852d4d07f1eSToby Isaac    Calling sequence for endhook:
1853d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1854d4d07f1eSToby Isaac 
1855d4d07f1eSToby Isaac +  global - global DM
1856d4d07f1eSToby Isaac .  l - local vector
1857d4d07f1eSToby Isaac .  mode - mode
1858d4d07f1eSToby Isaac .  g - global vector
1859d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1860d4d07f1eSToby Isaac 
1861d4d07f1eSToby Isaac    Level: advanced
1862d4d07f1eSToby Isaac 
1863d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1864d4d07f1eSToby Isaac @*/
1865d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1866d4d07f1eSToby Isaac {
1867d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1868d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1869d4d07f1eSToby Isaac 
1870d4d07f1eSToby Isaac   PetscFunctionBegin;
1871d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1872d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1873d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1874d4d07f1eSToby Isaac   link->beginhook = beginhook;
1875d4d07f1eSToby Isaac   link->endhook   = endhook;
1876d4d07f1eSToby Isaac   link->ctx       = ctx;
1877d4d07f1eSToby Isaac   link->next      = NULL;
1878d4d07f1eSToby Isaac   *p              = link;
1879d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1880d4d07f1eSToby Isaac }
1881d4d07f1eSToby Isaac 
1882d4d07f1eSToby Isaac #undef __FUNCT__
18834c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
18844c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
18854c274da1SToby Isaac {
18864c274da1SToby Isaac   Mat cMat;
18874c274da1SToby Isaac   Vec cVec;
18884c274da1SToby Isaac   PetscSection section, cSec;
18894c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18904c274da1SToby Isaac   PetscErrorCode ierr;
18914c274da1SToby Isaac 
18924c274da1SToby Isaac   PetscFunctionBegin;
18934c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18944c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18954c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
18964c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18977711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18984c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18994c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19004c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19014c274da1SToby Isaac       if (dof) {
19024c274da1SToby Isaac         PetscInt d;
19034c274da1SToby Isaac         PetscScalar *vals;
19044c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19054c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19064c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19074c274da1SToby Isaac          * we just extracted */
19084c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19094c274da1SToby Isaac           vals[d] = 0.;
19104c274da1SToby Isaac         }
19114c274da1SToby Isaac       }
19124c274da1SToby Isaac     }
19134c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19144c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19154c274da1SToby Isaac   }
19164c274da1SToby Isaac   PetscFunctionReturn(0);
19174c274da1SToby Isaac }
19184c274da1SToby Isaac 
19194c274da1SToby Isaac #undef __FUNCT__
19209a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
192147c6ae99SBarry Smith /*@
19229a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19239a42bb27SBarry Smith 
19249a42bb27SBarry Smith     Neighbor-wise Collective on DM
19259a42bb27SBarry Smith 
19269a42bb27SBarry Smith     Input Parameters:
19279a42bb27SBarry Smith +   dm - the DM object
1928f6813fd5SJed Brown .   l - the local vector
19291eb28f2eSBarry 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.
19301eb28f2eSBarry Smith -   g - the global vector
19319a42bb27SBarry Smith 
1932d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
193384330215SMatthew 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.
19349a42bb27SBarry Smith 
19359a42bb27SBarry Smith     Level: beginner
19369a42bb27SBarry Smith 
1937e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
19389a42bb27SBarry Smith 
19399a42bb27SBarry Smith @*/
19407087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
19419a42bb27SBarry Smith {
19427128ae9fSMatthew G Knepley   PetscSF                 sf;
194384330215SMatthew G. Knepley   PetscSection            s, gs;
1944d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
1945ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
1946ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
194784330215SMatthew G. Knepley   PetscBool               isInsert;
194884330215SMatthew G. Knepley   PetscErrorCode          ierr;
19499a42bb27SBarry Smith 
19509a42bb27SBarry Smith   PetscFunctionBegin;
1951171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1952d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1953d4d07f1eSToby Isaac     if (link->beginhook) {
1954d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1955d4d07f1eSToby Isaac     }
1956d4d07f1eSToby Isaac   }
19574c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
19587128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
195984330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19607128ae9fSMatthew G Knepley   switch (mode) {
19617128ae9fSMatthew G Knepley   case INSERT_VALUES:
19627128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
196384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
19647128ae9fSMatthew G Knepley   case ADD_VALUES:
19657128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
196684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
19677128ae9fSMatthew G Knepley   default:
196882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19697128ae9fSMatthew G Knepley   }
197084330215SMatthew G. Knepley   if (sf && !isInsert) {
1971ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19727128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
1973a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
1974ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
197584330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
197684330215SMatthew G. Knepley   } else if (s && isInsert) {
197784330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
197884330215SMatthew G. Knepley 
197984330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
198084330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
198184330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
1982ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
198384330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
198484330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
1985b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
198684330215SMatthew G. Knepley 
198784330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
198803442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
198984330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
1990b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
199184330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
199284330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
1993b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
199403442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
1995b3b16f48SMatthew 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);
1996b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
1997b3b16f48SMatthew G. Knepley       if (!gcdof) {
199884330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
1999b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2000b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
200184330215SMatthew G. Knepley         const PetscInt *cdofs;
200284330215SMatthew G. Knepley         PetscInt        cind = 0;
200384330215SMatthew G. Knepley 
200484330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2005b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
200684330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2007b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
200884330215SMatthew G. Knepley         }
2009b3b16f48SMatthew 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);
201084330215SMatthew G. Knepley     }
2011ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20127128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20137128ae9fSMatthew G Knepley   } else {
2014843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20157128ae9fSMatthew G Knepley   }
20169a42bb27SBarry Smith   PetscFunctionReturn(0);
20179a42bb27SBarry Smith }
20189a42bb27SBarry Smith 
20199a42bb27SBarry Smith #undef __FUNCT__
20209a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20219a42bb27SBarry Smith /*@
20229a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
202347c6ae99SBarry Smith 
202447c6ae99SBarry Smith     Neighbor-wise Collective on DM
202547c6ae99SBarry Smith 
202647c6ae99SBarry Smith     Input Parameters:
202747c6ae99SBarry Smith +   dm - the DM object
2028f6813fd5SJed Brown .   l - the local vector
202947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2030f6813fd5SJed Brown -   g - the global vector
203147c6ae99SBarry Smith 
203247c6ae99SBarry Smith 
203347c6ae99SBarry Smith     Level: beginner
203447c6ae99SBarry Smith 
2035e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
203647c6ae99SBarry Smith 
203747c6ae99SBarry Smith @*/
20387087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
203947c6ae99SBarry Smith {
20407128ae9fSMatthew G Knepley   PetscSF                 sf;
204184330215SMatthew G. Knepley   PetscSection            s;
2042d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
204384330215SMatthew G. Knepley   PetscBool               isInsert;
204484330215SMatthew G. Knepley   PetscErrorCode          ierr;
204547c6ae99SBarry Smith 
204647c6ae99SBarry Smith   PetscFunctionBegin;
2047171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20487128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
204984330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20507128ae9fSMatthew G Knepley   switch (mode) {
20517128ae9fSMatthew G Knepley   case INSERT_VALUES:
20527128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
205384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20547128ae9fSMatthew G Knepley   case ADD_VALUES:
20557128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
205684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20577128ae9fSMatthew G Knepley   default:
205882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20597128ae9fSMatthew G Knepley   }
206084330215SMatthew G. Knepley   if (sf && !isInsert) {
2061ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2062ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
206384330215SMatthew G. Knepley 
2064ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20657128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2066a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2067ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20687128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
206984330215SMatthew G. Knepley   } else if (s && isInsert) {
20707128ae9fSMatthew G Knepley   } else {
2071843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20727128ae9fSMatthew G Knepley   }
2073d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2074d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2075d4d07f1eSToby Isaac   }
207647c6ae99SBarry Smith   PetscFunctionReturn(0);
207747c6ae99SBarry Smith }
207847c6ae99SBarry Smith 
207947c6ae99SBarry Smith #undef __FUNCT__
2080f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2081f089877aSRichard Tran Mills /*@
2082bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2083bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2084d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2085f089877aSRichard Tran Mills 
2086bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2087f089877aSRichard Tran Mills 
2088f089877aSRichard Tran Mills    Input Parameters:
2089f089877aSRichard Tran Mills +  dm - the DM object
2090bc0a1609SRichard Tran Mills .  g - the original local vector
2091bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2092f089877aSRichard Tran Mills 
2093bc0a1609SRichard Tran Mills    Output Parameter:
2094bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2095f089877aSRichard Tran Mills 
2096f089877aSRichard Tran Mills    Level: intermediate
2097f089877aSRichard Tran Mills 
2098bc0a1609SRichard Tran Mills    Notes:
2099bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2100bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2101bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2102bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2103bc0a1609SRichard Tran Mills 
2104bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2105bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2106f089877aSRichard Tran Mills 
2107f089877aSRichard Tran Mills @*/
2108f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2109f089877aSRichard Tran Mills {
2110f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2111f089877aSRichard Tran Mills 
2112f089877aSRichard Tran Mills   PetscFunctionBegin;
2113f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2114f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2115f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2116f089877aSRichard Tran Mills }
2117f089877aSRichard Tran Mills 
2118f089877aSRichard Tran Mills #undef __FUNCT__
2119f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2120f089877aSRichard Tran Mills /*@
2121bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2122bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2123d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2124f089877aSRichard Tran Mills 
2125bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2126f089877aSRichard Tran Mills 
2127f089877aSRichard Tran Mills    Input Parameters:
2128bc0a1609SRichard Tran Mills +  da - the DM object
2129bc0a1609SRichard Tran Mills .  g - the original local vector
2130bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2131f089877aSRichard Tran Mills 
2132bc0a1609SRichard Tran Mills    Output Parameter:
2133bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2134f089877aSRichard Tran Mills 
2135f089877aSRichard Tran Mills    Level: intermediate
2136f089877aSRichard Tran Mills 
2137bc0a1609SRichard Tran Mills    Notes:
2138bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2139bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2140bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2141bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2142bc0a1609SRichard Tran Mills 
2143bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2144bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2145f089877aSRichard Tran Mills 
2146f089877aSRichard Tran Mills @*/
2147f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2148f089877aSRichard Tran Mills {
2149f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2150f089877aSRichard Tran Mills 
2151f089877aSRichard Tran Mills   PetscFunctionBegin;
2152f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2153f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2154f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2155f089877aSRichard Tran Mills }
2156f089877aSRichard Tran Mills 
2157f089877aSRichard Tran Mills 
2158f089877aSRichard Tran Mills #undef __FUNCT__
215947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
216047c6ae99SBarry Smith /*@
216147c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith     Collective on DM
216447c6ae99SBarry Smith 
216547c6ae99SBarry Smith     Input Parameter:
216647c6ae99SBarry Smith +   dm - the DM object
216791d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
216847c6ae99SBarry Smith 
216947c6ae99SBarry Smith     Output Parameter:
217047c6ae99SBarry Smith .   dmc - the coarsened DM
217147c6ae99SBarry Smith 
217247c6ae99SBarry Smith     Level: developer
217347c6ae99SBarry Smith 
2174e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith @*/
21777087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
217847c6ae99SBarry Smith {
217947c6ae99SBarry Smith   PetscErrorCode    ierr;
2180b17ce1afSJed Brown   DMCoarsenHookLink link;
218147c6ae99SBarry Smith 
218247c6ae99SBarry Smith   PetscFunctionBegin;
2183171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
218447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
218547c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
21868892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
218743842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
21888cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2189644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
21900598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2191656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2192e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2193b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2194b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2195b17ce1afSJed Brown   }
219647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2197b17ce1afSJed Brown   PetscFunctionReturn(0);
2198b17ce1afSJed Brown }
2199b17ce1afSJed Brown 
2200b17ce1afSJed Brown #undef __FUNCT__
2201b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2202bb9467b5SJed Brown /*@C
2203b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2204b17ce1afSJed Brown 
2205b17ce1afSJed Brown    Logically Collective
2206b17ce1afSJed Brown 
2207b17ce1afSJed Brown    Input Arguments:
2208b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2209b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2210b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22110298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2212b17ce1afSJed Brown 
2213b17ce1afSJed Brown    Calling sequence of coarsenhook:
2214b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2215b17ce1afSJed Brown 
2216b17ce1afSJed Brown +  fine - fine level DM
2217b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2218b17ce1afSJed Brown -  ctx - optional user-defined function context
2219b17ce1afSJed Brown 
2220b17ce1afSJed Brown    Calling sequence for restricthook:
2221c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2222b17ce1afSJed Brown 
2223b17ce1afSJed Brown +  fine - fine level DM
2224b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2225c833c3b5SJed Brown .  rscale - scaling vector for restriction
2226c833c3b5SJed Brown .  inject - matrix restricting by injection
2227b17ce1afSJed Brown .  coarse - coarse level DM to update
2228b17ce1afSJed Brown -  ctx - optional user-defined function context
2229b17ce1afSJed Brown 
2230b17ce1afSJed Brown    Level: advanced
2231b17ce1afSJed Brown 
2232b17ce1afSJed Brown    Notes:
2233b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2234b17ce1afSJed Brown 
2235b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2236b17ce1afSJed Brown 
2237b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2238b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2239b17ce1afSJed Brown 
2240bb9467b5SJed Brown    This function is currently not available from Fortran.
2241bb9467b5SJed Brown 
2242c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2243b17ce1afSJed Brown @*/
2244b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2245b17ce1afSJed Brown {
2246b17ce1afSJed Brown   PetscErrorCode    ierr;
2247b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2248b17ce1afSJed Brown 
2249b17ce1afSJed Brown   PetscFunctionBegin;
2250b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
22516bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2252b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2253b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2254b17ce1afSJed Brown   link->restricthook = restricthook;
2255b17ce1afSJed Brown   link->ctx          = ctx;
22560298fd71SBarry Smith   link->next         = NULL;
2257b17ce1afSJed Brown   *p                 = link;
2258b17ce1afSJed Brown   PetscFunctionReturn(0);
2259b17ce1afSJed Brown }
2260b17ce1afSJed Brown 
2261b17ce1afSJed Brown #undef __FUNCT__
2262b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2263b17ce1afSJed Brown /*@
2264b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2265b17ce1afSJed Brown 
2266b17ce1afSJed Brown    Collective if any hooks are
2267b17ce1afSJed Brown 
2268b17ce1afSJed Brown    Input Arguments:
2269b17ce1afSJed Brown +  fine - finer DM to use as a base
2270b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2271b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2272b17ce1afSJed Brown -  coarse - coarer DM to update
2273b17ce1afSJed Brown 
2274b17ce1afSJed Brown    Level: developer
2275b17ce1afSJed Brown 
2276b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2277b17ce1afSJed Brown @*/
2278b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2279b17ce1afSJed Brown {
2280b17ce1afSJed Brown   PetscErrorCode    ierr;
2281b17ce1afSJed Brown   DMCoarsenHookLink link;
2282b17ce1afSJed Brown 
2283b17ce1afSJed Brown   PetscFunctionBegin;
2284b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
22858865f1eaSKarl Rupp     if (link->restricthook) {
22868865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
22878865f1eaSKarl Rupp     }
2288b17ce1afSJed Brown   }
228947c6ae99SBarry Smith   PetscFunctionReturn(0);
229047c6ae99SBarry Smith }
229147c6ae99SBarry Smith 
229247c6ae99SBarry Smith #undef __FUNCT__
2293be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2294bb9467b5SJed Brown /*@C
2295be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
22965dbd56e3SPeter Brune 
22975dbd56e3SPeter Brune    Logically Collective
22985dbd56e3SPeter Brune 
22995dbd56e3SPeter Brune    Input Arguments:
23005dbd56e3SPeter Brune +  global - global DM
2301ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
23025dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
23030298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23045dbd56e3SPeter Brune 
2305ec4806b8SPeter Brune 
2306ec4806b8SPeter Brune    Calling sequence for ddhook:
2307ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2308ec4806b8SPeter Brune 
2309ec4806b8SPeter Brune +  global - global DM
2310ec4806b8SPeter Brune .  block  - block DM
2311ec4806b8SPeter Brune -  ctx - optional user-defined function context
2312ec4806b8SPeter Brune 
23135dbd56e3SPeter Brune    Calling sequence for restricthook:
2314ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23155dbd56e3SPeter Brune 
23165dbd56e3SPeter Brune +  global - global DM
23175dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23185dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2319ec4806b8SPeter Brune .  block  - block DM
23205dbd56e3SPeter Brune -  ctx - optional user-defined function context
23215dbd56e3SPeter Brune 
23225dbd56e3SPeter Brune    Level: advanced
23235dbd56e3SPeter Brune 
23245dbd56e3SPeter Brune    Notes:
2325ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23265dbd56e3SPeter Brune 
23275dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23285dbd56e3SPeter Brune 
23295dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2330ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
23315dbd56e3SPeter Brune 
2332bb9467b5SJed Brown    This function is currently not available from Fortran.
2333bb9467b5SJed Brown 
23345dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
23355dbd56e3SPeter Brune @*/
2336be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
23375dbd56e3SPeter Brune {
23385dbd56e3SPeter Brune   PetscErrorCode      ierr;
2339be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
23405dbd56e3SPeter Brune 
23415dbd56e3SPeter Brune   PetscFunctionBegin;
23425dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2343be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2344be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
23455dbd56e3SPeter Brune   link->restricthook = restricthook;
2346be081cd6SPeter Brune   link->ddhook       = ddhook;
23475dbd56e3SPeter Brune   link->ctx          = ctx;
23480298fd71SBarry Smith   link->next         = NULL;
23495dbd56e3SPeter Brune   *p                 = link;
23505dbd56e3SPeter Brune   PetscFunctionReturn(0);
23515dbd56e3SPeter Brune }
23525dbd56e3SPeter Brune 
23535dbd56e3SPeter Brune #undef __FUNCT__
2354be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
23555dbd56e3SPeter Brune /*@
2356be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
23575dbd56e3SPeter Brune 
23585dbd56e3SPeter Brune    Collective if any hooks are
23595dbd56e3SPeter Brune 
23605dbd56e3SPeter Brune    Input Arguments:
23615dbd56e3SPeter Brune +  fine - finer DM to use as a base
2362be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2363be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
23645dbd56e3SPeter Brune -  coarse - coarer DM to update
23655dbd56e3SPeter Brune 
23665dbd56e3SPeter Brune    Level: developer
23675dbd56e3SPeter Brune 
23685dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
23695dbd56e3SPeter Brune @*/
2370be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
23715dbd56e3SPeter Brune {
23725dbd56e3SPeter Brune   PetscErrorCode      ierr;
2373be081cd6SPeter Brune   DMSubDomainHookLink link;
23745dbd56e3SPeter Brune 
23755dbd56e3SPeter Brune   PetscFunctionBegin;
2376be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
23778865f1eaSKarl Rupp     if (link->restricthook) {
23788865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
23798865f1eaSKarl Rupp     }
23805dbd56e3SPeter Brune   }
23815dbd56e3SPeter Brune   PetscFunctionReturn(0);
23825dbd56e3SPeter Brune }
23835dbd56e3SPeter Brune 
23845dbd56e3SPeter Brune #undef __FUNCT__
23855fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
23865fe1f584SPeter Brune /*@
23876a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
23885fe1f584SPeter Brune 
23895fe1f584SPeter Brune     Not Collective
23905fe1f584SPeter Brune 
23915fe1f584SPeter Brune     Input Parameter:
23925fe1f584SPeter Brune .   dm - the DM object
23935fe1f584SPeter Brune 
23945fe1f584SPeter Brune     Output Parameter:
23956a7d9d85SPeter Brune .   level - number of coarsenings
23965fe1f584SPeter Brune 
23975fe1f584SPeter Brune     Level: developer
23985fe1f584SPeter Brune 
23996a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
24005fe1f584SPeter Brune 
24015fe1f584SPeter Brune @*/
24025fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
24035fe1f584SPeter Brune {
24045fe1f584SPeter Brune   PetscFunctionBegin;
24055fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24065fe1f584SPeter Brune   *level = dm->leveldown;
24075fe1f584SPeter Brune   PetscFunctionReturn(0);
24085fe1f584SPeter Brune }
24095fe1f584SPeter Brune 
24105fe1f584SPeter Brune 
24115fe1f584SPeter Brune 
24125fe1f584SPeter Brune #undef __FUNCT__
241347c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
241447c6ae99SBarry Smith /*@C
241547c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
241647c6ae99SBarry Smith 
241747c6ae99SBarry Smith     Collective on DM
241847c6ae99SBarry Smith 
241947c6ae99SBarry Smith     Input Parameter:
242047c6ae99SBarry Smith +   dm - the DM object
242147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
242247c6ae99SBarry Smith 
242347c6ae99SBarry Smith     Output Parameter:
242447c6ae99SBarry Smith .   dmf - the refined DM hierarchy
242547c6ae99SBarry Smith 
242647c6ae99SBarry Smith     Level: developer
242747c6ae99SBarry Smith 
2428e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith @*/
24317087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
243247c6ae99SBarry Smith {
243347c6ae99SBarry Smith   PetscErrorCode ierr;
243447c6ae99SBarry Smith 
243547c6ae99SBarry Smith   PetscFunctionBegin;
2436171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2437ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
243847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
243947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
244047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
244147c6ae99SBarry Smith   } else if (dm->ops->refine) {
244247c6ae99SBarry Smith     PetscInt i;
244347c6ae99SBarry Smith 
2444ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
244547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2446ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
244747c6ae99SBarry Smith     }
2448ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
244947c6ae99SBarry Smith   PetscFunctionReturn(0);
245047c6ae99SBarry Smith }
245147c6ae99SBarry Smith 
245247c6ae99SBarry Smith #undef __FUNCT__
245347c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
245447c6ae99SBarry Smith /*@C
245547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
245647c6ae99SBarry Smith 
245747c6ae99SBarry Smith     Collective on DM
245847c6ae99SBarry Smith 
245947c6ae99SBarry Smith     Input Parameter:
246047c6ae99SBarry Smith +   dm - the DM object
246147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
246247c6ae99SBarry Smith 
246347c6ae99SBarry Smith     Output Parameter:
246447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
246547c6ae99SBarry Smith 
246647c6ae99SBarry Smith     Level: developer
246747c6ae99SBarry Smith 
2468e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
246947c6ae99SBarry Smith 
247047c6ae99SBarry Smith @*/
24717087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
247247c6ae99SBarry Smith {
247347c6ae99SBarry Smith   PetscErrorCode ierr;
247447c6ae99SBarry Smith 
247547c6ae99SBarry Smith   PetscFunctionBegin;
2476171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2477ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
247847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
247947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
248047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
248147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
248247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
248347c6ae99SBarry Smith     PetscInt i;
248447c6ae99SBarry Smith 
2485ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
248647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2487ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
248847c6ae99SBarry Smith     }
2489ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
249047c6ae99SBarry Smith   PetscFunctionReturn(0);
249147c6ae99SBarry Smith }
249247c6ae99SBarry Smith 
249347c6ae99SBarry Smith #undef __FUNCT__
2494e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
249547c6ae99SBarry Smith /*@
2496e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
249747c6ae99SBarry Smith    grids associated with two DMs.
249847c6ae99SBarry Smith 
249947c6ae99SBarry Smith    Collective on DM
250047c6ae99SBarry Smith 
250147c6ae99SBarry Smith    Input Parameters:
250247c6ae99SBarry Smith +  dmc - the coarse grid DM
250347c6ae99SBarry Smith -  dmf - the fine grid DM
250447c6ae99SBarry Smith 
250547c6ae99SBarry Smith    Output Parameters:
250647c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
250747c6ae99SBarry Smith 
250847c6ae99SBarry Smith    Level: intermediate
250947c6ae99SBarry Smith 
251047c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
251147c6ae99SBarry Smith 
2512e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
251347c6ae99SBarry Smith @*/
2514e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
251547c6ae99SBarry Smith {
251647c6ae99SBarry Smith   PetscErrorCode ierr;
251747c6ae99SBarry Smith 
251847c6ae99SBarry Smith   PetscFunctionBegin;
2519171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2520171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
252147c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
252247c6ae99SBarry Smith   PetscFunctionReturn(0);
252347c6ae99SBarry Smith }
252447c6ae99SBarry Smith 
252547c6ae99SBarry Smith #undef __FUNCT__
25261a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25271a266240SBarry Smith /*@C
25281a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25291a266240SBarry Smith 
25301a266240SBarry Smith     Not Collective
25311a266240SBarry Smith 
25321a266240SBarry Smith     Input Parameters:
25331a266240SBarry Smith +   dm - the DM object
25341a266240SBarry Smith -   destroy - the destroy function
25351a266240SBarry Smith 
25361a266240SBarry Smith     Level: intermediate
25371a266240SBarry Smith 
2538e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
25391a266240SBarry Smith 
2540f07f9ceaSJed Brown @*/
25411a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
25421a266240SBarry Smith {
25431a266240SBarry Smith   PetscFunctionBegin;
2544171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25451a266240SBarry Smith   dm->ctxdestroy = destroy;
25461a266240SBarry Smith   PetscFunctionReturn(0);
25471a266240SBarry Smith }
25481a266240SBarry Smith 
25491a266240SBarry Smith #undef __FUNCT__
25501b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2551b07ff414SBarry Smith /*@
25521b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
255347c6ae99SBarry Smith 
255447c6ae99SBarry Smith     Not Collective
255547c6ae99SBarry Smith 
255647c6ae99SBarry Smith     Input Parameters:
255747c6ae99SBarry Smith +   dm - the DM object
255847c6ae99SBarry Smith -   ctx - the user context
255947c6ae99SBarry Smith 
256047c6ae99SBarry Smith     Level: intermediate
256147c6ae99SBarry Smith 
2562e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
256347c6ae99SBarry Smith 
256447c6ae99SBarry Smith @*/
25651b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
256647c6ae99SBarry Smith {
256747c6ae99SBarry Smith   PetscFunctionBegin;
2568171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
256947c6ae99SBarry Smith   dm->ctx = ctx;
257047c6ae99SBarry Smith   PetscFunctionReturn(0);
257147c6ae99SBarry Smith }
257247c6ae99SBarry Smith 
257347c6ae99SBarry Smith #undef __FUNCT__
25741b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
257547c6ae99SBarry Smith /*@
25761b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
257747c6ae99SBarry Smith 
257847c6ae99SBarry Smith     Not Collective
257947c6ae99SBarry Smith 
258047c6ae99SBarry Smith     Input Parameter:
258147c6ae99SBarry Smith .   dm - the DM object
258247c6ae99SBarry Smith 
258347c6ae99SBarry Smith     Output Parameter:
258447c6ae99SBarry Smith .   ctx - the user context
258547c6ae99SBarry Smith 
258647c6ae99SBarry Smith     Level: intermediate
258747c6ae99SBarry Smith 
2588e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
258947c6ae99SBarry Smith 
259047c6ae99SBarry Smith @*/
25911b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
259247c6ae99SBarry Smith {
259347c6ae99SBarry Smith   PetscFunctionBegin;
2594171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25951b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
259647c6ae99SBarry Smith   PetscFunctionReturn(0);
259747c6ae99SBarry Smith }
259847c6ae99SBarry Smith 
259947c6ae99SBarry Smith #undef __FUNCT__
260008da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
260108da532bSDmitry Karpeev /*@C
2602df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
260308da532bSDmitry Karpeev 
260408da532bSDmitry Karpeev     Logically Collective on DM
260508da532bSDmitry Karpeev 
260608da532bSDmitry Karpeev     Input Parameter:
260708da532bSDmitry Karpeev +   dm - the DM object
26080298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
260908da532bSDmitry Karpeev 
261008da532bSDmitry Karpeev     Level: intermediate
261108da532bSDmitry Karpeev 
2612835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
261308da532bSDmitry Karpeev          DMSetJacobian()
261408da532bSDmitry Karpeev 
261508da532bSDmitry Karpeev @*/
261608da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
261708da532bSDmitry Karpeev {
261808da532bSDmitry Karpeev   PetscFunctionBegin;
261908da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
262008da532bSDmitry Karpeev   PetscFunctionReturn(0);
262108da532bSDmitry Karpeev }
262208da532bSDmitry Karpeev 
262308da532bSDmitry Karpeev #undef __FUNCT__
262408da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
262508da532bSDmitry Karpeev /*@
262608da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
262708da532bSDmitry Karpeev 
262808da532bSDmitry Karpeev     Not Collective
262908da532bSDmitry Karpeev 
263008da532bSDmitry Karpeev     Input Parameter:
263108da532bSDmitry Karpeev .   dm - the DM object to destroy
263208da532bSDmitry Karpeev 
263308da532bSDmitry Karpeev     Output Parameter:
263408da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
263508da532bSDmitry Karpeev 
263608da532bSDmitry Karpeev     Level: developer
263708da532bSDmitry Karpeev 
263874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
263908da532bSDmitry Karpeev 
264008da532bSDmitry Karpeev @*/
264108da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
264208da532bSDmitry Karpeev {
264308da532bSDmitry Karpeev   PetscFunctionBegin;
264408da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
264508da532bSDmitry Karpeev   PetscFunctionReturn(0);
264608da532bSDmitry Karpeev }
264708da532bSDmitry Karpeev 
264808da532bSDmitry Karpeev #undef __FUNCT__
264908da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
265008da532bSDmitry Karpeev /*@C
265108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
265208da532bSDmitry Karpeev 
265308da532bSDmitry Karpeev     Logically Collective on DM
265408da532bSDmitry Karpeev 
265508da532bSDmitry Karpeev     Input Parameters:
2656907376e6SBarry Smith .   dm - the DM object
265708da532bSDmitry Karpeev 
265808da532bSDmitry Karpeev     Output parameters:
265908da532bSDmitry Karpeev +   xl - lower bound
266008da532bSDmitry Karpeev -   xu - upper bound
266108da532bSDmitry Karpeev 
2662907376e6SBarry Smith     Level: advanced
2663907376e6SBarry Smith 
2664907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
266508da532bSDmitry Karpeev 
266674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
266708da532bSDmitry Karpeev 
266808da532bSDmitry Karpeev @*/
266908da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
267008da532bSDmitry Karpeev {
267108da532bSDmitry Karpeev   PetscErrorCode ierr;
26725fd66863SKarl Rupp 
267308da532bSDmitry Karpeev   PetscFunctionBegin;
267408da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
267508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
267608da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
267708da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
26788865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
267908da532bSDmitry Karpeev   PetscFunctionReturn(0);
268008da532bSDmitry Karpeev }
268108da532bSDmitry Karpeev 
268208da532bSDmitry Karpeev #undef __FUNCT__
2683b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2684b0ae01b7SPeter Brune /*@
2685b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2686b0ae01b7SPeter Brune 
2687b0ae01b7SPeter Brune     Not Collective
2688b0ae01b7SPeter Brune 
2689b0ae01b7SPeter Brune     Input Parameter:
2690b0ae01b7SPeter Brune .   dm - the DM object
2691b0ae01b7SPeter Brune 
2692b0ae01b7SPeter Brune     Output Parameter:
2693b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2694b0ae01b7SPeter Brune 
2695b0ae01b7SPeter Brune     Level: developer
2696b0ae01b7SPeter Brune 
2697b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2698b0ae01b7SPeter Brune 
2699b0ae01b7SPeter Brune @*/
2700b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2701b0ae01b7SPeter Brune {
2702b0ae01b7SPeter Brune   PetscFunctionBegin;
2703b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2704b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2705b0ae01b7SPeter Brune }
2706b0ae01b7SPeter Brune 
2707b0ae01b7SPeter Brune #undef  __FUNCT__
270808da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2709748fac09SDmitry Karpeev /*@C
271008da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
271108da532bSDmitry Karpeev 
271208da532bSDmitry Karpeev     Collective on DM
271308da532bSDmitry Karpeev 
271408da532bSDmitry Karpeev     Input Parameter:
271508da532bSDmitry Karpeev +   dm - the DM object
27160298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
271708da532bSDmitry Karpeev 
271808da532bSDmitry Karpeev     Level: developer
271908da532bSDmitry Karpeev 
272074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
272108da532bSDmitry Karpeev 
272208da532bSDmitry Karpeev @*/
272308da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
272408da532bSDmitry Karpeev {
272508da532bSDmitry Karpeev   PetscErrorCode ierr;
27265fd66863SKarl Rupp 
272708da532bSDmitry Karpeev   PetscFunctionBegin;
272808da532bSDmitry Karpeev   if (x) {
272908da532bSDmitry Karpeev     if (!dm->x) {
273008da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
273108da532bSDmitry Karpeev     }
273208da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
27338865f1eaSKarl Rupp   } else if (dm->x) {
273408da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
273508da532bSDmitry Karpeev   }
273608da532bSDmitry Karpeev   PetscFunctionReturn(0);
273708da532bSDmitry Karpeev }
273808da532bSDmitry Karpeev 
27390298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2740264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2741264ace61SBarry Smith 
2742264ace61SBarry Smith #undef __FUNCT__
2743264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2744264ace61SBarry Smith /*@C
2745264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2746264ace61SBarry Smith 
2747264ace61SBarry Smith   Collective on DM
2748264ace61SBarry Smith 
2749264ace61SBarry Smith   Input Parameters:
2750264ace61SBarry Smith + dm     - The DM object
2751264ace61SBarry Smith - method - The name of the DM type
2752264ace61SBarry Smith 
2753264ace61SBarry Smith   Options Database Key:
2754264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2755264ace61SBarry Smith 
2756264ace61SBarry Smith   Notes:
2757e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2758264ace61SBarry Smith 
2759264ace61SBarry Smith   Level: intermediate
2760264ace61SBarry Smith 
2761264ace61SBarry Smith .keywords: DM, set, type
2762264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2763264ace61SBarry Smith @*/
276419fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2765264ace61SBarry Smith {
2766264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2767264ace61SBarry Smith   PetscBool      match;
2768264ace61SBarry Smith   PetscErrorCode ierr;
2769264ace61SBarry Smith 
2770264ace61SBarry Smith   PetscFunctionBegin;
2771264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2772251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2773264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2774264ace61SBarry Smith 
27750f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
27761c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2777ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2778264ace61SBarry Smith 
2779264ace61SBarry Smith   if (dm->ops->destroy) {
2780264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
27810298fd71SBarry Smith     dm->ops->destroy = NULL;
2782264ace61SBarry Smith   }
2783264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2784264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2785264ace61SBarry Smith   PetscFunctionReturn(0);
2786264ace61SBarry Smith }
2787264ace61SBarry Smith 
2788264ace61SBarry Smith #undef __FUNCT__
2789264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2790264ace61SBarry Smith /*@C
2791264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2792264ace61SBarry Smith 
2793264ace61SBarry Smith   Not Collective
2794264ace61SBarry Smith 
2795264ace61SBarry Smith   Input Parameter:
2796264ace61SBarry Smith . dm  - The DM
2797264ace61SBarry Smith 
2798264ace61SBarry Smith   Output Parameter:
2799264ace61SBarry Smith . type - The DM type name
2800264ace61SBarry Smith 
2801264ace61SBarry Smith   Level: intermediate
2802264ace61SBarry Smith 
2803264ace61SBarry Smith .keywords: DM, get, type, name
2804264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2805264ace61SBarry Smith @*/
280619fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2807264ace61SBarry Smith {
2808264ace61SBarry Smith   PetscErrorCode ierr;
2809264ace61SBarry Smith 
2810264ace61SBarry Smith   PetscFunctionBegin;
2811264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2812c959eef4SJed Brown   PetscValidPointer(type,2);
2813607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2814264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2815264ace61SBarry Smith   PetscFunctionReturn(0);
2816264ace61SBarry Smith }
2817264ace61SBarry Smith 
281867a56275SMatthew G Knepley #undef __FUNCT__
281967a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
282067a56275SMatthew G Knepley /*@C
282167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
282267a56275SMatthew G Knepley 
282367a56275SMatthew G Knepley   Collective on DM
282467a56275SMatthew G Knepley 
282567a56275SMatthew G Knepley   Input Parameters:
282667a56275SMatthew G Knepley + dm - the DM
282767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
282867a56275SMatthew G Knepley 
282967a56275SMatthew G Knepley   Output Parameter:
283067a56275SMatthew G Knepley . M - pointer to new DM
283167a56275SMatthew G Knepley 
283267a56275SMatthew G Knepley   Notes:
283367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
283467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
283567a56275SMatthew G Knepley   of the input DM.
283667a56275SMatthew G Knepley 
283767a56275SMatthew G Knepley   Level: intermediate
283867a56275SMatthew G Knepley 
283967a56275SMatthew G Knepley .seealso: DMCreate()
284067a56275SMatthew G Knepley @*/
284119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
284267a56275SMatthew G Knepley {
284367a56275SMatthew G Knepley   DM             B;
284467a56275SMatthew G Knepley   char           convname[256];
284567a56275SMatthew G Knepley   PetscBool      sametype, issame;
284667a56275SMatthew G Knepley   PetscErrorCode ierr;
284767a56275SMatthew G Knepley 
284867a56275SMatthew G Knepley   PetscFunctionBegin;
284967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
285067a56275SMatthew G Knepley   PetscValidType(dm,1);
285167a56275SMatthew G Knepley   PetscValidPointer(M,3);
2852251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
285367a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
285467a56275SMatthew G Knepley   {
28550298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
285667a56275SMatthew G Knepley 
285767a56275SMatthew G Knepley     /*
285867a56275SMatthew G Knepley        Order of precedence:
285967a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
286067a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
286167a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
286267a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
286367a56275SMatthew G Knepley        5) Use a really basic converter.
286467a56275SMatthew G Knepley     */
286567a56275SMatthew G Knepley 
286667a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
286767a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
286867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
286967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
287067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
287167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28720005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
287367a56275SMatthew G Knepley     if (conv) goto foundconv;
287467a56275SMatthew G Knepley 
287567a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
287682f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
287767a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
287867a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
287967a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
288067a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
288167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
288267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28830005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
288467a56275SMatthew G Knepley     if (conv) {
2885fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
288667a56275SMatthew G Knepley       goto foundconv;
288767a56275SMatthew G Knepley     }
288867a56275SMatthew G Knepley 
288967a56275SMatthew G Knepley #if 0
289067a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
289167a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2892fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
289367a56275SMatthew G Knepley     if (conv) goto foundconv;
289467a56275SMatthew G Knepley 
289567a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
289667a56275SMatthew G Knepley     if (dm->ops->convert) {
289767a56275SMatthew G Knepley       conv = dm->ops->convert;
289867a56275SMatthew G Knepley     }
289967a56275SMatthew G Knepley     if (conv) goto foundconv;
290067a56275SMatthew G Knepley #endif
290167a56275SMatthew G Knepley 
290267a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
290382f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
290467a56275SMatthew G Knepley 
290567a56275SMatthew G Knepley foundconv:
290667a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
290767a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
290867a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
290967a56275SMatthew G Knepley   }
291067a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
291167a56275SMatthew G Knepley   PetscFunctionReturn(0);
291267a56275SMatthew G Knepley }
2913264ace61SBarry Smith 
2914264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2915264ace61SBarry Smith 
2916264ace61SBarry Smith #undef __FUNCT__
2917264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2918264ace61SBarry Smith /*@C
29191c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29201c84c290SBarry Smith 
29211c84c290SBarry Smith   Not Collective
29221c84c290SBarry Smith 
29231c84c290SBarry Smith   Input Parameters:
29241c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29251c84c290SBarry Smith - create_func - The creation routine itself
29261c84c290SBarry Smith 
29271c84c290SBarry Smith   Notes:
29281c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29291c84c290SBarry Smith 
29301c84c290SBarry Smith 
29311c84c290SBarry Smith   Sample usage:
29321c84c290SBarry Smith .vb
2933bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
29341c84c290SBarry Smith .ve
29351c84c290SBarry Smith 
29361c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
29371c84c290SBarry Smith .vb
29381c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
29391c84c290SBarry Smith     DMSetType(DM,"my_da");
29401c84c290SBarry Smith .ve
29411c84c290SBarry Smith    or at runtime via the option
29421c84c290SBarry Smith .vb
29431c84c290SBarry Smith     -da_type my_da
29441c84c290SBarry Smith .ve
2945264ace61SBarry Smith 
2946264ace61SBarry Smith   Level: advanced
29471c84c290SBarry Smith 
29481c84c290SBarry Smith .keywords: DM, register
2949bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
29501c84c290SBarry Smith 
2951264ace61SBarry Smith @*/
2952bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2953264ace61SBarry Smith {
2954264ace61SBarry Smith   PetscErrorCode ierr;
2955264ace61SBarry Smith 
2956264ace61SBarry Smith   PetscFunctionBegin;
2957a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2958264ace61SBarry Smith   PetscFunctionReturn(0);
2959264ace61SBarry Smith }
2960264ace61SBarry Smith 
2961b859378eSBarry Smith #undef __FUNCT__
2962b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2963b859378eSBarry Smith /*@C
296455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2965b859378eSBarry Smith 
2966b859378eSBarry Smith   Collective on PetscViewer
2967b859378eSBarry Smith 
2968b859378eSBarry Smith   Input Parameters:
2969b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2970b859378eSBarry Smith            some related function before a call to DMLoad().
2971b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2972b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2973b859378eSBarry Smith 
2974b859378eSBarry Smith    Level: intermediate
2975b859378eSBarry Smith 
2976b859378eSBarry Smith   Notes:
297755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2978b859378eSBarry Smith 
2979b859378eSBarry Smith   Notes for advanced users:
2980b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2981b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2982b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2983b859378eSBarry Smith   format is
2984b859378eSBarry Smith .vb
2985b859378eSBarry Smith      has not yet been determined
2986b859378eSBarry Smith .ve
2987b859378eSBarry Smith 
2988b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2989b859378eSBarry Smith @*/
2990b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2991b859378eSBarry Smith {
29929331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2993b859378eSBarry Smith   PetscErrorCode ierr;
2994b859378eSBarry Smith 
2995b859378eSBarry Smith   PetscFunctionBegin;
2996b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2997b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
299832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
29999331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
30009331c7a4SMatthew G. Knepley   if (isbinary) {
30019331c7a4SMatthew G. Knepley     PetscInt classid;
30029331c7a4SMatthew G. Knepley     char     type[256];
3003b859378eSBarry Smith 
3004060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30059200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3006060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
300732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30089331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30099331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30109331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30119331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3012b859378eSBarry Smith   PetscFunctionReturn(0);
3013b859378eSBarry Smith }
3014b859378eSBarry Smith 
30157da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30167da65231SMatthew G Knepley 
30177da65231SMatthew G Knepley #undef __FUNCT__
30187da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3019a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3020a6dfd86eSKarl Rupp {
30211d47ebbbSSatish Balay   PetscInt       f;
30221b30c384SMatthew G Knepley   PetscErrorCode ierr;
30231b30c384SMatthew G Knepley 
30247da65231SMatthew G Knepley   PetscFunctionBegin;
302574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30261d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
302757622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30287da65231SMatthew G Knepley   }
30297da65231SMatthew G Knepley   PetscFunctionReturn(0);
30307da65231SMatthew G Knepley }
30317da65231SMatthew G Knepley 
30327da65231SMatthew G Knepley #undef __FUNCT__
30337da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3034a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3035a6dfd86eSKarl Rupp {
30361b30c384SMatthew G Knepley   PetscInt       f, g;
30377da65231SMatthew G Knepley   PetscErrorCode ierr;
30387da65231SMatthew G Knepley 
30397da65231SMatthew G Knepley   PetscFunctionBegin;
304074778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30411d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
304274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
30431d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3044e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
30457da65231SMatthew G Knepley     }
304674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30477da65231SMatthew G Knepley   }
30487da65231SMatthew G Knepley   PetscFunctionReturn(0);
30497da65231SMatthew G Knepley }
3050e7c4fc90SDmitry Karpeev 
3051970e74d5SMatthew G Knepley #undef __FUNCT__
3052e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
30536113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3054e759306cSMatthew G. Knepley {
3055e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3056e759306cSMatthew G. Knepley   PetscInt       p;
3057e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3058e759306cSMatthew G. Knepley 
3059e759306cSMatthew G. Knepley   PetscFunctionBegin;
3060e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3061e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3062e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3063e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3064e759306cSMatthew G. Knepley     if (p == rank) {
3065e759306cSMatthew G. Knepley       Vec x;
3066e759306cSMatthew G. Knepley 
3067e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3068e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
30696113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3070e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3071e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3072e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3073e759306cSMatthew G. Knepley     }
3074e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3075e759306cSMatthew G. Knepley   }
3076e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3077e759306cSMatthew G. Knepley }
3078e759306cSMatthew G. Knepley 
3079e759306cSMatthew G. Knepley #undef __FUNCT__
308088ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
308188ed4aceSMatthew G Knepley /*@
308288ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
308388ed4aceSMatthew G Knepley 
308488ed4aceSMatthew G Knepley   Input Parameter:
308588ed4aceSMatthew G Knepley . dm - The DM
308688ed4aceSMatthew G Knepley 
308788ed4aceSMatthew G Knepley   Output Parameter:
308888ed4aceSMatthew G Knepley . section - The PetscSection
308988ed4aceSMatthew G Knepley 
309088ed4aceSMatthew G Knepley   Level: intermediate
309188ed4aceSMatthew G Knepley 
309288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
309388ed4aceSMatthew G Knepley 
309488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
309588ed4aceSMatthew G Knepley @*/
30960adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
30970adebc6cSBarry Smith {
3098fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3099fd59a867SMatthew G. Knepley 
310088ed4aceSMatthew G Knepley   PetscFunctionBegin;
310188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
310288ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
31032f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31042f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3105685405a1SBarry Smith     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);
31062f0f8703SMatthew G. Knepley   }
310788ed4aceSMatthew G Knepley   *section = dm->defaultSection;
310888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
310988ed4aceSMatthew G Knepley }
311088ed4aceSMatthew G Knepley 
311188ed4aceSMatthew G Knepley #undef __FUNCT__
311288ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
311388ed4aceSMatthew G Knepley /*@
311488ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
311588ed4aceSMatthew G Knepley 
311688ed4aceSMatthew G Knepley   Input Parameters:
311788ed4aceSMatthew G Knepley + dm - The DM
311888ed4aceSMatthew G Knepley - section - The PetscSection
311988ed4aceSMatthew G Knepley 
312088ed4aceSMatthew G Knepley   Level: intermediate
312188ed4aceSMatthew G Knepley 
312288ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
312388ed4aceSMatthew G Knepley 
312488ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
312588ed4aceSMatthew G Knepley @*/
31260adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31270adebc6cSBarry Smith {
3128c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3129af122d2aSMatthew G Knepley   PetscInt       f;
313088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
313188ed4aceSMatthew G Knepley 
313288ed4aceSMatthew G Knepley   PetscFunctionBegin;
313388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3134c473ab19SMatthew G. Knepley   if (section) {
31351d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
31361d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3137c473ab19SMatthew G. Knepley   }
313888ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
313988ed4aceSMatthew G Knepley   dm->defaultSection = section;
3140c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3141af122d2aSMatthew G Knepley   if (numFields) {
3142af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3143af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
31440f21e855SMatthew G. Knepley       PetscObject disc;
3145af122d2aSMatthew G Knepley       const char *name;
3146af122d2aSMatthew G Knepley 
3147af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
31480f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
31490f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3150af122d2aSMatthew G Knepley     }
3151af122d2aSMatthew G Knepley   }
31521d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
31531d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
315488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
315588ed4aceSMatthew G Knepley }
315688ed4aceSMatthew G Knepley 
315788ed4aceSMatthew G Knepley #undef __FUNCT__
31589435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
31599435951eSToby Isaac /*@
31609435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
31619435951eSToby Isaac 
3162e228b242SToby Isaac   not collective
3163e228b242SToby Isaac 
31649435951eSToby Isaac   Input Parameter:
31659435951eSToby Isaac . dm - The DM
31669435951eSToby Isaac 
31679435951eSToby Isaac   Output Parameter:
31689435951eSToby 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.
31699435951eSToby 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.
31709435951eSToby Isaac 
31719435951eSToby Isaac   Level: advanced
31729435951eSToby Isaac 
31739435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
31749435951eSToby Isaac 
31759435951eSToby Isaac .seealso: DMSetDefaultConstraints()
31769435951eSToby Isaac @*/
31779435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
31789435951eSToby Isaac {
31799435951eSToby Isaac   PetscErrorCode ierr;
31809435951eSToby Isaac 
31819435951eSToby Isaac   PetscFunctionBegin;
31829435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31839435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
318445a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
318545a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
31869435951eSToby Isaac   PetscFunctionReturn(0);
31879435951eSToby Isaac }
31889435951eSToby Isaac 
31899435951eSToby Isaac #undef __FUNCT__
31909435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
31919435951eSToby Isaac /*@
31929435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
31939435951eSToby Isaac 
31949435951eSToby 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().
31959435951eSToby Isaac 
31969435951eSToby 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.
31979435951eSToby Isaac 
3198e228b242SToby Isaac   collective on dm
3199e228b242SToby Isaac 
32009435951eSToby Isaac   Input Parameters:
32019435951eSToby Isaac + dm - The DM
3202e228b242SToby 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).
3203e228b242SToby 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).
32049435951eSToby Isaac 
32059435951eSToby Isaac   Level: advanced
32069435951eSToby Isaac 
32079435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32089435951eSToby Isaac 
32099435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32109435951eSToby Isaac @*/
32119435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32129435951eSToby Isaac {
3213e228b242SToby Isaac   PetscMPIInt result;
32149435951eSToby Isaac   PetscErrorCode ierr;
32159435951eSToby Isaac 
32169435951eSToby Isaac   PetscFunctionBegin;
32179435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3218e228b242SToby Isaac   if (section) {
3219e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3220e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3221e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3222e228b242SToby Isaac   }
3223e228b242SToby Isaac   if (mat) {
3224e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3225e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3226e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3227e228b242SToby Isaac   }
32289435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32299435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
32309435951eSToby Isaac   dm->defaultConstraintSection = section;
32319435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
32329435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
32339435951eSToby Isaac   dm->defaultConstraintMat = mat;
32349435951eSToby Isaac   PetscFunctionReturn(0);
32359435951eSToby Isaac }
32369435951eSToby Isaac 
3237f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
32389435951eSToby Isaac #undef __FUNCT__
3239284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3240507e4973SMatthew G. Knepley /*
3241507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3242507e4973SMatthew G. Knepley 
3243507e4973SMatthew G. Knepley   Input Parameters:
3244507e4973SMatthew G. Knepley + dm - The DM
3245507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3246507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3247507e4973SMatthew G. Knepley 
3248507e4973SMatthew G. Knepley   Level: intermediate
3249507e4973SMatthew G. Knepley 
3250507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3251507e4973SMatthew G. Knepley */
3252f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3253507e4973SMatthew G. Knepley {
3254507e4973SMatthew G. Knepley   MPI_Comm        comm;
3255507e4973SMatthew G. Knepley   PetscLayout     layout;
3256507e4973SMatthew G. Knepley   const PetscInt *ranges;
3257507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3258507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3259507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3260507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3261507e4973SMatthew G. Knepley 
3262507e4973SMatthew G. Knepley   PetscFunctionBegin;
3263507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3264507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3265507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3266507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3267507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3268507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3269507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3270507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3271507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3272507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3273507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3274507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3275f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3276507e4973SMatthew G. Knepley 
3277507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3278507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3279507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3280507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3281507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3282507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3283507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3284507e4973SMatthew 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;}
3285507e4973SMatthew 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;}
3286507e4973SMatthew G. Knepley     if (gdof < 0) {
3287507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3288507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3289507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3290507e4973SMatthew G. Knepley 
3291507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3292507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3293507e4973SMatthew 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;}
3294507e4973SMatthew G. Knepley       }
3295507e4973SMatthew G. Knepley     }
3296507e4973SMatthew G. Knepley   }
3297507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3298507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3299*b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3300507e4973SMatthew G. Knepley   if (!gvalid) {
3301507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3302507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3303507e4973SMatthew G. Knepley   }
3304507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3305507e4973SMatthew G. Knepley }
3306f741bcd2SMatthew G. Knepley #endif
3307507e4973SMatthew G. Knepley 
3308507e4973SMatthew G. Knepley #undef __FUNCT__
330988ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
331088ed4aceSMatthew G Knepley /*@
331188ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
331288ed4aceSMatthew G Knepley 
33138b1ab98fSJed Brown   Collective on DM
33148b1ab98fSJed Brown 
331588ed4aceSMatthew G Knepley   Input Parameter:
331688ed4aceSMatthew G Knepley . dm - The DM
331788ed4aceSMatthew G Knepley 
331888ed4aceSMatthew G Knepley   Output Parameter:
331988ed4aceSMatthew G Knepley . section - The PetscSection
332088ed4aceSMatthew G Knepley 
332188ed4aceSMatthew G Knepley   Level: intermediate
332288ed4aceSMatthew G Knepley 
332388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
332488ed4aceSMatthew G Knepley 
332588ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
332688ed4aceSMatthew G Knepley @*/
33270adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33280adebc6cSBarry Smith {
332988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
333088ed4aceSMatthew G Knepley 
333188ed4aceSMatthew G Knepley   PetscFunctionBegin;
333288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
333388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
333488ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3335fd59a867SMatthew G. Knepley     PetscSection s;
3336fd59a867SMatthew G. Knepley 
3337fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3338fd59a867SMatthew 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");
3339fd59a867SMatthew 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");
334015b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3341cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3342ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3343685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
334488ed4aceSMatthew G Knepley   }
334588ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
334688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
334788ed4aceSMatthew G Knepley }
334888ed4aceSMatthew G Knepley 
334988ed4aceSMatthew G Knepley #undef __FUNCT__
3350b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3351b21d0597SMatthew G Knepley /*@
3352b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3353b21d0597SMatthew G Knepley 
3354b21d0597SMatthew G Knepley   Input Parameters:
3355b21d0597SMatthew G Knepley + dm - The DM
33565080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3357b21d0597SMatthew G Knepley 
3358b21d0597SMatthew G Knepley   Level: intermediate
3359b21d0597SMatthew G Knepley 
3360b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3361b21d0597SMatthew G Knepley 
3362b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3363b21d0597SMatthew G Knepley @*/
33640adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
33650adebc6cSBarry Smith {
3366b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3367b21d0597SMatthew G Knepley 
3368b21d0597SMatthew G Knepley   PetscFunctionBegin;
3369b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33705080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33711d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3372b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3373b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3374507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3375f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3376507e4973SMatthew G. Knepley #endif
3377b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3378b21d0597SMatthew G Knepley }
3379b21d0597SMatthew G Knepley 
3380b21d0597SMatthew G Knepley #undef __FUNCT__
338188ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
338288ed4aceSMatthew G Knepley /*@
338388ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
338488ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
338588ed4aceSMatthew G Knepley 
338688ed4aceSMatthew G Knepley   Input Parameter:
338788ed4aceSMatthew G Knepley . dm - The DM
338888ed4aceSMatthew G Knepley 
338988ed4aceSMatthew G Knepley   Output Parameter:
339088ed4aceSMatthew G Knepley . sf - The PetscSF
339188ed4aceSMatthew G Knepley 
339288ed4aceSMatthew G Knepley   Level: intermediate
339388ed4aceSMatthew G Knepley 
339488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
339588ed4aceSMatthew G Knepley 
339688ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
339788ed4aceSMatthew G Knepley @*/
33980adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
33990adebc6cSBarry Smith {
340088ed4aceSMatthew G Knepley   PetscInt       nroots;
340188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
340288ed4aceSMatthew G Knepley 
340388ed4aceSMatthew G Knepley   PetscFunctionBegin;
340488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340588ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34060298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
340788ed4aceSMatthew G Knepley   if (nroots < 0) {
340888ed4aceSMatthew G Knepley     PetscSection section, gSection;
340988ed4aceSMatthew G Knepley 
341088ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
341131ea6d37SMatthew G Knepley     if (section) {
341288ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
341388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
341431ea6d37SMatthew G Knepley     } else {
34150298fd71SBarry Smith       *sf = NULL;
341631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
341731ea6d37SMatthew G Knepley     }
341888ed4aceSMatthew G Knepley   }
341988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
342088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
342188ed4aceSMatthew G Knepley }
342288ed4aceSMatthew G Knepley 
342388ed4aceSMatthew G Knepley #undef __FUNCT__
342488ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
342588ed4aceSMatthew G Knepley /*@
342688ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
342788ed4aceSMatthew G Knepley 
342888ed4aceSMatthew G Knepley   Input Parameters:
342988ed4aceSMatthew G Knepley + dm - The DM
343088ed4aceSMatthew G Knepley - sf - The PetscSF
343188ed4aceSMatthew G Knepley 
343288ed4aceSMatthew G Knepley   Level: intermediate
343388ed4aceSMatthew G Knepley 
343488ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
343588ed4aceSMatthew G Knepley 
343688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
343788ed4aceSMatthew G Knepley @*/
34380adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
34390adebc6cSBarry Smith {
344088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
344188ed4aceSMatthew G Knepley 
344288ed4aceSMatthew G Knepley   PetscFunctionBegin;
344388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
344488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
344588ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
344688ed4aceSMatthew G Knepley   dm->defaultSF = sf;
344788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
344888ed4aceSMatthew G Knepley }
344988ed4aceSMatthew G Knepley 
345088ed4aceSMatthew G Knepley #undef __FUNCT__
345188ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
345288ed4aceSMatthew G Knepley /*@C
345388ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
345488ed4aceSMatthew G Knepley   describing the data layout.
345588ed4aceSMatthew G Knepley 
345688ed4aceSMatthew G Knepley   Input Parameters:
345788ed4aceSMatthew G Knepley + dm - The DM
345888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
345988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
346088ed4aceSMatthew G Knepley 
346188ed4aceSMatthew G Knepley   Level: intermediate
346288ed4aceSMatthew G Knepley 
346388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
346488ed4aceSMatthew G Knepley @*/
346588ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
346688ed4aceSMatthew G Knepley {
346782f516ccSBarry Smith   MPI_Comm       comm;
346888ed4aceSMatthew G Knepley   PetscLayout    layout;
346988ed4aceSMatthew G Knepley   const PetscInt *ranges;
347088ed4aceSMatthew G Knepley   PetscInt       *local;
347188ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3472ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
347388ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
347488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
347588ed4aceSMatthew G Knepley 
347688ed4aceSMatthew G Knepley   PetscFunctionBegin;
347782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
347888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
347988ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
348088ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
348188ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
348288ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
348388ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
348488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
348588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
348688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
348788ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3488ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
34896636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
349088ed4aceSMatthew G Knepley 
34916636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
34926636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3493235fbf56SMatthew 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));
34946636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
349588ed4aceSMatthew G Knepley   }
3496785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3497785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
349888ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
34991f588964SMatthew G Knepley     const PetscInt *cind;
35006636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
350188ed4aceSMatthew G Knepley 
350288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
350388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
350488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
350588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
350688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35076636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
350888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35096636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35106636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35116636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3512057b4bcdSMatthew 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);
35136636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35146636e97aSMatthew G Knepley     }
351588ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
351688ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
351788ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
351888ed4aceSMatthew G Knepley     }
351988ed4aceSMatthew G Knepley     if (gdof < 0) {
35206636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
352188ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
352288ed4aceSMatthew G Knepley 
352305376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
352431d3f06eSJed Brown         if (r < 0) r = -(r+2);
352505376888SMatthew 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);
352688ed4aceSMatthew G Knepley         remote[l].rank  = r;
352788ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
352888ed4aceSMatthew G Knepley       }
352988ed4aceSMatthew G Knepley     } else {
35306636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
353188ed4aceSMatthew G Knepley         remote[l].rank  = rank;
353288ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
353388ed4aceSMatthew G Knepley       }
353488ed4aceSMatthew G Knepley     }
353588ed4aceSMatthew G Knepley   }
35366636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
353788ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
353888ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
353988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
354088ed4aceSMatthew G Knepley }
3541af122d2aSMatthew G Knepley 
3542af122d2aSMatthew G Knepley #undef __FUNCT__
3543b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3544b21d0597SMatthew G Knepley /*@
3545b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3546b21d0597SMatthew G Knepley 
3547b21d0597SMatthew G Knepley   Input Parameter:
3548b21d0597SMatthew G Knepley . dm - The DM
3549b21d0597SMatthew G Knepley 
3550b21d0597SMatthew G Knepley   Output Parameter:
3551b21d0597SMatthew G Knepley . sf - The PetscSF
3552b21d0597SMatthew G Knepley 
3553b21d0597SMatthew G Knepley   Level: intermediate
3554b21d0597SMatthew G Knepley 
3555b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3556b21d0597SMatthew G Knepley 
3557057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3558b21d0597SMatthew G Knepley @*/
35590adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
35600adebc6cSBarry Smith {
3561b21d0597SMatthew G Knepley   PetscFunctionBegin;
3562b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3563b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3564b21d0597SMatthew G Knepley   *sf = dm->sf;
3565b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3566b21d0597SMatthew G Knepley }
3567b21d0597SMatthew G Knepley 
3568b21d0597SMatthew G Knepley #undef __FUNCT__
3569057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3570057b4bcdSMatthew G Knepley /*@
3571057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3572057b4bcdSMatthew G Knepley 
3573057b4bcdSMatthew G Knepley   Input Parameters:
3574057b4bcdSMatthew G Knepley + dm - The DM
3575057b4bcdSMatthew G Knepley - sf - The PetscSF
3576057b4bcdSMatthew G Knepley 
3577057b4bcdSMatthew G Knepley   Level: intermediate
3578057b4bcdSMatthew G Knepley 
3579057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3580057b4bcdSMatthew G Knepley @*/
35810adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
35820adebc6cSBarry Smith {
3583057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3584057b4bcdSMatthew G Knepley 
3585057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3586057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3587057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3588057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3589057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3590057b4bcdSMatthew G Knepley   dm->sf = sf;
3591057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3592057b4bcdSMatthew G Knepley }
3593057b4bcdSMatthew G Knepley 
3594057b4bcdSMatthew G Knepley #undef __FUNCT__
35952764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
35962764a2aaSMatthew G. Knepley /*@
35972764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
35982764a2aaSMatthew G. Knepley 
35992764a2aaSMatthew G. Knepley   Input Parameter:
36002764a2aaSMatthew G. Knepley . dm - The DM
36012764a2aaSMatthew G. Knepley 
36022764a2aaSMatthew G. Knepley   Output Parameter:
36032764a2aaSMatthew G. Knepley . prob - The PetscDS
36042764a2aaSMatthew G. Knepley 
36052764a2aaSMatthew G. Knepley   Level: developer
36062764a2aaSMatthew G. Knepley 
36072764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36082764a2aaSMatthew G. Knepley @*/
36092764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3610af122d2aSMatthew G Knepley {
3611af122d2aSMatthew G Knepley   PetscFunctionBegin;
3612af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36130f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36140f21e855SMatthew G. Knepley   *prob = dm->prob;
36150f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36160f21e855SMatthew G. Knepley }
36170f21e855SMatthew G. Knepley 
36180f21e855SMatthew G. Knepley #undef __FUNCT__
36192764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36202764a2aaSMatthew G. Knepley /*@
36212764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36222764a2aaSMatthew G. Knepley 
36232764a2aaSMatthew G. Knepley   Input Parameters:
36242764a2aaSMatthew G. Knepley + dm - The DM
36252764a2aaSMatthew G. Knepley - prob - The PetscDS
36262764a2aaSMatthew G. Knepley 
36272764a2aaSMatthew G. Knepley   Level: developer
36282764a2aaSMatthew G. Knepley 
36292764a2aaSMatthew G. Knepley .seealso: DMGetDS()
36302764a2aaSMatthew G. Knepley @*/
36312764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
36320f21e855SMatthew G. Knepley {
36330f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36340f21e855SMatthew G. Knepley 
36350f21e855SMatthew G. Knepley   PetscFunctionBegin;
36360f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36372764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
36382764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
36390f21e855SMatthew G. Knepley   dm->prob = prob;
36400f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
36410f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36420f21e855SMatthew G. Knepley }
36430f21e855SMatthew G. Knepley 
36440f21e855SMatthew G. Knepley #undef __FUNCT__
36450f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
36460f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
36470f21e855SMatthew G. Knepley {
36480f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36490f21e855SMatthew G. Knepley 
36500f21e855SMatthew G. Knepley   PetscFunctionBegin;
36510f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36522764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3653af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3654af122d2aSMatthew G Knepley }
3655af122d2aSMatthew G Knepley 
3656af122d2aSMatthew G Knepley #undef __FUNCT__
3657af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3658af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3659af122d2aSMatthew G Knepley {
36600f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3661af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3662af122d2aSMatthew G Knepley 
3663af122d2aSMatthew G Knepley   PetscFunctionBegin;
3664af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36652764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
36660f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
36670f21e855SMatthew G. Knepley     PetscContainer obj;
36680f21e855SMatthew G. Knepley 
36690f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
36702764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
36710f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3672af122d2aSMatthew G Knepley   }
3673af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3674af122d2aSMatthew G Knepley }
3675af122d2aSMatthew G Knepley 
3676af122d2aSMatthew G Knepley #undef __FUNCT__
3677af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3678c1929be8SMatthew G. Knepley /*@
3679c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3680c1929be8SMatthew G. Knepley 
3681c1929be8SMatthew G. Knepley   Not collective
3682c1929be8SMatthew G. Knepley 
3683c1929be8SMatthew G. Knepley   Input Parameters:
3684c1929be8SMatthew G. Knepley + dm - The DM
3685c1929be8SMatthew G. Knepley - f  - The field number
3686c1929be8SMatthew G. Knepley 
3687c1929be8SMatthew G. Knepley   Output Parameter:
3688c1929be8SMatthew G. Knepley . field - The discretization object
3689c1929be8SMatthew G. Knepley 
3690c1929be8SMatthew G. Knepley   Level: developer
3691c1929be8SMatthew G. Knepley 
3692c1929be8SMatthew G. Knepley .seealso: DMSetField()
3693c1929be8SMatthew G. Knepley @*/
3694af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3695af122d2aSMatthew G Knepley {
36960f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36970f21e855SMatthew G. Knepley 
3698af122d2aSMatthew G Knepley   PetscFunctionBegin;
3699af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37002764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3701decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3702decb47aaSMatthew G. Knepley }
3703decb47aaSMatthew G. Knepley 
3704decb47aaSMatthew G. Knepley #undef __FUNCT__
3705decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3706c1929be8SMatthew G. Knepley /*@
3707c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3708c1929be8SMatthew G. Knepley 
3709c1929be8SMatthew G. Knepley   Logically collective on DM
3710c1929be8SMatthew G. Knepley 
3711c1929be8SMatthew G. Knepley   Input Parameters:
3712c1929be8SMatthew G. Knepley + dm - The DM
3713c1929be8SMatthew G. Knepley . f  - The field number
3714c1929be8SMatthew G. Knepley - field - The discretization object
3715c1929be8SMatthew G. Knepley 
3716c1929be8SMatthew G. Knepley   Level: developer
3717c1929be8SMatthew G. Knepley 
3718c1929be8SMatthew G. Knepley .seealso: DMGetField()
3719c1929be8SMatthew G. Knepley @*/
3720decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3721decb47aaSMatthew G. Knepley {
3722decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3723decb47aaSMatthew G. Knepley 
3724decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3725decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37262764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3727af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3728af122d2aSMatthew G Knepley }
37296636e97aSMatthew G Knepley 
37306636e97aSMatthew G Knepley #undef __FUNCT__
3731b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3732b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3733b64e0483SPeter Brune {
3734b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3735b64e0483SPeter Brune   PetscErrorCode ierr;
3736b64e0483SPeter Brune   Vec coords,ccoords;
37376dbf9973SLawrence Mitchell   Mat inject;
3738b64e0483SPeter Brune   PetscFunctionBegin;
3739b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3740b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3741b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3742b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3743b64e0483SPeter Brune   if (coords && !ccoords) {
3744b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
37456dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
37462adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
37476dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3748b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3749b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3750b64e0483SPeter Brune   }
3751b64e0483SPeter Brune   PetscFunctionReturn(0);
3752b64e0483SPeter Brune }
3753b64e0483SPeter Brune 
3754b64e0483SPeter Brune #undef __FUNCT__
375503dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
375603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
375703dadc2fSPeter Brune {
375803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
375903dadc2fSPeter Brune   PetscErrorCode ierr;
376003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
376103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
376203dadc2fSPeter Brune   PetscFunctionBegin;
376303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
376403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
376503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
376603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
376703dadc2fSPeter Brune   if (coords && !ccoords) {
376803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
376903dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
377003dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
377103dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
377203dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
377303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
377403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
377503dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
377603dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
377703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
377803dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
377903dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
378003dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
378103dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
378203dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
378303dadc2fSPeter Brune   }
378403dadc2fSPeter Brune   PetscFunctionReturn(0);
378503dadc2fSPeter Brune }
378603dadc2fSPeter Brune 
378703dadc2fSPeter Brune #undef __FUNCT__
3788c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3789c73cfb54SMatthew G. Knepley /*@
3790c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3791c73cfb54SMatthew G. Knepley 
3792c73cfb54SMatthew G. Knepley   Not collective
3793c73cfb54SMatthew G. Knepley 
3794c73cfb54SMatthew G. Knepley   Input Parameter:
3795c73cfb54SMatthew G. Knepley . dm - The DM
3796c73cfb54SMatthew G. Knepley 
3797c73cfb54SMatthew G. Knepley   Output Parameter:
3798c73cfb54SMatthew G. Knepley . dim - The topological dimension
3799c73cfb54SMatthew G. Knepley 
3800c73cfb54SMatthew G. Knepley   Level: beginner
3801c73cfb54SMatthew G. Knepley 
3802c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3803c73cfb54SMatthew G. Knepley @*/
3804c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3805c73cfb54SMatthew G. Knepley {
3806c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3807c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3808c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3809c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3810c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3811c73cfb54SMatthew G. Knepley }
3812c73cfb54SMatthew G. Knepley 
3813c73cfb54SMatthew G. Knepley #undef __FUNCT__
3814c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3815c73cfb54SMatthew G. Knepley /*@
3816c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3817c73cfb54SMatthew G. Knepley 
3818c73cfb54SMatthew G. Knepley   Collective on dm
3819c73cfb54SMatthew G. Knepley 
3820c73cfb54SMatthew G. Knepley   Input Parameters:
3821c73cfb54SMatthew G. Knepley + dm - The DM
3822c73cfb54SMatthew G. Knepley - dim - The topological dimension
3823c73cfb54SMatthew G. Knepley 
3824c73cfb54SMatthew G. Knepley   Level: beginner
3825c73cfb54SMatthew G. Knepley 
3826c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3827c73cfb54SMatthew G. Knepley @*/
3828c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3829c73cfb54SMatthew G. Knepley {
3830c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3831c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3832c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3833c73cfb54SMatthew G. Knepley   dm->dim = dim;
3834c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3835c73cfb54SMatthew G. Knepley }
3836c73cfb54SMatthew G. Knepley 
3837793f3fe5SMatthew G. Knepley #undef __FUNCT__
3838793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3839793f3fe5SMatthew G. Knepley /*@
3840793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3841793f3fe5SMatthew G. Knepley 
3842793f3fe5SMatthew G. Knepley   Collective on DM
3843793f3fe5SMatthew G. Knepley 
3844793f3fe5SMatthew G. Knepley   Input Parameters:
3845793f3fe5SMatthew G. Knepley + dm - the DM
3846793f3fe5SMatthew G. Knepley - dim - the dimension
3847793f3fe5SMatthew G. Knepley 
3848793f3fe5SMatthew G. Knepley   Output Parameters:
3849793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3850793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3851793f3fe5SMatthew G. Knepley 
3852793f3fe5SMatthew G. Knepley   Note:
3853793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3854793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3855793f3fe5SMatthew G. Knepley   then the interval is empty.
3856793f3fe5SMatthew G. Knepley 
3857793f3fe5SMatthew G. Knepley   Level: intermediate
3858793f3fe5SMatthew G. Knepley 
3859793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3860793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3861793f3fe5SMatthew G. Knepley @*/
3862793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3863793f3fe5SMatthew G. Knepley {
3864793f3fe5SMatthew G. Knepley   PetscInt       d;
3865793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3866793f3fe5SMatthew G. Knepley 
3867793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3868793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3869793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3870793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3871793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3872793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3873793f3fe5SMatthew G. Knepley }
3874793f3fe5SMatthew G. Knepley 
3875793f3fe5SMatthew G. Knepley #undef __FUNCT__
38766636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
38776636e97aSMatthew G Knepley /*@
38786636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
38796636e97aSMatthew G Knepley 
38806636e97aSMatthew G Knepley   Collective on DM
38816636e97aSMatthew G Knepley 
38826636e97aSMatthew G Knepley   Input Parameters:
38836636e97aSMatthew G Knepley + dm - the DM
38846636e97aSMatthew G Knepley - c - coordinate vector
38856636e97aSMatthew G Knepley 
38866636e97aSMatthew G Knepley   Note:
38876636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
38886636e97aSMatthew G Knepley 
38896636e97aSMatthew G Knepley   Level: intermediate
38906636e97aSMatthew G Knepley 
38916636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38926636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
38936636e97aSMatthew G Knepley @*/
38946636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
38956636e97aSMatthew G Knepley {
38966636e97aSMatthew G Knepley   PetscErrorCode ierr;
38976636e97aSMatthew G Knepley 
38986636e97aSMatthew G Knepley   PetscFunctionBegin;
38996636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39006636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39016636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39026636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39036636e97aSMatthew G Knepley   dm->coordinates = c;
39046636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3905b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
390603dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39076636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39086636e97aSMatthew G Knepley }
39096636e97aSMatthew G Knepley 
39106636e97aSMatthew G Knepley #undef __FUNCT__
39116636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39126636e97aSMatthew G Knepley /*@
39136636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39146636e97aSMatthew G Knepley 
39156636e97aSMatthew G Knepley   Collective on DM
39166636e97aSMatthew G Knepley 
39176636e97aSMatthew G Knepley    Input Parameters:
39186636e97aSMatthew G Knepley +  dm - the DM
39196636e97aSMatthew G Knepley -  c - coordinate vector
39206636e97aSMatthew G Knepley 
39216636e97aSMatthew G Knepley   Note:
39226636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39236636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39246636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39256636e97aSMatthew G Knepley 
39266636e97aSMatthew G Knepley   Level: intermediate
39276636e97aSMatthew G Knepley 
39286636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39296636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39306636e97aSMatthew G Knepley @*/
39316636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
39326636e97aSMatthew G Knepley {
39336636e97aSMatthew G Knepley   PetscErrorCode ierr;
39346636e97aSMatthew G Knepley 
39356636e97aSMatthew G Knepley   PetscFunctionBegin;
39366636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39376636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39386636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39396636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
39408865f1eaSKarl Rupp 
39416636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
39428865f1eaSKarl Rupp 
39436636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39446636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39456636e97aSMatthew G Knepley }
39466636e97aSMatthew G Knepley 
39476636e97aSMatthew G Knepley #undef __FUNCT__
39486636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
39496636e97aSMatthew G Knepley /*@
39506636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
39516636e97aSMatthew G Knepley 
39526636e97aSMatthew G Knepley   Not Collective
39536636e97aSMatthew G Knepley 
39546636e97aSMatthew G Knepley   Input Parameter:
39556636e97aSMatthew G Knepley . dm - the DM
39566636e97aSMatthew G Knepley 
39576636e97aSMatthew G Knepley   Output Parameter:
39586636e97aSMatthew G Knepley . c - global coordinate vector
39596636e97aSMatthew G Knepley 
39606636e97aSMatthew G Knepley   Note:
39616636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
39626636e97aSMatthew G Knepley 
39636636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
39646636e97aSMatthew G Knepley 
39656636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
39666636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
39676636e97aSMatthew G Knepley 
39686636e97aSMatthew G Knepley   Level: intermediate
39696636e97aSMatthew G Knepley 
39706636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39716636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
39726636e97aSMatthew G Knepley @*/
39736636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
39746636e97aSMatthew G Knepley {
39756636e97aSMatthew G Knepley   PetscErrorCode ierr;
39766636e97aSMatthew G Knepley 
39776636e97aSMatthew G Knepley   PetscFunctionBegin;
39786636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39796636e97aSMatthew G Knepley   PetscValidPointer(c,2);
39801f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
39810298fd71SBarry Smith     DM cdm = NULL;
39826636e97aSMatthew G Knepley 
39836636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
39846636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
39856636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
39866636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
39876636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
39886636e97aSMatthew G Knepley   }
39896636e97aSMatthew G Knepley   *c = dm->coordinates;
39906636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39916636e97aSMatthew G Knepley }
39926636e97aSMatthew G Knepley 
39936636e97aSMatthew G Knepley #undef __FUNCT__
39946636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
39956636e97aSMatthew G Knepley /*@
39966636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
39976636e97aSMatthew G Knepley 
39986636e97aSMatthew G Knepley   Collective on DM
39996636e97aSMatthew G Knepley 
40006636e97aSMatthew G Knepley   Input Parameter:
40016636e97aSMatthew G Knepley . dm - the DM
40026636e97aSMatthew G Knepley 
40036636e97aSMatthew G Knepley   Output Parameter:
40046636e97aSMatthew G Knepley . c - coordinate vector
40056636e97aSMatthew G Knepley 
40066636e97aSMatthew G Knepley   Note:
40076636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40086636e97aSMatthew G Knepley 
40096636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40106636e97aSMatthew G Knepley 
40116636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40126636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40136636e97aSMatthew G Knepley 
40146636e97aSMatthew G Knepley   Level: intermediate
40156636e97aSMatthew G Knepley 
40166636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40176636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40186636e97aSMatthew G Knepley @*/
40196636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40206636e97aSMatthew G Knepley {
40216636e97aSMatthew G Knepley   PetscErrorCode ierr;
40226636e97aSMatthew G Knepley 
40236636e97aSMatthew G Knepley   PetscFunctionBegin;
40246636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40256636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40261f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40270298fd71SBarry Smith     DM cdm = NULL;
40286636e97aSMatthew G Knepley 
40296636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40306636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
40316636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
40326636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40336636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40346636e97aSMatthew G Knepley   }
40356636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
40366636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40376636e97aSMatthew G Knepley }
40386636e97aSMatthew G Knepley 
40396636e97aSMatthew G Knepley #undef __FUNCT__
40406636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
40416636e97aSMatthew G Knepley /*@
40421cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
40436636e97aSMatthew G Knepley 
40446636e97aSMatthew G Knepley   Collective on DM
40456636e97aSMatthew G Knepley 
40466636e97aSMatthew G Knepley   Input Parameter:
40476636e97aSMatthew G Knepley . dm - the DM
40486636e97aSMatthew G Knepley 
40496636e97aSMatthew G Knepley   Output Parameter:
40506636e97aSMatthew G Knepley . cdm - coordinate DM
40516636e97aSMatthew G Knepley 
40526636e97aSMatthew G Knepley   Level: intermediate
40536636e97aSMatthew G Knepley 
40546636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40551cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40566636e97aSMatthew G Knepley @*/
40576636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
40586636e97aSMatthew G Knepley {
40596636e97aSMatthew G Knepley   PetscErrorCode ierr;
40606636e97aSMatthew G Knepley 
40616636e97aSMatthew G Knepley   PetscFunctionBegin;
40626636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40636636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
40646636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
406582f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
40666636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
40676636e97aSMatthew G Knepley   }
40686636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
40696636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40706636e97aSMatthew G Knepley }
4071e87bb0d3SMatthew G Knepley 
4072e87bb0d3SMatthew G Knepley #undef __FUNCT__
40731cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
40741cfe2091SMatthew G. Knepley /*@
40751cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
40761cfe2091SMatthew G. Knepley 
40771cfe2091SMatthew G. Knepley   Logically Collective on DM
40781cfe2091SMatthew G. Knepley 
40791cfe2091SMatthew G. Knepley   Input Parameters:
40801cfe2091SMatthew G. Knepley + dm - the DM
40811cfe2091SMatthew G. Knepley - cdm - coordinate DM
40821cfe2091SMatthew G. Knepley 
40831cfe2091SMatthew G. Knepley   Level: intermediate
40841cfe2091SMatthew G. Knepley 
40851cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40861cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40871cfe2091SMatthew G. Knepley @*/
40881cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
40891cfe2091SMatthew G. Knepley {
40901cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
40911cfe2091SMatthew G. Knepley 
40921cfe2091SMatthew G. Knepley   PetscFunctionBegin;
40931cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40941cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
40951cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
40961cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
40971cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
40981cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
40991cfe2091SMatthew G. Knepley }
41001cfe2091SMatthew G. Knepley 
41011cfe2091SMatthew G. Knepley #undef __FUNCT__
410246e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
410346e270d4SMatthew G. Knepley /*@
410446e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
410546e270d4SMatthew G. Knepley 
410646e270d4SMatthew G. Knepley   Not Collective
410746e270d4SMatthew G. Knepley 
410846e270d4SMatthew G. Knepley   Input Parameter:
410946e270d4SMatthew G. Knepley . dm - The DM object
411046e270d4SMatthew G. Knepley 
411146e270d4SMatthew G. Knepley   Output Parameter:
411246e270d4SMatthew G. Knepley . dim - The embedding dimension
411346e270d4SMatthew G. Knepley 
411446e270d4SMatthew G. Knepley   Level: intermediate
411546e270d4SMatthew G. Knepley 
411646e270d4SMatthew G. Knepley .keywords: mesh, coordinates
411746e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
411846e270d4SMatthew G. Knepley @*/
411946e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
412046e270d4SMatthew G. Knepley {
412146e270d4SMatthew G. Knepley   PetscFunctionBegin;
412246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
412346e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41249a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41259a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41269a9a41abSToby Isaac   }
412746e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
412846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
412946e270d4SMatthew G. Knepley }
413046e270d4SMatthew G. Knepley 
413146e270d4SMatthew G. Knepley #undef __FUNCT__
413246e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
413346e270d4SMatthew G. Knepley /*@
413446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
413546e270d4SMatthew G. Knepley 
413646e270d4SMatthew G. Knepley   Not Collective
413746e270d4SMatthew G. Knepley 
413846e270d4SMatthew G. Knepley   Input Parameters:
413946e270d4SMatthew G. Knepley + dm  - The DM object
414046e270d4SMatthew G. Knepley - dim - The embedding dimension
414146e270d4SMatthew G. Knepley 
414246e270d4SMatthew G. Knepley   Level: intermediate
414346e270d4SMatthew G. Knepley 
414446e270d4SMatthew G. Knepley .keywords: mesh, coordinates
414546e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
414646e270d4SMatthew G. Knepley @*/
414746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
414846e270d4SMatthew G. Knepley {
414946e270d4SMatthew G. Knepley   PetscFunctionBegin;
415046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
415146e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
415246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
415346e270d4SMatthew G. Knepley }
415446e270d4SMatthew G. Knepley 
415546e270d4SMatthew G. Knepley #undef __FUNCT__
4156e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4157e8abe2deSMatthew G. Knepley /*@
4158e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4159e8abe2deSMatthew G. Knepley 
4160e8abe2deSMatthew G. Knepley   Not Collective
4161e8abe2deSMatthew G. Knepley 
4162e8abe2deSMatthew G. Knepley   Input Parameter:
4163e8abe2deSMatthew G. Knepley . dm - The DM object
4164e8abe2deSMatthew G. Knepley 
4165e8abe2deSMatthew G. Knepley   Output Parameter:
4166e8abe2deSMatthew G. Knepley . section - The PetscSection object
4167e8abe2deSMatthew G. Knepley 
4168e8abe2deSMatthew G. Knepley   Level: intermediate
4169e8abe2deSMatthew G. Knepley 
4170e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4171e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4172e8abe2deSMatthew G. Knepley @*/
4173e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4174e8abe2deSMatthew G. Knepley {
4175e8abe2deSMatthew G. Knepley   DM             cdm;
4176e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4177e8abe2deSMatthew G. Knepley 
4178e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4179e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4180e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4181e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4182e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4183e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4184e8abe2deSMatthew G. Knepley }
4185e8abe2deSMatthew G. Knepley 
4186e8abe2deSMatthew G. Knepley #undef __FUNCT__
4187e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4188e8abe2deSMatthew G. Knepley /*@
4189e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4190e8abe2deSMatthew G. Knepley 
4191e8abe2deSMatthew G. Knepley   Not Collective
4192e8abe2deSMatthew G. Knepley 
4193e8abe2deSMatthew G. Knepley   Input Parameters:
4194e8abe2deSMatthew G. Knepley + dm      - The DM object
419546e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4196e8abe2deSMatthew G. Knepley - section - The PetscSection object
4197e8abe2deSMatthew G. Knepley 
4198e8abe2deSMatthew G. Knepley   Level: intermediate
4199e8abe2deSMatthew G. Knepley 
4200e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4201e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4202e8abe2deSMatthew G. Knepley @*/
420346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4204e8abe2deSMatthew G. Knepley {
4205e8abe2deSMatthew G. Knepley   DM             cdm;
4206e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4207e8abe2deSMatthew G. Knepley 
4208e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4209e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
421046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4211e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4212e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
421346e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
421446e270d4SMatthew G. Knepley     PetscInt d = dim;
421546e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
421646e270d4SMatthew G. Knepley 
421746e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
421846e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
421946e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
422046e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
422146e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
422246e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
422346e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
422446e270d4SMatthew G. Knepley     }
422546e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
422646e270d4SMatthew G. Knepley   }
4227e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4228e8abe2deSMatthew G. Knepley }
4229e8abe2deSMatthew G. Knepley 
4230e8abe2deSMatthew G. Knepley #undef __FUNCT__
4231c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
42325dc8c3f7SMatthew G. Knepley /*@C
42335dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42345dc8c3f7SMatthew G. Knepley 
42355dc8c3f7SMatthew G. Knepley   Input Parameters:
42365dc8c3f7SMatthew G. Knepley + dm      - The DM object
42375dc8c3f7SMatthew 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
42385dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42395dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42405dc8c3f7SMatthew G. Knepley 
42415dc8c3f7SMatthew G. Knepley   Level: developer
42425dc8c3f7SMatthew G. Knepley 
42435dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42445dc8c3f7SMatthew G. Knepley @*/
42455dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4246c6b900c6SMatthew G. Knepley {
4247c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4248c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4249c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4250c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
42515dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4252c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4253c6b900c6SMatthew G. Knepley }
4254c6b900c6SMatthew G. Knepley 
4255c6b900c6SMatthew G. Knepley #undef __FUNCT__
4256c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
42575dc8c3f7SMatthew G. Knepley /*@C
42585dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42595dc8c3f7SMatthew G. Knepley 
42605dc8c3f7SMatthew G. Knepley   Input Parameters:
42615dc8c3f7SMatthew G. Knepley + dm      - The DM object
42625dc8c3f7SMatthew 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
42635dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42645dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42655dc8c3f7SMatthew G. Knepley 
42665dc8c3f7SMatthew G. Knepley   Level: developer
42675dc8c3f7SMatthew G. Knepley 
42685dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42695dc8c3f7SMatthew G. Knepley @*/
42705dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4271c6b900c6SMatthew G. Knepley {
4272c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4273c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4274c6b900c6SMatthew G. Knepley 
4275c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4276c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42775dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
42785dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
42795dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
42805dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
42815dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4282c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4283c6b900c6SMatthew G. Knepley }
4284c6b900c6SMatthew G. Knepley 
4285c6b900c6SMatthew G. Knepley #undef __FUNCT__
4286e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4287e87bb0d3SMatthew G Knepley /*@
4288e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4289e87bb0d3SMatthew G Knepley 
4290e87bb0d3SMatthew G Knepley   Not collective
4291e87bb0d3SMatthew G Knepley 
4292e87bb0d3SMatthew G Knepley   Input Parameters:
4293e87bb0d3SMatthew G Knepley + dm - The DM
4294e87bb0d3SMatthew G Knepley - v - The Vec of points
4295e87bb0d3SMatthew G Knepley 
429661e3bb9bSMatthew G Knepley   Output Parameter:
4297e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4298e87bb0d3SMatthew G Knepley 
4299e87bb0d3SMatthew G Knepley   Level: developer
430061e3bb9bSMatthew G Knepley 
430161e3bb9bSMatthew G Knepley .keywords: point location, mesh
430261e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
430361e3bb9bSMatthew G Knepley @*/
4304e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4305e87bb0d3SMatthew G Knepley {
4306735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4307735aa83eSMatthew G Knepley 
4308e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4309e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4310e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4311e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
431247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4313735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4314735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
431582f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
431647a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4317e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4318e87bb0d3SMatthew G Knepley }
431914f150ffSMatthew G. Knepley 
432014f150ffSMatthew G. Knepley #undef __FUNCT__
432114f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4322f4d763aaSMatthew G. Knepley /*@
4323f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4324f4d763aaSMatthew G. Knepley 
4325f4d763aaSMatthew G. Knepley   Input Parameter:
4326f4d763aaSMatthew G. Knepley . dm - The original DM
4327f4d763aaSMatthew G. Knepley 
4328f4d763aaSMatthew G. Knepley   Output Parameter:
4329f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4330f4d763aaSMatthew G. Knepley 
4331f4d763aaSMatthew G. Knepley   Level: intermediate
4332f4d763aaSMatthew G. Knepley 
4333f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4334f4d763aaSMatthew G. Knepley @*/
433514f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
433614f150ffSMatthew G. Knepley {
4337c26acbdeSMatthew G. Knepley   PetscSection   section;
4338c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
433914f150ffSMatthew G. Knepley   PetscErrorCode ierr;
434014f150ffSMatthew G. Knepley 
434114f150ffSMatthew G. Knepley   PetscFunctionBegin;
434214f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
434314f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4344c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4345c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4346c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4347c26acbdeSMatthew G. Knepley     *odm = dm;
4348c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4349c26acbdeSMatthew G. Knepley   }
435014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4351c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
435214f150ffSMatthew G. Knepley     PetscSF      sf;
435314f150ffSMatthew G. Knepley 
435414f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
435514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
435614f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
435714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
435814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
435915b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
436014f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
436114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
436214f150ffSMatthew G. Knepley   }
436314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
436414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
436514f150ffSMatthew G. Knepley }
4366f4d763aaSMatthew G. Knepley 
4367f4d763aaSMatthew G. Knepley #undef __FUNCT__
4368f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4369f4d763aaSMatthew G. Knepley /*@
4370cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4371f4d763aaSMatthew G. Knepley 
4372f4d763aaSMatthew G. Knepley   Input Parameter:
4373f4d763aaSMatthew G. Knepley . dm - The original DM
4374f4d763aaSMatthew G. Knepley 
4375cdb7a50dSMatthew G. Knepley   Output Parameters:
4376cdb7a50dSMatthew G. Knepley + num - The output sequence number
4377cdb7a50dSMatthew G. Knepley - val - The output sequence value
4378f4d763aaSMatthew G. Knepley 
4379f4d763aaSMatthew G. Knepley   Level: intermediate
4380f4d763aaSMatthew G. Knepley 
4381f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4382f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4383f4d763aaSMatthew G. Knepley 
4384f4d763aaSMatthew G. Knepley .seealso: VecView()
4385f4d763aaSMatthew G. Knepley @*/
4386cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4387f4d763aaSMatthew G. Knepley {
4388f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4389f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4390cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4391cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4392f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4393f4d763aaSMatthew G. Knepley }
4394f4d763aaSMatthew G. Knepley 
4395f4d763aaSMatthew G. Knepley #undef __FUNCT__
4396f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4397f4d763aaSMatthew G. Knepley /*@
4398cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4399f4d763aaSMatthew G. Knepley 
4400f4d763aaSMatthew G. Knepley   Input Parameters:
4401f4d763aaSMatthew G. Knepley + dm - The original DM
4402cdb7a50dSMatthew G. Knepley . num - The output sequence number
4403cdb7a50dSMatthew G. Knepley - val - The output sequence value
4404f4d763aaSMatthew G. Knepley 
4405f4d763aaSMatthew G. Knepley   Level: intermediate
4406f4d763aaSMatthew G. Knepley 
4407f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4408f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4409f4d763aaSMatthew G. Knepley 
4410f4d763aaSMatthew G. Knepley .seealso: VecView()
4411f4d763aaSMatthew G. Knepley @*/
4412cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4413f4d763aaSMatthew G. Knepley {
4414f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4415f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4416f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4417cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4418cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4419cdb7a50dSMatthew G. Knepley }
4420cdb7a50dSMatthew G. Knepley 
4421cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4422cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4423cdb7a50dSMatthew G. Knepley /*@C
4424cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4425cdb7a50dSMatthew G. Knepley 
4426cdb7a50dSMatthew G. Knepley   Input Parameters:
4427cdb7a50dSMatthew G. Knepley + dm   - The original DM
4428cdb7a50dSMatthew G. Knepley . name - The sequence name
4429cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4430cdb7a50dSMatthew G. Knepley 
4431cdb7a50dSMatthew G. Knepley   Output Parameter:
4432cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4433cdb7a50dSMatthew G. Knepley 
4434cdb7a50dSMatthew G. Knepley   Level: intermediate
4435cdb7a50dSMatthew G. Knepley 
4436cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4437cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4438cdb7a50dSMatthew G. Knepley 
4439cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4440cdb7a50dSMatthew G. Knepley @*/
4441cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4442cdb7a50dSMatthew G. Knepley {
4443cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4444cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4445cdb7a50dSMatthew G. Knepley 
4446cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4447cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4448cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4449cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4450cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4451cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4452cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4453cdb7a50dSMatthew G. Knepley     PetscScalar value;
4454cdb7a50dSMatthew G. Knepley 
4455cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
44564aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4457cdb7a50dSMatthew G. Knepley #endif
4458cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4459f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4460f4d763aaSMatthew G. Knepley }
44618e4ac7eaSMatthew G. Knepley 
44628e4ac7eaSMatthew G. Knepley #undef __FUNCT__
44638e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
44648e4ac7eaSMatthew G. Knepley /*@
44658e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
44668e4ac7eaSMatthew G. Knepley 
44678e4ac7eaSMatthew G. Knepley   Not collective
44688e4ac7eaSMatthew G. Knepley 
44698e4ac7eaSMatthew G. Knepley   Input Parameter:
44708e4ac7eaSMatthew G. Knepley . dm - The DM
44718e4ac7eaSMatthew G. Knepley 
44728e4ac7eaSMatthew G. Knepley   Output Parameter:
44738e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
44748e4ac7eaSMatthew G. Knepley 
44758e4ac7eaSMatthew G. Knepley   Level: beginner
44768e4ac7eaSMatthew G. Knepley 
44778e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
44788e4ac7eaSMatthew G. Knepley @*/
44798e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
44808e4ac7eaSMatthew G. Knepley {
44818e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
44828e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44838e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
44848e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
44858e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
44868e4ac7eaSMatthew G. Knepley }
44878e4ac7eaSMatthew G. Knepley 
44888e4ac7eaSMatthew G. Knepley #undef __FUNCT__
44898e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
44908e4ac7eaSMatthew G. Knepley /*@
44918e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
44928e4ac7eaSMatthew G. Knepley 
44938e4ac7eaSMatthew G. Knepley   Collective on dm
44948e4ac7eaSMatthew G. Knepley 
44958e4ac7eaSMatthew G. Knepley   Input Parameters:
44968e4ac7eaSMatthew G. Knepley + dm - The DM
44978e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
44988e4ac7eaSMatthew G. Knepley 
44998e4ac7eaSMatthew G. Knepley   Level: beginner
45008e4ac7eaSMatthew G. Knepley 
45018e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
45028e4ac7eaSMatthew G. Knepley @*/
45038e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
45048e4ac7eaSMatthew G. Knepley {
45058e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45068e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45078e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45088e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45098e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45108e4ac7eaSMatthew G. Knepley }
4511