xref: /petsc/src/dm/interface/dm.c (revision 8472ad0fefe209573edbd759cdaa5f83149bedba)
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;
6f089877aSRichard Tran Mills PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal;
767a56275SMatthew G Knepley 
8bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
9bff4a2f0SMatthew G. Knepley 
1047c6ae99SBarry Smith #undef __FUNCT__
11a4121054SBarry Smith #define __FUNCT__ "DMCreate"
12a4121054SBarry Smith /*@
13de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
14a4121054SBarry Smith 
15a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
16a4121054SBarry Smith    error when you try to use the vector.
17a4121054SBarry Smith 
18a4121054SBarry Smith   Collective on MPI_Comm
19a4121054SBarry Smith 
20a4121054SBarry Smith   Input Parameter:
21a4121054SBarry Smith . comm - The communicator for the DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Output Parameter:
24a4121054SBarry Smith . dm - The DM object
25a4121054SBarry Smith 
26a4121054SBarry Smith   Level: beginner
27a4121054SBarry Smith 
28*8472ad0fSDave 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 
134*8472ad0fSDave 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 
145*8472ad0fSDave 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 
163*8472ad0fSDave 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 
173*8472ad0fSDave 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 /*@
456*8472ad0fSDave 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
701aa219208SBarry Smith     DMView - Views a vector packer or DMDA.
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 
70947c6ae99SBarry Smith     Level: developer
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 /*@
743*8472ad0fSDave 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 /*@
771*8472ad0fSDave 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 /*@
887*8472ad0fSDave 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);
91825296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
91947c6ae99SBarry Smith   PetscFunctionReturn(0);
92047c6ae99SBarry Smith }
92147c6ae99SBarry Smith 
92247c6ae99SBarry Smith #undef __FUNCT__
923e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
92447c6ae99SBarry Smith /*@
925*8472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
92647c6ae99SBarry Smith 
92747c6ae99SBarry Smith     Collective on DM
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith     Input Parameter:
93047c6ae99SBarry Smith +   dm1 - the DM object
93147c6ae99SBarry Smith -   dm2 - the second, finer DM object
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith     Output Parameter:
9346dbf9973SLawrence Mitchell .   mat - the injection
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith     Level: developer
93747c6ae99SBarry Smith 
93885afcc9aSBarry 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
93985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
94085afcc9aSBarry Smith 
941e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith @*/
9446dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
94547c6ae99SBarry Smith {
94647c6ae99SBarry Smith   PetscErrorCode ierr;
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith   PetscFunctionBegin;
949171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
950171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
9516dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
95247c6ae99SBarry Smith   PetscFunctionReturn(0);
95347c6ae99SBarry Smith }
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith #undef __FUNCT__
956e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
957b412c318SBarry Smith /*@
958b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
95947c6ae99SBarry Smith 
96047c6ae99SBarry Smith     Collective on DM
96147c6ae99SBarry Smith 
96247c6ae99SBarry Smith     Input Parameter:
96347c6ae99SBarry Smith +   dm - the DM object
964b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith     Output Parameter:
96747c6ae99SBarry Smith .   coloring - the coloring
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith     Level: developer
97047c6ae99SBarry Smith 
971b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
97247c6ae99SBarry Smith 
973aab9d709SJed Brown @*/
974b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
97547c6ae99SBarry Smith {
97647c6ae99SBarry Smith   PetscErrorCode ierr;
97747c6ae99SBarry Smith 
97847c6ae99SBarry Smith   PetscFunctionBegin;
979171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
980ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
981b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
98247c6ae99SBarry Smith   PetscFunctionReturn(0);
98347c6ae99SBarry Smith }
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith #undef __FUNCT__
986950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
987b412c318SBarry Smith /*@
988*8472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
98947c6ae99SBarry Smith 
99047c6ae99SBarry Smith     Collective on DM
99147c6ae99SBarry Smith 
99247c6ae99SBarry Smith     Input Parameter:
993b412c318SBarry Smith .   dm - the DM object
99447c6ae99SBarry Smith 
99547c6ae99SBarry Smith     Output Parameter:
99647c6ae99SBarry Smith .   mat - the empty Jacobian
99747c6ae99SBarry Smith 
998073dac72SJed Brown     Level: beginner
99947c6ae99SBarry Smith 
100094013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
100194013140SBarry Smith        do not need to do it yourself.
100294013140SBarry Smith 
100394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1004aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
100594013140SBarry Smith 
100694013140SBarry 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
100794013140SBarry Smith        internally by PETSc.
100894013140SBarry Smith 
100994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1010aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
101194013140SBarry Smith 
1012b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
101347c6ae99SBarry Smith 
1014aab9d709SJed Brown @*/
1015b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
101647c6ae99SBarry Smith {
101747c6ae99SBarry Smith   PetscErrorCode ierr;
101847c6ae99SBarry Smith 
101947c6ae99SBarry Smith   PetscFunctionBegin;
1020171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1021607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1022c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1023c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1024b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
102547c6ae99SBarry Smith   PetscFunctionReturn(0);
102647c6ae99SBarry Smith }
102747c6ae99SBarry Smith 
102847c6ae99SBarry Smith #undef __FUNCT__
1029732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1030732e2eb9SMatthew G Knepley /*@
1031950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1032732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1033732e2eb9SMatthew G Knepley 
1034*8472ad0fSDave May   Logically Collective on DM
1035732e2eb9SMatthew G Knepley 
1036732e2eb9SMatthew G Knepley   Input Parameter:
1037732e2eb9SMatthew G Knepley + dm - the DM
1038732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1039732e2eb9SMatthew G Knepley 
1040732e2eb9SMatthew G Knepley   Level: developer
1041950540a4SJed Brown .seealso DMCreateMatrix()
1042732e2eb9SMatthew G Knepley @*/
1043732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1044732e2eb9SMatthew G Knepley {
1045732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1046732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1047732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1048732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1049732e2eb9SMatthew G Knepley }
1050732e2eb9SMatthew G Knepley 
1051732e2eb9SMatthew G Knepley #undef __FUNCT__
1052a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1053a89ea682SMatthew G Knepley /*@C
1054aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1055a89ea682SMatthew G Knepley 
1056a89ea682SMatthew G Knepley   Not Collective
1057a89ea682SMatthew G Knepley 
1058a89ea682SMatthew G Knepley   Input Parameters:
1059a89ea682SMatthew G Knepley + dm - the DM object
1060aa1993deSMatthew G Knepley . count - The minium size
1061aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1062a89ea682SMatthew G Knepley 
1063a89ea682SMatthew G Knepley   Output Parameter:
1064a89ea682SMatthew G Knepley . array - the work array
1065a89ea682SMatthew G Knepley 
1066a89ea682SMatthew G Knepley   Level: developer
1067a89ea682SMatthew G Knepley 
1068a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1069a89ea682SMatthew G Knepley @*/
1070aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1071a89ea682SMatthew G Knepley {
1072a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1073aa1993deSMatthew G Knepley   DMWorkLink     link;
1074854ce69bSBarry Smith   size_t         dsize;
1075a89ea682SMatthew G Knepley 
1076a89ea682SMatthew G Knepley   PetscFunctionBegin;
1077a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1078aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1079aa1993deSMatthew G Knepley   if (dm->workin) {
1080aa1993deSMatthew G Knepley     link       = dm->workin;
1081aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1082aa1993deSMatthew G Knepley   } else {
1083b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1084a89ea682SMatthew G Knepley   }
1085854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1086854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1087aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1088854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1089854ce69bSBarry Smith     link->bytes = dsize*count;
1090aa1993deSMatthew G Knepley   }
1091aa1993deSMatthew G Knepley   link->next   = dm->workout;
1092aa1993deSMatthew G Knepley   dm->workout  = link;
1093aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1094a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1095a89ea682SMatthew G Knepley }
1096a89ea682SMatthew G Knepley 
1097aa1993deSMatthew G Knepley #undef __FUNCT__
1098aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1099aa1993deSMatthew G Knepley /*@C
1100aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1101aa1993deSMatthew G Knepley 
1102aa1993deSMatthew G Knepley   Not Collective
1103aa1993deSMatthew G Knepley 
1104aa1993deSMatthew G Knepley   Input Parameters:
1105aa1993deSMatthew G Knepley + dm - the DM object
1106aa1993deSMatthew G Knepley . count - The minium size
1107aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1108aa1993deSMatthew G Knepley 
1109aa1993deSMatthew G Knepley   Output Parameter:
1110aa1993deSMatthew G Knepley . array - the work array
1111aa1993deSMatthew G Knepley 
1112aa1993deSMatthew G Knepley   Level: developer
1113aa1993deSMatthew G Knepley 
1114aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1115aa1993deSMatthew G Knepley @*/
1116aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1117aa1993deSMatthew G Knepley {
1118aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1119aa1993deSMatthew G Knepley 
1120aa1993deSMatthew G Knepley   PetscFunctionBegin;
1121aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1122aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1123aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1124aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1125aa1993deSMatthew G Knepley       *p           = link->next;
1126aa1993deSMatthew G Knepley       link->next   = dm->workin;
1127aa1993deSMatthew G Knepley       dm->workin   = link;
11280298fd71SBarry Smith       *(void**)mem = NULL;
1129aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1130aa1993deSMatthew G Knepley     }
1131aa1993deSMatthew G Knepley   }
1132aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1133aa1993deSMatthew G Knepley }
1134e7c4fc90SDmitry Karpeev 
1135e7c4fc90SDmitry Karpeev #undef __FUNCT__
1136435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1137435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1138435a35e8SMatthew G Knepley {
1139435a35e8SMatthew G Knepley   PetscFunctionBegin;
1140435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
114182f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1142435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1143435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1144435a35e8SMatthew G Knepley }
1145435a35e8SMatthew G Knepley 
1146435a35e8SMatthew G Knepley #undef __FUNCT__
11474d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
11484f3b5142SJed Brown /*@C
11494d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
11504d343eeaSMatthew G Knepley 
11514d343eeaSMatthew G Knepley   Not collective
11524d343eeaSMatthew G Knepley 
11534d343eeaSMatthew G Knepley   Input Parameter:
11544d343eeaSMatthew G Knepley . dm - the DM object
11554d343eeaSMatthew G Knepley 
11564d343eeaSMatthew G Knepley   Output Parameters:
11570298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
11580298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
11590298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
11604d343eeaSMatthew G Knepley 
11614d343eeaSMatthew G Knepley   Level: intermediate
11624d343eeaSMatthew G Knepley 
116321c9b008SJed Brown   Notes:
116421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
116521c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
116621c9b008SJed Brown   PetscFree().
116721c9b008SJed Brown 
11684d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11694d343eeaSMatthew G Knepley @*/
117037d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11714d343eeaSMatthew G Knepley {
117237d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11734d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11744d343eeaSMatthew G Knepley 
11754d343eeaSMatthew G Knepley   PetscFunctionBegin;
11764d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
117769ca1f37SDmitry Karpeev   if (numFields) {
117869ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
117969ca1f37SDmitry Karpeev     *numFields = 0;
118069ca1f37SDmitry Karpeev   }
118137d0c07bSMatthew G Knepley   if (fieldNames) {
118237d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11830298fd71SBarry Smith     *fieldNames = NULL;
118469ca1f37SDmitry Karpeev   }
118569ca1f37SDmitry Karpeev   if (fields) {
118669ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
11870298fd71SBarry Smith     *fields = NULL;
118869ca1f37SDmitry Karpeev   }
118937d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
119037d0c07bSMatthew G Knepley   if (section) {
119137d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
119237d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
119337d0c07bSMatthew G Knepley 
119437d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
119537d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1196dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
119737d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
119837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
119937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
120037d0c07bSMatthew G Knepley     }
120137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
120237d0c07bSMatthew G Knepley       PetscInt gdof;
120337d0c07bSMatthew G Knepley 
120437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
120537d0c07bSMatthew G Knepley       if (gdof > 0) {
120637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
120737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
120837d0c07bSMatthew G Knepley 
120937d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
121037d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
121137d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
121237d0c07bSMatthew G Knepley         }
121337d0c07bSMatthew G Knepley       }
121437d0c07bSMatthew G Knepley     }
121537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1216785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
121737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
121837d0c07bSMatthew G Knepley     }
121937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
122037d0c07bSMatthew G Knepley       PetscInt gdof, goff;
122137d0c07bSMatthew G Knepley 
122237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
122337d0c07bSMatthew G Knepley       if (gdof > 0) {
122437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
122537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
122637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
122737d0c07bSMatthew G Knepley 
122837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
122937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
123037d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
123137d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
123237d0c07bSMatthew G Knepley           }
123337d0c07bSMatthew G Knepley         }
123437d0c07bSMatthew G Knepley       }
123537d0c07bSMatthew G Knepley     }
12368865f1eaSKarl Rupp     if (numFields) *numFields = nF;
123737d0c07bSMatthew G Knepley     if (fieldNames) {
1238785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
123937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
124037d0c07bSMatthew G Knepley         const char *fieldName;
124137d0c07bSMatthew G Knepley 
124237d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
124337d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
124437d0c07bSMatthew G Knepley       }
124537d0c07bSMatthew G Knepley     }
124637d0c07bSMatthew G Knepley     if (fields) {
1247785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
124837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
124982f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
125037d0c07bSMatthew G Knepley       }
125137d0c07bSMatthew G Knepley     }
125237d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
12538865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
12548865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
125569ca1f37SDmitry Karpeev   }
12564d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
12574d343eeaSMatthew G Knepley }
12584d343eeaSMatthew G Knepley 
125916621825SDmitry Karpeev 
126016621825SDmitry Karpeev #undef __FUNCT__
126116621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
126216621825SDmitry Karpeev /*@C
126316621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
126416621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
126516621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1266e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1267e7c4fc90SDmitry Karpeev 
1268e7c4fc90SDmitry Karpeev   Not collective
1269e7c4fc90SDmitry Karpeev 
1270e7c4fc90SDmitry Karpeev   Input Parameter:
1271e7c4fc90SDmitry Karpeev . dm - the DM object
1272e7c4fc90SDmitry Karpeev 
1273e7c4fc90SDmitry Karpeev   Output Parameters:
12740298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12750298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12760298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12770298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1278e7c4fc90SDmitry Karpeev 
1279e7c4fc90SDmitry Karpeev   Level: intermediate
1280e7c4fc90SDmitry Karpeev 
1281e7c4fc90SDmitry Karpeev   Notes:
1282e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1283e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1284e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1285e7c4fc90SDmitry Karpeev 
1286e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1287e7c4fc90SDmitry Karpeev @*/
128816621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1289e7c4fc90SDmitry Karpeev {
1290e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1291e7c4fc90SDmitry Karpeev 
1292e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1293e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12948865f1eaSKarl Rupp   if (len) {
12958865f1eaSKarl Rupp     PetscValidPointer(len,2);
12968865f1eaSKarl Rupp     *len = 0;
12978865f1eaSKarl Rupp   }
12988865f1eaSKarl Rupp   if (namelist) {
12998865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13008865f1eaSKarl Rupp     *namelist = 0;
13018865f1eaSKarl Rupp   }
13028865f1eaSKarl Rupp   if (islist) {
13038865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13048865f1eaSKarl Rupp     *islist = 0;
13058865f1eaSKarl Rupp   }
13068865f1eaSKarl Rupp   if (dmlist) {
13078865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13088865f1eaSKarl Rupp     *dmlist = 0;
13098865f1eaSKarl Rupp   }
1310f3f0edfdSDmitry Karpeev   /*
1311f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1312f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1313f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1314f3f0edfdSDmitry Karpeev    */
1315ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
131616621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1317435a35e8SMatthew G Knepley     PetscSection section;
1318435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1319435a35e8SMatthew G Knepley 
1320435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1321435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1322435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1323435a35e8SMatthew G Knepley       *len = numFields;
132403dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
132503dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
132603dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1327435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1328435a35e8SMatthew G Knepley         const char *fieldName;
1329435a35e8SMatthew G Knepley 
133003dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
133103dc3394SMatthew G. Knepley         if (namelist) {
1332435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1333435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1334435a35e8SMatthew G Knepley         }
133503dc3394SMatthew G. Knepley       }
1336435a35e8SMatthew G Knepley     } else {
133769ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1338e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
13390298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1340e7c4fc90SDmitry Karpeev     }
13418865f1eaSKarl Rupp   } else {
134216621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
134316621825SDmitry Karpeev   }
134416621825SDmitry Karpeev   PetscFunctionReturn(0);
134516621825SDmitry Karpeev }
134616621825SDmitry Karpeev 
134716621825SDmitry Karpeev #undef __FUNCT__
1348435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1349564cec59SMatthew G. Knepley /*@
1350435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1351435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1352435a35e8SMatthew G Knepley 
1353435a35e8SMatthew G Knepley   Not collective
1354435a35e8SMatthew G Knepley 
1355435a35e8SMatthew G Knepley   Input Parameters:
1356435a35e8SMatthew G Knepley + dm - the DM object
1357435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
13580298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1359435a35e8SMatthew G Knepley 
1360435a35e8SMatthew G Knepley   Output Parameters:
1361435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1362435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1363435a35e8SMatthew G Knepley 
1364435a35e8SMatthew G Knepley   Level: intermediate
1365435a35e8SMatthew G Knepley 
1366435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1367435a35e8SMatthew G Knepley @*/
1368435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1369435a35e8SMatthew G Knepley {
1370435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1371435a35e8SMatthew G Knepley 
1372435a35e8SMatthew G Knepley   PetscFunctionBegin;
1373435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1374435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13758865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13768865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1377435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1378435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
137982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1380435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1381435a35e8SMatthew G Knepley }
1382435a35e8SMatthew G Knepley 
138316621825SDmitry Karpeev 
138416621825SDmitry Karpeev #undef __FUNCT__
138516621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
138616621825SDmitry Karpeev /*@C
13878d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
13888d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
13898d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
13908d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
13918d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
139216621825SDmitry Karpeev 
139316621825SDmitry Karpeev   Not collective
139416621825SDmitry Karpeev 
139516621825SDmitry Karpeev   Input Parameter:
139616621825SDmitry Karpeev . dm - the DM object
139716621825SDmitry Karpeev 
139816621825SDmitry Karpeev   Output Parameters:
13990298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14000298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14010298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14020298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14030298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
140416621825SDmitry Karpeev 
140516621825SDmitry Karpeev   Level: intermediate
140616621825SDmitry Karpeev 
140716621825SDmitry Karpeev   Notes:
140816621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
140916621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
141016621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
141116621825SDmitry Karpeev 
14128d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
141316621825SDmitry Karpeev @*/
14148d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
141516621825SDmitry Karpeev {
141616621825SDmitry Karpeev   PetscErrorCode      ierr;
1417be081cd6SPeter Brune   DMSubDomainHookLink link;
1418be081cd6SPeter Brune   PetscInt            i,l;
141916621825SDmitry Karpeev 
142016621825SDmitry Karpeev   PetscFunctionBegin;
142116621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
142214a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14230298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14240298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14250298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14260298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1427f3f0edfdSDmitry Karpeev   /*
1428f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1429f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1430f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1431f3f0edfdSDmitry Karpeev    */
1432ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
143316621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1434be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
143514a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
143614a18fd3SPeter Brune     if (dmlist) {
1437be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1438be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1439be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1440be081cd6SPeter Brune         }
1441d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1442e7c4fc90SDmitry Karpeev       }
144314a18fd3SPeter Brune     }
144414a18fd3SPeter Brune     if (len) *len = l;
144514a18fd3SPeter Brune   }
1446e30e807fSPeter Brune   PetscFunctionReturn(0);
1447e30e807fSPeter Brune }
1448e30e807fSPeter Brune 
1449e30e807fSPeter Brune 
1450e30e807fSPeter Brune #undef __FUNCT__
1451e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1452e30e807fSPeter Brune /*@C
1453e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1454e30e807fSPeter Brune 
1455e30e807fSPeter Brune   Not collective
1456e30e807fSPeter Brune 
1457e30e807fSPeter Brune   Input Parameters:
1458e30e807fSPeter Brune + dm - the DM object
1459e30e807fSPeter Brune . n  - the number of subdomain scatters
1460e30e807fSPeter Brune - subdms - the local subdomains
1461e30e807fSPeter Brune 
1462e30e807fSPeter Brune   Output Parameters:
1463e30e807fSPeter Brune + n     - the number of scatters returned
1464e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1465e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1466e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1467e30e807fSPeter Brune 
1468e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1469e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1470e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1471e30e807fSPeter Brune   solution and residual data.
1472e30e807fSPeter Brune 
1473e30e807fSPeter Brune   Level: developer
1474e30e807fSPeter Brune 
1475e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1476e30e807fSPeter Brune @*/
1477e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1478e30e807fSPeter Brune {
1479e30e807fSPeter Brune   PetscErrorCode ierr;
1480e30e807fSPeter Brune 
1481e30e807fSPeter Brune   PetscFunctionBegin;
1482e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1483e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1484e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1485e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
148682f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1487e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1488e7c4fc90SDmitry Karpeev }
1489e7c4fc90SDmitry Karpeev 
1490731c8d9eSDmitry Karpeev #undef __FUNCT__
149147c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
149247c6ae99SBarry Smith /*@
149347c6ae99SBarry Smith   DMRefine - Refines a DM object
149447c6ae99SBarry Smith 
149547c6ae99SBarry Smith   Collective on DM
149647c6ae99SBarry Smith 
149747c6ae99SBarry Smith   Input Parameter:
149847c6ae99SBarry Smith + dm   - the DM object
149991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
150047c6ae99SBarry Smith 
150147c6ae99SBarry Smith   Output Parameter:
15020298fd71SBarry Smith . dmf - the refined DM, or NULL
1503ae0a1c52SMatthew G Knepley 
15040298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
150547c6ae99SBarry Smith 
150647c6ae99SBarry Smith   Level: developer
150747c6ae99SBarry Smith 
1508e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
150947c6ae99SBarry Smith @*/
15107087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
151147c6ae99SBarry Smith {
151247c6ae99SBarry Smith   PetscErrorCode   ierr;
1513c833c3b5SJed Brown   DMRefineHookLink link;
151447c6ae99SBarry Smith 
151547c6ae99SBarry Smith   PetscFunctionBegin;
1516732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15184057135bSMatthew G Knepley   if (*dmf) {
151943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15208865f1eaSKarl Rupp 
15218cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15228865f1eaSKarl Rupp 
1523644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15240598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1525656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15268865f1eaSKarl Rupp 
1527e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1528c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15298865f1eaSKarl Rupp       if (link->refinehook) {
15308865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
15318865f1eaSKarl Rupp       }
1532c833c3b5SJed Brown     }
1533c833c3b5SJed Brown   }
1534c833c3b5SJed Brown   PetscFunctionReturn(0);
1535c833c3b5SJed Brown }
1536c833c3b5SJed Brown 
1537c833c3b5SJed Brown #undef __FUNCT__
1538c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1539bb9467b5SJed Brown /*@C
1540c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1541c833c3b5SJed Brown 
1542c833c3b5SJed Brown    Logically Collective
1543c833c3b5SJed Brown 
1544c833c3b5SJed Brown    Input Arguments:
1545c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1546c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1547c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
15480298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1549c833c3b5SJed Brown 
1550c833c3b5SJed Brown    Calling sequence of refinehook:
1551c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1552c833c3b5SJed Brown 
1553c833c3b5SJed Brown +  coarse - coarse level DM
1554c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1555c833c3b5SJed Brown -  ctx - optional user-defined function context
1556c833c3b5SJed Brown 
1557c833c3b5SJed Brown    Calling sequence for interphook:
1558c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1559c833c3b5SJed Brown 
1560c833c3b5SJed Brown +  coarse - coarse level DM
1561c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1562c833c3b5SJed Brown .  fine - fine level DM to update
1563c833c3b5SJed Brown -  ctx - optional user-defined function context
1564c833c3b5SJed Brown 
1565c833c3b5SJed Brown    Level: advanced
1566c833c3b5SJed Brown 
1567c833c3b5SJed Brown    Notes:
1568c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1569c833c3b5SJed Brown 
1570c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1571c833c3b5SJed Brown 
1572bb9467b5SJed Brown    This function is currently not available from Fortran.
1573bb9467b5SJed Brown 
1574c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1575c833c3b5SJed Brown @*/
1576c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1577c833c3b5SJed Brown {
1578c833c3b5SJed Brown   PetscErrorCode   ierr;
1579c833c3b5SJed Brown   DMRefineHookLink link,*p;
1580c833c3b5SJed Brown 
1581c833c3b5SJed Brown   PetscFunctionBegin;
1582c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1583c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1584c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1585c833c3b5SJed Brown   link->refinehook = refinehook;
1586c833c3b5SJed Brown   link->interphook = interphook;
1587c833c3b5SJed Brown   link->ctx        = ctx;
15880298fd71SBarry Smith   link->next       = NULL;
1589c833c3b5SJed Brown   *p               = link;
1590c833c3b5SJed Brown   PetscFunctionReturn(0);
1591c833c3b5SJed Brown }
1592c833c3b5SJed Brown 
1593c833c3b5SJed Brown #undef __FUNCT__
1594c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1595c833c3b5SJed Brown /*@
1596c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1597c833c3b5SJed Brown 
1598c833c3b5SJed Brown    Collective if any hooks are
1599c833c3b5SJed Brown 
1600c833c3b5SJed Brown    Input Arguments:
1601c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1602c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1603c833c3b5SJed Brown -  fine - finer DM to update
1604c833c3b5SJed Brown 
1605c833c3b5SJed Brown    Level: developer
1606c833c3b5SJed Brown 
1607c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1608c833c3b5SJed Brown @*/
1609c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1610c833c3b5SJed Brown {
1611c833c3b5SJed Brown   PetscErrorCode   ierr;
1612c833c3b5SJed Brown   DMRefineHookLink link;
1613c833c3b5SJed Brown 
1614c833c3b5SJed Brown   PetscFunctionBegin;
1615c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16168865f1eaSKarl Rupp     if (link->interphook) {
16178865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16188865f1eaSKarl Rupp     }
16194057135bSMatthew G Knepley   }
162047c6ae99SBarry Smith   PetscFunctionReturn(0);
162147c6ae99SBarry Smith }
162247c6ae99SBarry Smith 
162347c6ae99SBarry Smith #undef __FUNCT__
1624eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1625eb3f98d2SBarry Smith /*@
1626eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1627eb3f98d2SBarry Smith 
1628eb3f98d2SBarry Smith     Not Collective
1629eb3f98d2SBarry Smith 
1630eb3f98d2SBarry Smith     Input Parameter:
1631eb3f98d2SBarry Smith .   dm - the DM object
1632eb3f98d2SBarry Smith 
1633eb3f98d2SBarry Smith     Output Parameter:
1634eb3f98d2SBarry Smith .   level - number of refinements
1635eb3f98d2SBarry Smith 
1636eb3f98d2SBarry Smith     Level: developer
1637eb3f98d2SBarry Smith 
16386a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1639eb3f98d2SBarry Smith 
1640eb3f98d2SBarry Smith @*/
1641eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1642eb3f98d2SBarry Smith {
1643eb3f98d2SBarry Smith   PetscFunctionBegin;
1644eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1645eb3f98d2SBarry Smith   *level = dm->levelup;
1646eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1647eb3f98d2SBarry Smith }
1648eb3f98d2SBarry Smith 
1649eb3f98d2SBarry Smith #undef __FUNCT__
1650baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1651bb9467b5SJed Brown /*@C
1652baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1653baf369e7SPeter Brune 
1654baf369e7SPeter Brune    Logically Collective
1655baf369e7SPeter Brune 
1656baf369e7SPeter Brune    Input Arguments:
1657baf369e7SPeter Brune +  dm - the DM
1658baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1659baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
16600298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1661baf369e7SPeter Brune 
1662baf369e7SPeter Brune    Calling sequence for beginhook:
1663baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1664baf369e7SPeter Brune 
1665baf369e7SPeter Brune +  dm - global DM
1666baf369e7SPeter Brune .  g - global vector
1667baf369e7SPeter Brune .  mode - mode
1668baf369e7SPeter Brune .  l - local vector
1669baf369e7SPeter Brune -  ctx - optional user-defined function context
1670baf369e7SPeter Brune 
1671baf369e7SPeter Brune 
1672baf369e7SPeter Brune    Calling sequence for endhook:
1673ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1674baf369e7SPeter Brune 
1675baf369e7SPeter Brune +  global - global DM
1676baf369e7SPeter Brune -  ctx - optional user-defined function context
1677baf369e7SPeter Brune 
1678baf369e7SPeter Brune    Level: advanced
1679baf369e7SPeter Brune 
1680baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1681baf369e7SPeter Brune @*/
1682baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1683baf369e7SPeter Brune {
1684baf369e7SPeter Brune   PetscErrorCode          ierr;
1685baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1686baf369e7SPeter Brune 
1687baf369e7SPeter Brune   PetscFunctionBegin;
1688baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1689baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1690baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1691baf369e7SPeter Brune   link->beginhook = beginhook;
1692baf369e7SPeter Brune   link->endhook   = endhook;
1693baf369e7SPeter Brune   link->ctx       = ctx;
16940298fd71SBarry Smith   link->next      = NULL;
1695baf369e7SPeter Brune   *p              = link;
1696baf369e7SPeter Brune   PetscFunctionReturn(0);
1697baf369e7SPeter Brune }
1698baf369e7SPeter Brune 
1699baf369e7SPeter Brune #undef __FUNCT__
17004c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17014c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17024c274da1SToby Isaac {
17034c274da1SToby Isaac   Mat cMat;
17044c274da1SToby Isaac   Vec cVec;
17054c274da1SToby Isaac   PetscSection section, cSec;
17064c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17074c274da1SToby Isaac   PetscErrorCode ierr;
17084c274da1SToby Isaac 
17094c274da1SToby Isaac   PetscFunctionBegin;
17104c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17114c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17124c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17134c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17147711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17154c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17164c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17174c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17184c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17194c274da1SToby Isaac       if (dof) {
17204c274da1SToby Isaac         PetscScalar *vals;
17214c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17224c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17234c274da1SToby Isaac       }
17244c274da1SToby Isaac     }
17254c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17264c274da1SToby Isaac   }
17274c274da1SToby Isaac   PetscFunctionReturn(0);
17284c274da1SToby Isaac }
17294c274da1SToby Isaac 
17304c274da1SToby Isaac #undef __FUNCT__
173147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
173247c6ae99SBarry Smith /*@
173347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
173447c6ae99SBarry Smith 
173547c6ae99SBarry Smith     Neighbor-wise Collective on DM
173647c6ae99SBarry Smith 
173747c6ae99SBarry Smith     Input Parameters:
173847c6ae99SBarry Smith +   dm - the DM object
173947c6ae99SBarry Smith .   g - the global vector
174047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
174147c6ae99SBarry Smith -   l - the local vector
174247c6ae99SBarry Smith 
174347c6ae99SBarry Smith 
174447c6ae99SBarry Smith     Level: beginner
174547c6ae99SBarry Smith 
1746e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
174747c6ae99SBarry Smith 
174847c6ae99SBarry Smith @*/
17497087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
175047c6ae99SBarry Smith {
17517128ae9fSMatthew G Knepley   PetscSF                 sf;
175247c6ae99SBarry Smith   PetscErrorCode          ierr;
1753baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
175447c6ae99SBarry Smith 
175547c6ae99SBarry Smith   PetscFunctionBegin;
1756171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1757baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
17588865f1eaSKarl Rupp     if (link->beginhook) {
17598865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
17608865f1eaSKarl Rupp     }
1761baf369e7SPeter Brune   }
17627128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17637128ae9fSMatthew G Knepley   if (sf) {
1764ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1765ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
17667128ae9fSMatthew G Knepley 
176782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17687128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1769ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17707128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17717128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1772ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17737128ae9fSMatthew G Knepley   } else {
1774843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17757128ae9fSMatthew G Knepley   }
177647c6ae99SBarry Smith   PetscFunctionReturn(0);
177747c6ae99SBarry Smith }
177847c6ae99SBarry Smith 
177947c6ae99SBarry Smith #undef __FUNCT__
178047c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
178147c6ae99SBarry Smith /*@
178247c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
178347c6ae99SBarry Smith 
178447c6ae99SBarry Smith     Neighbor-wise Collective on DM
178547c6ae99SBarry Smith 
178647c6ae99SBarry Smith     Input Parameters:
178747c6ae99SBarry Smith +   dm - the DM object
178847c6ae99SBarry Smith .   g - the global vector
178947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
179047c6ae99SBarry Smith -   l - the local vector
179147c6ae99SBarry Smith 
179247c6ae99SBarry Smith 
179347c6ae99SBarry Smith     Level: beginner
179447c6ae99SBarry Smith 
1795e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
179647c6ae99SBarry Smith 
179747c6ae99SBarry Smith @*/
17987087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
179947c6ae99SBarry Smith {
18007128ae9fSMatthew G Knepley   PetscSF                 sf;
180147c6ae99SBarry Smith   PetscErrorCode          ierr;
1802ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1803ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1804baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
180547c6ae99SBarry Smith 
180647c6ae99SBarry Smith   PetscFunctionBegin;
1807171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18087128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18097128ae9fSMatthew G Knepley   if (sf) {
181082f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18117128ae9fSMatthew G Knepley 
18127128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1813ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18147128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18157128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1816ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18177128ae9fSMatthew G Knepley   } else {
1818843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18197128ae9fSMatthew G Knepley   }
18204c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1821baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1822baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1823baf369e7SPeter Brune   }
182447c6ae99SBarry Smith   PetscFunctionReturn(0);
182547c6ae99SBarry Smith }
182647c6ae99SBarry Smith 
182747c6ae99SBarry Smith #undef __FUNCT__
1828d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1829d4d07f1eSToby Isaac /*@C
1830d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1831d4d07f1eSToby Isaac 
1832d4d07f1eSToby Isaac    Logically Collective
1833d4d07f1eSToby Isaac 
1834d4d07f1eSToby Isaac    Input Arguments:
1835d4d07f1eSToby Isaac +  dm - the DM
1836d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1837d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1838d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1839d4d07f1eSToby Isaac 
1840d4d07f1eSToby Isaac    Calling sequence for beginhook:
1841d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1842d4d07f1eSToby Isaac 
1843d4d07f1eSToby Isaac +  dm - global DM
1844d4d07f1eSToby Isaac .  l - local vector
1845d4d07f1eSToby Isaac .  mode - mode
1846d4d07f1eSToby Isaac .  g - global vector
1847d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1848d4d07f1eSToby Isaac 
1849d4d07f1eSToby Isaac 
1850d4d07f1eSToby Isaac    Calling sequence for endhook:
1851d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1852d4d07f1eSToby Isaac 
1853d4d07f1eSToby Isaac +  global - global DM
1854d4d07f1eSToby Isaac .  l - local vector
1855d4d07f1eSToby Isaac .  mode - mode
1856d4d07f1eSToby Isaac .  g - global vector
1857d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1858d4d07f1eSToby Isaac 
1859d4d07f1eSToby Isaac    Level: advanced
1860d4d07f1eSToby Isaac 
1861d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1862d4d07f1eSToby Isaac @*/
1863d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1864d4d07f1eSToby Isaac {
1865d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1866d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1867d4d07f1eSToby Isaac 
1868d4d07f1eSToby Isaac   PetscFunctionBegin;
1869d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1870d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1871d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1872d4d07f1eSToby Isaac   link->beginhook = beginhook;
1873d4d07f1eSToby Isaac   link->endhook   = endhook;
1874d4d07f1eSToby Isaac   link->ctx       = ctx;
1875d4d07f1eSToby Isaac   link->next      = NULL;
1876d4d07f1eSToby Isaac   *p              = link;
1877d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1878d4d07f1eSToby Isaac }
1879d4d07f1eSToby Isaac 
1880d4d07f1eSToby Isaac #undef __FUNCT__
18814c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
18824c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
18834c274da1SToby Isaac {
18844c274da1SToby Isaac   Mat cMat;
18854c274da1SToby Isaac   Vec cVec;
18864c274da1SToby Isaac   PetscSection section, cSec;
18874c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
18884c274da1SToby Isaac   PetscErrorCode ierr;
18894c274da1SToby Isaac 
18904c274da1SToby Isaac   PetscFunctionBegin;
18914c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
18924c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
18934c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
18944c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
18957711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
18964c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
18974c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
18984c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
18994c274da1SToby Isaac       if (dof) {
19004c274da1SToby Isaac         PetscInt d;
19014c274da1SToby Isaac         PetscScalar *vals;
19024c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19034c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19044c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19054c274da1SToby Isaac          * we just extracted */
19064c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19074c274da1SToby Isaac           vals[d] = 0.;
19084c274da1SToby Isaac         }
19094c274da1SToby Isaac       }
19104c274da1SToby Isaac     }
19114c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19124c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19134c274da1SToby Isaac   }
19144c274da1SToby Isaac   PetscFunctionReturn(0);
19154c274da1SToby Isaac }
19164c274da1SToby Isaac 
19174c274da1SToby Isaac #undef __FUNCT__
19189a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
191947c6ae99SBarry Smith /*@
19209a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19219a42bb27SBarry Smith 
19229a42bb27SBarry Smith     Neighbor-wise Collective on DM
19239a42bb27SBarry Smith 
19249a42bb27SBarry Smith     Input Parameters:
19259a42bb27SBarry Smith +   dm - the DM object
1926f6813fd5SJed Brown .   l - the local vector
19271eb28f2eSBarry 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.
19281eb28f2eSBarry Smith -   g - the global vector
19299a42bb27SBarry Smith 
1930d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
193184330215SMatthew 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.
19329a42bb27SBarry Smith 
19339a42bb27SBarry Smith     Level: beginner
19349a42bb27SBarry Smith 
1935e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
19369a42bb27SBarry Smith 
19379a42bb27SBarry Smith @*/
19387087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
19399a42bb27SBarry Smith {
19407128ae9fSMatthew G Knepley   PetscSF                 sf;
194184330215SMatthew G. Knepley   PetscSection            s, gs;
1942d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
1943ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
1944ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
194584330215SMatthew G. Knepley   PetscBool               isInsert;
194684330215SMatthew G. Knepley   PetscErrorCode          ierr;
19479a42bb27SBarry Smith 
19489a42bb27SBarry Smith   PetscFunctionBegin;
1949171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1950d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1951d4d07f1eSToby Isaac     if (link->beginhook) {
1952d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1953d4d07f1eSToby Isaac     }
1954d4d07f1eSToby Isaac   }
19554c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
19567128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
195784330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19587128ae9fSMatthew G Knepley   switch (mode) {
19597128ae9fSMatthew G Knepley   case INSERT_VALUES:
19607128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
196184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
19627128ae9fSMatthew G Knepley   case ADD_VALUES:
19637128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
196484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
19657128ae9fSMatthew G Knepley   default:
196682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19677128ae9fSMatthew G Knepley   }
196884330215SMatthew G. Knepley   if (sf && !isInsert) {
1969ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19707128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
1971a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
1972ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
197384330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
197484330215SMatthew G. Knepley   } else if (s && isInsert) {
197584330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
197684330215SMatthew G. Knepley 
197784330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
197884330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
197984330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
1980ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
198184330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
198284330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
1983b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
198484330215SMatthew G. Knepley 
198584330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
198603442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
198784330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
1988b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
198984330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
199084330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
1991b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
199203442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
1993b3b16f48SMatthew 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);
1994b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
1995b3b16f48SMatthew G. Knepley       if (!gcdof) {
199684330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
1997b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
1998b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
199984330215SMatthew G. Knepley         const PetscInt *cdofs;
200084330215SMatthew G. Knepley         PetscInt        cind = 0;
200184330215SMatthew G. Knepley 
200284330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2003b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
200484330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2005b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
200684330215SMatthew G. Knepley         }
2007b3b16f48SMatthew 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);
200884330215SMatthew G. Knepley     }
2009ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20107128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20117128ae9fSMatthew G Knepley   } else {
2012843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20137128ae9fSMatthew G Knepley   }
20149a42bb27SBarry Smith   PetscFunctionReturn(0);
20159a42bb27SBarry Smith }
20169a42bb27SBarry Smith 
20179a42bb27SBarry Smith #undef __FUNCT__
20189a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20199a42bb27SBarry Smith /*@
20209a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
202147c6ae99SBarry Smith 
202247c6ae99SBarry Smith     Neighbor-wise Collective on DM
202347c6ae99SBarry Smith 
202447c6ae99SBarry Smith     Input Parameters:
202547c6ae99SBarry Smith +   dm - the DM object
2026f6813fd5SJed Brown .   l - the local vector
202747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2028f6813fd5SJed Brown -   g - the global vector
202947c6ae99SBarry Smith 
203047c6ae99SBarry Smith 
203147c6ae99SBarry Smith     Level: beginner
203247c6ae99SBarry Smith 
2033e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
203447c6ae99SBarry Smith 
203547c6ae99SBarry Smith @*/
20367087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
203747c6ae99SBarry Smith {
20387128ae9fSMatthew G Knepley   PetscSF                 sf;
203984330215SMatthew G. Knepley   PetscSection            s;
2040d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
204184330215SMatthew G. Knepley   PetscBool               isInsert;
204284330215SMatthew G. Knepley   PetscErrorCode          ierr;
204347c6ae99SBarry Smith 
204447c6ae99SBarry Smith   PetscFunctionBegin;
2045171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20467128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
204784330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20487128ae9fSMatthew G Knepley   switch (mode) {
20497128ae9fSMatthew G Knepley   case INSERT_VALUES:
20507128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
205184330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20527128ae9fSMatthew G Knepley   case ADD_VALUES:
20537128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
205484330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20557128ae9fSMatthew G Knepley   default:
205682f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20577128ae9fSMatthew G Knepley   }
205884330215SMatthew G. Knepley   if (sf && !isInsert) {
2059ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2060ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
206184330215SMatthew G. Knepley 
2062ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20637128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2064a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2065ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20667128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
206784330215SMatthew G. Knepley   } else if (s && isInsert) {
20687128ae9fSMatthew G Knepley   } else {
2069843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20707128ae9fSMatthew G Knepley   }
2071d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2072d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2073d4d07f1eSToby Isaac   }
207447c6ae99SBarry Smith   PetscFunctionReturn(0);
207547c6ae99SBarry Smith }
207647c6ae99SBarry Smith 
207747c6ae99SBarry Smith #undef __FUNCT__
2078f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2079f089877aSRichard Tran Mills /*@
2080bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2081bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2082d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2083f089877aSRichard Tran Mills 
2084bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2085f089877aSRichard Tran Mills 
2086f089877aSRichard Tran Mills    Input Parameters:
2087f089877aSRichard Tran Mills +  dm - the DM object
2088bc0a1609SRichard Tran Mills .  g - the original local vector
2089bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2090f089877aSRichard Tran Mills 
2091bc0a1609SRichard Tran Mills    Output Parameter:
2092bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2093f089877aSRichard Tran Mills 
2094f089877aSRichard Tran Mills    Level: intermediate
2095f089877aSRichard Tran Mills 
2096bc0a1609SRichard Tran Mills    Notes:
2097bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2098bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2099bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2100bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2101bc0a1609SRichard Tran Mills 
2102bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2103bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2104f089877aSRichard Tran Mills 
2105f089877aSRichard Tran Mills @*/
2106f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2107f089877aSRichard Tran Mills {
2108f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2109f089877aSRichard Tran Mills 
2110f089877aSRichard Tran Mills   PetscFunctionBegin;
2111f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2112f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2113f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2114f089877aSRichard Tran Mills }
2115f089877aSRichard Tran Mills 
2116f089877aSRichard Tran Mills #undef __FUNCT__
2117f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2118f089877aSRichard Tran Mills /*@
2119bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2120bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2121d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2122f089877aSRichard Tran Mills 
2123bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2124f089877aSRichard Tran Mills 
2125f089877aSRichard Tran Mills    Input Parameters:
2126bc0a1609SRichard Tran Mills +  da - the DM object
2127bc0a1609SRichard Tran Mills .  g - the original local vector
2128bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2129f089877aSRichard Tran Mills 
2130bc0a1609SRichard Tran Mills    Output Parameter:
2131bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2132f089877aSRichard Tran Mills 
2133f089877aSRichard Tran Mills    Level: intermediate
2134f089877aSRichard Tran Mills 
2135bc0a1609SRichard Tran Mills    Notes:
2136bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2137bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2138bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2139bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2140bc0a1609SRichard Tran Mills 
2141bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2142bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2143f089877aSRichard Tran Mills 
2144f089877aSRichard Tran Mills @*/
2145f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2146f089877aSRichard Tran Mills {
2147f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2148f089877aSRichard Tran Mills 
2149f089877aSRichard Tran Mills   PetscFunctionBegin;
2150f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2151f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2152f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2153f089877aSRichard Tran Mills }
2154f089877aSRichard Tran Mills 
2155f089877aSRichard Tran Mills 
2156f089877aSRichard Tran Mills #undef __FUNCT__
215747c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
215847c6ae99SBarry Smith /*@
215947c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
216047c6ae99SBarry Smith 
216147c6ae99SBarry Smith     Collective on DM
216247c6ae99SBarry Smith 
216347c6ae99SBarry Smith     Input Parameter:
216447c6ae99SBarry Smith +   dm - the DM object
216591d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
216647c6ae99SBarry Smith 
216747c6ae99SBarry Smith     Output Parameter:
216847c6ae99SBarry Smith .   dmc - the coarsened DM
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith     Level: developer
217147c6ae99SBarry Smith 
2172e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
217347c6ae99SBarry Smith 
217447c6ae99SBarry Smith @*/
21757087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
217647c6ae99SBarry Smith {
217747c6ae99SBarry Smith   PetscErrorCode    ierr;
2178b17ce1afSJed Brown   DMCoarsenHookLink link;
217947c6ae99SBarry Smith 
218047c6ae99SBarry Smith   PetscFunctionBegin;
2181171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
218247c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
21838892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
218443842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
21858cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2186644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
21870598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2188656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2189e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2190b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2191b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2192b17ce1afSJed Brown   }
2193b17ce1afSJed Brown   PetscFunctionReturn(0);
2194b17ce1afSJed Brown }
2195b17ce1afSJed Brown 
2196b17ce1afSJed Brown #undef __FUNCT__
2197b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2198bb9467b5SJed Brown /*@C
2199b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2200b17ce1afSJed Brown 
2201b17ce1afSJed Brown    Logically Collective
2202b17ce1afSJed Brown 
2203b17ce1afSJed Brown    Input Arguments:
2204b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2205b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2206b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22070298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2208b17ce1afSJed Brown 
2209b17ce1afSJed Brown    Calling sequence of coarsenhook:
2210b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2211b17ce1afSJed Brown 
2212b17ce1afSJed Brown +  fine - fine level DM
2213b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2214b17ce1afSJed Brown -  ctx - optional user-defined function context
2215b17ce1afSJed Brown 
2216b17ce1afSJed Brown    Calling sequence for restricthook:
2217c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2218b17ce1afSJed Brown 
2219b17ce1afSJed Brown +  fine - fine level DM
2220b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2221c833c3b5SJed Brown .  rscale - scaling vector for restriction
2222c833c3b5SJed Brown .  inject - matrix restricting by injection
2223b17ce1afSJed Brown .  coarse - coarse level DM to update
2224b17ce1afSJed Brown -  ctx - optional user-defined function context
2225b17ce1afSJed Brown 
2226b17ce1afSJed Brown    Level: advanced
2227b17ce1afSJed Brown 
2228b17ce1afSJed Brown    Notes:
2229b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2230b17ce1afSJed Brown 
2231b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2232b17ce1afSJed Brown 
2233b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2234b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2235b17ce1afSJed Brown 
2236bb9467b5SJed Brown    This function is currently not available from Fortran.
2237bb9467b5SJed Brown 
2238c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2239b17ce1afSJed Brown @*/
2240b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2241b17ce1afSJed Brown {
2242b17ce1afSJed Brown   PetscErrorCode    ierr;
2243b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2244b17ce1afSJed Brown 
2245b17ce1afSJed Brown   PetscFunctionBegin;
2246b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
22476bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2248b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2249b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2250b17ce1afSJed Brown   link->restricthook = restricthook;
2251b17ce1afSJed Brown   link->ctx          = ctx;
22520298fd71SBarry Smith   link->next         = NULL;
2253b17ce1afSJed Brown   *p                 = link;
2254b17ce1afSJed Brown   PetscFunctionReturn(0);
2255b17ce1afSJed Brown }
2256b17ce1afSJed Brown 
2257b17ce1afSJed Brown #undef __FUNCT__
2258b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2259b17ce1afSJed Brown /*@
2260b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2261b17ce1afSJed Brown 
2262b17ce1afSJed Brown    Collective if any hooks are
2263b17ce1afSJed Brown 
2264b17ce1afSJed Brown    Input Arguments:
2265b17ce1afSJed Brown +  fine - finer DM to use as a base
2266b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2267b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2268b17ce1afSJed Brown -  coarse - coarer DM to update
2269b17ce1afSJed Brown 
2270b17ce1afSJed Brown    Level: developer
2271b17ce1afSJed Brown 
2272b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2273b17ce1afSJed Brown @*/
2274b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2275b17ce1afSJed Brown {
2276b17ce1afSJed Brown   PetscErrorCode    ierr;
2277b17ce1afSJed Brown   DMCoarsenHookLink link;
2278b17ce1afSJed Brown 
2279b17ce1afSJed Brown   PetscFunctionBegin;
2280b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
22818865f1eaSKarl Rupp     if (link->restricthook) {
22828865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
22838865f1eaSKarl Rupp     }
2284b17ce1afSJed Brown   }
228547c6ae99SBarry Smith   PetscFunctionReturn(0);
228647c6ae99SBarry Smith }
228747c6ae99SBarry Smith 
228847c6ae99SBarry Smith #undef __FUNCT__
2289be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2290bb9467b5SJed Brown /*@C
2291be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
22925dbd56e3SPeter Brune 
22935dbd56e3SPeter Brune    Logically Collective
22945dbd56e3SPeter Brune 
22955dbd56e3SPeter Brune    Input Arguments:
22965dbd56e3SPeter Brune +  global - global DM
2297ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
22985dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
22990298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23005dbd56e3SPeter Brune 
2301ec4806b8SPeter Brune 
2302ec4806b8SPeter Brune    Calling sequence for ddhook:
2303ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2304ec4806b8SPeter Brune 
2305ec4806b8SPeter Brune +  global - global DM
2306ec4806b8SPeter Brune .  block  - block DM
2307ec4806b8SPeter Brune -  ctx - optional user-defined function context
2308ec4806b8SPeter Brune 
23095dbd56e3SPeter Brune    Calling sequence for restricthook:
2310ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23115dbd56e3SPeter Brune 
23125dbd56e3SPeter Brune +  global - global DM
23135dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23145dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2315ec4806b8SPeter Brune .  block  - block DM
23165dbd56e3SPeter Brune -  ctx - optional user-defined function context
23175dbd56e3SPeter Brune 
23185dbd56e3SPeter Brune    Level: advanced
23195dbd56e3SPeter Brune 
23205dbd56e3SPeter Brune    Notes:
2321ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23225dbd56e3SPeter Brune 
23235dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23245dbd56e3SPeter Brune 
23255dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2326ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
23275dbd56e3SPeter Brune 
2328bb9467b5SJed Brown    This function is currently not available from Fortran.
2329bb9467b5SJed Brown 
23305dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
23315dbd56e3SPeter Brune @*/
2332be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
23335dbd56e3SPeter Brune {
23345dbd56e3SPeter Brune   PetscErrorCode      ierr;
2335be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
23365dbd56e3SPeter Brune 
23375dbd56e3SPeter Brune   PetscFunctionBegin;
23385dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2339be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2340be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
23415dbd56e3SPeter Brune   link->restricthook = restricthook;
2342be081cd6SPeter Brune   link->ddhook       = ddhook;
23435dbd56e3SPeter Brune   link->ctx          = ctx;
23440298fd71SBarry Smith   link->next         = NULL;
23455dbd56e3SPeter Brune   *p                 = link;
23465dbd56e3SPeter Brune   PetscFunctionReturn(0);
23475dbd56e3SPeter Brune }
23485dbd56e3SPeter Brune 
23495dbd56e3SPeter Brune #undef __FUNCT__
2350be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
23515dbd56e3SPeter Brune /*@
2352be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
23535dbd56e3SPeter Brune 
23545dbd56e3SPeter Brune    Collective if any hooks are
23555dbd56e3SPeter Brune 
23565dbd56e3SPeter Brune    Input Arguments:
23575dbd56e3SPeter Brune +  fine - finer DM to use as a base
2358be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2359be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
23605dbd56e3SPeter Brune -  coarse - coarer DM to update
23615dbd56e3SPeter Brune 
23625dbd56e3SPeter Brune    Level: developer
23635dbd56e3SPeter Brune 
23645dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
23655dbd56e3SPeter Brune @*/
2366be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
23675dbd56e3SPeter Brune {
23685dbd56e3SPeter Brune   PetscErrorCode      ierr;
2369be081cd6SPeter Brune   DMSubDomainHookLink link;
23705dbd56e3SPeter Brune 
23715dbd56e3SPeter Brune   PetscFunctionBegin;
2372be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
23738865f1eaSKarl Rupp     if (link->restricthook) {
23748865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
23758865f1eaSKarl Rupp     }
23765dbd56e3SPeter Brune   }
23775dbd56e3SPeter Brune   PetscFunctionReturn(0);
23785dbd56e3SPeter Brune }
23795dbd56e3SPeter Brune 
23805dbd56e3SPeter Brune #undef __FUNCT__
23815fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
23825fe1f584SPeter Brune /*@
23836a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
23845fe1f584SPeter Brune 
23855fe1f584SPeter Brune     Not Collective
23865fe1f584SPeter Brune 
23875fe1f584SPeter Brune     Input Parameter:
23885fe1f584SPeter Brune .   dm - the DM object
23895fe1f584SPeter Brune 
23905fe1f584SPeter Brune     Output Parameter:
23916a7d9d85SPeter Brune .   level - number of coarsenings
23925fe1f584SPeter Brune 
23935fe1f584SPeter Brune     Level: developer
23945fe1f584SPeter Brune 
23956a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
23965fe1f584SPeter Brune 
23975fe1f584SPeter Brune @*/
23985fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
23995fe1f584SPeter Brune {
24005fe1f584SPeter Brune   PetscFunctionBegin;
24015fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24025fe1f584SPeter Brune   *level = dm->leveldown;
24035fe1f584SPeter Brune   PetscFunctionReturn(0);
24045fe1f584SPeter Brune }
24055fe1f584SPeter Brune 
24065fe1f584SPeter Brune 
24075fe1f584SPeter Brune 
24085fe1f584SPeter Brune #undef __FUNCT__
240947c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
241047c6ae99SBarry Smith /*@C
241147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
241247c6ae99SBarry Smith 
241347c6ae99SBarry Smith     Collective on DM
241447c6ae99SBarry Smith 
241547c6ae99SBarry Smith     Input Parameter:
241647c6ae99SBarry Smith +   dm - the DM object
241747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
241847c6ae99SBarry Smith 
241947c6ae99SBarry Smith     Output Parameter:
242047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
242147c6ae99SBarry Smith 
242247c6ae99SBarry Smith     Level: developer
242347c6ae99SBarry Smith 
2424e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
242547c6ae99SBarry Smith 
242647c6ae99SBarry Smith @*/
24277087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
242847c6ae99SBarry Smith {
242947c6ae99SBarry Smith   PetscErrorCode ierr;
243047c6ae99SBarry Smith 
243147c6ae99SBarry Smith   PetscFunctionBegin;
2432171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2433ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
243447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
243547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
243647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
243747c6ae99SBarry Smith   } else if (dm->ops->refine) {
243847c6ae99SBarry Smith     PetscInt i;
243947c6ae99SBarry Smith 
2440ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
244147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2442ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
244347c6ae99SBarry Smith     }
2444ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
244547c6ae99SBarry Smith   PetscFunctionReturn(0);
244647c6ae99SBarry Smith }
244747c6ae99SBarry Smith 
244847c6ae99SBarry Smith #undef __FUNCT__
244947c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
245047c6ae99SBarry Smith /*@C
245147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
245247c6ae99SBarry Smith 
245347c6ae99SBarry Smith     Collective on DM
245447c6ae99SBarry Smith 
245547c6ae99SBarry Smith     Input Parameter:
245647c6ae99SBarry Smith +   dm - the DM object
245747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
245847c6ae99SBarry Smith 
245947c6ae99SBarry Smith     Output Parameter:
246047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
246147c6ae99SBarry Smith 
246247c6ae99SBarry Smith     Level: developer
246347c6ae99SBarry Smith 
2464e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
246547c6ae99SBarry Smith 
246647c6ae99SBarry Smith @*/
24677087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
246847c6ae99SBarry Smith {
246947c6ae99SBarry Smith   PetscErrorCode ierr;
247047c6ae99SBarry Smith 
247147c6ae99SBarry Smith   PetscFunctionBegin;
2472171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2473ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
247447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
247547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
247647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
247747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
247847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
247947c6ae99SBarry Smith     PetscInt i;
248047c6ae99SBarry Smith 
2481ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
248247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2483ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
248447c6ae99SBarry Smith     }
2485ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
248647c6ae99SBarry Smith   PetscFunctionReturn(0);
248747c6ae99SBarry Smith }
248847c6ae99SBarry Smith 
248947c6ae99SBarry Smith #undef __FUNCT__
2490e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
249147c6ae99SBarry Smith /*@
2492e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
249347c6ae99SBarry Smith    grids associated with two DMs.
249447c6ae99SBarry Smith 
249547c6ae99SBarry Smith    Collective on DM
249647c6ae99SBarry Smith 
249747c6ae99SBarry Smith    Input Parameters:
249847c6ae99SBarry Smith +  dmc - the coarse grid DM
249947c6ae99SBarry Smith -  dmf - the fine grid DM
250047c6ae99SBarry Smith 
250147c6ae99SBarry Smith    Output Parameters:
250247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
250347c6ae99SBarry Smith 
250447c6ae99SBarry Smith    Level: intermediate
250547c6ae99SBarry Smith 
250647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
250747c6ae99SBarry Smith 
2508e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
250947c6ae99SBarry Smith @*/
2510e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
251147c6ae99SBarry Smith {
251247c6ae99SBarry Smith   PetscErrorCode ierr;
251347c6ae99SBarry Smith 
251447c6ae99SBarry Smith   PetscFunctionBegin;
2515171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2516171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
251747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
251847c6ae99SBarry Smith   PetscFunctionReturn(0);
251947c6ae99SBarry Smith }
252047c6ae99SBarry Smith 
252147c6ae99SBarry Smith #undef __FUNCT__
25221a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25231a266240SBarry Smith /*@C
25241a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25251a266240SBarry Smith 
25261a266240SBarry Smith     Not Collective
25271a266240SBarry Smith 
25281a266240SBarry Smith     Input Parameters:
25291a266240SBarry Smith +   dm - the DM object
25301a266240SBarry Smith -   destroy - the destroy function
25311a266240SBarry Smith 
25321a266240SBarry Smith     Level: intermediate
25331a266240SBarry Smith 
2534e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
25351a266240SBarry Smith 
2536f07f9ceaSJed Brown @*/
25371a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
25381a266240SBarry Smith {
25391a266240SBarry Smith   PetscFunctionBegin;
2540171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25411a266240SBarry Smith   dm->ctxdestroy = destroy;
25421a266240SBarry Smith   PetscFunctionReturn(0);
25431a266240SBarry Smith }
25441a266240SBarry Smith 
25451a266240SBarry Smith #undef __FUNCT__
25461b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2547b07ff414SBarry Smith /*@
25481b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
254947c6ae99SBarry Smith 
255047c6ae99SBarry Smith     Not Collective
255147c6ae99SBarry Smith 
255247c6ae99SBarry Smith     Input Parameters:
255347c6ae99SBarry Smith +   dm - the DM object
255447c6ae99SBarry Smith -   ctx - the user context
255547c6ae99SBarry Smith 
255647c6ae99SBarry Smith     Level: intermediate
255747c6ae99SBarry Smith 
2558e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
255947c6ae99SBarry Smith 
256047c6ae99SBarry Smith @*/
25611b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
256247c6ae99SBarry Smith {
256347c6ae99SBarry Smith   PetscFunctionBegin;
2564171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
256547c6ae99SBarry Smith   dm->ctx = ctx;
256647c6ae99SBarry Smith   PetscFunctionReturn(0);
256747c6ae99SBarry Smith }
256847c6ae99SBarry Smith 
256947c6ae99SBarry Smith #undef __FUNCT__
25701b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
257147c6ae99SBarry Smith /*@
25721b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
257347c6ae99SBarry Smith 
257447c6ae99SBarry Smith     Not Collective
257547c6ae99SBarry Smith 
257647c6ae99SBarry Smith     Input Parameter:
257747c6ae99SBarry Smith .   dm - the DM object
257847c6ae99SBarry Smith 
257947c6ae99SBarry Smith     Output Parameter:
258047c6ae99SBarry Smith .   ctx - the user context
258147c6ae99SBarry Smith 
258247c6ae99SBarry Smith     Level: intermediate
258347c6ae99SBarry Smith 
2584e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
258547c6ae99SBarry Smith 
258647c6ae99SBarry Smith @*/
25871b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
258847c6ae99SBarry Smith {
258947c6ae99SBarry Smith   PetscFunctionBegin;
2590171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25911b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
259247c6ae99SBarry Smith   PetscFunctionReturn(0);
259347c6ae99SBarry Smith }
259447c6ae99SBarry Smith 
259547c6ae99SBarry Smith #undef __FUNCT__
259608da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
259708da532bSDmitry Karpeev /*@C
2598df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
259908da532bSDmitry Karpeev 
260008da532bSDmitry Karpeev     Logically Collective on DM
260108da532bSDmitry Karpeev 
260208da532bSDmitry Karpeev     Input Parameter:
260308da532bSDmitry Karpeev +   dm - the DM object
26040298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
260508da532bSDmitry Karpeev 
260608da532bSDmitry Karpeev     Level: intermediate
260708da532bSDmitry Karpeev 
2608835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
260908da532bSDmitry Karpeev          DMSetJacobian()
261008da532bSDmitry Karpeev 
261108da532bSDmitry Karpeev @*/
261208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
261308da532bSDmitry Karpeev {
261408da532bSDmitry Karpeev   PetscFunctionBegin;
261508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
261608da532bSDmitry Karpeev   PetscFunctionReturn(0);
261708da532bSDmitry Karpeev }
261808da532bSDmitry Karpeev 
261908da532bSDmitry Karpeev #undef __FUNCT__
262008da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
262108da532bSDmitry Karpeev /*@
262208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
262308da532bSDmitry Karpeev 
262408da532bSDmitry Karpeev     Not Collective
262508da532bSDmitry Karpeev 
262608da532bSDmitry Karpeev     Input Parameter:
262708da532bSDmitry Karpeev .   dm - the DM object to destroy
262808da532bSDmitry Karpeev 
262908da532bSDmitry Karpeev     Output Parameter:
263008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
263108da532bSDmitry Karpeev 
263208da532bSDmitry Karpeev     Level: developer
263308da532bSDmitry Karpeev 
263474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
263508da532bSDmitry Karpeev 
263608da532bSDmitry Karpeev @*/
263708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
263808da532bSDmitry Karpeev {
263908da532bSDmitry Karpeev   PetscFunctionBegin;
264008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
264108da532bSDmitry Karpeev   PetscFunctionReturn(0);
264208da532bSDmitry Karpeev }
264308da532bSDmitry Karpeev 
264408da532bSDmitry Karpeev #undef __FUNCT__
264508da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
264608da532bSDmitry Karpeev /*@C
264708da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
264808da532bSDmitry Karpeev 
264908da532bSDmitry Karpeev     Logically Collective on DM
265008da532bSDmitry Karpeev 
265108da532bSDmitry Karpeev     Input Parameters:
2652907376e6SBarry Smith .   dm - the DM object
265308da532bSDmitry Karpeev 
265408da532bSDmitry Karpeev     Output parameters:
265508da532bSDmitry Karpeev +   xl - lower bound
265608da532bSDmitry Karpeev -   xu - upper bound
265708da532bSDmitry Karpeev 
2658907376e6SBarry Smith     Level: advanced
2659907376e6SBarry Smith 
2660907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
266108da532bSDmitry Karpeev 
266274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
266308da532bSDmitry Karpeev 
266408da532bSDmitry Karpeev @*/
266508da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
266608da532bSDmitry Karpeev {
266708da532bSDmitry Karpeev   PetscErrorCode ierr;
26685fd66863SKarl Rupp 
266908da532bSDmitry Karpeev   PetscFunctionBegin;
267008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
267108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
267208da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
267308da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
26748865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
267508da532bSDmitry Karpeev   PetscFunctionReturn(0);
267608da532bSDmitry Karpeev }
267708da532bSDmitry Karpeev 
267808da532bSDmitry Karpeev #undef __FUNCT__
2679b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2680b0ae01b7SPeter Brune /*@
2681b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2682b0ae01b7SPeter Brune 
2683b0ae01b7SPeter Brune     Not Collective
2684b0ae01b7SPeter Brune 
2685b0ae01b7SPeter Brune     Input Parameter:
2686b0ae01b7SPeter Brune .   dm - the DM object
2687b0ae01b7SPeter Brune 
2688b0ae01b7SPeter Brune     Output Parameter:
2689b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2690b0ae01b7SPeter Brune 
2691b0ae01b7SPeter Brune     Level: developer
2692b0ae01b7SPeter Brune 
2693b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2694b0ae01b7SPeter Brune 
2695b0ae01b7SPeter Brune @*/
2696b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2697b0ae01b7SPeter Brune {
2698b0ae01b7SPeter Brune   PetscFunctionBegin;
2699b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2700b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2701b0ae01b7SPeter Brune }
2702b0ae01b7SPeter Brune 
2703b0ae01b7SPeter Brune #undef  __FUNCT__
270408da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2705748fac09SDmitry Karpeev /*@C
270608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
270708da532bSDmitry Karpeev 
270808da532bSDmitry Karpeev     Collective on DM
270908da532bSDmitry Karpeev 
271008da532bSDmitry Karpeev     Input Parameter:
271108da532bSDmitry Karpeev +   dm - the DM object
27120298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
271308da532bSDmitry Karpeev 
271408da532bSDmitry Karpeev     Level: developer
271508da532bSDmitry Karpeev 
271674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
271708da532bSDmitry Karpeev 
271808da532bSDmitry Karpeev @*/
271908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
272008da532bSDmitry Karpeev {
272108da532bSDmitry Karpeev   PetscErrorCode ierr;
27225fd66863SKarl Rupp 
272308da532bSDmitry Karpeev   PetscFunctionBegin;
272408da532bSDmitry Karpeev   if (x) {
272508da532bSDmitry Karpeev     if (!dm->x) {
272608da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
272708da532bSDmitry Karpeev     }
272808da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
27298865f1eaSKarl Rupp   } else if (dm->x) {
273008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
273108da532bSDmitry Karpeev   }
273208da532bSDmitry Karpeev   PetscFunctionReturn(0);
273308da532bSDmitry Karpeev }
273408da532bSDmitry Karpeev 
27350298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2736264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2737264ace61SBarry Smith 
2738264ace61SBarry Smith #undef __FUNCT__
2739264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2740264ace61SBarry Smith /*@C
2741264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2742264ace61SBarry Smith 
2743264ace61SBarry Smith   Collective on DM
2744264ace61SBarry Smith 
2745264ace61SBarry Smith   Input Parameters:
2746264ace61SBarry Smith + dm     - The DM object
2747264ace61SBarry Smith - method - The name of the DM type
2748264ace61SBarry Smith 
2749264ace61SBarry Smith   Options Database Key:
2750264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2751264ace61SBarry Smith 
2752264ace61SBarry Smith   Notes:
2753e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2754264ace61SBarry Smith 
2755264ace61SBarry Smith   Level: intermediate
2756264ace61SBarry Smith 
2757264ace61SBarry Smith .keywords: DM, set, type
2758264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2759264ace61SBarry Smith @*/
276019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2761264ace61SBarry Smith {
2762264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2763264ace61SBarry Smith   PetscBool      match;
2764264ace61SBarry Smith   PetscErrorCode ierr;
2765264ace61SBarry Smith 
2766264ace61SBarry Smith   PetscFunctionBegin;
2767264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2768251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2769264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2770264ace61SBarry Smith 
27710f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
27721c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2773ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2774264ace61SBarry Smith 
2775264ace61SBarry Smith   if (dm->ops->destroy) {
2776264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
27770298fd71SBarry Smith     dm->ops->destroy = NULL;
2778264ace61SBarry Smith   }
2779264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2780264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2781264ace61SBarry Smith   PetscFunctionReturn(0);
2782264ace61SBarry Smith }
2783264ace61SBarry Smith 
2784264ace61SBarry Smith #undef __FUNCT__
2785264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2786264ace61SBarry Smith /*@C
2787264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2788264ace61SBarry Smith 
2789264ace61SBarry Smith   Not Collective
2790264ace61SBarry Smith 
2791264ace61SBarry Smith   Input Parameter:
2792264ace61SBarry Smith . dm  - The DM
2793264ace61SBarry Smith 
2794264ace61SBarry Smith   Output Parameter:
2795264ace61SBarry Smith . type - The DM type name
2796264ace61SBarry Smith 
2797264ace61SBarry Smith   Level: intermediate
2798264ace61SBarry Smith 
2799264ace61SBarry Smith .keywords: DM, get, type, name
2800264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2801264ace61SBarry Smith @*/
280219fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2803264ace61SBarry Smith {
2804264ace61SBarry Smith   PetscErrorCode ierr;
2805264ace61SBarry Smith 
2806264ace61SBarry Smith   PetscFunctionBegin;
2807264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2808c959eef4SJed Brown   PetscValidPointer(type,2);
2809607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2810264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2811264ace61SBarry Smith   PetscFunctionReturn(0);
2812264ace61SBarry Smith }
2813264ace61SBarry Smith 
281467a56275SMatthew G Knepley #undef __FUNCT__
281567a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
281667a56275SMatthew G Knepley /*@C
281767a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
281867a56275SMatthew G Knepley 
281967a56275SMatthew G Knepley   Collective on DM
282067a56275SMatthew G Knepley 
282167a56275SMatthew G Knepley   Input Parameters:
282267a56275SMatthew G Knepley + dm - the DM
282367a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
282467a56275SMatthew G Knepley 
282567a56275SMatthew G Knepley   Output Parameter:
282667a56275SMatthew G Knepley . M - pointer to new DM
282767a56275SMatthew G Knepley 
282867a56275SMatthew G Knepley   Notes:
282967a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
283067a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
283167a56275SMatthew G Knepley   of the input DM.
283267a56275SMatthew G Knepley 
283367a56275SMatthew G Knepley   Level: intermediate
283467a56275SMatthew G Knepley 
283567a56275SMatthew G Knepley .seealso: DMCreate()
283667a56275SMatthew G Knepley @*/
283719fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
283867a56275SMatthew G Knepley {
283967a56275SMatthew G Knepley   DM             B;
284067a56275SMatthew G Knepley   char           convname[256];
284167a56275SMatthew G Knepley   PetscBool      sametype, issame;
284267a56275SMatthew G Knepley   PetscErrorCode ierr;
284367a56275SMatthew G Knepley 
284467a56275SMatthew G Knepley   PetscFunctionBegin;
284567a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
284667a56275SMatthew G Knepley   PetscValidType(dm,1);
284767a56275SMatthew G Knepley   PetscValidPointer(M,3);
2848251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
284967a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
285067a56275SMatthew G Knepley   {
28510298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
285267a56275SMatthew G Knepley 
285367a56275SMatthew G Knepley     /*
285467a56275SMatthew G Knepley        Order of precedence:
285567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
285667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
285767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
285867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
285967a56275SMatthew G Knepley        5) Use a really basic converter.
286067a56275SMatthew G Knepley     */
286167a56275SMatthew G Knepley 
286267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
286367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
286467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
286567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
286667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
286767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28680005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
286967a56275SMatthew G Knepley     if (conv) goto foundconv;
287067a56275SMatthew G Knepley 
287167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
287282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
287367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
287467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
287567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
287667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
287767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
287867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28790005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
288067a56275SMatthew G Knepley     if (conv) {
2881fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
288267a56275SMatthew G Knepley       goto foundconv;
288367a56275SMatthew G Knepley     }
288467a56275SMatthew G Knepley 
288567a56275SMatthew G Knepley #if 0
288667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
288767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2888fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
288967a56275SMatthew G Knepley     if (conv) goto foundconv;
289067a56275SMatthew G Knepley 
289167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
289267a56275SMatthew G Knepley     if (dm->ops->convert) {
289367a56275SMatthew G Knepley       conv = dm->ops->convert;
289467a56275SMatthew G Knepley     }
289567a56275SMatthew G Knepley     if (conv) goto foundconv;
289667a56275SMatthew G Knepley #endif
289767a56275SMatthew G Knepley 
289867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
289982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
290067a56275SMatthew G Knepley 
290167a56275SMatthew G Knepley foundconv:
290267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
290367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
290467a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
290567a56275SMatthew G Knepley   }
290667a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
290767a56275SMatthew G Knepley   PetscFunctionReturn(0);
290867a56275SMatthew G Knepley }
2909264ace61SBarry Smith 
2910264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2911264ace61SBarry Smith 
2912264ace61SBarry Smith #undef __FUNCT__
2913264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2914264ace61SBarry Smith /*@C
29151c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29161c84c290SBarry Smith 
29171c84c290SBarry Smith   Not Collective
29181c84c290SBarry Smith 
29191c84c290SBarry Smith   Input Parameters:
29201c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29211c84c290SBarry Smith - create_func - The creation routine itself
29221c84c290SBarry Smith 
29231c84c290SBarry Smith   Notes:
29241c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29251c84c290SBarry Smith 
29261c84c290SBarry Smith 
29271c84c290SBarry Smith   Sample usage:
29281c84c290SBarry Smith .vb
2929bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
29301c84c290SBarry Smith .ve
29311c84c290SBarry Smith 
29321c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
29331c84c290SBarry Smith .vb
29341c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
29351c84c290SBarry Smith     DMSetType(DM,"my_da");
29361c84c290SBarry Smith .ve
29371c84c290SBarry Smith    or at runtime via the option
29381c84c290SBarry Smith .vb
29391c84c290SBarry Smith     -da_type my_da
29401c84c290SBarry Smith .ve
2941264ace61SBarry Smith 
2942264ace61SBarry Smith   Level: advanced
29431c84c290SBarry Smith 
29441c84c290SBarry Smith .keywords: DM, register
2945bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
29461c84c290SBarry Smith 
2947264ace61SBarry Smith @*/
2948bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2949264ace61SBarry Smith {
2950264ace61SBarry Smith   PetscErrorCode ierr;
2951264ace61SBarry Smith 
2952264ace61SBarry Smith   PetscFunctionBegin;
2953a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2954264ace61SBarry Smith   PetscFunctionReturn(0);
2955264ace61SBarry Smith }
2956264ace61SBarry Smith 
2957b859378eSBarry Smith #undef __FUNCT__
2958b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2959b859378eSBarry Smith /*@C
296055849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2961b859378eSBarry Smith 
2962b859378eSBarry Smith   Collective on PetscViewer
2963b859378eSBarry Smith 
2964b859378eSBarry Smith   Input Parameters:
2965b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2966b859378eSBarry Smith            some related function before a call to DMLoad().
2967b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2968b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2969b859378eSBarry Smith 
2970b859378eSBarry Smith    Level: intermediate
2971b859378eSBarry Smith 
2972b859378eSBarry Smith   Notes:
297355849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2974b859378eSBarry Smith 
2975b859378eSBarry Smith   Notes for advanced users:
2976b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2977b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2978b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2979b859378eSBarry Smith   format is
2980b859378eSBarry Smith .vb
2981b859378eSBarry Smith      has not yet been determined
2982b859378eSBarry Smith .ve
2983b859378eSBarry Smith 
2984b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
2985b859378eSBarry Smith @*/
2986b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
2987b859378eSBarry Smith {
29889331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
2989b859378eSBarry Smith   PetscErrorCode ierr;
2990b859378eSBarry Smith 
2991b859378eSBarry Smith   PetscFunctionBegin;
2992b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
2993b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
299432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
29959331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
29969331c7a4SMatthew G. Knepley   if (isbinary) {
29979331c7a4SMatthew G. Knepley     PetscInt classid;
29989331c7a4SMatthew G. Knepley     char     type[256];
2999b859378eSBarry Smith 
3000060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30019200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3002060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
300332c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30049331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30059331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30069331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30079331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3008b859378eSBarry Smith   PetscFunctionReturn(0);
3009b859378eSBarry Smith }
3010b859378eSBarry Smith 
30117da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30127da65231SMatthew G Knepley 
30137da65231SMatthew G Knepley #undef __FUNCT__
30147da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3015a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3016a6dfd86eSKarl Rupp {
30171d47ebbbSSatish Balay   PetscInt       f;
30181b30c384SMatthew G Knepley   PetscErrorCode ierr;
30191b30c384SMatthew G Knepley 
30207da65231SMatthew G Knepley   PetscFunctionBegin;
302174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30221d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
302357622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30247da65231SMatthew G Knepley   }
30257da65231SMatthew G Knepley   PetscFunctionReturn(0);
30267da65231SMatthew G Knepley }
30277da65231SMatthew G Knepley 
30287da65231SMatthew G Knepley #undef __FUNCT__
30297da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3030a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3031a6dfd86eSKarl Rupp {
30321b30c384SMatthew G Knepley   PetscInt       f, g;
30337da65231SMatthew G Knepley   PetscErrorCode ierr;
30347da65231SMatthew G Knepley 
30357da65231SMatthew G Knepley   PetscFunctionBegin;
303674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30371d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
303874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
30391d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3040e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
30417da65231SMatthew G Knepley     }
304274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30437da65231SMatthew G Knepley   }
30447da65231SMatthew G Knepley   PetscFunctionReturn(0);
30457da65231SMatthew G Knepley }
3046e7c4fc90SDmitry Karpeev 
3047970e74d5SMatthew G Knepley #undef __FUNCT__
3048e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
30496113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3050e759306cSMatthew G. Knepley {
3051e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3052e759306cSMatthew G. Knepley   PetscInt       p;
3053e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3054e759306cSMatthew G. Knepley 
3055e759306cSMatthew G. Knepley   PetscFunctionBegin;
3056e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3057e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3058e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3059e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3060e759306cSMatthew G. Knepley     if (p == rank) {
3061e759306cSMatthew G. Knepley       Vec x;
3062e759306cSMatthew G. Knepley 
3063e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3064e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
30656113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3066e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3067e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3068e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3069e759306cSMatthew G. Knepley     }
3070e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3071e759306cSMatthew G. Knepley   }
3072e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3073e759306cSMatthew G. Knepley }
3074e759306cSMatthew G. Knepley 
3075e759306cSMatthew G. Knepley #undef __FUNCT__
307688ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
307788ed4aceSMatthew G Knepley /*@
307888ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
307988ed4aceSMatthew G Knepley 
308088ed4aceSMatthew G Knepley   Input Parameter:
308188ed4aceSMatthew G Knepley . dm - The DM
308288ed4aceSMatthew G Knepley 
308388ed4aceSMatthew G Knepley   Output Parameter:
308488ed4aceSMatthew G Knepley . section - The PetscSection
308588ed4aceSMatthew G Knepley 
308688ed4aceSMatthew G Knepley   Level: intermediate
308788ed4aceSMatthew G Knepley 
308888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
308988ed4aceSMatthew G Knepley 
309088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
309188ed4aceSMatthew G Knepley @*/
30920adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
30930adebc6cSBarry Smith {
3094fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3095fd59a867SMatthew G. Knepley 
309688ed4aceSMatthew G Knepley   PetscFunctionBegin;
309788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
309888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
30992f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31002f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3101685405a1SBarry Smith     ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);
31022f0f8703SMatthew G. Knepley   }
310388ed4aceSMatthew G Knepley   *section = dm->defaultSection;
310488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
310588ed4aceSMatthew G Knepley }
310688ed4aceSMatthew G Knepley 
310788ed4aceSMatthew G Knepley #undef __FUNCT__
310888ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
310988ed4aceSMatthew G Knepley /*@
311088ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
311188ed4aceSMatthew G Knepley 
311288ed4aceSMatthew G Knepley   Input Parameters:
311388ed4aceSMatthew G Knepley + dm - The DM
311488ed4aceSMatthew G Knepley - section - The PetscSection
311588ed4aceSMatthew G Knepley 
311688ed4aceSMatthew G Knepley   Level: intermediate
311788ed4aceSMatthew G Knepley 
311888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
311988ed4aceSMatthew G Knepley 
312088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
312188ed4aceSMatthew G Knepley @*/
31220adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31230adebc6cSBarry Smith {
3124c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3125af122d2aSMatthew G Knepley   PetscInt       f;
312688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
312788ed4aceSMatthew G Knepley 
312888ed4aceSMatthew G Knepley   PetscFunctionBegin;
312988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3130c473ab19SMatthew G. Knepley   if (section) {
31311d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
31321d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3133c473ab19SMatthew G. Knepley   }
313488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
313588ed4aceSMatthew G Knepley   dm->defaultSection = section;
3136c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3137af122d2aSMatthew G Knepley   if (numFields) {
3138af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3139af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
31400f21e855SMatthew G. Knepley       PetscObject disc;
3141af122d2aSMatthew G Knepley       const char *name;
3142af122d2aSMatthew G Knepley 
3143af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
31440f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
31450f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3146af122d2aSMatthew G Knepley     }
3147af122d2aSMatthew G Knepley   }
31481d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
31491d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
315088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
315188ed4aceSMatthew G Knepley }
315288ed4aceSMatthew G Knepley 
315388ed4aceSMatthew G Knepley #undef __FUNCT__
31549435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
31559435951eSToby Isaac /*@
31569435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
31579435951eSToby Isaac 
3158e228b242SToby Isaac   not collective
3159e228b242SToby Isaac 
31609435951eSToby Isaac   Input Parameter:
31619435951eSToby Isaac . dm - The DM
31629435951eSToby Isaac 
31639435951eSToby Isaac   Output Parameter:
31649435951eSToby 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.
31659435951eSToby 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.
31669435951eSToby Isaac 
31679435951eSToby Isaac   Level: advanced
31689435951eSToby Isaac 
31699435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
31709435951eSToby Isaac 
31719435951eSToby Isaac .seealso: DMSetDefaultConstraints()
31729435951eSToby Isaac @*/
31739435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
31749435951eSToby Isaac {
31759435951eSToby Isaac   PetscErrorCode ierr;
31769435951eSToby Isaac 
31779435951eSToby Isaac   PetscFunctionBegin;
31789435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31799435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
318045a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
318145a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
31829435951eSToby Isaac   PetscFunctionReturn(0);
31839435951eSToby Isaac }
31849435951eSToby Isaac 
31859435951eSToby Isaac #undef __FUNCT__
31869435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
31879435951eSToby Isaac /*@
31889435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
31899435951eSToby Isaac 
31909435951eSToby 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().
31919435951eSToby Isaac 
31929435951eSToby 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.
31939435951eSToby Isaac 
3194e228b242SToby Isaac   collective on dm
3195e228b242SToby Isaac 
31969435951eSToby Isaac   Input Parameters:
31979435951eSToby Isaac + dm - The DM
3198e228b242SToby 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).
3199e228b242SToby 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).
32009435951eSToby Isaac 
32019435951eSToby Isaac   Level: advanced
32029435951eSToby Isaac 
32039435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32049435951eSToby Isaac 
32059435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32069435951eSToby Isaac @*/
32079435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32089435951eSToby Isaac {
3209e228b242SToby Isaac   PetscMPIInt result;
32109435951eSToby Isaac   PetscErrorCode ierr;
32119435951eSToby Isaac 
32129435951eSToby Isaac   PetscFunctionBegin;
32139435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3214e228b242SToby Isaac   if (section) {
3215e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3216e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3217e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3218e228b242SToby Isaac   }
3219e228b242SToby Isaac   if (mat) {
3220e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3221e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3222e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3223e228b242SToby Isaac   }
32249435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32259435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
32269435951eSToby Isaac   dm->defaultConstraintSection = section;
32279435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
32289435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
32299435951eSToby Isaac   dm->defaultConstraintMat = mat;
32309435951eSToby Isaac   PetscFunctionReturn(0);
32319435951eSToby Isaac }
32329435951eSToby Isaac 
3233f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
32349435951eSToby Isaac #undef __FUNCT__
3235284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3236507e4973SMatthew G. Knepley /*
3237507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3238507e4973SMatthew G. Knepley 
3239507e4973SMatthew G. Knepley   Input Parameters:
3240507e4973SMatthew G. Knepley + dm - The DM
3241507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3242507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3243507e4973SMatthew G. Knepley 
3244507e4973SMatthew G. Knepley   Level: intermediate
3245507e4973SMatthew G. Knepley 
3246507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3247507e4973SMatthew G. Knepley */
3248f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3249507e4973SMatthew G. Knepley {
3250507e4973SMatthew G. Knepley   MPI_Comm        comm;
3251507e4973SMatthew G. Knepley   PetscLayout     layout;
3252507e4973SMatthew G. Knepley   const PetscInt *ranges;
3253507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3254507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3255507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3256507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3257507e4973SMatthew G. Knepley 
3258507e4973SMatthew G. Knepley   PetscFunctionBegin;
3259507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3260507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3261507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3262507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3263507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3264507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3265507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3266507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3267507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3268507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3269507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3270507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3271f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3272507e4973SMatthew G. Knepley 
3273507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3274507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3275507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3276507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3277507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3278507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3279507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3280507e4973SMatthew 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;}
3281507e4973SMatthew 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;}
3282507e4973SMatthew G. Knepley     if (gdof < 0) {
3283507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3284507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3285507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3286507e4973SMatthew G. Knepley 
3287507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3288507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3289507e4973SMatthew 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;}
3290507e4973SMatthew G. Knepley       }
3291507e4973SMatthew G. Knepley     }
3292507e4973SMatthew G. Knepley   }
3293507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3294507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3295507e4973SMatthew G. Knepley   ierr = MPI_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3296507e4973SMatthew G. Knepley   if (!gvalid) {
3297507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3298507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3299507e4973SMatthew G. Knepley   }
3300507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3301507e4973SMatthew G. Knepley }
3302f741bcd2SMatthew G. Knepley #endif
3303507e4973SMatthew G. Knepley 
3304507e4973SMatthew G. Knepley #undef __FUNCT__
330588ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
330688ed4aceSMatthew G Knepley /*@
330788ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
330888ed4aceSMatthew G Knepley 
33098b1ab98fSJed Brown   Collective on DM
33108b1ab98fSJed Brown 
331188ed4aceSMatthew G Knepley   Input Parameter:
331288ed4aceSMatthew G Knepley . dm - The DM
331388ed4aceSMatthew G Knepley 
331488ed4aceSMatthew G Knepley   Output Parameter:
331588ed4aceSMatthew G Knepley . section - The PetscSection
331688ed4aceSMatthew G Knepley 
331788ed4aceSMatthew G Knepley   Level: intermediate
331888ed4aceSMatthew G Knepley 
331988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
332088ed4aceSMatthew G Knepley 
332188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
332288ed4aceSMatthew G Knepley @*/
33230adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33240adebc6cSBarry Smith {
332588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
332688ed4aceSMatthew G Knepley 
332788ed4aceSMatthew G Knepley   PetscFunctionBegin;
332888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
332988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
333088ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3331fd59a867SMatthew G. Knepley     PetscSection s;
3332fd59a867SMatthew G. Knepley 
3333fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3334fd59a867SMatthew 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");
3335fd59a867SMatthew 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");
333615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3337cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3338ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3339685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
334088ed4aceSMatthew G Knepley   }
334188ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
334288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
334388ed4aceSMatthew G Knepley }
334488ed4aceSMatthew G Knepley 
334588ed4aceSMatthew G Knepley #undef __FUNCT__
3346b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3347b21d0597SMatthew G Knepley /*@
3348b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3349b21d0597SMatthew G Knepley 
3350b21d0597SMatthew G Knepley   Input Parameters:
3351b21d0597SMatthew G Knepley + dm - The DM
33525080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3353b21d0597SMatthew G Knepley 
3354b21d0597SMatthew G Knepley   Level: intermediate
3355b21d0597SMatthew G Knepley 
3356b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3357b21d0597SMatthew G Knepley 
3358b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3359b21d0597SMatthew G Knepley @*/
33600adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
33610adebc6cSBarry Smith {
3362b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3363b21d0597SMatthew G Knepley 
3364b21d0597SMatthew G Knepley   PetscFunctionBegin;
3365b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33665080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33671d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3368b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3369b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3370507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3371f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3372507e4973SMatthew G. Knepley #endif
3373b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3374b21d0597SMatthew G Knepley }
3375b21d0597SMatthew G Knepley 
3376b21d0597SMatthew G Knepley #undef __FUNCT__
337788ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
337888ed4aceSMatthew G Knepley /*@
337988ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
338088ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
338188ed4aceSMatthew G Knepley 
338288ed4aceSMatthew G Knepley   Input Parameter:
338388ed4aceSMatthew G Knepley . dm - The DM
338488ed4aceSMatthew G Knepley 
338588ed4aceSMatthew G Knepley   Output Parameter:
338688ed4aceSMatthew G Knepley . sf - The PetscSF
338788ed4aceSMatthew G Knepley 
338888ed4aceSMatthew G Knepley   Level: intermediate
338988ed4aceSMatthew G Knepley 
339088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
339188ed4aceSMatthew G Knepley 
339288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
339388ed4aceSMatthew G Knepley @*/
33940adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
33950adebc6cSBarry Smith {
339688ed4aceSMatthew G Knepley   PetscInt       nroots;
339788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
339888ed4aceSMatthew G Knepley 
339988ed4aceSMatthew G Knepley   PetscFunctionBegin;
340088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
340188ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34020298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
340388ed4aceSMatthew G Knepley   if (nroots < 0) {
340488ed4aceSMatthew G Knepley     PetscSection section, gSection;
340588ed4aceSMatthew G Knepley 
340688ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
340731ea6d37SMatthew G Knepley     if (section) {
340888ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
340988ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
341031ea6d37SMatthew G Knepley     } else {
34110298fd71SBarry Smith       *sf = NULL;
341231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
341331ea6d37SMatthew G Knepley     }
341488ed4aceSMatthew G Knepley   }
341588ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
341688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
341788ed4aceSMatthew G Knepley }
341888ed4aceSMatthew G Knepley 
341988ed4aceSMatthew G Knepley #undef __FUNCT__
342088ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
342188ed4aceSMatthew G Knepley /*@
342288ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
342388ed4aceSMatthew G Knepley 
342488ed4aceSMatthew G Knepley   Input Parameters:
342588ed4aceSMatthew G Knepley + dm - The DM
342688ed4aceSMatthew G Knepley - sf - The PetscSF
342788ed4aceSMatthew G Knepley 
342888ed4aceSMatthew G Knepley   Level: intermediate
342988ed4aceSMatthew G Knepley 
343088ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
343188ed4aceSMatthew G Knepley 
343288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
343388ed4aceSMatthew G Knepley @*/
34340adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
34350adebc6cSBarry Smith {
343688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
343788ed4aceSMatthew G Knepley 
343888ed4aceSMatthew G Knepley   PetscFunctionBegin;
343988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
344088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
344188ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
344288ed4aceSMatthew G Knepley   dm->defaultSF = sf;
344388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
344488ed4aceSMatthew G Knepley }
344588ed4aceSMatthew G Knepley 
344688ed4aceSMatthew G Knepley #undef __FUNCT__
344788ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
344888ed4aceSMatthew G Knepley /*@C
344988ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
345088ed4aceSMatthew G Knepley   describing the data layout.
345188ed4aceSMatthew G Knepley 
345288ed4aceSMatthew G Knepley   Input Parameters:
345388ed4aceSMatthew G Knepley + dm - The DM
345488ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
345588ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
345688ed4aceSMatthew G Knepley 
345788ed4aceSMatthew G Knepley   Level: intermediate
345888ed4aceSMatthew G Knepley 
345988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
346088ed4aceSMatthew G Knepley @*/
346188ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
346288ed4aceSMatthew G Knepley {
346382f516ccSBarry Smith   MPI_Comm       comm;
346488ed4aceSMatthew G Knepley   PetscLayout    layout;
346588ed4aceSMatthew G Knepley   const PetscInt *ranges;
346688ed4aceSMatthew G Knepley   PetscInt       *local;
346788ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3468ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
346988ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
347088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
347188ed4aceSMatthew G Knepley 
347288ed4aceSMatthew G Knepley   PetscFunctionBegin;
347382f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
347488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
347588ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
347688ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
347788ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
347888ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
347988ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
348088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
348188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
348288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
348388ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3484ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
34856636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
348688ed4aceSMatthew G Knepley 
34876636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
34886636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3489235fbf56SMatthew 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));
34906636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
349188ed4aceSMatthew G Knepley   }
3492785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3493785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
349488ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
34951f588964SMatthew G Knepley     const PetscInt *cind;
34966636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
349788ed4aceSMatthew G Knepley 
349888ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
349988ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
350088ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
350188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
350288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35036636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
350488ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35056636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35066636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35076636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3508057b4bcdSMatthew 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);
35096636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35106636e97aSMatthew G Knepley     }
351188ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
351288ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
351388ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
351488ed4aceSMatthew G Knepley     }
351588ed4aceSMatthew G Knepley     if (gdof < 0) {
35166636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
351788ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
351888ed4aceSMatthew G Knepley 
351905376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
352031d3f06eSJed Brown         if (r < 0) r = -(r+2);
352105376888SMatthew 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);
352288ed4aceSMatthew G Knepley         remote[l].rank  = r;
352388ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
352488ed4aceSMatthew G Knepley       }
352588ed4aceSMatthew G Knepley     } else {
35266636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
352788ed4aceSMatthew G Knepley         remote[l].rank  = rank;
352888ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
352988ed4aceSMatthew G Knepley       }
353088ed4aceSMatthew G Knepley     }
353188ed4aceSMatthew G Knepley   }
35326636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
353388ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
353488ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
353588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
353688ed4aceSMatthew G Knepley }
3537af122d2aSMatthew G Knepley 
3538af122d2aSMatthew G Knepley #undef __FUNCT__
3539b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3540b21d0597SMatthew G Knepley /*@
3541b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3542b21d0597SMatthew G Knepley 
3543b21d0597SMatthew G Knepley   Input Parameter:
3544b21d0597SMatthew G Knepley . dm - The DM
3545b21d0597SMatthew G Knepley 
3546b21d0597SMatthew G Knepley   Output Parameter:
3547b21d0597SMatthew G Knepley . sf - The PetscSF
3548b21d0597SMatthew G Knepley 
3549b21d0597SMatthew G Knepley   Level: intermediate
3550b21d0597SMatthew G Knepley 
3551b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3552b21d0597SMatthew G Knepley 
3553057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3554b21d0597SMatthew G Knepley @*/
35550adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
35560adebc6cSBarry Smith {
3557b21d0597SMatthew G Knepley   PetscFunctionBegin;
3558b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3559b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3560b21d0597SMatthew G Knepley   *sf = dm->sf;
3561b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3562b21d0597SMatthew G Knepley }
3563b21d0597SMatthew G Knepley 
3564b21d0597SMatthew G Knepley #undef __FUNCT__
3565057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3566057b4bcdSMatthew G Knepley /*@
3567057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3568057b4bcdSMatthew G Knepley 
3569057b4bcdSMatthew G Knepley   Input Parameters:
3570057b4bcdSMatthew G Knepley + dm - The DM
3571057b4bcdSMatthew G Knepley - sf - The PetscSF
3572057b4bcdSMatthew G Knepley 
3573057b4bcdSMatthew G Knepley   Level: intermediate
3574057b4bcdSMatthew G Knepley 
3575057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3576057b4bcdSMatthew G Knepley @*/
35770adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
35780adebc6cSBarry Smith {
3579057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3580057b4bcdSMatthew G Knepley 
3581057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3582057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3583057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3584057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3585057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3586057b4bcdSMatthew G Knepley   dm->sf = sf;
3587057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3588057b4bcdSMatthew G Knepley }
3589057b4bcdSMatthew G Knepley 
3590057b4bcdSMatthew G Knepley #undef __FUNCT__
35912764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
35922764a2aaSMatthew G. Knepley /*@
35932764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
35942764a2aaSMatthew G. Knepley 
35952764a2aaSMatthew G. Knepley   Input Parameter:
35962764a2aaSMatthew G. Knepley . dm - The DM
35972764a2aaSMatthew G. Knepley 
35982764a2aaSMatthew G. Knepley   Output Parameter:
35992764a2aaSMatthew G. Knepley . prob - The PetscDS
36002764a2aaSMatthew G. Knepley 
36012764a2aaSMatthew G. Knepley   Level: developer
36022764a2aaSMatthew G. Knepley 
36032764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36042764a2aaSMatthew G. Knepley @*/
36052764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3606af122d2aSMatthew G Knepley {
3607af122d2aSMatthew G Knepley   PetscFunctionBegin;
3608af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36090f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36100f21e855SMatthew G. Knepley   *prob = dm->prob;
36110f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36120f21e855SMatthew G. Knepley }
36130f21e855SMatthew G. Knepley 
36140f21e855SMatthew G. Knepley #undef __FUNCT__
36152764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36162764a2aaSMatthew G. Knepley /*@
36172764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36182764a2aaSMatthew G. Knepley 
36192764a2aaSMatthew G. Knepley   Input Parameters:
36202764a2aaSMatthew G. Knepley + dm - The DM
36212764a2aaSMatthew G. Knepley - prob - The PetscDS
36222764a2aaSMatthew G. Knepley 
36232764a2aaSMatthew G. Knepley   Level: developer
36242764a2aaSMatthew G. Knepley 
36252764a2aaSMatthew G. Knepley .seealso: DMGetDS()
36262764a2aaSMatthew G. Knepley @*/
36272764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
36280f21e855SMatthew G. Knepley {
36290f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36300f21e855SMatthew G. Knepley 
36310f21e855SMatthew G. Knepley   PetscFunctionBegin;
36320f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36332764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
36342764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
36350f21e855SMatthew G. Knepley   dm->prob = prob;
36360f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
36370f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36380f21e855SMatthew G. Knepley }
36390f21e855SMatthew G. Knepley 
36400f21e855SMatthew G. Knepley #undef __FUNCT__
36410f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
36420f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
36430f21e855SMatthew G. Knepley {
36440f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36450f21e855SMatthew G. Knepley 
36460f21e855SMatthew G. Knepley   PetscFunctionBegin;
36470f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36482764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3649af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3650af122d2aSMatthew G Knepley }
3651af122d2aSMatthew G Knepley 
3652af122d2aSMatthew G Knepley #undef __FUNCT__
3653af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3654af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3655af122d2aSMatthew G Knepley {
36560f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3657af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3658af122d2aSMatthew G Knepley 
3659af122d2aSMatthew G Knepley   PetscFunctionBegin;
3660af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36612764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
36620f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
36630f21e855SMatthew G. Knepley     PetscContainer obj;
36640f21e855SMatthew G. Knepley 
36650f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
36662764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
36670f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3668af122d2aSMatthew G Knepley   }
3669af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3670af122d2aSMatthew G Knepley }
3671af122d2aSMatthew G Knepley 
3672af122d2aSMatthew G Knepley #undef __FUNCT__
3673af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3674c1929be8SMatthew G. Knepley /*@
3675c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3676c1929be8SMatthew G. Knepley 
3677c1929be8SMatthew G. Knepley   Not collective
3678c1929be8SMatthew G. Knepley 
3679c1929be8SMatthew G. Knepley   Input Parameters:
3680c1929be8SMatthew G. Knepley + dm - The DM
3681c1929be8SMatthew G. Knepley - f  - The field number
3682c1929be8SMatthew G. Knepley 
3683c1929be8SMatthew G. Knepley   Output Parameter:
3684c1929be8SMatthew G. Knepley . field - The discretization object
3685c1929be8SMatthew G. Knepley 
3686c1929be8SMatthew G. Knepley   Level: developer
3687c1929be8SMatthew G. Knepley 
3688c1929be8SMatthew G. Knepley .seealso: DMSetField()
3689c1929be8SMatthew G. Knepley @*/
3690af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3691af122d2aSMatthew G Knepley {
36920f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36930f21e855SMatthew G. Knepley 
3694af122d2aSMatthew G Knepley   PetscFunctionBegin;
3695af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36962764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3697decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3698decb47aaSMatthew G. Knepley }
3699decb47aaSMatthew G. Knepley 
3700decb47aaSMatthew G. Knepley #undef __FUNCT__
3701decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3702c1929be8SMatthew G. Knepley /*@
3703c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3704c1929be8SMatthew G. Knepley 
3705c1929be8SMatthew G. Knepley   Logically collective on DM
3706c1929be8SMatthew G. Knepley 
3707c1929be8SMatthew G. Knepley   Input Parameters:
3708c1929be8SMatthew G. Knepley + dm - The DM
3709c1929be8SMatthew G. Knepley . f  - The field number
3710c1929be8SMatthew G. Knepley - field - The discretization object
3711c1929be8SMatthew G. Knepley 
3712c1929be8SMatthew G. Knepley   Level: developer
3713c1929be8SMatthew G. Knepley 
3714c1929be8SMatthew G. Knepley .seealso: DMGetField()
3715c1929be8SMatthew G. Knepley @*/
3716decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3717decb47aaSMatthew G. Knepley {
3718decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3719decb47aaSMatthew G. Knepley 
3720decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3721decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37222764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3723af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3724af122d2aSMatthew G Knepley }
37256636e97aSMatthew G Knepley 
37266636e97aSMatthew G Knepley #undef __FUNCT__
3727b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3728b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3729b64e0483SPeter Brune {
3730b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3731b64e0483SPeter Brune   PetscErrorCode ierr;
3732b64e0483SPeter Brune   Vec coords,ccoords;
37336dbf9973SLawrence Mitchell   Mat inject;
3734b64e0483SPeter Brune   PetscFunctionBegin;
3735b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3736b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3737b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3738b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3739b64e0483SPeter Brune   if (coords && !ccoords) {
3740b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
37416dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
37422adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
37436dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3744b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3745b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3746b64e0483SPeter Brune   }
3747b64e0483SPeter Brune   PetscFunctionReturn(0);
3748b64e0483SPeter Brune }
3749b64e0483SPeter Brune 
3750b64e0483SPeter Brune #undef __FUNCT__
375103dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
375203dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
375303dadc2fSPeter Brune {
375403dadc2fSPeter Brune   DM dm_coord,subdm_coord;
375503dadc2fSPeter Brune   PetscErrorCode ierr;
375603dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
375703dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
375803dadc2fSPeter Brune   PetscFunctionBegin;
375903dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
376003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
376103dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
376203dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
376303dadc2fSPeter Brune   if (coords && !ccoords) {
376403dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
376503dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
376603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
376703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
376803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
376903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
377003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
377103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
377203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
377303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
377403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
377503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
377603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
377703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
377803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
377903dadc2fSPeter Brune   }
378003dadc2fSPeter Brune   PetscFunctionReturn(0);
378103dadc2fSPeter Brune }
378203dadc2fSPeter Brune 
378303dadc2fSPeter Brune #undef __FUNCT__
3784c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3785c73cfb54SMatthew G. Knepley /*@
3786c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3787c73cfb54SMatthew G. Knepley 
3788c73cfb54SMatthew G. Knepley   Not collective
3789c73cfb54SMatthew G. Knepley 
3790c73cfb54SMatthew G. Knepley   Input Parameter:
3791c73cfb54SMatthew G. Knepley . dm - The DM
3792c73cfb54SMatthew G. Knepley 
3793c73cfb54SMatthew G. Knepley   Output Parameter:
3794c73cfb54SMatthew G. Knepley . dim - The topological dimension
3795c73cfb54SMatthew G. Knepley 
3796c73cfb54SMatthew G. Knepley   Level: beginner
3797c73cfb54SMatthew G. Knepley 
3798c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3799c73cfb54SMatthew G. Knepley @*/
3800c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3801c73cfb54SMatthew G. Knepley {
3802c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3803c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3804c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3805c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3806c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3807c73cfb54SMatthew G. Knepley }
3808c73cfb54SMatthew G. Knepley 
3809c73cfb54SMatthew G. Knepley #undef __FUNCT__
3810c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3811c73cfb54SMatthew G. Knepley /*@
3812c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3813c73cfb54SMatthew G. Knepley 
3814c73cfb54SMatthew G. Knepley   Collective on dm
3815c73cfb54SMatthew G. Knepley 
3816c73cfb54SMatthew G. Knepley   Input Parameters:
3817c73cfb54SMatthew G. Knepley + dm - The DM
3818c73cfb54SMatthew G. Knepley - dim - The topological dimension
3819c73cfb54SMatthew G. Knepley 
3820c73cfb54SMatthew G. Knepley   Level: beginner
3821c73cfb54SMatthew G. Knepley 
3822c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3823c73cfb54SMatthew G. Knepley @*/
3824c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3825c73cfb54SMatthew G. Knepley {
3826c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3827c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3828c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3829c73cfb54SMatthew G. Knepley   dm->dim = dim;
3830c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3831c73cfb54SMatthew G. Knepley }
3832c73cfb54SMatthew G. Knepley 
3833793f3fe5SMatthew G. Knepley #undef __FUNCT__
3834793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3835793f3fe5SMatthew G. Knepley /*@
3836793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3837793f3fe5SMatthew G. Knepley 
3838793f3fe5SMatthew G. Knepley   Collective on DM
3839793f3fe5SMatthew G. Knepley 
3840793f3fe5SMatthew G. Knepley   Input Parameters:
3841793f3fe5SMatthew G. Knepley + dm - the DM
3842793f3fe5SMatthew G. Knepley - dim - the dimension
3843793f3fe5SMatthew G. Knepley 
3844793f3fe5SMatthew G. Knepley   Output Parameters:
3845793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3846793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3847793f3fe5SMatthew G. Knepley 
3848793f3fe5SMatthew G. Knepley   Note:
3849793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3850793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3851793f3fe5SMatthew G. Knepley   then the interval is empty.
3852793f3fe5SMatthew G. Knepley 
3853793f3fe5SMatthew G. Knepley   Level: intermediate
3854793f3fe5SMatthew G. Knepley 
3855793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3856793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3857793f3fe5SMatthew G. Knepley @*/
3858793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3859793f3fe5SMatthew G. Knepley {
3860793f3fe5SMatthew G. Knepley   PetscInt       d;
3861793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3862793f3fe5SMatthew G. Knepley 
3863793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3864793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3865793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3866793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3867793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3868793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3869793f3fe5SMatthew G. Knepley }
3870793f3fe5SMatthew G. Knepley 
3871793f3fe5SMatthew G. Knepley #undef __FUNCT__
38726636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
38736636e97aSMatthew G Knepley /*@
38746636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
38756636e97aSMatthew G Knepley 
38766636e97aSMatthew G Knepley   Collective on DM
38776636e97aSMatthew G Knepley 
38786636e97aSMatthew G Knepley   Input Parameters:
38796636e97aSMatthew G Knepley + dm - the DM
38806636e97aSMatthew G Knepley - c - coordinate vector
38816636e97aSMatthew G Knepley 
38826636e97aSMatthew G Knepley   Note:
38836636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
38846636e97aSMatthew G Knepley 
38856636e97aSMatthew G Knepley   Level: intermediate
38866636e97aSMatthew G Knepley 
38876636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
38886636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
38896636e97aSMatthew G Knepley @*/
38906636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
38916636e97aSMatthew G Knepley {
38926636e97aSMatthew G Knepley   PetscErrorCode ierr;
38936636e97aSMatthew G Knepley 
38946636e97aSMatthew G Knepley   PetscFunctionBegin;
38956636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
38966636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
38976636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
38986636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
38996636e97aSMatthew G Knepley   dm->coordinates = c;
39006636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3901b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
390203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39036636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39046636e97aSMatthew G Knepley }
39056636e97aSMatthew G Knepley 
39066636e97aSMatthew G Knepley #undef __FUNCT__
39076636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39086636e97aSMatthew G Knepley /*@
39096636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39106636e97aSMatthew G Knepley 
39116636e97aSMatthew G Knepley   Collective on DM
39126636e97aSMatthew G Knepley 
39136636e97aSMatthew G Knepley    Input Parameters:
39146636e97aSMatthew G Knepley +  dm - the DM
39156636e97aSMatthew G Knepley -  c - coordinate vector
39166636e97aSMatthew G Knepley 
39176636e97aSMatthew G Knepley   Note:
39186636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39196636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39206636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39216636e97aSMatthew G Knepley 
39226636e97aSMatthew G Knepley   Level: intermediate
39236636e97aSMatthew G Knepley 
39246636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39256636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39266636e97aSMatthew G Knepley @*/
39276636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
39286636e97aSMatthew G Knepley {
39296636e97aSMatthew G Knepley   PetscErrorCode ierr;
39306636e97aSMatthew G Knepley 
39316636e97aSMatthew G Knepley   PetscFunctionBegin;
39326636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39336636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39346636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39356636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
39368865f1eaSKarl Rupp 
39376636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
39388865f1eaSKarl Rupp 
39396636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39406636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39416636e97aSMatthew G Knepley }
39426636e97aSMatthew G Knepley 
39436636e97aSMatthew G Knepley #undef __FUNCT__
39446636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
39456636e97aSMatthew G Knepley /*@
39466636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
39476636e97aSMatthew G Knepley 
39486636e97aSMatthew G Knepley   Not Collective
39496636e97aSMatthew G Knepley 
39506636e97aSMatthew G Knepley   Input Parameter:
39516636e97aSMatthew G Knepley . dm - the DM
39526636e97aSMatthew G Knepley 
39536636e97aSMatthew G Knepley   Output Parameter:
39546636e97aSMatthew G Knepley . c - global coordinate vector
39556636e97aSMatthew G Knepley 
39566636e97aSMatthew G Knepley   Note:
39576636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
39586636e97aSMatthew G Knepley 
39596636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
39606636e97aSMatthew G Knepley 
39616636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
39626636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
39636636e97aSMatthew G Knepley 
39646636e97aSMatthew G Knepley   Level: intermediate
39656636e97aSMatthew G Knepley 
39666636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39676636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
39686636e97aSMatthew G Knepley @*/
39696636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
39706636e97aSMatthew G Knepley {
39716636e97aSMatthew G Knepley   PetscErrorCode ierr;
39726636e97aSMatthew G Knepley 
39736636e97aSMatthew G Knepley   PetscFunctionBegin;
39746636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39756636e97aSMatthew G Knepley   PetscValidPointer(c,2);
39761f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
39770298fd71SBarry Smith     DM cdm = NULL;
39786636e97aSMatthew G Knepley 
39796636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
39806636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
39816636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
39826636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
39836636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
39846636e97aSMatthew G Knepley   }
39856636e97aSMatthew G Knepley   *c = dm->coordinates;
39866636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39876636e97aSMatthew G Knepley }
39886636e97aSMatthew G Knepley 
39896636e97aSMatthew G Knepley #undef __FUNCT__
39906636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
39916636e97aSMatthew G Knepley /*@
39926636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
39936636e97aSMatthew G Knepley 
39946636e97aSMatthew G Knepley   Collective on DM
39956636e97aSMatthew G Knepley 
39966636e97aSMatthew G Knepley   Input Parameter:
39976636e97aSMatthew G Knepley . dm - the DM
39986636e97aSMatthew G Knepley 
39996636e97aSMatthew G Knepley   Output Parameter:
40006636e97aSMatthew G Knepley . c - coordinate vector
40016636e97aSMatthew G Knepley 
40026636e97aSMatthew G Knepley   Note:
40036636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40046636e97aSMatthew G Knepley 
40056636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40066636e97aSMatthew G Knepley 
40076636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40086636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40096636e97aSMatthew G Knepley 
40106636e97aSMatthew G Knepley   Level: intermediate
40116636e97aSMatthew G Knepley 
40126636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40136636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40146636e97aSMatthew G Knepley @*/
40156636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40166636e97aSMatthew G Knepley {
40176636e97aSMatthew G Knepley   PetscErrorCode ierr;
40186636e97aSMatthew G Knepley 
40196636e97aSMatthew G Knepley   PetscFunctionBegin;
40206636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40216636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40221f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40230298fd71SBarry Smith     DM cdm = NULL;
40246636e97aSMatthew G Knepley 
40256636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40266636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
40276636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
40286636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40296636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40306636e97aSMatthew G Knepley   }
40316636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
40326636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40336636e97aSMatthew G Knepley }
40346636e97aSMatthew G Knepley 
40356636e97aSMatthew G Knepley #undef __FUNCT__
40366636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
40376636e97aSMatthew G Knepley /*@
40381cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
40396636e97aSMatthew G Knepley 
40406636e97aSMatthew G Knepley   Collective on DM
40416636e97aSMatthew G Knepley 
40426636e97aSMatthew G Knepley   Input Parameter:
40436636e97aSMatthew G Knepley . dm - the DM
40446636e97aSMatthew G Knepley 
40456636e97aSMatthew G Knepley   Output Parameter:
40466636e97aSMatthew G Knepley . cdm - coordinate DM
40476636e97aSMatthew G Knepley 
40486636e97aSMatthew G Knepley   Level: intermediate
40496636e97aSMatthew G Knepley 
40506636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40511cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40526636e97aSMatthew G Knepley @*/
40536636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
40546636e97aSMatthew G Knepley {
40556636e97aSMatthew G Knepley   PetscErrorCode ierr;
40566636e97aSMatthew G Knepley 
40576636e97aSMatthew G Knepley   PetscFunctionBegin;
40586636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40596636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
40606636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
406182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
40626636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
40636636e97aSMatthew G Knepley   }
40646636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
40656636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40666636e97aSMatthew G Knepley }
4067e87bb0d3SMatthew G Knepley 
4068e87bb0d3SMatthew G Knepley #undef __FUNCT__
40691cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
40701cfe2091SMatthew G. Knepley /*@
40711cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
40721cfe2091SMatthew G. Knepley 
40731cfe2091SMatthew G. Knepley   Logically Collective on DM
40741cfe2091SMatthew G. Knepley 
40751cfe2091SMatthew G. Knepley   Input Parameters:
40761cfe2091SMatthew G. Knepley + dm - the DM
40771cfe2091SMatthew G. Knepley - cdm - coordinate DM
40781cfe2091SMatthew G. Knepley 
40791cfe2091SMatthew G. Knepley   Level: intermediate
40801cfe2091SMatthew G. Knepley 
40811cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40821cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40831cfe2091SMatthew G. Knepley @*/
40841cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
40851cfe2091SMatthew G. Knepley {
40861cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
40871cfe2091SMatthew G. Knepley 
40881cfe2091SMatthew G. Knepley   PetscFunctionBegin;
40891cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40901cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
40911cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
40921cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
40931cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
40941cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
40951cfe2091SMatthew G. Knepley }
40961cfe2091SMatthew G. Knepley 
40971cfe2091SMatthew G. Knepley #undef __FUNCT__
409846e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
409946e270d4SMatthew G. Knepley /*@
410046e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
410146e270d4SMatthew G. Knepley 
410246e270d4SMatthew G. Knepley   Not Collective
410346e270d4SMatthew G. Knepley 
410446e270d4SMatthew G. Knepley   Input Parameter:
410546e270d4SMatthew G. Knepley . dm - The DM object
410646e270d4SMatthew G. Knepley 
410746e270d4SMatthew G. Knepley   Output Parameter:
410846e270d4SMatthew G. Knepley . dim - The embedding dimension
410946e270d4SMatthew G. Knepley 
411046e270d4SMatthew G. Knepley   Level: intermediate
411146e270d4SMatthew G. Knepley 
411246e270d4SMatthew G. Knepley .keywords: mesh, coordinates
411346e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
411446e270d4SMatthew G. Knepley @*/
411546e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
411646e270d4SMatthew G. Knepley {
411746e270d4SMatthew G. Knepley   PetscFunctionBegin;
411846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
411946e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41209a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41219a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41229a9a41abSToby Isaac   }
412346e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
412446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
412546e270d4SMatthew G. Knepley }
412646e270d4SMatthew G. Knepley 
412746e270d4SMatthew G. Knepley #undef __FUNCT__
412846e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
412946e270d4SMatthew G. Knepley /*@
413046e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
413146e270d4SMatthew G. Knepley 
413246e270d4SMatthew G. Knepley   Not Collective
413346e270d4SMatthew G. Knepley 
413446e270d4SMatthew G. Knepley   Input Parameters:
413546e270d4SMatthew G. Knepley + dm  - The DM object
413646e270d4SMatthew G. Knepley - dim - The embedding dimension
413746e270d4SMatthew G. Knepley 
413846e270d4SMatthew G. Knepley   Level: intermediate
413946e270d4SMatthew G. Knepley 
414046e270d4SMatthew G. Knepley .keywords: mesh, coordinates
414146e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
414246e270d4SMatthew G. Knepley @*/
414346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
414446e270d4SMatthew G. Knepley {
414546e270d4SMatthew G. Knepley   PetscFunctionBegin;
414646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
414746e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
414846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
414946e270d4SMatthew G. Knepley }
415046e270d4SMatthew G. Knepley 
415146e270d4SMatthew G. Knepley #undef __FUNCT__
4152e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4153e8abe2deSMatthew G. Knepley /*@
4154e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4155e8abe2deSMatthew G. Knepley 
4156e8abe2deSMatthew G. Knepley   Not Collective
4157e8abe2deSMatthew G. Knepley 
4158e8abe2deSMatthew G. Knepley   Input Parameter:
4159e8abe2deSMatthew G. Knepley . dm - The DM object
4160e8abe2deSMatthew G. Knepley 
4161e8abe2deSMatthew G. Knepley   Output Parameter:
4162e8abe2deSMatthew G. Knepley . section - The PetscSection object
4163e8abe2deSMatthew G. Knepley 
4164e8abe2deSMatthew G. Knepley   Level: intermediate
4165e8abe2deSMatthew G. Knepley 
4166e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4167e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4168e8abe2deSMatthew G. Knepley @*/
4169e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4170e8abe2deSMatthew G. Knepley {
4171e8abe2deSMatthew G. Knepley   DM             cdm;
4172e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4173e8abe2deSMatthew G. Knepley 
4174e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4175e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4176e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4177e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4178e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4179e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4180e8abe2deSMatthew G. Knepley }
4181e8abe2deSMatthew G. Knepley 
4182e8abe2deSMatthew G. Knepley #undef __FUNCT__
4183e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4184e8abe2deSMatthew G. Knepley /*@
4185e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4186e8abe2deSMatthew G. Knepley 
4187e8abe2deSMatthew G. Knepley   Not Collective
4188e8abe2deSMatthew G. Knepley 
4189e8abe2deSMatthew G. Knepley   Input Parameters:
4190e8abe2deSMatthew G. Knepley + dm      - The DM object
419146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4192e8abe2deSMatthew G. Knepley - section - The PetscSection object
4193e8abe2deSMatthew G. Knepley 
4194e8abe2deSMatthew G. Knepley   Level: intermediate
4195e8abe2deSMatthew G. Knepley 
4196e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4197e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4198e8abe2deSMatthew G. Knepley @*/
419946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4200e8abe2deSMatthew G. Knepley {
4201e8abe2deSMatthew G. Knepley   DM             cdm;
4202e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4203e8abe2deSMatthew G. Knepley 
4204e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4205e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
420646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4207e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4208e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
420946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
421046e270d4SMatthew G. Knepley     PetscInt d = dim;
421146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
421246e270d4SMatthew G. Knepley 
421346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
421446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
421546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
421646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
421746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
421846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
421946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
422046e270d4SMatthew G. Knepley     }
422146e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
422246e270d4SMatthew G. Knepley   }
4223e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4224e8abe2deSMatthew G. Knepley }
4225e8abe2deSMatthew G. Knepley 
4226e8abe2deSMatthew G. Knepley #undef __FUNCT__
4227c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
42285dc8c3f7SMatthew G. Knepley /*@C
42295dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42305dc8c3f7SMatthew G. Knepley 
42315dc8c3f7SMatthew G. Knepley   Input Parameters:
42325dc8c3f7SMatthew G. Knepley + dm      - The DM object
42335dc8c3f7SMatthew 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
42345dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42355dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42365dc8c3f7SMatthew G. Knepley 
42375dc8c3f7SMatthew G. Knepley   Level: developer
42385dc8c3f7SMatthew G. Knepley 
42395dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42405dc8c3f7SMatthew G. Knepley @*/
42415dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4242c6b900c6SMatthew G. Knepley {
4243c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4244c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4245c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4246c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
42475dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4248c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4249c6b900c6SMatthew G. Knepley }
4250c6b900c6SMatthew G. Knepley 
4251c6b900c6SMatthew G. Knepley #undef __FUNCT__
4252c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
42535dc8c3f7SMatthew G. Knepley /*@C
42545dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42555dc8c3f7SMatthew G. Knepley 
42565dc8c3f7SMatthew G. Knepley   Input Parameters:
42575dc8c3f7SMatthew G. Knepley + dm      - The DM object
42585dc8c3f7SMatthew 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
42595dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42605dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42615dc8c3f7SMatthew G. Knepley 
42625dc8c3f7SMatthew G. Knepley   Level: developer
42635dc8c3f7SMatthew G. Knepley 
42645dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42655dc8c3f7SMatthew G. Knepley @*/
42665dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4267c6b900c6SMatthew G. Knepley {
4268c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4269c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4270c6b900c6SMatthew G. Knepley 
4271c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4272c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42735dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
42745dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
42755dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
42765dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
42775dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4278c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4279c6b900c6SMatthew G. Knepley }
4280c6b900c6SMatthew G. Knepley 
4281c6b900c6SMatthew G. Knepley #undef __FUNCT__
4282e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4283e87bb0d3SMatthew G Knepley /*@
4284e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4285e87bb0d3SMatthew G Knepley 
4286e87bb0d3SMatthew G Knepley   Not collective
4287e87bb0d3SMatthew G Knepley 
4288e87bb0d3SMatthew G Knepley   Input Parameters:
4289e87bb0d3SMatthew G Knepley + dm - The DM
4290e87bb0d3SMatthew G Knepley - v - The Vec of points
4291e87bb0d3SMatthew G Knepley 
429261e3bb9bSMatthew G Knepley   Output Parameter:
4293e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4294e87bb0d3SMatthew G Knepley 
4295e87bb0d3SMatthew G Knepley   Level: developer
429661e3bb9bSMatthew G Knepley 
429761e3bb9bSMatthew G Knepley .keywords: point location, mesh
429861e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
429961e3bb9bSMatthew G Knepley @*/
4300e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4301e87bb0d3SMatthew G Knepley {
4302735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4303735aa83eSMatthew G Knepley 
4304e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4305e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4306e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4307e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
4308735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4309735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
431082f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
4311e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4312e87bb0d3SMatthew G Knepley }
431314f150ffSMatthew G. Knepley 
431414f150ffSMatthew G. Knepley #undef __FUNCT__
431514f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4316f4d763aaSMatthew G. Knepley /*@
4317f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4318f4d763aaSMatthew G. Knepley 
4319f4d763aaSMatthew G. Knepley   Input Parameter:
4320f4d763aaSMatthew G. Knepley . dm - The original DM
4321f4d763aaSMatthew G. Knepley 
4322f4d763aaSMatthew G. Knepley   Output Parameter:
4323f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4324f4d763aaSMatthew G. Knepley 
4325f4d763aaSMatthew G. Knepley   Level: intermediate
4326f4d763aaSMatthew G. Knepley 
4327f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4328f4d763aaSMatthew G. Knepley @*/
432914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
433014f150ffSMatthew G. Knepley {
4331c26acbdeSMatthew G. Knepley   PetscSection   section;
4332c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
433314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
433414f150ffSMatthew G. Knepley 
433514f150ffSMatthew G. Knepley   PetscFunctionBegin;
433614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
433714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4338c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4339c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4340c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4341c26acbdeSMatthew G. Knepley     *odm = dm;
4342c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4343c26acbdeSMatthew G. Knepley   }
434414f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4345c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
434614f150ffSMatthew G. Knepley     PetscSF      sf;
434714f150ffSMatthew G. Knepley 
434814f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
434914f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
435014f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
435114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
435214f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
435315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
435414f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
435514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
435614f150ffSMatthew G. Knepley   }
435714f150ffSMatthew G. Knepley   *odm = dm->dmBC;
435814f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
435914f150ffSMatthew G. Knepley }
4360f4d763aaSMatthew G. Knepley 
4361f4d763aaSMatthew G. Knepley #undef __FUNCT__
4362f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4363f4d763aaSMatthew G. Knepley /*@
4364cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4365f4d763aaSMatthew G. Knepley 
4366f4d763aaSMatthew G. Knepley   Input Parameter:
4367f4d763aaSMatthew G. Knepley . dm - The original DM
4368f4d763aaSMatthew G. Knepley 
4369cdb7a50dSMatthew G. Knepley   Output Parameters:
4370cdb7a50dSMatthew G. Knepley + num - The output sequence number
4371cdb7a50dSMatthew G. Knepley - val - The output sequence value
4372f4d763aaSMatthew G. Knepley 
4373f4d763aaSMatthew G. Knepley   Level: intermediate
4374f4d763aaSMatthew G. Knepley 
4375f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4376f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4377f4d763aaSMatthew G. Knepley 
4378f4d763aaSMatthew G. Knepley .seealso: VecView()
4379f4d763aaSMatthew G. Knepley @*/
4380cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4381f4d763aaSMatthew G. Knepley {
4382f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4383f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4384cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4385cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4386f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4387f4d763aaSMatthew G. Knepley }
4388f4d763aaSMatthew G. Knepley 
4389f4d763aaSMatthew G. Knepley #undef __FUNCT__
4390f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4391f4d763aaSMatthew G. Knepley /*@
4392cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4393f4d763aaSMatthew G. Knepley 
4394f4d763aaSMatthew G. Knepley   Input Parameters:
4395f4d763aaSMatthew G. Knepley + dm - The original DM
4396cdb7a50dSMatthew G. Knepley . num - The output sequence number
4397cdb7a50dSMatthew G. Knepley - val - The output sequence value
4398f4d763aaSMatthew G. Knepley 
4399f4d763aaSMatthew G. Knepley   Level: intermediate
4400f4d763aaSMatthew G. Knepley 
4401f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4402f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4403f4d763aaSMatthew G. Knepley 
4404f4d763aaSMatthew G. Knepley .seealso: VecView()
4405f4d763aaSMatthew G. Knepley @*/
4406cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4407f4d763aaSMatthew G. Knepley {
4408f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4409f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4410f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4411cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4412cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4413cdb7a50dSMatthew G. Knepley }
4414cdb7a50dSMatthew G. Knepley 
4415cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4416cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4417cdb7a50dSMatthew G. Knepley /*@C
4418cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4419cdb7a50dSMatthew G. Knepley 
4420cdb7a50dSMatthew G. Knepley   Input Parameters:
4421cdb7a50dSMatthew G. Knepley + dm   - The original DM
4422cdb7a50dSMatthew G. Knepley . name - The sequence name
4423cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4424cdb7a50dSMatthew G. Knepley 
4425cdb7a50dSMatthew G. Knepley   Output Parameter:
4426cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4427cdb7a50dSMatthew G. Knepley 
4428cdb7a50dSMatthew G. Knepley   Level: intermediate
4429cdb7a50dSMatthew G. Knepley 
4430cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4431cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4432cdb7a50dSMatthew G. Knepley 
4433cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4434cdb7a50dSMatthew G. Knepley @*/
4435cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4436cdb7a50dSMatthew G. Knepley {
4437cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4438cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4439cdb7a50dSMatthew G. Knepley 
4440cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4441cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4442cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4443cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4444cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4445cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4446cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4447cdb7a50dSMatthew G. Knepley     PetscScalar value;
4448cdb7a50dSMatthew G. Knepley 
4449cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
44504aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4451cdb7a50dSMatthew G. Knepley #endif
4452cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4453f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4454f4d763aaSMatthew G. Knepley }
44558e4ac7eaSMatthew G. Knepley 
44568e4ac7eaSMatthew G. Knepley #undef __FUNCT__
44578e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
44588e4ac7eaSMatthew G. Knepley /*@
44598e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
44608e4ac7eaSMatthew G. Knepley 
44618e4ac7eaSMatthew G. Knepley   Not collective
44628e4ac7eaSMatthew G. Knepley 
44638e4ac7eaSMatthew G. Knepley   Input Parameter:
44648e4ac7eaSMatthew G. Knepley . dm - The DM
44658e4ac7eaSMatthew G. Knepley 
44668e4ac7eaSMatthew G. Knepley   Output Parameter:
44678e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
44688e4ac7eaSMatthew G. Knepley 
44698e4ac7eaSMatthew G. Knepley   Level: beginner
44708e4ac7eaSMatthew G. Knepley 
44718e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
44728e4ac7eaSMatthew G. Knepley @*/
44738e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
44748e4ac7eaSMatthew G. Knepley {
44758e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
44768e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44778e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
44788e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
44798e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
44808e4ac7eaSMatthew G. Knepley }
44818e4ac7eaSMatthew G. Knepley 
44828e4ac7eaSMatthew G. Knepley #undef __FUNCT__
44838e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
44848e4ac7eaSMatthew G. Knepley /*@
44858e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
44868e4ac7eaSMatthew G. Knepley 
44878e4ac7eaSMatthew G. Knepley   Collective on dm
44888e4ac7eaSMatthew G. Knepley 
44898e4ac7eaSMatthew G. Knepley   Input Parameters:
44908e4ac7eaSMatthew G. Knepley + dm - The DM
44918e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
44928e4ac7eaSMatthew G. Knepley 
44938e4ac7eaSMatthew G. Knepley   Level: beginner
44948e4ac7eaSMatthew G. Knepley 
44958e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
44968e4ac7eaSMatthew G. Knepley @*/
44978e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
44988e4ac7eaSMatthew G. Knepley {
44998e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45008e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45018e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45028e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45038e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45048e4ac7eaSMatthew G. Knepley }
4505