xref: /petsc/src/dm/interface/dm.c (revision ae71db08854436db1798aba527d2f0323b4bfaca)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>     /*I      "petscdm.h"     I*/
20c312b8eSJed Brown #include <petscsf.h>
32764a2aaSMatthew G. Knepley #include <petscds.h>
447c6ae99SBarry Smith 
5732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
6cea54e9dSPatrick Farrell PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_CreateInterpolation;
767a56275SMatthew G Knepley 
8bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
9bff4a2f0SMatthew G. Knepley 
1047c6ae99SBarry Smith #undef __FUNCT__
11a4121054SBarry Smith #define __FUNCT__ "DMCreate"
12a4121054SBarry Smith /*@
13de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
14a4121054SBarry Smith 
15a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
16a4121054SBarry Smith    error when you try to use the vector.
17a4121054SBarry Smith 
18a4121054SBarry Smith   Collective on MPI_Comm
19a4121054SBarry Smith 
20a4121054SBarry Smith   Input Parameter:
21a4121054SBarry Smith . comm - The communicator for the DM object
22a4121054SBarry Smith 
23a4121054SBarry Smith   Output Parameter:
24a4121054SBarry Smith . dm - The DM object
25a4121054SBarry Smith 
26a4121054SBarry Smith   Level: beginner
27a4121054SBarry Smith 
288472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
29a4121054SBarry Smith @*/
307087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
31a4121054SBarry Smith {
32a4121054SBarry Smith   DM             v;
33a4121054SBarry Smith   PetscErrorCode ierr;
34a4121054SBarry Smith 
35a4121054SBarry Smith   PetscFunctionBegin;
361411c6eeSJed Brown   PetscValidPointer(dm,2);
370298fd71SBarry Smith   *dm = NULL;
388b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
39607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
40607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
41607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
42a4121054SBarry Smith 
4373107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
44e7c4fc90SDmitry Karpeev 
450298fd71SBarry Smith   v->ltogmap                  = NULL;
461411c6eeSJed Brown   v->bs                       = 1;
47171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
4888ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
4988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
500298fd71SBarry Smith   v->defaultSection           = NULL;
510298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
52fba222abSToby Isaac   v->defaultConstraintSection = NULL;
53fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
54c6b900c6SMatthew G. Knepley   v->L                        = NULL;
55c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
565dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
579a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
58435a35e8SMatthew G Knepley   {
59435a35e8SMatthew G Knepley     PetscInt i;
60435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
610298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
62435a35e8SMatthew G Knepley     }
63435a35e8SMatthew G Knepley   }
642764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
6514f150ffSMatthew G. Knepley   v->dmBC = NULL;
66f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
67cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
68c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
69b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
701411c6eeSJed Brown   *dm = v;
71a4121054SBarry Smith   PetscFunctionReturn(0);
72a4121054SBarry Smith }
73a4121054SBarry Smith 
74a4121054SBarry Smith #undef __FUNCT__
7538221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
7638221697SMatthew G. Knepley /*@
7738221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
7838221697SMatthew G. Knepley 
7938221697SMatthew G. Knepley   Collective on MPI_Comm
8038221697SMatthew G. Knepley 
8138221697SMatthew G. Knepley   Input Parameter:
8238221697SMatthew G. Knepley . dm - The original DM object
8338221697SMatthew G. Knepley 
8438221697SMatthew G. Knepley   Output Parameter:
8538221697SMatthew G. Knepley . newdm  - The new DM object
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley   Level: beginner
8838221697SMatthew G. Knepley 
8938221697SMatthew G. Knepley .keywords: DM, topology, create
9038221697SMatthew G. Knepley @*/
9138221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
9238221697SMatthew G. Knepley {
9338221697SMatthew G. Knepley   PetscSF        sf;
9438221697SMatthew G. Knepley   Vec            coords;
9538221697SMatthew G. Knepley   void          *ctx;
961de53e9aSMatthew G. Knepley   PetscInt       dim;
9738221697SMatthew G. Knepley   PetscErrorCode ierr;
9838221697SMatthew G. Knepley 
9938221697SMatthew G. Knepley   PetscFunctionBegin;
10038221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10138221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
10238221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject)dm), newdm);CHKERRQ(ierr);
1031de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1041de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
10538221697SMatthew G. Knepley   if (dm->ops->clone) {
10638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
10738221697SMatthew G. Knepley   }
108cd27c7c9SMatthew G. Knepley   (*newdm)->setupcalled = PETSC_TRUE;
10938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
11038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
11138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
11238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
113be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
114be4c1c3eSMatthew G. Knepley     DM           ncdm;
115be4c1c3eSMatthew G. Knepley     PetscSection cs;
116be4c1c3eSMatthew G. Knepley     PetscInt     pEnd = -1;
117be4c1c3eSMatthew G. Knepley 
118be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
119be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
120be4c1c3eSMatthew G. Knepley     if (pEnd >= 0) {
121be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
122be4c1c3eSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
123be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
124be4c1c3eSMatthew G. Knepley     }
125be4c1c3eSMatthew G. Knepley   }
12638221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
12738221697SMatthew G. Knepley   if (coords) {
12838221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
12938221697SMatthew G. Knepley   } else {
13038221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
13138221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
13238221697SMatthew G. Knepley   }
133c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
134c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1355dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1365dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1375dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
138c6b900c6SMatthew G. Knepley   }
13938221697SMatthew G. Knepley   PetscFunctionReturn(0);
14038221697SMatthew G. Knepley }
14138221697SMatthew G. Knepley 
14238221697SMatthew G. Knepley #undef __FUNCT__
1439a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1449a42bb27SBarry Smith /*@C
145564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1469a42bb27SBarry Smith 
1478472ad0fSDave May    Logically Collective on DM
1489a42bb27SBarry Smith 
1499a42bb27SBarry Smith    Input Parameter:
1509a42bb27SBarry Smith +  da - initial distributed array
1518154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1529a42bb27SBarry Smith 
1539a42bb27SBarry Smith    Options Database:
154dd85299cSBarry Smith .   -dm_vec_type ctype
1559a42bb27SBarry Smith 
1569a42bb27SBarry Smith    Level: intermediate
1579a42bb27SBarry Smith 
1588472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1599a42bb27SBarry Smith @*/
16019fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1619a42bb27SBarry Smith {
1629a42bb27SBarry Smith   PetscErrorCode ierr;
1639a42bb27SBarry Smith 
1649a42bb27SBarry Smith   PetscFunctionBegin;
1659a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1669a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
16719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1689a42bb27SBarry Smith   PetscFunctionReturn(0);
1699a42bb27SBarry Smith }
1709a42bb27SBarry Smith 
1719a42bb27SBarry Smith #undef __FUNCT__
172c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
173c0dedaeaSBarry Smith /*@C
174c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
175c0dedaeaSBarry Smith 
1768472ad0fSDave May    Logically Collective on DM
177c0dedaeaSBarry Smith 
178c0dedaeaSBarry Smith    Input Parameter:
179c0dedaeaSBarry Smith .  da - initial distributed array
180c0dedaeaSBarry Smith 
181c0dedaeaSBarry Smith    Output Parameter:
182c0dedaeaSBarry Smith .  ctype - the vector type
183c0dedaeaSBarry Smith 
184c0dedaeaSBarry Smith    Level: intermediate
185c0dedaeaSBarry Smith 
1868472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
187c0dedaeaSBarry Smith @*/
188c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
189c0dedaeaSBarry Smith {
190c0dedaeaSBarry Smith   PetscFunctionBegin;
191c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
192c0dedaeaSBarry Smith   *ctype = da->vectype;
193c0dedaeaSBarry Smith   PetscFunctionReturn(0);
194c0dedaeaSBarry Smith }
195c0dedaeaSBarry Smith 
196c0dedaeaSBarry Smith #undef __FUNCT__
1975f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
1985f1ad066SMatthew G Knepley /*@
19934f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2005f1ad066SMatthew G Knepley 
2015f1ad066SMatthew G Knepley   Not collective
2025f1ad066SMatthew G Knepley 
2035f1ad066SMatthew G Knepley   Input Parameter:
2045f1ad066SMatthew G Knepley . v - The Vec
2055f1ad066SMatthew G Knepley 
2065f1ad066SMatthew G Knepley   Output Parameter:
2075f1ad066SMatthew G Knepley . dm - The DM
2085f1ad066SMatthew G Knepley 
2095f1ad066SMatthew G Knepley   Level: intermediate
2105f1ad066SMatthew G Knepley 
2115f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2125f1ad066SMatthew G Knepley @*/
2135f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2145f1ad066SMatthew G Knepley {
2155f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2165f1ad066SMatthew G Knepley 
2175f1ad066SMatthew G Knepley   PetscFunctionBegin;
2185f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2195f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2205f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2215f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2225f1ad066SMatthew G Knepley }
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley #undef __FUNCT__
2255f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2265f1ad066SMatthew G Knepley /*@
227d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2285f1ad066SMatthew G Knepley 
2295f1ad066SMatthew G Knepley   Not collective
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley   Input Parameters:
2325f1ad066SMatthew G Knepley + v - The Vec
2335f1ad066SMatthew G Knepley - dm - The DM
2345f1ad066SMatthew G Knepley 
235d9805387SMatthew 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.
236d9805387SMatthew G. Knepley 
2375f1ad066SMatthew G Knepley   Level: intermediate
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2405f1ad066SMatthew G Knepley @*/
2415f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2425f1ad066SMatthew G Knepley {
2435f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley   PetscFunctionBegin;
2465f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
247d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2485f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2495f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2505f1ad066SMatthew G Knepley }
2515f1ad066SMatthew G Knepley 
2525f1ad066SMatthew G Knepley #undef __FUNCT__
253521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
254521d9a4cSLisandro Dalcin /*@C
255521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
256521d9a4cSLisandro Dalcin 
257521d9a4cSLisandro Dalcin    Logically Collective on DM
258521d9a4cSLisandro Dalcin 
259521d9a4cSLisandro Dalcin    Input Parameter:
260521d9a4cSLisandro Dalcin +  dm - the DM context
261521d9a4cSLisandro Dalcin .  ctype - the matrix type
262521d9a4cSLisandro Dalcin 
263521d9a4cSLisandro Dalcin    Options Database:
264521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
265521d9a4cSLisandro Dalcin 
266521d9a4cSLisandro Dalcin    Level: intermediate
267521d9a4cSLisandro Dalcin 
268c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
269521d9a4cSLisandro Dalcin @*/
27019fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
271521d9a4cSLisandro Dalcin {
272521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
27388f0584fSBarry Smith 
274521d9a4cSLisandro Dalcin   PetscFunctionBegin;
275521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
276521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
27719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
278521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
279521d9a4cSLisandro Dalcin }
280521d9a4cSLisandro Dalcin 
281521d9a4cSLisandro Dalcin #undef __FUNCT__
282c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
283c0dedaeaSBarry Smith /*@C
284c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
285c0dedaeaSBarry Smith 
286c0dedaeaSBarry Smith    Logically Collective on DM
287c0dedaeaSBarry Smith 
288c0dedaeaSBarry Smith    Input Parameter:
289c0dedaeaSBarry Smith .  dm - the DM context
290c0dedaeaSBarry Smith 
291c0dedaeaSBarry Smith    Output Parameter:
292c0dedaeaSBarry Smith .  ctype - the matrix type
293c0dedaeaSBarry Smith 
294c0dedaeaSBarry Smith    Options Database:
295c0dedaeaSBarry Smith .   -dm_mat_type ctype
296c0dedaeaSBarry Smith 
297c0dedaeaSBarry Smith    Level: intermediate
298c0dedaeaSBarry Smith 
299c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
300c0dedaeaSBarry Smith @*/
301c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
302c0dedaeaSBarry Smith {
303c0dedaeaSBarry Smith   PetscFunctionBegin;
304c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
305c0dedaeaSBarry Smith   *ctype = dm->mattype;
306c0dedaeaSBarry Smith   PetscFunctionReturn(0);
307c0dedaeaSBarry Smith }
308c0dedaeaSBarry Smith 
309c0dedaeaSBarry Smith #undef __FUNCT__
310c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
311c688c046SMatthew G Knepley /*@
31234f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
313c688c046SMatthew G Knepley 
314c688c046SMatthew G Knepley   Not collective
315c688c046SMatthew G Knepley 
316c688c046SMatthew G Knepley   Input Parameter:
317c688c046SMatthew G Knepley . A - The Mat
318c688c046SMatthew G Knepley 
319c688c046SMatthew G Knepley   Output Parameter:
320c688c046SMatthew G Knepley . dm - The DM
321c688c046SMatthew G Knepley 
322c688c046SMatthew G Knepley   Level: intermediate
323c688c046SMatthew G Knepley 
324c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
325c688c046SMatthew G Knepley @*/
326c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
327c688c046SMatthew G Knepley {
328c688c046SMatthew G Knepley   PetscErrorCode ierr;
329c688c046SMatthew G Knepley 
330c688c046SMatthew G Knepley   PetscFunctionBegin;
331c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
332c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
333c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
334c688c046SMatthew G Knepley   PetscFunctionReturn(0);
335c688c046SMatthew G Knepley }
336c688c046SMatthew G Knepley 
337c688c046SMatthew G Knepley #undef __FUNCT__
338c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
339c688c046SMatthew G Knepley /*@
340c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
341c688c046SMatthew G Knepley 
342c688c046SMatthew G Knepley   Not collective
343c688c046SMatthew G Knepley 
344c688c046SMatthew G Knepley   Input Parameters:
345c688c046SMatthew G Knepley + A - The Mat
346c688c046SMatthew G Knepley - dm - The DM
347c688c046SMatthew G Knepley 
348c688c046SMatthew G Knepley   Level: intermediate
349c688c046SMatthew G Knepley 
350c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
351c688c046SMatthew G Knepley @*/
352c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
353c688c046SMatthew G Knepley {
354c688c046SMatthew G Knepley   PetscErrorCode ierr;
355c688c046SMatthew G Knepley 
356c688c046SMatthew G Knepley   PetscFunctionBegin;
357c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3588865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
359c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
360c688c046SMatthew G Knepley   PetscFunctionReturn(0);
361c688c046SMatthew G Knepley }
362c688c046SMatthew G Knepley 
363c688c046SMatthew G Knepley #undef __FUNCT__
3649a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3659a42bb27SBarry Smith /*@C
3669a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3676757b960SDave May    DM options in the database.
3689a42bb27SBarry Smith 
3698353ddbbSDave May    Logically Collective on DM
3709a42bb27SBarry Smith 
3719a42bb27SBarry Smith    Input Parameter:
3728353ddbbSDave May +  da - the DM context
3739a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3749a42bb27SBarry Smith 
3759a42bb27SBarry Smith    Notes:
3769a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3779a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3789a42bb27SBarry Smith 
3799a42bb27SBarry Smith    Level: advanced
3809a42bb27SBarry Smith 
3818353ddbbSDave May .keywords: DM, set, options, prefix, database
3829a42bb27SBarry Smith 
3839a42bb27SBarry Smith .seealso: DMSetFromOptions()
3849a42bb27SBarry Smith @*/
3857087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
3869a42bb27SBarry Smith {
3879a42bb27SBarry Smith   PetscErrorCode ierr;
3889a42bb27SBarry Smith 
3899a42bb27SBarry Smith   PetscFunctionBegin;
3909a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3919a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
392691be533SLawrence Mitchell   if (dm->sf) {
393691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
394691be533SLawrence Mitchell   }
395691be533SLawrence Mitchell   if (dm->defaultSF) {
396691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
397691be533SLawrence Mitchell   }
3989a42bb27SBarry Smith   PetscFunctionReturn(0);
3999a42bb27SBarry Smith }
4009a42bb27SBarry Smith 
4019a42bb27SBarry Smith #undef __FUNCT__
40231697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
40331697293SDave May /*@C
40431697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
40531697293SDave May    DM options in the database.
40631697293SDave May 
40731697293SDave May    Logically Collective on DM
40831697293SDave May 
40931697293SDave May    Input Parameters:
41031697293SDave May +  dm - the DM context
41131697293SDave May -  prefix - the prefix string to prepend to all DM option requests
41231697293SDave May 
41331697293SDave May    Notes:
41431697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
41531697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
41631697293SDave May 
41731697293SDave May    Level: advanced
41831697293SDave May 
41931697293SDave May .keywords: DM, append, options, prefix, database
42031697293SDave May 
42131697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
42231697293SDave May @*/
42331697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
42431697293SDave May {
42531697293SDave May   PetscErrorCode ierr;
42631697293SDave May 
42731697293SDave May   PetscFunctionBegin;
42831697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42931697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
43031697293SDave May   PetscFunctionReturn(0);
43131697293SDave May }
43231697293SDave May 
43331697293SDave May #undef __FUNCT__
43431697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
43531697293SDave May /*@C
43631697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
43731697293SDave May    DM options in the database.
43831697293SDave May 
43931697293SDave May    Not Collective
44031697293SDave May 
44131697293SDave May    Input Parameters:
44231697293SDave May .  dm - the DM context
44331697293SDave May 
44431697293SDave May    Output Parameters:
44531697293SDave May .  prefix - pointer to the prefix string used is returned
44631697293SDave May 
44731697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
44831697293SDave May    sufficient length to hold the prefix.
44931697293SDave May 
45031697293SDave May    Level: advanced
45131697293SDave May 
45231697293SDave May .keywords: DM, set, options, prefix, database
45331697293SDave May 
45431697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
45531697293SDave May @*/
45631697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
45731697293SDave May {
45831697293SDave May   PetscErrorCode ierr;
45931697293SDave May 
46031697293SDave May   PetscFunctionBegin;
46131697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46231697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
46331697293SDave May   PetscFunctionReturn(0);
46431697293SDave May }
46531697293SDave May 
46631697293SDave May #undef __FUNCT__
46747c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
46847c6ae99SBarry Smith /*@
4698472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
47047c6ae99SBarry Smith 
47147c6ae99SBarry Smith     Collective on DM
47247c6ae99SBarry Smith 
47347c6ae99SBarry Smith     Input Parameter:
47447c6ae99SBarry Smith .   dm - the DM object to destroy
47547c6ae99SBarry Smith 
47647c6ae99SBarry Smith     Level: developer
47747c6ae99SBarry Smith 
478e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
47947c6ae99SBarry Smith 
48047c6ae99SBarry Smith @*/
481fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
48247c6ae99SBarry Smith {
483c83e3748SMatthew G. Knepley   PetscInt       i, cnt = 0;
484dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
48547c6ae99SBarry Smith   PetscErrorCode ierr;
48647c6ae99SBarry Smith 
48747c6ae99SBarry Smith   PetscFunctionBegin;
4886bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
4896bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
49087e657c6SBarry Smith 
49187e657c6SBarry Smith   /* count all the circular references of DM and its contained Vecs */
492732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
4938865f1eaSKarl Rupp     if ((*dm)->localin[i])  cnt++;
4948865f1eaSKarl Rupp     if ((*dm)->globalin[i]) cnt++;
495732e2eb9SMatthew G Knepley   }
496dfe15315SJed Brown   for (nlink=(*dm)->namedglobal; nlink; nlink=nlink->next) cnt++;
4972348bcf4SPeter Brune   for (nlink=(*dm)->namedlocal; nlink; nlink=nlink->next) cnt++;
4982348bcf4SPeter Brune   if ((*dm)->x) {
4992348bcf4SPeter Brune     DM obj;
5002348bcf4SPeter Brune     ierr = VecGetDM((*dm)->x, &obj);CHKERRQ(ierr);
5012348bcf4SPeter Brune     if (obj == *dm) cnt++;
5022348bcf4SPeter Brune   }
503732e2eb9SMatthew G Knepley 
5046bf464f9SBarry Smith   if (--((PetscObject)(*dm))->refct - cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
505732e2eb9SMatthew G Knepley   /*
506732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
507732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
508732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
509732e2eb9SMatthew G Knepley   */
5106bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5116bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
512732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5136bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5146bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
515732e2eb9SMatthew G Knepley   }
516f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5170298fd71SBarry Smith   (*dm)->namedglobal = NULL;
518f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5192348bcf4SPeter Brune     nnext = nlink->next;
5202348bcf4SPeter 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);
5212348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5222348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5232348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5242348bcf4SPeter Brune   }
525f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5260298fd71SBarry Smith   (*dm)->namedlocal = NULL;
527f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
528f490541aSPeter Brune     nnext = nlink->next;
529f490541aSPeter 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);
530f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
531f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
532f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
533f490541aSPeter Brune   }
5342348bcf4SPeter Brune 
535b17ce1afSJed Brown   /* Destroy the list of hooks */
536c833c3b5SJed Brown   {
537c833c3b5SJed Brown     DMCoarsenHookLink link,next;
538b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
539b17ce1afSJed Brown       next = link->next;
540b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
541b17ce1afSJed Brown     }
5420298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
543c833c3b5SJed Brown   }
544c833c3b5SJed Brown   {
545c833c3b5SJed Brown     DMRefineHookLink link,next;
546c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
547c833c3b5SJed Brown       next = link->next;
548c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
549c833c3b5SJed Brown     }
5500298fd71SBarry Smith     (*dm)->refinehook = NULL;
551c833c3b5SJed Brown   }
552be081cd6SPeter Brune   {
553be081cd6SPeter Brune     DMSubDomainHookLink link,next;
554be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
555be081cd6SPeter Brune       next = link->next;
556be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
557be081cd6SPeter Brune     }
5580298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
559be081cd6SPeter Brune   }
560baf369e7SPeter Brune   {
561baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
562baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
563baf369e7SPeter Brune       next = link->next;
564baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
565baf369e7SPeter Brune     }
5660298fd71SBarry Smith     (*dm)->gtolhook = NULL;
567baf369e7SPeter Brune   }
568d4d07f1eSToby Isaac   {
569d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
570d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
571d4d07f1eSToby Isaac       next = link->next;
572d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
573d4d07f1eSToby Isaac     }
574d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
575d4d07f1eSToby Isaac   }
576aa1993deSMatthew G Knepley   /* Destroy the work arrays */
577aa1993deSMatthew G Knepley   {
578aa1993deSMatthew G Knepley     DMWorkLink link,next;
579aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
580aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
581aa1993deSMatthew G Knepley       next = link->next;
582aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
583aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
584aa1993deSMatthew G Knepley     }
5850298fd71SBarry Smith     (*dm)->workin = NULL;
586aa1993deSMatthew G Knepley   }
587b17ce1afSJed Brown 
58852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
58952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
59052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
59152536dc3SBarry Smith 
5921a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
5931a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
5941a266240SBarry Smith   }
59587e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
59671cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
5974dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
5986bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
5996bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
600073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
60188ed4aceSMatthew G Knepley 
60288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
60388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6048b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
605fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
606fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
60788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
60888ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
6098e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
610af122d2aSMatthew G Knepley 
6116636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
6126636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
6136636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
6145dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
6156636e97aSMatthew G Knepley 
6162764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
61714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
618e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
619e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
620732e2eb9SMatthew G Knepley 
6216bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
622435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
623732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
62447c6ae99SBarry Smith   PetscFunctionReturn(0);
62547c6ae99SBarry Smith }
62647c6ae99SBarry Smith 
62747c6ae99SBarry Smith #undef __FUNCT__
628d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
629d7bf68aeSBarry Smith /*@
630d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
631d7bf68aeSBarry Smith 
632d7bf68aeSBarry Smith     Collective on DM
633d7bf68aeSBarry Smith 
634d7bf68aeSBarry Smith     Input Parameter:
635d7bf68aeSBarry Smith .   dm - the DM object to setup
636d7bf68aeSBarry Smith 
637d7bf68aeSBarry Smith     Level: developer
638d7bf68aeSBarry Smith 
639e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
640d7bf68aeSBarry Smith 
641d7bf68aeSBarry Smith @*/
6427087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
643d7bf68aeSBarry Smith {
644d7bf68aeSBarry Smith   PetscErrorCode ierr;
645d7bf68aeSBarry Smith 
646d7bf68aeSBarry Smith   PetscFunctionBegin;
647171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6488387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
649d7bf68aeSBarry Smith   if (dm->ops->setup) {
650d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
651d7bf68aeSBarry Smith   }
6528387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
653d7bf68aeSBarry Smith   PetscFunctionReturn(0);
654d7bf68aeSBarry Smith }
655d7bf68aeSBarry Smith 
656d7bf68aeSBarry Smith #undef __FUNCT__
657d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
658d7bf68aeSBarry Smith /*@
659d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
660d7bf68aeSBarry Smith 
661d7bf68aeSBarry Smith     Collective on DM
662d7bf68aeSBarry Smith 
663d7bf68aeSBarry Smith     Input Parameter:
664d7bf68aeSBarry Smith .   dm - the DM object to set options for
665d7bf68aeSBarry Smith 
666732e2eb9SMatthew G Knepley     Options Database:
6674164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
6684164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
6694164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
6704164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
671732e2eb9SMatthew G Knepley 
672d7bf68aeSBarry Smith     Level: developer
673d7bf68aeSBarry Smith 
674e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
675d7bf68aeSBarry Smith 
676d7bf68aeSBarry Smith @*/
6777087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
678d7bf68aeSBarry Smith {
6797781c08eSBarry Smith   char           typeName[256];
680ca266f36SBarry Smith   PetscBool      flg;
681d7bf68aeSBarry Smith   PetscErrorCode ierr;
682d7bf68aeSBarry Smith 
683d7bf68aeSBarry Smith   PetscFunctionBegin;
684171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
685691be533SLawrence Mitchell   if (dm->sf) {
686691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
687691be533SLawrence Mitchell   }
688691be533SLawrence Mitchell   if (dm->defaultSF) {
689691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
690691be533SLawrence Mitchell   }
6913194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
6920298fd71SBarry 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);
693a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
694f9ba7244SBarry Smith   if (flg) {
695f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
696f9ba7244SBarry Smith   }
697a264d7a6SBarry 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);
698073dac72SJed Brown   if (flg) {
699521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
700073dac72SJed Brown   }
7010298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
702f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
703e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
704f9ba7244SBarry Smith   }
705f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
7060633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
70782fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
708d7bf68aeSBarry Smith   PetscFunctionReturn(0);
709d7bf68aeSBarry Smith }
710d7bf68aeSBarry Smith 
711d7bf68aeSBarry Smith #undef __FUNCT__
71247c6ae99SBarry Smith #define __FUNCT__ "DMView"
713fc9bc008SSatish Balay /*@C
714224748a4SBarry Smith     DMView - Views a DM
71547c6ae99SBarry Smith 
71647c6ae99SBarry Smith     Collective on DM
71747c6ae99SBarry Smith 
71847c6ae99SBarry Smith     Input Parameter:
71947c6ae99SBarry Smith +   dm - the DM object to view
72047c6ae99SBarry Smith -   v - the viewer
72147c6ae99SBarry Smith 
722224748a4SBarry Smith     Level: beginner
72347c6ae99SBarry Smith 
724e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
72547c6ae99SBarry Smith 
72647c6ae99SBarry Smith @*/
7277087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
72847c6ae99SBarry Smith {
72947c6ae99SBarry Smith   PetscErrorCode ierr;
73032c0f0efSBarry Smith   PetscBool      isbinary;
73147c6ae99SBarry Smith 
73247c6ae99SBarry Smith   PetscFunctionBegin;
733171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7343014e516SBarry Smith   if (!v) {
735ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
7363014e516SBarry Smith   }
73798c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
73832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
73932c0f0efSBarry Smith   if (isbinary) {
74055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
74132c0f0efSBarry Smith     char     type[256];
74232c0f0efSBarry Smith 
74332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
74432c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
74532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
74632c0f0efSBarry Smith   }
7470c010503SBarry Smith   if (dm->ops->view) {
7480c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
74947c6ae99SBarry Smith   }
75047c6ae99SBarry Smith   PetscFunctionReturn(0);
75147c6ae99SBarry Smith }
75247c6ae99SBarry Smith 
75347c6ae99SBarry Smith #undef __FUNCT__
75447c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
75547c6ae99SBarry Smith /*@
7568472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
75747c6ae99SBarry Smith 
75847c6ae99SBarry Smith     Collective on DM
75947c6ae99SBarry Smith 
76047c6ae99SBarry Smith     Input Parameter:
76147c6ae99SBarry Smith .   dm - the DM object
76247c6ae99SBarry Smith 
76347c6ae99SBarry Smith     Output Parameter:
76447c6ae99SBarry Smith .   vec - the global vector
76547c6ae99SBarry Smith 
766073dac72SJed Brown     Level: beginner
76747c6ae99SBarry Smith 
768e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
76947c6ae99SBarry Smith 
77047c6ae99SBarry Smith @*/
7717087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
77247c6ae99SBarry Smith {
77347c6ae99SBarry Smith   PetscErrorCode ierr;
77447c6ae99SBarry Smith 
77547c6ae99SBarry Smith   PetscFunctionBegin;
776171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77747c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
77847c6ae99SBarry Smith   PetscFunctionReturn(0);
77947c6ae99SBarry Smith }
78047c6ae99SBarry Smith 
78147c6ae99SBarry Smith #undef __FUNCT__
78247c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
78347c6ae99SBarry Smith /*@
7848472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
78547c6ae99SBarry Smith 
78647c6ae99SBarry Smith     Not Collective
78747c6ae99SBarry Smith 
78847c6ae99SBarry Smith     Input Parameter:
78947c6ae99SBarry Smith .   dm - the DM object
79047c6ae99SBarry Smith 
79147c6ae99SBarry Smith     Output Parameter:
79247c6ae99SBarry Smith .   vec - the local vector
79347c6ae99SBarry Smith 
794073dac72SJed Brown     Level: beginner
79547c6ae99SBarry Smith 
796e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
79747c6ae99SBarry Smith 
79847c6ae99SBarry Smith @*/
7997087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
80047c6ae99SBarry Smith {
80147c6ae99SBarry Smith   PetscErrorCode ierr;
80247c6ae99SBarry Smith 
80347c6ae99SBarry Smith   PetscFunctionBegin;
804171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80547c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
80647c6ae99SBarry Smith   PetscFunctionReturn(0);
80747c6ae99SBarry Smith }
80847c6ae99SBarry Smith 
80947c6ae99SBarry Smith #undef __FUNCT__
8101411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
8111411c6eeSJed Brown /*@
8121411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
8131411c6eeSJed Brown 
8141411c6eeSJed Brown    Collective on DM
8151411c6eeSJed Brown 
8161411c6eeSJed Brown    Input Parameter:
8171411c6eeSJed Brown .  dm - the DM that provides the mapping
8181411c6eeSJed Brown 
8191411c6eeSJed Brown    Output Parameter:
8201411c6eeSJed Brown .  ltog - the mapping
8211411c6eeSJed Brown 
8221411c6eeSJed Brown    Level: intermediate
8231411c6eeSJed Brown 
8241411c6eeSJed Brown    Notes:
8251411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
8261411c6eeSJed Brown    MatSetLocalToGlobalMapping().
8271411c6eeSJed Brown 
828fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
8291411c6eeSJed Brown @*/
8307087cfbeSBarry Smith PetscErrorCode  DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
8311411c6eeSJed Brown {
8321411c6eeSJed Brown   PetscErrorCode ierr;
8331411c6eeSJed Brown 
8341411c6eeSJed Brown   PetscFunctionBegin;
8351411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8361411c6eeSJed Brown   PetscValidPointer(ltog,2);
8371411c6eeSJed Brown   if (!dm->ltogmap) {
83837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
83937d0c07bSMatthew G Knepley 
84037d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
84137d0c07bSMatthew G Knepley     if (section) {
84237d0c07bSMatthew G Knepley       PetscInt *ltog;
84337d0c07bSMatthew G Knepley       PetscInt pStart, pEnd, size, p, l;
84437d0c07bSMatthew G Knepley 
84537d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
84637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
84737d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
848785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
84937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
85037d0c07bSMatthew G Knepley         PetscInt dof, off, c;
85137d0c07bSMatthew G Knepley 
85237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
85337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
85437d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
85537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
85637d0c07bSMatthew G Knepley           ltog[l] = off+c;
85737d0c07bSMatthew G Knepley         }
85837d0c07bSMatthew G Knepley       }
859f0413b6fSBarry Smith       ierr = ISLocalToGlobalMappingCreate(PETSC_COMM_SELF, 1,size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
8603bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
86137d0c07bSMatthew G Knepley     } else {
862184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
863184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
8641411c6eeSJed Brown     }
86537d0c07bSMatthew G Knepley   }
8661411c6eeSJed Brown   *ltog = dm->ltogmap;
8671411c6eeSJed Brown   PetscFunctionReturn(0);
8681411c6eeSJed Brown }
8691411c6eeSJed Brown 
8701411c6eeSJed Brown #undef __FUNCT__
8711411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
8721411c6eeSJed Brown /*@
8731411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
8741411c6eeSJed Brown 
8751411c6eeSJed Brown    Not Collective
8761411c6eeSJed Brown 
8771411c6eeSJed Brown    Input Parameter:
8781411c6eeSJed Brown .  dm - the DM with block structure
8791411c6eeSJed Brown 
8801411c6eeSJed Brown    Output Parameter:
8811411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
8821411c6eeSJed Brown 
8831411c6eeSJed Brown    Level: intermediate
8841411c6eeSJed Brown 
885fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
8861411c6eeSJed Brown @*/
8877087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
8881411c6eeSJed Brown {
8891411c6eeSJed Brown   PetscFunctionBegin;
8901411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8911411c6eeSJed Brown   PetscValidPointer(bs,2);
8921411c6eeSJed 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");
8931411c6eeSJed Brown   *bs = dm->bs;
8941411c6eeSJed Brown   PetscFunctionReturn(0);
8951411c6eeSJed Brown }
8961411c6eeSJed Brown 
8971411c6eeSJed Brown #undef __FUNCT__
898e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
89947c6ae99SBarry Smith /*@
9008472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
90147c6ae99SBarry Smith 
90247c6ae99SBarry Smith     Collective on DM
90347c6ae99SBarry Smith 
90447c6ae99SBarry Smith     Input Parameter:
90547c6ae99SBarry Smith +   dm1 - the DM object
90647c6ae99SBarry Smith -   dm2 - the second, finer DM object
90747c6ae99SBarry Smith 
90847c6ae99SBarry Smith     Output Parameter:
90947c6ae99SBarry Smith +  mat - the interpolation
91047c6ae99SBarry Smith -  vec - the scaling (optional)
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Level: developer
91347c6ae99SBarry Smith 
91485afcc9aSBarry 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
91585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
916d52bd9f3SBarry Smith 
9171f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
918d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
91985afcc9aSBarry Smith 
92085afcc9aSBarry Smith 
921e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen()
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith @*/
924e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
92547c6ae99SBarry Smith {
92647c6ae99SBarry Smith   PetscErrorCode ierr;
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith   PetscFunctionBegin;
929171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
930171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
931cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
93225296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
933cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
93447c6ae99SBarry Smith   PetscFunctionReturn(0);
93547c6ae99SBarry Smith }
93647c6ae99SBarry Smith 
93747c6ae99SBarry Smith #undef __FUNCT__
938e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
93947c6ae99SBarry Smith /*@
9408472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
94147c6ae99SBarry Smith 
94247c6ae99SBarry Smith     Collective on DM
94347c6ae99SBarry Smith 
94447c6ae99SBarry Smith     Input Parameter:
94547c6ae99SBarry Smith +   dm1 - the DM object
94647c6ae99SBarry Smith -   dm2 - the second, finer DM object
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith     Output Parameter:
9496dbf9973SLawrence Mitchell .   mat - the injection
95047c6ae99SBarry Smith 
95147c6ae99SBarry Smith     Level: developer
95247c6ae99SBarry Smith 
95385afcc9aSBarry 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
95485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
95585afcc9aSBarry Smith 
956e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
95747c6ae99SBarry Smith 
95847c6ae99SBarry Smith @*/
9596dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
96047c6ae99SBarry Smith {
96147c6ae99SBarry Smith   PetscErrorCode ierr;
96247c6ae99SBarry Smith 
96347c6ae99SBarry Smith   PetscFunctionBegin;
964171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
965171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
9666dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
96747c6ae99SBarry Smith   PetscFunctionReturn(0);
96847c6ae99SBarry Smith }
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith #undef __FUNCT__
971e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
972b412c318SBarry Smith /*@
973b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
97447c6ae99SBarry Smith 
97547c6ae99SBarry Smith     Collective on DM
97647c6ae99SBarry Smith 
97747c6ae99SBarry Smith     Input Parameter:
97847c6ae99SBarry Smith +   dm - the DM object
979b412c318SBarry Smith -   ctype - IS_COLORING_GHOSTED or IS_COLORING_GLOBAL
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith     Output Parameter:
98247c6ae99SBarry Smith .   coloring - the coloring
98347c6ae99SBarry Smith 
98447c6ae99SBarry Smith     Level: developer
98547c6ae99SBarry Smith 
986b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
98747c6ae99SBarry Smith 
988aab9d709SJed Brown @*/
989b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
99047c6ae99SBarry Smith {
99147c6ae99SBarry Smith   PetscErrorCode ierr;
99247c6ae99SBarry Smith 
99347c6ae99SBarry Smith   PetscFunctionBegin;
994171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
995ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
996b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
99747c6ae99SBarry Smith   PetscFunctionReturn(0);
99847c6ae99SBarry Smith }
99947c6ae99SBarry Smith 
100047c6ae99SBarry Smith #undef __FUNCT__
1001950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1002b412c318SBarry Smith /*@
10038472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
100447c6ae99SBarry Smith 
100547c6ae99SBarry Smith     Collective on DM
100647c6ae99SBarry Smith 
100747c6ae99SBarry Smith     Input Parameter:
1008b412c318SBarry Smith .   dm - the DM object
100947c6ae99SBarry Smith 
101047c6ae99SBarry Smith     Output Parameter:
101147c6ae99SBarry Smith .   mat - the empty Jacobian
101247c6ae99SBarry Smith 
1013073dac72SJed Brown     Level: beginner
101447c6ae99SBarry Smith 
101594013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
101694013140SBarry Smith        do not need to do it yourself.
101794013140SBarry Smith 
101894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
1019aa219208SBarry Smith        the nonzero pattern call DMDASetMatPreallocateOnly()
102094013140SBarry Smith 
102194013140SBarry 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
102294013140SBarry Smith        internally by PETSc.
102394013140SBarry Smith 
102494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1025aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
102694013140SBarry Smith 
1027b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
102847c6ae99SBarry Smith 
1029aab9d709SJed Brown @*/
1030b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
103147c6ae99SBarry Smith {
103247c6ae99SBarry Smith   PetscErrorCode ierr;
103347c6ae99SBarry Smith 
103447c6ae99SBarry Smith   PetscFunctionBegin;
1035171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1036607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1037c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1038c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1039b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
104047c6ae99SBarry Smith   PetscFunctionReturn(0);
104147c6ae99SBarry Smith }
104247c6ae99SBarry Smith 
104347c6ae99SBarry Smith #undef __FUNCT__
1044732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1045732e2eb9SMatthew G Knepley /*@
1046950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1047732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1048732e2eb9SMatthew G Knepley 
10498472ad0fSDave May   Logically Collective on DM
1050732e2eb9SMatthew G Knepley 
1051732e2eb9SMatthew G Knepley   Input Parameter:
1052732e2eb9SMatthew G Knepley + dm - the DM
1053732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1054732e2eb9SMatthew G Knepley 
1055732e2eb9SMatthew G Knepley   Level: developer
1056950540a4SJed Brown .seealso DMCreateMatrix()
1057732e2eb9SMatthew G Knepley @*/
1058732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1059732e2eb9SMatthew G Knepley {
1060732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1061732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1062732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1063732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1064732e2eb9SMatthew G Knepley }
1065732e2eb9SMatthew G Knepley 
1066732e2eb9SMatthew G Knepley #undef __FUNCT__
1067a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1068a89ea682SMatthew G Knepley /*@C
1069aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1070a89ea682SMatthew G Knepley 
1071a89ea682SMatthew G Knepley   Not Collective
1072a89ea682SMatthew G Knepley 
1073a89ea682SMatthew G Knepley   Input Parameters:
1074a89ea682SMatthew G Knepley + dm - the DM object
1075aa1993deSMatthew G Knepley . count - The minium size
1076aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1077a89ea682SMatthew G Knepley 
1078a89ea682SMatthew G Knepley   Output Parameter:
1079a89ea682SMatthew G Knepley . array - the work array
1080a89ea682SMatthew G Knepley 
1081a89ea682SMatthew G Knepley   Level: developer
1082a89ea682SMatthew G Knepley 
1083a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1084a89ea682SMatthew G Knepley @*/
1085aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1086a89ea682SMatthew G Knepley {
1087a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1088aa1993deSMatthew G Knepley   DMWorkLink     link;
1089854ce69bSBarry Smith   size_t         dsize;
1090a89ea682SMatthew G Knepley 
1091a89ea682SMatthew G Knepley   PetscFunctionBegin;
1092a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1093aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1094aa1993deSMatthew G Knepley   if (dm->workin) {
1095aa1993deSMatthew G Knepley     link       = dm->workin;
1096aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1097aa1993deSMatthew G Knepley   } else {
1098b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1099a89ea682SMatthew G Knepley   }
1100854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1101854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1102aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1103854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1104854ce69bSBarry Smith     link->bytes = dsize*count;
1105aa1993deSMatthew G Knepley   }
1106aa1993deSMatthew G Knepley   link->next   = dm->workout;
1107aa1993deSMatthew G Knepley   dm->workout  = link;
1108aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1109a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1110a89ea682SMatthew G Knepley }
1111a89ea682SMatthew G Knepley 
1112aa1993deSMatthew G Knepley #undef __FUNCT__
1113aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1114aa1993deSMatthew G Knepley /*@C
1115aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1116aa1993deSMatthew G Knepley 
1117aa1993deSMatthew G Knepley   Not Collective
1118aa1993deSMatthew G Knepley 
1119aa1993deSMatthew G Knepley   Input Parameters:
1120aa1993deSMatthew G Knepley + dm - the DM object
1121aa1993deSMatthew G Knepley . count - The minium size
1122aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1123aa1993deSMatthew G Knepley 
1124aa1993deSMatthew G Knepley   Output Parameter:
1125aa1993deSMatthew G Knepley . array - the work array
1126aa1993deSMatthew G Knepley 
1127aa1993deSMatthew G Knepley   Level: developer
1128aa1993deSMatthew G Knepley 
1129aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1130aa1993deSMatthew G Knepley @*/
1131aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1132aa1993deSMatthew G Knepley {
1133aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1134aa1993deSMatthew G Knepley 
1135aa1993deSMatthew G Knepley   PetscFunctionBegin;
1136aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1137aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1138aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1139aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1140aa1993deSMatthew G Knepley       *p           = link->next;
1141aa1993deSMatthew G Knepley       link->next   = dm->workin;
1142aa1993deSMatthew G Knepley       dm->workin   = link;
11430298fd71SBarry Smith       *(void**)mem = NULL;
1144aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1145aa1993deSMatthew G Knepley     }
1146aa1993deSMatthew G Knepley   }
1147aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1148aa1993deSMatthew G Knepley }
1149e7c4fc90SDmitry Karpeev 
1150e7c4fc90SDmitry Karpeev #undef __FUNCT__
1151435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1152435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1153435a35e8SMatthew G Knepley {
1154435a35e8SMatthew G Knepley   PetscFunctionBegin;
1155435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
115682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1157435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1158435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1159435a35e8SMatthew G Knepley }
1160435a35e8SMatthew G Knepley 
1161435a35e8SMatthew G Knepley #undef __FUNCT__
11624d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
11634f3b5142SJed Brown /*@C
11644d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
11654d343eeaSMatthew G Knepley 
11664d343eeaSMatthew G Knepley   Not collective
11674d343eeaSMatthew G Knepley 
11684d343eeaSMatthew G Knepley   Input Parameter:
11694d343eeaSMatthew G Knepley . dm - the DM object
11704d343eeaSMatthew G Knepley 
11714d343eeaSMatthew G Knepley   Output Parameters:
11720298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
11730298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
11740298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
11754d343eeaSMatthew G Knepley 
11764d343eeaSMatthew G Knepley   Level: intermediate
11774d343eeaSMatthew G Knepley 
117821c9b008SJed Brown   Notes:
117921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
118021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
118121c9b008SJed Brown   PetscFree().
118221c9b008SJed Brown 
11834d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
11844d343eeaSMatthew G Knepley @*/
118537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
11864d343eeaSMatthew G Knepley {
118737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
11884d343eeaSMatthew G Knepley   PetscErrorCode ierr;
11894d343eeaSMatthew G Knepley 
11904d343eeaSMatthew G Knepley   PetscFunctionBegin;
11914d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
119269ca1f37SDmitry Karpeev   if (numFields) {
119369ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
119469ca1f37SDmitry Karpeev     *numFields = 0;
119569ca1f37SDmitry Karpeev   }
119637d0c07bSMatthew G Knepley   if (fieldNames) {
119737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
11980298fd71SBarry Smith     *fieldNames = NULL;
119969ca1f37SDmitry Karpeev   }
120069ca1f37SDmitry Karpeev   if (fields) {
120169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
12020298fd71SBarry Smith     *fields = NULL;
120369ca1f37SDmitry Karpeev   }
120437d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
120537d0c07bSMatthew G Knepley   if (section) {
120637d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
120737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
120837d0c07bSMatthew G Knepley 
120937d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
121037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1211dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
121237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
121337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
121437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
121537d0c07bSMatthew G Knepley     }
121637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
121737d0c07bSMatthew G Knepley       PetscInt gdof;
121837d0c07bSMatthew G Knepley 
121937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
122037d0c07bSMatthew G Knepley       if (gdof > 0) {
122137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
122237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
122337d0c07bSMatthew G Knepley 
122437d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
122537d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
122637d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
122737d0c07bSMatthew G Knepley         }
122837d0c07bSMatthew G Knepley       }
122937d0c07bSMatthew G Knepley     }
123037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1231785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
123237d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
123337d0c07bSMatthew G Knepley     }
123437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
123537d0c07bSMatthew G Knepley       PetscInt gdof, goff;
123637d0c07bSMatthew G Knepley 
123737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
123837d0c07bSMatthew G Knepley       if (gdof > 0) {
123937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
124037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
124137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
124237d0c07bSMatthew G Knepley 
124337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
124437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
124537d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
124637d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
124737d0c07bSMatthew G Knepley           }
124837d0c07bSMatthew G Knepley         }
124937d0c07bSMatthew G Knepley       }
125037d0c07bSMatthew G Knepley     }
12518865f1eaSKarl Rupp     if (numFields) *numFields = nF;
125237d0c07bSMatthew G Knepley     if (fieldNames) {
1253785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
125437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
125537d0c07bSMatthew G Knepley         const char *fieldName;
125637d0c07bSMatthew G Knepley 
125737d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
125837d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
125937d0c07bSMatthew G Knepley       }
126037d0c07bSMatthew G Knepley     }
126137d0c07bSMatthew G Knepley     if (fields) {
1262785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
126337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
126482f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
126537d0c07bSMatthew G Knepley       }
126637d0c07bSMatthew G Knepley     }
126737d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
12688865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
12698865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
127069ca1f37SDmitry Karpeev   }
12714d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
12724d343eeaSMatthew G Knepley }
12734d343eeaSMatthew G Knepley 
127416621825SDmitry Karpeev 
127516621825SDmitry Karpeev #undef __FUNCT__
127616621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
127716621825SDmitry Karpeev /*@C
127816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
127916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
128016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1281e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1282e7c4fc90SDmitry Karpeev 
1283e7c4fc90SDmitry Karpeev   Not collective
1284e7c4fc90SDmitry Karpeev 
1285e7c4fc90SDmitry Karpeev   Input Parameter:
1286e7c4fc90SDmitry Karpeev . dm - the DM object
1287e7c4fc90SDmitry Karpeev 
1288e7c4fc90SDmitry Karpeev   Output Parameters:
12890298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
12900298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
12910298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
12920298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1293e7c4fc90SDmitry Karpeev 
1294e7c4fc90SDmitry Karpeev   Level: intermediate
1295e7c4fc90SDmitry Karpeev 
1296e7c4fc90SDmitry Karpeev   Notes:
1297e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1298e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1299e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1300e7c4fc90SDmitry Karpeev 
1301e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1302e7c4fc90SDmitry Karpeev @*/
130316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1304e7c4fc90SDmitry Karpeev {
1305e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1306e7c4fc90SDmitry Karpeev 
1307e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1308e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13098865f1eaSKarl Rupp   if (len) {
13108865f1eaSKarl Rupp     PetscValidPointer(len,2);
13118865f1eaSKarl Rupp     *len = 0;
13128865f1eaSKarl Rupp   }
13138865f1eaSKarl Rupp   if (namelist) {
13148865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
13158865f1eaSKarl Rupp     *namelist = 0;
13168865f1eaSKarl Rupp   }
13178865f1eaSKarl Rupp   if (islist) {
13188865f1eaSKarl Rupp     PetscValidPointer(islist,4);
13198865f1eaSKarl Rupp     *islist = 0;
13208865f1eaSKarl Rupp   }
13218865f1eaSKarl Rupp   if (dmlist) {
13228865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
13238865f1eaSKarl Rupp     *dmlist = 0;
13248865f1eaSKarl Rupp   }
1325f3f0edfdSDmitry Karpeev   /*
1326f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1327f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1328f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1329f3f0edfdSDmitry Karpeev    */
1330ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
133116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1332435a35e8SMatthew G Knepley     PetscSection section;
1333435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1334435a35e8SMatthew G Knepley 
1335435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1336435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1337435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1338435a35e8SMatthew G Knepley       *len = numFields;
133903dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
134003dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
134103dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1342435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1343435a35e8SMatthew G Knepley         const char *fieldName;
1344435a35e8SMatthew G Knepley 
134503dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
134603dc3394SMatthew G. Knepley         if (namelist) {
1347435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1348435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1349435a35e8SMatthew G Knepley         }
135003dc3394SMatthew G. Knepley       }
1351435a35e8SMatthew G Knepley     } else {
135269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1353e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
13540298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1355e7c4fc90SDmitry Karpeev     }
13568865f1eaSKarl Rupp   } else {
135716621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
135816621825SDmitry Karpeev   }
135916621825SDmitry Karpeev   PetscFunctionReturn(0);
136016621825SDmitry Karpeev }
136116621825SDmitry Karpeev 
136216621825SDmitry Karpeev #undef __FUNCT__
1363435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1364564cec59SMatthew G. Knepley /*@
1365435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1366435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1367435a35e8SMatthew G Knepley 
1368435a35e8SMatthew G Knepley   Not collective
1369435a35e8SMatthew G Knepley 
1370435a35e8SMatthew G Knepley   Input Parameters:
1371435a35e8SMatthew G Knepley + dm - the DM object
1372435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
13730298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1374435a35e8SMatthew G Knepley 
1375435a35e8SMatthew G Knepley   Output Parameters:
1376435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1377435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1378435a35e8SMatthew G Knepley 
1379435a35e8SMatthew G Knepley   Level: intermediate
1380435a35e8SMatthew G Knepley 
1381435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1382435a35e8SMatthew G Knepley @*/
1383435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1384435a35e8SMatthew G Knepley {
1385435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1386435a35e8SMatthew G Knepley 
1387435a35e8SMatthew G Knepley   PetscFunctionBegin;
1388435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1389435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
13908865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
13918865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1392435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1393435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
139482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1395435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1396435a35e8SMatthew G Knepley }
1397435a35e8SMatthew G Knepley 
139816621825SDmitry Karpeev 
139916621825SDmitry Karpeev #undef __FUNCT__
140016621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
140116621825SDmitry Karpeev /*@C
14028d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
14038d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
14048d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
14058d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
14068d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
140716621825SDmitry Karpeev 
140816621825SDmitry Karpeev   Not collective
140916621825SDmitry Karpeev 
141016621825SDmitry Karpeev   Input Parameter:
141116621825SDmitry Karpeev . dm - the DM object
141216621825SDmitry Karpeev 
141316621825SDmitry Karpeev   Output Parameters:
14140298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
14150298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
14160298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
14170298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
14180298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
141916621825SDmitry Karpeev 
142016621825SDmitry Karpeev   Level: intermediate
142116621825SDmitry Karpeev 
142216621825SDmitry Karpeev   Notes:
142316621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
142416621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
142516621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
142616621825SDmitry Karpeev 
14278d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
142816621825SDmitry Karpeev @*/
14298d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
143016621825SDmitry Karpeev {
143116621825SDmitry Karpeev   PetscErrorCode      ierr;
1432be081cd6SPeter Brune   DMSubDomainHookLink link;
1433be081cd6SPeter Brune   PetscInt            i,l;
143416621825SDmitry Karpeev 
143516621825SDmitry Karpeev   PetscFunctionBegin;
143616621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143714a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
14380298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
14390298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
14400298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
14410298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1442f3f0edfdSDmitry Karpeev   /*
1443f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1444f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1445f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1446f3f0edfdSDmitry Karpeev    */
1447ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
144816621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1449be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
145014a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
145114a18fd3SPeter Brune     if (dmlist) {
1452be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1453be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1454be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1455be081cd6SPeter Brune         }
1456d425c654SPeter Brune         (*dmlist)[i]->ctx = dm->ctx;
1457e7c4fc90SDmitry Karpeev       }
145814a18fd3SPeter Brune     }
145914a18fd3SPeter Brune     if (len) *len = l;
146014a18fd3SPeter Brune   }
1461e30e807fSPeter Brune   PetscFunctionReturn(0);
1462e30e807fSPeter Brune }
1463e30e807fSPeter Brune 
1464e30e807fSPeter Brune 
1465e30e807fSPeter Brune #undef __FUNCT__
1466e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1467e30e807fSPeter Brune /*@C
1468e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1469e30e807fSPeter Brune 
1470e30e807fSPeter Brune   Not collective
1471e30e807fSPeter Brune 
1472e30e807fSPeter Brune   Input Parameters:
1473e30e807fSPeter Brune + dm - the DM object
1474e30e807fSPeter Brune . n  - the number of subdomain scatters
1475e30e807fSPeter Brune - subdms - the local subdomains
1476e30e807fSPeter Brune 
1477e30e807fSPeter Brune   Output Parameters:
1478e30e807fSPeter Brune + n     - the number of scatters returned
1479e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1480e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1481e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1482e30e807fSPeter Brune 
1483e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1484e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1485e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1486e30e807fSPeter Brune   solution and residual data.
1487e30e807fSPeter Brune 
1488e30e807fSPeter Brune   Level: developer
1489e30e807fSPeter Brune 
1490e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1491e30e807fSPeter Brune @*/
1492e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1493e30e807fSPeter Brune {
1494e30e807fSPeter Brune   PetscErrorCode ierr;
1495e30e807fSPeter Brune 
1496e30e807fSPeter Brune   PetscFunctionBegin;
1497e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1498e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1499e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1500e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
150182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionLocalScatter implementation defined");
1502e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1503e7c4fc90SDmitry Karpeev }
1504e7c4fc90SDmitry Karpeev 
1505731c8d9eSDmitry Karpeev #undef __FUNCT__
150647c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
150747c6ae99SBarry Smith /*@
150847c6ae99SBarry Smith   DMRefine - Refines a DM object
150947c6ae99SBarry Smith 
151047c6ae99SBarry Smith   Collective on DM
151147c6ae99SBarry Smith 
151247c6ae99SBarry Smith   Input Parameter:
151347c6ae99SBarry Smith + dm   - the DM object
151491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
151547c6ae99SBarry Smith 
151647c6ae99SBarry Smith   Output Parameter:
15170298fd71SBarry Smith . dmf - the refined DM, or NULL
1518ae0a1c52SMatthew G Knepley 
15190298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
152047c6ae99SBarry Smith 
152147c6ae99SBarry Smith   Level: developer
152247c6ae99SBarry Smith 
1523e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
152447c6ae99SBarry Smith @*/
15257087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
152647c6ae99SBarry Smith {
152747c6ae99SBarry Smith   PetscErrorCode   ierr;
1528c833c3b5SJed Brown   DMRefineHookLink link;
152947c6ae99SBarry Smith 
153047c6ae99SBarry Smith   PetscFunctionBegin;
1531732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
153247c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
15334057135bSMatthew G Knepley   if (*dmf) {
153443842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
15358865f1eaSKarl Rupp 
15368cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
15378865f1eaSKarl Rupp 
1538644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
15390598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1540656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
15418865f1eaSKarl Rupp 
1542e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1543c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
15448865f1eaSKarl Rupp       if (link->refinehook) {
15458865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
15468865f1eaSKarl Rupp       }
1547c833c3b5SJed Brown     }
1548c833c3b5SJed Brown   }
1549c833c3b5SJed Brown   PetscFunctionReturn(0);
1550c833c3b5SJed Brown }
1551c833c3b5SJed Brown 
1552c833c3b5SJed Brown #undef __FUNCT__
1553c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1554bb9467b5SJed Brown /*@C
1555c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1556c833c3b5SJed Brown 
1557c833c3b5SJed Brown    Logically Collective
1558c833c3b5SJed Brown 
1559c833c3b5SJed Brown    Input Arguments:
1560c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1561c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1562c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
15630298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1564c833c3b5SJed Brown 
1565c833c3b5SJed Brown    Calling sequence of refinehook:
1566c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1567c833c3b5SJed Brown 
1568c833c3b5SJed Brown +  coarse - coarse level DM
1569c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1570c833c3b5SJed Brown -  ctx - optional user-defined function context
1571c833c3b5SJed Brown 
1572c833c3b5SJed Brown    Calling sequence for interphook:
1573c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1574c833c3b5SJed Brown 
1575c833c3b5SJed Brown +  coarse - coarse level DM
1576c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1577c833c3b5SJed Brown .  fine - fine level DM to update
1578c833c3b5SJed Brown -  ctx - optional user-defined function context
1579c833c3b5SJed Brown 
1580c833c3b5SJed Brown    Level: advanced
1581c833c3b5SJed Brown 
1582c833c3b5SJed Brown    Notes:
1583c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1584c833c3b5SJed Brown 
1585c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1586c833c3b5SJed Brown 
1587bb9467b5SJed Brown    This function is currently not available from Fortran.
1588bb9467b5SJed Brown 
1589c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1590c833c3b5SJed Brown @*/
1591c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1592c833c3b5SJed Brown {
1593c833c3b5SJed Brown   PetscErrorCode   ierr;
1594c833c3b5SJed Brown   DMRefineHookLink link,*p;
1595c833c3b5SJed Brown 
1596c833c3b5SJed Brown   PetscFunctionBegin;
1597c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1598c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1599c833c3b5SJed Brown   ierr             = PetscMalloc(sizeof(struct _DMRefineHookLink),&link);CHKERRQ(ierr);
1600c833c3b5SJed Brown   link->refinehook = refinehook;
1601c833c3b5SJed Brown   link->interphook = interphook;
1602c833c3b5SJed Brown   link->ctx        = ctx;
16030298fd71SBarry Smith   link->next       = NULL;
1604c833c3b5SJed Brown   *p               = link;
1605c833c3b5SJed Brown   PetscFunctionReturn(0);
1606c833c3b5SJed Brown }
1607c833c3b5SJed Brown 
1608c833c3b5SJed Brown #undef __FUNCT__
1609c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1610c833c3b5SJed Brown /*@
1611c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1612c833c3b5SJed Brown 
1613c833c3b5SJed Brown    Collective if any hooks are
1614c833c3b5SJed Brown 
1615c833c3b5SJed Brown    Input Arguments:
1616c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1617c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1618c833c3b5SJed Brown -  fine - finer DM to update
1619c833c3b5SJed Brown 
1620c833c3b5SJed Brown    Level: developer
1621c833c3b5SJed Brown 
1622c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1623c833c3b5SJed Brown @*/
1624c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1625c833c3b5SJed Brown {
1626c833c3b5SJed Brown   PetscErrorCode   ierr;
1627c833c3b5SJed Brown   DMRefineHookLink link;
1628c833c3b5SJed Brown 
1629c833c3b5SJed Brown   PetscFunctionBegin;
1630c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
16318865f1eaSKarl Rupp     if (link->interphook) {
16328865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
16338865f1eaSKarl Rupp     }
16344057135bSMatthew G Knepley   }
163547c6ae99SBarry Smith   PetscFunctionReturn(0);
163647c6ae99SBarry Smith }
163747c6ae99SBarry Smith 
163847c6ae99SBarry Smith #undef __FUNCT__
1639eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1640eb3f98d2SBarry Smith /*@
1641eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1642eb3f98d2SBarry Smith 
1643eb3f98d2SBarry Smith     Not Collective
1644eb3f98d2SBarry Smith 
1645eb3f98d2SBarry Smith     Input Parameter:
1646eb3f98d2SBarry Smith .   dm - the DM object
1647eb3f98d2SBarry Smith 
1648eb3f98d2SBarry Smith     Output Parameter:
1649eb3f98d2SBarry Smith .   level - number of refinements
1650eb3f98d2SBarry Smith 
1651eb3f98d2SBarry Smith     Level: developer
1652eb3f98d2SBarry Smith 
16536a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1654eb3f98d2SBarry Smith 
1655eb3f98d2SBarry Smith @*/
1656eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1657eb3f98d2SBarry Smith {
1658eb3f98d2SBarry Smith   PetscFunctionBegin;
1659eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1660eb3f98d2SBarry Smith   *level = dm->levelup;
1661eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1662eb3f98d2SBarry Smith }
1663eb3f98d2SBarry Smith 
1664eb3f98d2SBarry Smith #undef __FUNCT__
1665baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1666bb9467b5SJed Brown /*@C
1667baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1668baf369e7SPeter Brune 
1669baf369e7SPeter Brune    Logically Collective
1670baf369e7SPeter Brune 
1671baf369e7SPeter Brune    Input Arguments:
1672baf369e7SPeter Brune +  dm - the DM
1673baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1674baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
16750298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1676baf369e7SPeter Brune 
1677baf369e7SPeter Brune    Calling sequence for beginhook:
1678baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1679baf369e7SPeter Brune 
1680baf369e7SPeter Brune +  dm - global DM
1681baf369e7SPeter Brune .  g - global vector
1682baf369e7SPeter Brune .  mode - mode
1683baf369e7SPeter Brune .  l - local vector
1684baf369e7SPeter Brune -  ctx - optional user-defined function context
1685baf369e7SPeter Brune 
1686baf369e7SPeter Brune 
1687baf369e7SPeter Brune    Calling sequence for endhook:
1688ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1689baf369e7SPeter Brune 
1690baf369e7SPeter Brune +  global - global DM
1691baf369e7SPeter Brune -  ctx - optional user-defined function context
1692baf369e7SPeter Brune 
1693baf369e7SPeter Brune    Level: advanced
1694baf369e7SPeter Brune 
1695baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1696baf369e7SPeter Brune @*/
1697baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1698baf369e7SPeter Brune {
1699baf369e7SPeter Brune   PetscErrorCode          ierr;
1700baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1701baf369e7SPeter Brune 
1702baf369e7SPeter Brune   PetscFunctionBegin;
1703baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1704baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1705baf369e7SPeter Brune   ierr            = PetscMalloc(sizeof(struct _DMGlobalToLocalHookLink),&link);CHKERRQ(ierr);
1706baf369e7SPeter Brune   link->beginhook = beginhook;
1707baf369e7SPeter Brune   link->endhook   = endhook;
1708baf369e7SPeter Brune   link->ctx       = ctx;
17090298fd71SBarry Smith   link->next      = NULL;
1710baf369e7SPeter Brune   *p              = link;
1711baf369e7SPeter Brune   PetscFunctionReturn(0);
1712baf369e7SPeter Brune }
1713baf369e7SPeter Brune 
1714baf369e7SPeter Brune #undef __FUNCT__
17154c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
17164c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
17174c274da1SToby Isaac {
17184c274da1SToby Isaac   Mat cMat;
17194c274da1SToby Isaac   Vec cVec;
17204c274da1SToby Isaac   PetscSection section, cSec;
17214c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
17224c274da1SToby Isaac   PetscErrorCode ierr;
17234c274da1SToby Isaac 
17244c274da1SToby Isaac   PetscFunctionBegin;
17254c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
17264c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
17274c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
17284c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
17297711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
17304c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
17314c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
17324c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
17334c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
17344c274da1SToby Isaac       if (dof) {
17354c274da1SToby Isaac         PetscScalar *vals;
17364c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
17374c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
17384c274da1SToby Isaac       }
17394c274da1SToby Isaac     }
17404c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
17414c274da1SToby Isaac   }
17424c274da1SToby Isaac   PetscFunctionReturn(0);
17434c274da1SToby Isaac }
17444c274da1SToby Isaac 
17454c274da1SToby Isaac #undef __FUNCT__
174647c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
174747c6ae99SBarry Smith /*@
174847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
174947c6ae99SBarry Smith 
175047c6ae99SBarry Smith     Neighbor-wise Collective on DM
175147c6ae99SBarry Smith 
175247c6ae99SBarry Smith     Input Parameters:
175347c6ae99SBarry Smith +   dm - the DM object
175447c6ae99SBarry Smith .   g - the global vector
175547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
175647c6ae99SBarry Smith -   l - the local vector
175747c6ae99SBarry Smith 
175847c6ae99SBarry Smith 
175947c6ae99SBarry Smith     Level: beginner
176047c6ae99SBarry Smith 
1761e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
176247c6ae99SBarry Smith 
176347c6ae99SBarry Smith @*/
17647087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
176547c6ae99SBarry Smith {
17667128ae9fSMatthew G Knepley   PetscSF                 sf;
176747c6ae99SBarry Smith   PetscErrorCode          ierr;
1768baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
176947c6ae99SBarry Smith 
177047c6ae99SBarry Smith   PetscFunctionBegin;
1771171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1772baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
17738865f1eaSKarl Rupp     if (link->beginhook) {
17748865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
17758865f1eaSKarl Rupp     }
1776baf369e7SPeter Brune   }
17777128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
17787128ae9fSMatthew G Knepley   if (sf) {
1779ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1780ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
17817128ae9fSMatthew G Knepley 
178282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
17837128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1784ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
17857128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
17867128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1787ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
17887128ae9fSMatthew G Knepley   } else {
1789843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
17907128ae9fSMatthew G Knepley   }
179147c6ae99SBarry Smith   PetscFunctionReturn(0);
179247c6ae99SBarry Smith }
179347c6ae99SBarry Smith 
179447c6ae99SBarry Smith #undef __FUNCT__
179547c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
179647c6ae99SBarry Smith /*@
179747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
179847c6ae99SBarry Smith 
179947c6ae99SBarry Smith     Neighbor-wise Collective on DM
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith     Input Parameters:
180247c6ae99SBarry Smith +   dm - the DM object
180347c6ae99SBarry Smith .   g - the global vector
180447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
180547c6ae99SBarry Smith -   l - the local vector
180647c6ae99SBarry Smith 
180747c6ae99SBarry Smith 
180847c6ae99SBarry Smith     Level: beginner
180947c6ae99SBarry Smith 
1810e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
181147c6ae99SBarry Smith 
181247c6ae99SBarry Smith @*/
18137087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
181447c6ae99SBarry Smith {
18157128ae9fSMatthew G Knepley   PetscSF                 sf;
181647c6ae99SBarry Smith   PetscErrorCode          ierr;
1817ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
1818ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
1819baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
182047c6ae99SBarry Smith 
182147c6ae99SBarry Smith   PetscFunctionBegin;
1822171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18237128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
18247128ae9fSMatthew G Knepley   if (sf) {
182582f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
18267128ae9fSMatthew G Knepley 
18277128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1828ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
18297128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
18307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1831ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
18327128ae9fSMatthew G Knepley   } else {
1833843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
18347128ae9fSMatthew G Knepley   }
18354c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
1836baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
1837baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
1838baf369e7SPeter Brune   }
183947c6ae99SBarry Smith   PetscFunctionReturn(0);
184047c6ae99SBarry Smith }
184147c6ae99SBarry Smith 
184247c6ae99SBarry Smith #undef __FUNCT__
1843d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
1844d4d07f1eSToby Isaac /*@C
1845d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
1846d4d07f1eSToby Isaac 
1847d4d07f1eSToby Isaac    Logically Collective
1848d4d07f1eSToby Isaac 
1849d4d07f1eSToby Isaac    Input Arguments:
1850d4d07f1eSToby Isaac +  dm - the DM
1851d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
1852d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
1853d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1854d4d07f1eSToby Isaac 
1855d4d07f1eSToby Isaac    Calling sequence for beginhook:
1856d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1857d4d07f1eSToby Isaac 
1858d4d07f1eSToby Isaac +  dm - global DM
1859d4d07f1eSToby Isaac .  l - local vector
1860d4d07f1eSToby Isaac .  mode - mode
1861d4d07f1eSToby Isaac .  g - global vector
1862d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1863d4d07f1eSToby Isaac 
1864d4d07f1eSToby Isaac 
1865d4d07f1eSToby Isaac    Calling sequence for endhook:
1866d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
1867d4d07f1eSToby Isaac 
1868d4d07f1eSToby Isaac +  global - global DM
1869d4d07f1eSToby Isaac .  l - local vector
1870d4d07f1eSToby Isaac .  mode - mode
1871d4d07f1eSToby Isaac .  g - global vector
1872d4d07f1eSToby Isaac -  ctx - optional user-defined function context
1873d4d07f1eSToby Isaac 
1874d4d07f1eSToby Isaac    Level: advanced
1875d4d07f1eSToby Isaac 
1876d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1877d4d07f1eSToby Isaac @*/
1878d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1879d4d07f1eSToby Isaac {
1880d4d07f1eSToby Isaac   PetscErrorCode          ierr;
1881d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
1882d4d07f1eSToby Isaac 
1883d4d07f1eSToby Isaac   PetscFunctionBegin;
1884d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1885d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
1886d4d07f1eSToby Isaac   ierr            = PetscMalloc(sizeof(struct _DMLocalToGlobalHookLink),&link);CHKERRQ(ierr);
1887d4d07f1eSToby Isaac   link->beginhook = beginhook;
1888d4d07f1eSToby Isaac   link->endhook   = endhook;
1889d4d07f1eSToby Isaac   link->ctx       = ctx;
1890d4d07f1eSToby Isaac   link->next      = NULL;
1891d4d07f1eSToby Isaac   *p              = link;
1892d4d07f1eSToby Isaac   PetscFunctionReturn(0);
1893d4d07f1eSToby Isaac }
1894d4d07f1eSToby Isaac 
1895d4d07f1eSToby Isaac #undef __FUNCT__
18964c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
18974c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
18984c274da1SToby Isaac {
18994c274da1SToby Isaac   Mat cMat;
19004c274da1SToby Isaac   Vec cVec;
19014c274da1SToby Isaac   PetscSection section, cSec;
19024c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19034c274da1SToby Isaac   PetscErrorCode ierr;
19044c274da1SToby Isaac 
19054c274da1SToby Isaac   PetscFunctionBegin;
19064c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19074c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19084c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
19094c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19107711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19114c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19124c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19134c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19144c274da1SToby Isaac       if (dof) {
19154c274da1SToby Isaac         PetscInt d;
19164c274da1SToby Isaac         PetscScalar *vals;
19174c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
19184c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
19194c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
19204c274da1SToby Isaac          * we just extracted */
19214c274da1SToby Isaac         for (d = 0; d < dof; d++) {
19224c274da1SToby Isaac           vals[d] = 0.;
19234c274da1SToby Isaac         }
19244c274da1SToby Isaac       }
19254c274da1SToby Isaac     }
19264c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
19274c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19284c274da1SToby Isaac   }
19294c274da1SToby Isaac   PetscFunctionReturn(0);
19304c274da1SToby Isaac }
19314c274da1SToby Isaac 
19324c274da1SToby Isaac #undef __FUNCT__
19339a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
193447c6ae99SBarry Smith /*@
19359a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
19369a42bb27SBarry Smith 
19379a42bb27SBarry Smith     Neighbor-wise Collective on DM
19389a42bb27SBarry Smith 
19399a42bb27SBarry Smith     Input Parameters:
19409a42bb27SBarry Smith +   dm - the DM object
1941f6813fd5SJed Brown .   l - the local vector
19421eb28f2eSBarry 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.
19431eb28f2eSBarry Smith -   g - the global vector
19449a42bb27SBarry Smith 
1945d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
194684330215SMatthew 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.
19479a42bb27SBarry Smith 
19489a42bb27SBarry Smith     Level: beginner
19499a42bb27SBarry Smith 
1950e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
19519a42bb27SBarry Smith 
19529a42bb27SBarry Smith @*/
19537087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
19549a42bb27SBarry Smith {
19557128ae9fSMatthew G Knepley   PetscSF                 sf;
195684330215SMatthew G. Knepley   PetscSection            s, gs;
1957d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
1958ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
1959ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
196084330215SMatthew G. Knepley   PetscBool               isInsert;
196184330215SMatthew G. Knepley   PetscErrorCode          ierr;
19629a42bb27SBarry Smith 
19639a42bb27SBarry Smith   PetscFunctionBegin;
1964171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1965d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
1966d4d07f1eSToby Isaac     if (link->beginhook) {
1967d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
1968d4d07f1eSToby Isaac     }
1969d4d07f1eSToby Isaac   }
19704c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
19717128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
197284330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
19737128ae9fSMatthew G Knepley   switch (mode) {
19747128ae9fSMatthew G Knepley   case INSERT_VALUES:
19757128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
197684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
19777128ae9fSMatthew G Knepley   case ADD_VALUES:
19787128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
197984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
19807128ae9fSMatthew G Knepley   default:
198182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19827128ae9fSMatthew G Knepley   }
198384330215SMatthew G. Knepley   if (sf && !isInsert) {
1984ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
19857128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
1986a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
1987ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
198884330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
198984330215SMatthew G. Knepley   } else if (s && isInsert) {
199084330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
199184330215SMatthew G. Knepley 
199284330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
199384330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
199484330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
1995ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
199684330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
199784330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
1998b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
199984330215SMatthew G. Knepley 
200084330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
200103442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
200284330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2003b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
200484330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
200584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2006b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
200703442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2008b3b16f48SMatthew 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);
2009b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2010b3b16f48SMatthew G. Knepley       if (!gcdof) {
201184330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2012b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2013b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
201484330215SMatthew G. Knepley         const PetscInt *cdofs;
201584330215SMatthew G. Knepley         PetscInt        cind = 0;
201684330215SMatthew G. Knepley 
201784330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2018b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
201984330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2020b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
202184330215SMatthew G. Knepley         }
2022b3b16f48SMatthew 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);
202384330215SMatthew G. Knepley     }
2024ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20257128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
20267128ae9fSMatthew G Knepley   } else {
2027843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20287128ae9fSMatthew G Knepley   }
20299a42bb27SBarry Smith   PetscFunctionReturn(0);
20309a42bb27SBarry Smith }
20319a42bb27SBarry Smith 
20329a42bb27SBarry Smith #undef __FUNCT__
20339a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
20349a42bb27SBarry Smith /*@
20359a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
203647c6ae99SBarry Smith 
203747c6ae99SBarry Smith     Neighbor-wise Collective on DM
203847c6ae99SBarry Smith 
203947c6ae99SBarry Smith     Input Parameters:
204047c6ae99SBarry Smith +   dm - the DM object
2041f6813fd5SJed Brown .   l - the local vector
204247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2043f6813fd5SJed Brown -   g - the global vector
204447c6ae99SBarry Smith 
204547c6ae99SBarry Smith 
204647c6ae99SBarry Smith     Level: beginner
204747c6ae99SBarry Smith 
2048e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
204947c6ae99SBarry Smith 
205047c6ae99SBarry Smith @*/
20517087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
205247c6ae99SBarry Smith {
20537128ae9fSMatthew G Knepley   PetscSF                 sf;
205484330215SMatthew G. Knepley   PetscSection            s;
2055d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
205684330215SMatthew G. Knepley   PetscBool               isInsert;
205784330215SMatthew G. Knepley   PetscErrorCode          ierr;
205847c6ae99SBarry Smith 
205947c6ae99SBarry Smith   PetscFunctionBegin;
2060171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20617128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
206284330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
20637128ae9fSMatthew G Knepley   switch (mode) {
20647128ae9fSMatthew G Knepley   case INSERT_VALUES:
20657128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
206684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
20677128ae9fSMatthew G Knepley   case ADD_VALUES:
20687128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
206984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
20707128ae9fSMatthew G Knepley   default:
207182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20727128ae9fSMatthew G Knepley   }
207384330215SMatthew G. Knepley   if (sf && !isInsert) {
2074ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2075ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
207684330215SMatthew G. Knepley 
2077ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
20787128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2079a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2080ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
20817128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
208284330215SMatthew G. Knepley   } else if (s && isInsert) {
20837128ae9fSMatthew G Knepley   } else {
2084843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
20857128ae9fSMatthew G Knepley   }
2086d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2087d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2088d4d07f1eSToby Isaac   }
208947c6ae99SBarry Smith   PetscFunctionReturn(0);
209047c6ae99SBarry Smith }
209147c6ae99SBarry Smith 
209247c6ae99SBarry Smith #undef __FUNCT__
2093f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2094f089877aSRichard Tran Mills /*@
2095bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2096bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2097d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2098f089877aSRichard Tran Mills 
2099bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2100f089877aSRichard Tran Mills 
2101f089877aSRichard Tran Mills    Input Parameters:
2102f089877aSRichard Tran Mills +  dm - the DM object
2103bc0a1609SRichard Tran Mills .  g - the original local vector
2104bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2105f089877aSRichard Tran Mills 
2106bc0a1609SRichard Tran Mills    Output Parameter:
2107bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2108f089877aSRichard Tran Mills 
2109f089877aSRichard Tran Mills    Level: intermediate
2110f089877aSRichard Tran Mills 
2111bc0a1609SRichard Tran Mills    Notes:
2112bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2113bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2114bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2115bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2116bc0a1609SRichard Tran Mills 
2117bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2118bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2119f089877aSRichard Tran Mills 
2120f089877aSRichard Tran Mills @*/
2121f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2122f089877aSRichard Tran Mills {
2123f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2124f089877aSRichard Tran Mills 
2125f089877aSRichard Tran Mills   PetscFunctionBegin;
2126f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2127f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2128f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2129f089877aSRichard Tran Mills }
2130f089877aSRichard Tran Mills 
2131f089877aSRichard Tran Mills #undef __FUNCT__
2132f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2133f089877aSRichard Tran Mills /*@
2134bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2135bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2136d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2137f089877aSRichard Tran Mills 
2138bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2139f089877aSRichard Tran Mills 
2140f089877aSRichard Tran Mills    Input Parameters:
2141bc0a1609SRichard Tran Mills +  da - the DM object
2142bc0a1609SRichard Tran Mills .  g - the original local vector
2143bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2144f089877aSRichard Tran Mills 
2145bc0a1609SRichard Tran Mills    Output Parameter:
2146bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2147f089877aSRichard Tran Mills 
2148f089877aSRichard Tran Mills    Level: intermediate
2149f089877aSRichard Tran Mills 
2150bc0a1609SRichard Tran Mills    Notes:
2151bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2152bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2153bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2154bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2155bc0a1609SRichard Tran Mills 
2156bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2157bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2158f089877aSRichard Tran Mills 
2159f089877aSRichard Tran Mills @*/
2160f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2161f089877aSRichard Tran Mills {
2162f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2163f089877aSRichard Tran Mills 
2164f089877aSRichard Tran Mills   PetscFunctionBegin;
2165f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2166f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2167f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2168f089877aSRichard Tran Mills }
2169f089877aSRichard Tran Mills 
2170f089877aSRichard Tran Mills 
2171f089877aSRichard Tran Mills #undef __FUNCT__
217247c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
217347c6ae99SBarry Smith /*@
217447c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
217547c6ae99SBarry Smith 
217647c6ae99SBarry Smith     Collective on DM
217747c6ae99SBarry Smith 
217847c6ae99SBarry Smith     Input Parameter:
217947c6ae99SBarry Smith +   dm - the DM object
218091d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
218147c6ae99SBarry Smith 
218247c6ae99SBarry Smith     Output Parameter:
218347c6ae99SBarry Smith .   dmc - the coarsened DM
218447c6ae99SBarry Smith 
218547c6ae99SBarry Smith     Level: developer
218647c6ae99SBarry Smith 
2187e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith @*/
21907087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
219147c6ae99SBarry Smith {
219247c6ae99SBarry Smith   PetscErrorCode    ierr;
2193b17ce1afSJed Brown   DMCoarsenHookLink link;
219447c6ae99SBarry Smith 
219547c6ae99SBarry Smith   PetscFunctionBegin;
2196171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
219747a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
219847c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
21998892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
220043842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
22018cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2202644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
22030598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2204656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2205e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2206b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2207b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2208b17ce1afSJed Brown   }
220947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2210b17ce1afSJed Brown   PetscFunctionReturn(0);
2211b17ce1afSJed Brown }
2212b17ce1afSJed Brown 
2213b17ce1afSJed Brown #undef __FUNCT__
2214b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2215bb9467b5SJed Brown /*@C
2216b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2217b17ce1afSJed Brown 
2218b17ce1afSJed Brown    Logically Collective
2219b17ce1afSJed Brown 
2220b17ce1afSJed Brown    Input Arguments:
2221b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2222b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2223b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
22240298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2225b17ce1afSJed Brown 
2226b17ce1afSJed Brown    Calling sequence of coarsenhook:
2227b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2228b17ce1afSJed Brown 
2229b17ce1afSJed Brown +  fine - fine level DM
2230b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2231b17ce1afSJed Brown -  ctx - optional user-defined function context
2232b17ce1afSJed Brown 
2233b17ce1afSJed Brown    Calling sequence for restricthook:
2234c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2235b17ce1afSJed Brown 
2236b17ce1afSJed Brown +  fine - fine level DM
2237b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2238c833c3b5SJed Brown .  rscale - scaling vector for restriction
2239c833c3b5SJed Brown .  inject - matrix restricting by injection
2240b17ce1afSJed Brown .  coarse - coarse level DM to update
2241b17ce1afSJed Brown -  ctx - optional user-defined function context
2242b17ce1afSJed Brown 
2243b17ce1afSJed Brown    Level: advanced
2244b17ce1afSJed Brown 
2245b17ce1afSJed Brown    Notes:
2246b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2247b17ce1afSJed Brown 
2248b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2249b17ce1afSJed Brown 
2250b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2251b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2252b17ce1afSJed Brown 
2253bb9467b5SJed Brown    This function is currently not available from Fortran.
2254bb9467b5SJed Brown 
2255c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2256b17ce1afSJed Brown @*/
2257b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2258b17ce1afSJed Brown {
2259b17ce1afSJed Brown   PetscErrorCode    ierr;
2260b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2261b17ce1afSJed Brown 
2262b17ce1afSJed Brown   PetscFunctionBegin;
2263b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
22646bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2265b17ce1afSJed Brown   ierr               = PetscMalloc(sizeof(struct _DMCoarsenHookLink),&link);CHKERRQ(ierr);
2266b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2267b17ce1afSJed Brown   link->restricthook = restricthook;
2268b17ce1afSJed Brown   link->ctx          = ctx;
22690298fd71SBarry Smith   link->next         = NULL;
2270b17ce1afSJed Brown   *p                 = link;
2271b17ce1afSJed Brown   PetscFunctionReturn(0);
2272b17ce1afSJed Brown }
2273b17ce1afSJed Brown 
2274b17ce1afSJed Brown #undef __FUNCT__
2275b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2276b17ce1afSJed Brown /*@
2277b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2278b17ce1afSJed Brown 
2279b17ce1afSJed Brown    Collective if any hooks are
2280b17ce1afSJed Brown 
2281b17ce1afSJed Brown    Input Arguments:
2282b17ce1afSJed Brown +  fine - finer DM to use as a base
2283b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2284b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2285b17ce1afSJed Brown -  coarse - coarer DM to update
2286b17ce1afSJed Brown 
2287b17ce1afSJed Brown    Level: developer
2288b17ce1afSJed Brown 
2289b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2290b17ce1afSJed Brown @*/
2291b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2292b17ce1afSJed Brown {
2293b17ce1afSJed Brown   PetscErrorCode    ierr;
2294b17ce1afSJed Brown   DMCoarsenHookLink link;
2295b17ce1afSJed Brown 
2296b17ce1afSJed Brown   PetscFunctionBegin;
2297b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
22988865f1eaSKarl Rupp     if (link->restricthook) {
22998865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
23008865f1eaSKarl Rupp     }
2301b17ce1afSJed Brown   }
230247c6ae99SBarry Smith   PetscFunctionReturn(0);
230347c6ae99SBarry Smith }
230447c6ae99SBarry Smith 
230547c6ae99SBarry Smith #undef __FUNCT__
2306be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2307bb9467b5SJed Brown /*@C
2308be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
23095dbd56e3SPeter Brune 
23105dbd56e3SPeter Brune    Logically Collective
23115dbd56e3SPeter Brune 
23125dbd56e3SPeter Brune    Input Arguments:
23135dbd56e3SPeter Brune +  global - global DM
2314ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
23155dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
23160298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
23175dbd56e3SPeter Brune 
2318ec4806b8SPeter Brune 
2319ec4806b8SPeter Brune    Calling sequence for ddhook:
2320ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2321ec4806b8SPeter Brune 
2322ec4806b8SPeter Brune +  global - global DM
2323ec4806b8SPeter Brune .  block  - block DM
2324ec4806b8SPeter Brune -  ctx - optional user-defined function context
2325ec4806b8SPeter Brune 
23265dbd56e3SPeter Brune    Calling sequence for restricthook:
2327ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
23285dbd56e3SPeter Brune 
23295dbd56e3SPeter Brune +  global - global DM
23305dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
23315dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2332ec4806b8SPeter Brune .  block  - block DM
23335dbd56e3SPeter Brune -  ctx - optional user-defined function context
23345dbd56e3SPeter Brune 
23355dbd56e3SPeter Brune    Level: advanced
23365dbd56e3SPeter Brune 
23375dbd56e3SPeter Brune    Notes:
2338ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
23395dbd56e3SPeter Brune 
23405dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
23415dbd56e3SPeter Brune 
23425dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2343ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
23445dbd56e3SPeter Brune 
2345bb9467b5SJed Brown    This function is currently not available from Fortran.
2346bb9467b5SJed Brown 
23475dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
23485dbd56e3SPeter Brune @*/
2349be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
23505dbd56e3SPeter Brune {
23515dbd56e3SPeter Brune   PetscErrorCode      ierr;
2352be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
23535dbd56e3SPeter Brune 
23545dbd56e3SPeter Brune   PetscFunctionBegin;
23555dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2356be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
2357be081cd6SPeter Brune   ierr               = PetscMalloc(sizeof(struct _DMSubDomainHookLink),&link);CHKERRQ(ierr);
23585dbd56e3SPeter Brune   link->restricthook = restricthook;
2359be081cd6SPeter Brune   link->ddhook       = ddhook;
23605dbd56e3SPeter Brune   link->ctx          = ctx;
23610298fd71SBarry Smith   link->next         = NULL;
23625dbd56e3SPeter Brune   *p                 = link;
23635dbd56e3SPeter Brune   PetscFunctionReturn(0);
23645dbd56e3SPeter Brune }
23655dbd56e3SPeter Brune 
23665dbd56e3SPeter Brune #undef __FUNCT__
2367be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
23685dbd56e3SPeter Brune /*@
2369be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
23705dbd56e3SPeter Brune 
23715dbd56e3SPeter Brune    Collective if any hooks are
23725dbd56e3SPeter Brune 
23735dbd56e3SPeter Brune    Input Arguments:
23745dbd56e3SPeter Brune +  fine - finer DM to use as a base
2375be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2376be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
23775dbd56e3SPeter Brune -  coarse - coarer DM to update
23785dbd56e3SPeter Brune 
23795dbd56e3SPeter Brune    Level: developer
23805dbd56e3SPeter Brune 
23815dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
23825dbd56e3SPeter Brune @*/
2383be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
23845dbd56e3SPeter Brune {
23855dbd56e3SPeter Brune   PetscErrorCode      ierr;
2386be081cd6SPeter Brune   DMSubDomainHookLink link;
23875dbd56e3SPeter Brune 
23885dbd56e3SPeter Brune   PetscFunctionBegin;
2389be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
23908865f1eaSKarl Rupp     if (link->restricthook) {
23918865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
23928865f1eaSKarl Rupp     }
23935dbd56e3SPeter Brune   }
23945dbd56e3SPeter Brune   PetscFunctionReturn(0);
23955dbd56e3SPeter Brune }
23965dbd56e3SPeter Brune 
23975dbd56e3SPeter Brune #undef __FUNCT__
23985fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
23995fe1f584SPeter Brune /*@
24006a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
24015fe1f584SPeter Brune 
24025fe1f584SPeter Brune     Not Collective
24035fe1f584SPeter Brune 
24045fe1f584SPeter Brune     Input Parameter:
24055fe1f584SPeter Brune .   dm - the DM object
24065fe1f584SPeter Brune 
24075fe1f584SPeter Brune     Output Parameter:
24086a7d9d85SPeter Brune .   level - number of coarsenings
24095fe1f584SPeter Brune 
24105fe1f584SPeter Brune     Level: developer
24115fe1f584SPeter Brune 
24126a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
24135fe1f584SPeter Brune 
24145fe1f584SPeter Brune @*/
24155fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
24165fe1f584SPeter Brune {
24175fe1f584SPeter Brune   PetscFunctionBegin;
24185fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24195fe1f584SPeter Brune   *level = dm->leveldown;
24205fe1f584SPeter Brune   PetscFunctionReturn(0);
24215fe1f584SPeter Brune }
24225fe1f584SPeter Brune 
24235fe1f584SPeter Brune 
24245fe1f584SPeter Brune 
24255fe1f584SPeter Brune #undef __FUNCT__
242647c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
242747c6ae99SBarry Smith /*@C
242847c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
242947c6ae99SBarry Smith 
243047c6ae99SBarry Smith     Collective on DM
243147c6ae99SBarry Smith 
243247c6ae99SBarry Smith     Input Parameter:
243347c6ae99SBarry Smith +   dm - the DM object
243447c6ae99SBarry Smith -   nlevels - the number of levels of refinement
243547c6ae99SBarry Smith 
243647c6ae99SBarry Smith     Output Parameter:
243747c6ae99SBarry Smith .   dmf - the refined DM hierarchy
243847c6ae99SBarry Smith 
243947c6ae99SBarry Smith     Level: developer
244047c6ae99SBarry Smith 
2441e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
244247c6ae99SBarry Smith 
244347c6ae99SBarry Smith @*/
24447087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
244547c6ae99SBarry Smith {
244647c6ae99SBarry Smith   PetscErrorCode ierr;
244747c6ae99SBarry Smith 
244847c6ae99SBarry Smith   PetscFunctionBegin;
2449171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2450ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
245147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
245247c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
245347c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
245447c6ae99SBarry Smith   } else if (dm->ops->refine) {
245547c6ae99SBarry Smith     PetscInt i;
245647c6ae99SBarry Smith 
2457ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
245847c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2459ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
246047c6ae99SBarry Smith     }
2461ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
246247c6ae99SBarry Smith   PetscFunctionReturn(0);
246347c6ae99SBarry Smith }
246447c6ae99SBarry Smith 
246547c6ae99SBarry Smith #undef __FUNCT__
246647c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
246747c6ae99SBarry Smith /*@C
246847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
246947c6ae99SBarry Smith 
247047c6ae99SBarry Smith     Collective on DM
247147c6ae99SBarry Smith 
247247c6ae99SBarry Smith     Input Parameter:
247347c6ae99SBarry Smith +   dm - the DM object
247447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
247547c6ae99SBarry Smith 
247647c6ae99SBarry Smith     Output Parameter:
247747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
247847c6ae99SBarry Smith 
247947c6ae99SBarry Smith     Level: developer
248047c6ae99SBarry Smith 
2481e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
248247c6ae99SBarry Smith 
248347c6ae99SBarry Smith @*/
24847087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
248547c6ae99SBarry Smith {
248647c6ae99SBarry Smith   PetscErrorCode ierr;
248747c6ae99SBarry Smith 
248847c6ae99SBarry Smith   PetscFunctionBegin;
2489171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2490ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
249147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
249247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
249347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
249447c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
249547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
249647c6ae99SBarry Smith     PetscInt i;
249747c6ae99SBarry Smith 
2498ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
249947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2500ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
250147c6ae99SBarry Smith     }
2502ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
250347c6ae99SBarry Smith   PetscFunctionReturn(0);
250447c6ae99SBarry Smith }
250547c6ae99SBarry Smith 
250647c6ae99SBarry Smith #undef __FUNCT__
2507e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
250847c6ae99SBarry Smith /*@
2509e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
251047c6ae99SBarry Smith    grids associated with two DMs.
251147c6ae99SBarry Smith 
251247c6ae99SBarry Smith    Collective on DM
251347c6ae99SBarry Smith 
251447c6ae99SBarry Smith    Input Parameters:
251547c6ae99SBarry Smith +  dmc - the coarse grid DM
251647c6ae99SBarry Smith -  dmf - the fine grid DM
251747c6ae99SBarry Smith 
251847c6ae99SBarry Smith    Output Parameters:
251947c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
252047c6ae99SBarry Smith 
252147c6ae99SBarry Smith    Level: intermediate
252247c6ae99SBarry Smith 
252347c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
252447c6ae99SBarry Smith 
2525e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
252647c6ae99SBarry Smith @*/
2527e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
252847c6ae99SBarry Smith {
252947c6ae99SBarry Smith   PetscErrorCode ierr;
253047c6ae99SBarry Smith 
253147c6ae99SBarry Smith   PetscFunctionBegin;
2532171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2533171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
253447c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
253547c6ae99SBarry Smith   PetscFunctionReturn(0);
253647c6ae99SBarry Smith }
253747c6ae99SBarry Smith 
253847c6ae99SBarry Smith #undef __FUNCT__
25391a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
25401a266240SBarry Smith /*@C
25411a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
25421a266240SBarry Smith 
25431a266240SBarry Smith     Not Collective
25441a266240SBarry Smith 
25451a266240SBarry Smith     Input Parameters:
25461a266240SBarry Smith +   dm - the DM object
25471a266240SBarry Smith -   destroy - the destroy function
25481a266240SBarry Smith 
25491a266240SBarry Smith     Level: intermediate
25501a266240SBarry Smith 
2551e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
25521a266240SBarry Smith 
2553f07f9ceaSJed Brown @*/
25541a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
25551a266240SBarry Smith {
25561a266240SBarry Smith   PetscFunctionBegin;
2557171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25581a266240SBarry Smith   dm->ctxdestroy = destroy;
25591a266240SBarry Smith   PetscFunctionReturn(0);
25601a266240SBarry Smith }
25611a266240SBarry Smith 
25621a266240SBarry Smith #undef __FUNCT__
25631b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2564b07ff414SBarry Smith /*@
25651b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
256647c6ae99SBarry Smith 
256747c6ae99SBarry Smith     Not Collective
256847c6ae99SBarry Smith 
256947c6ae99SBarry Smith     Input Parameters:
257047c6ae99SBarry Smith +   dm - the DM object
257147c6ae99SBarry Smith -   ctx - the user context
257247c6ae99SBarry Smith 
257347c6ae99SBarry Smith     Level: intermediate
257447c6ae99SBarry Smith 
2575e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
257647c6ae99SBarry Smith 
257747c6ae99SBarry Smith @*/
25781b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
257947c6ae99SBarry Smith {
258047c6ae99SBarry Smith   PetscFunctionBegin;
2581171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
258247c6ae99SBarry Smith   dm->ctx = ctx;
258347c6ae99SBarry Smith   PetscFunctionReturn(0);
258447c6ae99SBarry Smith }
258547c6ae99SBarry Smith 
258647c6ae99SBarry Smith #undef __FUNCT__
25871b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
258847c6ae99SBarry Smith /*@
25891b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
259047c6ae99SBarry Smith 
259147c6ae99SBarry Smith     Not Collective
259247c6ae99SBarry Smith 
259347c6ae99SBarry Smith     Input Parameter:
259447c6ae99SBarry Smith .   dm - the DM object
259547c6ae99SBarry Smith 
259647c6ae99SBarry Smith     Output Parameter:
259747c6ae99SBarry Smith .   ctx - the user context
259847c6ae99SBarry Smith 
259947c6ae99SBarry Smith     Level: intermediate
260047c6ae99SBarry Smith 
2601e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
260247c6ae99SBarry Smith 
260347c6ae99SBarry Smith @*/
26041b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
260547c6ae99SBarry Smith {
260647c6ae99SBarry Smith   PetscFunctionBegin;
2607171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26081b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
260947c6ae99SBarry Smith   PetscFunctionReturn(0);
261047c6ae99SBarry Smith }
261147c6ae99SBarry Smith 
261247c6ae99SBarry Smith #undef __FUNCT__
261308da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
261408da532bSDmitry Karpeev /*@C
2615df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
261608da532bSDmitry Karpeev 
261708da532bSDmitry Karpeev     Logically Collective on DM
261808da532bSDmitry Karpeev 
261908da532bSDmitry Karpeev     Input Parameter:
262008da532bSDmitry Karpeev +   dm - the DM object
26210298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
262208da532bSDmitry Karpeev 
262308da532bSDmitry Karpeev     Level: intermediate
262408da532bSDmitry Karpeev 
2625835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
262608da532bSDmitry Karpeev          DMSetJacobian()
262708da532bSDmitry Karpeev 
262808da532bSDmitry Karpeev @*/
262908da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
263008da532bSDmitry Karpeev {
263108da532bSDmitry Karpeev   PetscFunctionBegin;
263208da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
263308da532bSDmitry Karpeev   PetscFunctionReturn(0);
263408da532bSDmitry Karpeev }
263508da532bSDmitry Karpeev 
263608da532bSDmitry Karpeev #undef __FUNCT__
263708da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
263808da532bSDmitry Karpeev /*@
263908da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
264008da532bSDmitry Karpeev 
264108da532bSDmitry Karpeev     Not Collective
264208da532bSDmitry Karpeev 
264308da532bSDmitry Karpeev     Input Parameter:
264408da532bSDmitry Karpeev .   dm - the DM object to destroy
264508da532bSDmitry Karpeev 
264608da532bSDmitry Karpeev     Output Parameter:
264708da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
264808da532bSDmitry Karpeev 
264908da532bSDmitry Karpeev     Level: developer
265008da532bSDmitry Karpeev 
265174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
265208da532bSDmitry Karpeev 
265308da532bSDmitry Karpeev @*/
265408da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
265508da532bSDmitry Karpeev {
265608da532bSDmitry Karpeev   PetscFunctionBegin;
265708da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
265808da532bSDmitry Karpeev   PetscFunctionReturn(0);
265908da532bSDmitry Karpeev }
266008da532bSDmitry Karpeev 
266108da532bSDmitry Karpeev #undef __FUNCT__
266208da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
266308da532bSDmitry Karpeev /*@C
266408da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
266508da532bSDmitry Karpeev 
266608da532bSDmitry Karpeev     Logically Collective on DM
266708da532bSDmitry Karpeev 
266808da532bSDmitry Karpeev     Input Parameters:
2669907376e6SBarry Smith .   dm - the DM object
267008da532bSDmitry Karpeev 
267108da532bSDmitry Karpeev     Output parameters:
267208da532bSDmitry Karpeev +   xl - lower bound
267308da532bSDmitry Karpeev -   xu - upper bound
267408da532bSDmitry Karpeev 
2675907376e6SBarry Smith     Level: advanced
2676907376e6SBarry Smith 
2677907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
267808da532bSDmitry Karpeev 
267974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
268008da532bSDmitry Karpeev 
268108da532bSDmitry Karpeev @*/
268208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
268308da532bSDmitry Karpeev {
268408da532bSDmitry Karpeev   PetscErrorCode ierr;
26855fd66863SKarl Rupp 
268608da532bSDmitry Karpeev   PetscFunctionBegin;
268708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
268808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
268908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
269008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
26918865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
269208da532bSDmitry Karpeev   PetscFunctionReturn(0);
269308da532bSDmitry Karpeev }
269408da532bSDmitry Karpeev 
269508da532bSDmitry Karpeev #undef __FUNCT__
2696b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2697b0ae01b7SPeter Brune /*@
2698b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2699b0ae01b7SPeter Brune 
2700b0ae01b7SPeter Brune     Not Collective
2701b0ae01b7SPeter Brune 
2702b0ae01b7SPeter Brune     Input Parameter:
2703b0ae01b7SPeter Brune .   dm - the DM object
2704b0ae01b7SPeter Brune 
2705b0ae01b7SPeter Brune     Output Parameter:
2706b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2707b0ae01b7SPeter Brune 
2708b0ae01b7SPeter Brune     Level: developer
2709b0ae01b7SPeter Brune 
2710b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2711b0ae01b7SPeter Brune 
2712b0ae01b7SPeter Brune @*/
2713b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2714b0ae01b7SPeter Brune {
2715b0ae01b7SPeter Brune   PetscFunctionBegin;
2716b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2717b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2718b0ae01b7SPeter Brune }
2719b0ae01b7SPeter Brune 
2720b0ae01b7SPeter Brune #undef  __FUNCT__
272108da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2722748fac09SDmitry Karpeev /*@C
272308da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
272408da532bSDmitry Karpeev 
272508da532bSDmitry Karpeev     Collective on DM
272608da532bSDmitry Karpeev 
272708da532bSDmitry Karpeev     Input Parameter:
272808da532bSDmitry Karpeev +   dm - the DM object
27290298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
273008da532bSDmitry Karpeev 
273108da532bSDmitry Karpeev     Level: developer
273208da532bSDmitry Karpeev 
273374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
273408da532bSDmitry Karpeev 
273508da532bSDmitry Karpeev @*/
273608da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
273708da532bSDmitry Karpeev {
273808da532bSDmitry Karpeev   PetscErrorCode ierr;
27395fd66863SKarl Rupp 
274008da532bSDmitry Karpeev   PetscFunctionBegin;
274108da532bSDmitry Karpeev   if (x) {
274208da532bSDmitry Karpeev     if (!dm->x) {
274308da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
274408da532bSDmitry Karpeev     }
274508da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
27468865f1eaSKarl Rupp   } else if (dm->x) {
274708da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
274808da532bSDmitry Karpeev   }
274908da532bSDmitry Karpeev   PetscFunctionReturn(0);
275008da532bSDmitry Karpeev }
275108da532bSDmitry Karpeev 
27520298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2753264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2754264ace61SBarry Smith 
2755264ace61SBarry Smith #undef __FUNCT__
2756264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2757264ace61SBarry Smith /*@C
2758264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2759264ace61SBarry Smith 
2760264ace61SBarry Smith   Collective on DM
2761264ace61SBarry Smith 
2762264ace61SBarry Smith   Input Parameters:
2763264ace61SBarry Smith + dm     - The DM object
2764264ace61SBarry Smith - method - The name of the DM type
2765264ace61SBarry Smith 
2766264ace61SBarry Smith   Options Database Key:
2767264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2768264ace61SBarry Smith 
2769264ace61SBarry Smith   Notes:
2770e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
2771264ace61SBarry Smith 
2772264ace61SBarry Smith   Level: intermediate
2773264ace61SBarry Smith 
2774264ace61SBarry Smith .keywords: DM, set, type
2775264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
2776264ace61SBarry Smith @*/
277719fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
2778264ace61SBarry Smith {
2779264ace61SBarry Smith   PetscErrorCode (*r)(DM);
2780264ace61SBarry Smith   PetscBool      match;
2781264ace61SBarry Smith   PetscErrorCode ierr;
2782264ace61SBarry Smith 
2783264ace61SBarry Smith   PetscFunctionBegin;
2784264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2785251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
2786264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
2787264ace61SBarry Smith 
27880f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
27891c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
2790ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
2791264ace61SBarry Smith 
2792264ace61SBarry Smith   if (dm->ops->destroy) {
2793264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
27940298fd71SBarry Smith     dm->ops->destroy = NULL;
2795264ace61SBarry Smith   }
2796264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
2797264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
2798264ace61SBarry Smith   PetscFunctionReturn(0);
2799264ace61SBarry Smith }
2800264ace61SBarry Smith 
2801264ace61SBarry Smith #undef __FUNCT__
2802264ace61SBarry Smith #define __FUNCT__ "DMGetType"
2803264ace61SBarry Smith /*@C
2804264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
2805264ace61SBarry Smith 
2806264ace61SBarry Smith   Not Collective
2807264ace61SBarry Smith 
2808264ace61SBarry Smith   Input Parameter:
2809264ace61SBarry Smith . dm  - The DM
2810264ace61SBarry Smith 
2811264ace61SBarry Smith   Output Parameter:
2812264ace61SBarry Smith . type - The DM type name
2813264ace61SBarry Smith 
2814264ace61SBarry Smith   Level: intermediate
2815264ace61SBarry Smith 
2816264ace61SBarry Smith .keywords: DM, get, type, name
2817264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
2818264ace61SBarry Smith @*/
281919fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
2820264ace61SBarry Smith {
2821264ace61SBarry Smith   PetscErrorCode ierr;
2822264ace61SBarry Smith 
2823264ace61SBarry Smith   PetscFunctionBegin;
2824264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
2825c959eef4SJed Brown   PetscValidPointer(type,2);
2826607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
2827264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
2828264ace61SBarry Smith   PetscFunctionReturn(0);
2829264ace61SBarry Smith }
2830264ace61SBarry Smith 
283167a56275SMatthew G Knepley #undef __FUNCT__
283267a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
283367a56275SMatthew G Knepley /*@C
283467a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
283567a56275SMatthew G Knepley 
283667a56275SMatthew G Knepley   Collective on DM
283767a56275SMatthew G Knepley 
283867a56275SMatthew G Knepley   Input Parameters:
283967a56275SMatthew G Knepley + dm - the DM
284067a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
284167a56275SMatthew G Knepley 
284267a56275SMatthew G Knepley   Output Parameter:
284367a56275SMatthew G Knepley . M - pointer to new DM
284467a56275SMatthew G Knepley 
284567a56275SMatthew G Knepley   Notes:
284667a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
284767a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
284867a56275SMatthew G Knepley   of the input DM.
284967a56275SMatthew G Knepley 
285067a56275SMatthew G Knepley   Level: intermediate
285167a56275SMatthew G Knepley 
285267a56275SMatthew G Knepley .seealso: DMCreate()
285367a56275SMatthew G Knepley @*/
285419fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
285567a56275SMatthew G Knepley {
285667a56275SMatthew G Knepley   DM             B;
285767a56275SMatthew G Knepley   char           convname[256];
285867a56275SMatthew G Knepley   PetscBool      sametype, issame;
285967a56275SMatthew G Knepley   PetscErrorCode ierr;
286067a56275SMatthew G Knepley 
286167a56275SMatthew G Knepley   PetscFunctionBegin;
286267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
286367a56275SMatthew G Knepley   PetscValidType(dm,1);
286467a56275SMatthew G Knepley   PetscValidPointer(M,3);
2865251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
286667a56275SMatthew G Knepley   ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr);
286767a56275SMatthew G Knepley   {
28680298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
286967a56275SMatthew G Knepley 
287067a56275SMatthew G Knepley     /*
287167a56275SMatthew G Knepley        Order of precedence:
287267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
287367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
287467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
287567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
287667a56275SMatthew G Knepley        5) Use a really basic converter.
287767a56275SMatthew G Knepley     */
287867a56275SMatthew G Knepley 
287967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
288067a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
288167a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
288267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
288367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
288467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28850005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
288667a56275SMatthew G Knepley     if (conv) goto foundconv;
288767a56275SMatthew G Knepley 
288867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
288982f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
289067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
289167a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
289267a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
289367a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
289467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
289567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
28960005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
289767a56275SMatthew G Knepley     if (conv) {
2898fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
289967a56275SMatthew G Knepley       goto foundconv;
290067a56275SMatthew G Knepley     }
290167a56275SMatthew G Knepley 
290267a56275SMatthew G Knepley #if 0
290367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
290467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
2905fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
290667a56275SMatthew G Knepley     if (conv) goto foundconv;
290767a56275SMatthew G Knepley 
290867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
290967a56275SMatthew G Knepley     if (dm->ops->convert) {
291067a56275SMatthew G Knepley       conv = dm->ops->convert;
291167a56275SMatthew G Knepley     }
291267a56275SMatthew G Knepley     if (conv) goto foundconv;
291367a56275SMatthew G Knepley #endif
291467a56275SMatthew G Knepley 
291567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
291682f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
291767a56275SMatthew G Knepley 
291867a56275SMatthew G Knepley foundconv:
291967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
292067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
292167a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
292267a56275SMatthew G Knepley   }
292367a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
292467a56275SMatthew G Knepley   PetscFunctionReturn(0);
292567a56275SMatthew G Knepley }
2926264ace61SBarry Smith 
2927264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
2928264ace61SBarry Smith 
2929264ace61SBarry Smith #undef __FUNCT__
2930264ace61SBarry Smith #define __FUNCT__ "DMRegister"
2931264ace61SBarry Smith /*@C
29321c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
29331c84c290SBarry Smith 
29341c84c290SBarry Smith   Not Collective
29351c84c290SBarry Smith 
29361c84c290SBarry Smith   Input Parameters:
29371c84c290SBarry Smith + name        - The name of a new user-defined creation routine
29381c84c290SBarry Smith - create_func - The creation routine itself
29391c84c290SBarry Smith 
29401c84c290SBarry Smith   Notes:
29411c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
29421c84c290SBarry Smith 
29431c84c290SBarry Smith 
29441c84c290SBarry Smith   Sample usage:
29451c84c290SBarry Smith .vb
2946bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
29471c84c290SBarry Smith .ve
29481c84c290SBarry Smith 
29491c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
29501c84c290SBarry Smith .vb
29511c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
29521c84c290SBarry Smith     DMSetType(DM,"my_da");
29531c84c290SBarry Smith .ve
29541c84c290SBarry Smith    or at runtime via the option
29551c84c290SBarry Smith .vb
29561c84c290SBarry Smith     -da_type my_da
29571c84c290SBarry Smith .ve
2958264ace61SBarry Smith 
2959264ace61SBarry Smith   Level: advanced
29601c84c290SBarry Smith 
29611c84c290SBarry Smith .keywords: DM, register
2962bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
29631c84c290SBarry Smith 
2964264ace61SBarry Smith @*/
2965bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
2966264ace61SBarry Smith {
2967264ace61SBarry Smith   PetscErrorCode ierr;
2968264ace61SBarry Smith 
2969264ace61SBarry Smith   PetscFunctionBegin;
2970a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
2971264ace61SBarry Smith   PetscFunctionReturn(0);
2972264ace61SBarry Smith }
2973264ace61SBarry Smith 
2974b859378eSBarry Smith #undef __FUNCT__
2975b859378eSBarry Smith #define __FUNCT__ "DMLoad"
2976b859378eSBarry Smith /*@C
297755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
2978b859378eSBarry Smith 
2979b859378eSBarry Smith   Collective on PetscViewer
2980b859378eSBarry Smith 
2981b859378eSBarry Smith   Input Parameters:
2982b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
2983b859378eSBarry Smith            some related function before a call to DMLoad().
2984b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
2985b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
2986b859378eSBarry Smith 
2987b859378eSBarry Smith    Level: intermediate
2988b859378eSBarry Smith 
2989b859378eSBarry Smith   Notes:
299055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
2991b859378eSBarry Smith 
2992b859378eSBarry Smith   Notes for advanced users:
2993b859378eSBarry Smith   Most users should not need to know the details of the binary storage
2994b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
2995b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
2996b859378eSBarry Smith   format is
2997b859378eSBarry Smith .vb
2998b859378eSBarry Smith      has not yet been determined
2999b859378eSBarry Smith .ve
3000b859378eSBarry Smith 
3001b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3002b859378eSBarry Smith @*/
3003b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3004b859378eSBarry Smith {
30059331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3006b859378eSBarry Smith   PetscErrorCode ierr;
3007b859378eSBarry Smith 
3008b859378eSBarry Smith   PetscFunctionBegin;
3009b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3010b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
301132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
30129331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
30139331c7a4SMatthew G. Knepley   if (isbinary) {
30149331c7a4SMatthew G. Knepley     PetscInt classid;
30159331c7a4SMatthew G. Knepley     char     type[256];
3016b859378eSBarry Smith 
3017060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
30189200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3019060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
302032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
30219331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30229331c7a4SMatthew G. Knepley   } else if (ishdf5) {
30239331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
30249331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3025b859378eSBarry Smith   PetscFunctionReturn(0);
3026b859378eSBarry Smith }
3027b859378eSBarry Smith 
30287da65231SMatthew G Knepley /******************************** FEM Support **********************************/
30297da65231SMatthew G Knepley 
30307da65231SMatthew G Knepley #undef __FUNCT__
30317da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3032a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3033a6dfd86eSKarl Rupp {
30341d47ebbbSSatish Balay   PetscInt       f;
30351b30c384SMatthew G Knepley   PetscErrorCode ierr;
30361b30c384SMatthew G Knepley 
30377da65231SMatthew G Knepley   PetscFunctionBegin;
303874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30391d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
304057622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
30417da65231SMatthew G Knepley   }
30427da65231SMatthew G Knepley   PetscFunctionReturn(0);
30437da65231SMatthew G Knepley }
30447da65231SMatthew G Knepley 
30457da65231SMatthew G Knepley #undef __FUNCT__
30467da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3047a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3048a6dfd86eSKarl Rupp {
30491b30c384SMatthew G Knepley   PetscInt       f, g;
30507da65231SMatthew G Knepley   PetscErrorCode ierr;
30517da65231SMatthew G Knepley 
30527da65231SMatthew G Knepley   PetscFunctionBegin;
305374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
30541d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
305574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
30561d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3057e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
30587da65231SMatthew G Knepley     }
305974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
30607da65231SMatthew G Knepley   }
30617da65231SMatthew G Knepley   PetscFunctionReturn(0);
30627da65231SMatthew G Knepley }
3063e7c4fc90SDmitry Karpeev 
3064970e74d5SMatthew G Knepley #undef __FUNCT__
3065e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
30666113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3067e759306cSMatthew G. Knepley {
3068e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3069e759306cSMatthew G. Knepley   PetscInt       p;
3070e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3071e759306cSMatthew G. Knepley 
3072e759306cSMatthew G. Knepley   PetscFunctionBegin;
3073e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3074e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3075e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3076e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3077e759306cSMatthew G. Knepley     if (p == rank) {
3078e759306cSMatthew G. Knepley       Vec x;
3079e759306cSMatthew G. Knepley 
3080e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3081e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
30826113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3083e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3084e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3085e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3086e759306cSMatthew G. Knepley     }
3087e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3088e759306cSMatthew G. Knepley   }
3089e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3090e759306cSMatthew G. Knepley }
3091e759306cSMatthew G. Knepley 
3092e759306cSMatthew G. Knepley #undef __FUNCT__
309388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
309488ed4aceSMatthew G Knepley /*@
309588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
309688ed4aceSMatthew G Knepley 
309788ed4aceSMatthew G Knepley   Input Parameter:
309888ed4aceSMatthew G Knepley . dm - The DM
309988ed4aceSMatthew G Knepley 
310088ed4aceSMatthew G Knepley   Output Parameter:
310188ed4aceSMatthew G Knepley . section - The PetscSection
310288ed4aceSMatthew G Knepley 
310388ed4aceSMatthew G Knepley   Level: intermediate
310488ed4aceSMatthew G Knepley 
310588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
310688ed4aceSMatthew G Knepley 
310788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
310888ed4aceSMatthew G Knepley @*/
31090adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
31100adebc6cSBarry Smith {
3111fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3112fd59a867SMatthew G. Knepley 
311388ed4aceSMatthew G Knepley   PetscFunctionBegin;
311488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
311588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
31162f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
31172f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3118*ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
31192f0f8703SMatthew G. Knepley   }
312088ed4aceSMatthew G Knepley   *section = dm->defaultSection;
312188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
312288ed4aceSMatthew G Knepley }
312388ed4aceSMatthew G Knepley 
312488ed4aceSMatthew G Knepley #undef __FUNCT__
312588ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
312688ed4aceSMatthew G Knepley /*@
312788ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
312888ed4aceSMatthew G Knepley 
312988ed4aceSMatthew G Knepley   Input Parameters:
313088ed4aceSMatthew G Knepley + dm - The DM
313188ed4aceSMatthew G Knepley - section - The PetscSection
313288ed4aceSMatthew G Knepley 
313388ed4aceSMatthew G Knepley   Level: intermediate
313488ed4aceSMatthew G Knepley 
313588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
313688ed4aceSMatthew G Knepley 
313788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
313888ed4aceSMatthew G Knepley @*/
31390adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
31400adebc6cSBarry Smith {
3141c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3142af122d2aSMatthew G Knepley   PetscInt       f;
314388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
314488ed4aceSMatthew G Knepley 
314588ed4aceSMatthew G Knepley   PetscFunctionBegin;
314688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3147c473ab19SMatthew G. Knepley   if (section) {
31481d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
31491d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3150c473ab19SMatthew G. Knepley   }
315188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
315288ed4aceSMatthew G Knepley   dm->defaultSection = section;
3153c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3154af122d2aSMatthew G Knepley   if (numFields) {
3155af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3156af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
31570f21e855SMatthew G. Knepley       PetscObject disc;
3158af122d2aSMatthew G Knepley       const char *name;
3159af122d2aSMatthew G Knepley 
3160af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
31610f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
31620f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3163af122d2aSMatthew G Knepley     }
3164af122d2aSMatthew G Knepley   }
31651d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
31661d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
316788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
316888ed4aceSMatthew G Knepley }
316988ed4aceSMatthew G Knepley 
317088ed4aceSMatthew G Knepley #undef __FUNCT__
31719435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
31729435951eSToby Isaac /*@
31739435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
31749435951eSToby Isaac 
3175e228b242SToby Isaac   not collective
3176e228b242SToby Isaac 
31779435951eSToby Isaac   Input Parameter:
31789435951eSToby Isaac . dm - The DM
31799435951eSToby Isaac 
31809435951eSToby Isaac   Output Parameter:
31819435951eSToby 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.
31829435951eSToby 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.
31839435951eSToby Isaac 
31849435951eSToby Isaac   Level: advanced
31859435951eSToby Isaac 
31869435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
31879435951eSToby Isaac 
31889435951eSToby Isaac .seealso: DMSetDefaultConstraints()
31899435951eSToby Isaac @*/
31909435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
31919435951eSToby Isaac {
31929435951eSToby Isaac   PetscErrorCode ierr;
31939435951eSToby Isaac 
31949435951eSToby Isaac   PetscFunctionBegin;
31959435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
31969435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
319745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
319845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
31999435951eSToby Isaac   PetscFunctionReturn(0);
32009435951eSToby Isaac }
32019435951eSToby Isaac 
32029435951eSToby Isaac #undef __FUNCT__
32039435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
32049435951eSToby Isaac /*@
32059435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
32069435951eSToby Isaac 
32079435951eSToby 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().
32089435951eSToby Isaac 
32099435951eSToby 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.
32109435951eSToby Isaac 
3211e228b242SToby Isaac   collective on dm
3212e228b242SToby Isaac 
32139435951eSToby Isaac   Input Parameters:
32149435951eSToby Isaac + dm - The DM
3215e228b242SToby 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).
3216e228b242SToby 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).
32179435951eSToby Isaac 
32189435951eSToby Isaac   Level: advanced
32199435951eSToby Isaac 
32209435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
32219435951eSToby Isaac 
32229435951eSToby Isaac .seealso: DMGetDefaultConstraints()
32239435951eSToby Isaac @*/
32249435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
32259435951eSToby Isaac {
3226e228b242SToby Isaac   PetscMPIInt result;
32279435951eSToby Isaac   PetscErrorCode ierr;
32289435951eSToby Isaac 
32299435951eSToby Isaac   PetscFunctionBegin;
32309435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3231e228b242SToby Isaac   if (section) {
3232e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3233e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3234e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3235e228b242SToby Isaac   }
3236e228b242SToby Isaac   if (mat) {
3237e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3238e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3239e228b242SToby Isaac     if (result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3240e228b242SToby Isaac   }
32419435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
32429435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
32439435951eSToby Isaac   dm->defaultConstraintSection = section;
32449435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
32459435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
32469435951eSToby Isaac   dm->defaultConstraintMat = mat;
32479435951eSToby Isaac   PetscFunctionReturn(0);
32489435951eSToby Isaac }
32499435951eSToby Isaac 
3250f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
32519435951eSToby Isaac #undef __FUNCT__
3252284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3253507e4973SMatthew G. Knepley /*
3254507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3255507e4973SMatthew G. Knepley 
3256507e4973SMatthew G. Knepley   Input Parameters:
3257507e4973SMatthew G. Knepley + dm - The DM
3258507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3259507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3260507e4973SMatthew G. Knepley 
3261507e4973SMatthew G. Knepley   Level: intermediate
3262507e4973SMatthew G. Knepley 
3263507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3264507e4973SMatthew G. Knepley */
3265f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3266507e4973SMatthew G. Knepley {
3267507e4973SMatthew G. Knepley   MPI_Comm        comm;
3268507e4973SMatthew G. Knepley   PetscLayout     layout;
3269507e4973SMatthew G. Knepley   const PetscInt *ranges;
3270507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3271507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3272507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3273507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3274507e4973SMatthew G. Knepley 
3275507e4973SMatthew G. Knepley   PetscFunctionBegin;
3276507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3277507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3278507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3279507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3280507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3281507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3282507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3283507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3284507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3285507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3286507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3287507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3288f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3289507e4973SMatthew G. Knepley 
3290507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3291507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3292507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3293507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3294507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3295507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3296507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3297507e4973SMatthew 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;}
3298507e4973SMatthew 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;}
3299507e4973SMatthew G. Knepley     if (gdof < 0) {
3300507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3301507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3302507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3303507e4973SMatthew G. Knepley 
3304507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3305507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3306507e4973SMatthew 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;}
3307507e4973SMatthew G. Knepley       }
3308507e4973SMatthew G. Knepley     }
3309507e4973SMatthew G. Knepley   }
3310507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3311507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3312b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3313507e4973SMatthew G. Knepley   if (!gvalid) {
3314507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3315507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3316507e4973SMatthew G. Knepley   }
3317507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3318507e4973SMatthew G. Knepley }
3319f741bcd2SMatthew G. Knepley #endif
3320507e4973SMatthew G. Knepley 
3321507e4973SMatthew G. Knepley #undef __FUNCT__
332288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
332388ed4aceSMatthew G Knepley /*@
332488ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
332588ed4aceSMatthew G Knepley 
33268b1ab98fSJed Brown   Collective on DM
33278b1ab98fSJed Brown 
332888ed4aceSMatthew G Knepley   Input Parameter:
332988ed4aceSMatthew G Knepley . dm - The DM
333088ed4aceSMatthew G Knepley 
333188ed4aceSMatthew G Knepley   Output Parameter:
333288ed4aceSMatthew G Knepley . section - The PetscSection
333388ed4aceSMatthew G Knepley 
333488ed4aceSMatthew G Knepley   Level: intermediate
333588ed4aceSMatthew G Knepley 
333688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
333788ed4aceSMatthew G Knepley 
333888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
333988ed4aceSMatthew G Knepley @*/
33400adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
33410adebc6cSBarry Smith {
334288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
334388ed4aceSMatthew G Knepley 
334488ed4aceSMatthew G Knepley   PetscFunctionBegin;
334588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
334688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
334788ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3348fd59a867SMatthew G. Knepley     PetscSection s;
3349fd59a867SMatthew G. Knepley 
3350fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3351fd59a867SMatthew 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");
3352fd59a867SMatthew 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");
335315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3354cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3355ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3356685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
335788ed4aceSMatthew G Knepley   }
335888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
335988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
336088ed4aceSMatthew G Knepley }
336188ed4aceSMatthew G Knepley 
336288ed4aceSMatthew G Knepley #undef __FUNCT__
3363b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3364b21d0597SMatthew G Knepley /*@
3365b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3366b21d0597SMatthew G Knepley 
3367b21d0597SMatthew G Knepley   Input Parameters:
3368b21d0597SMatthew G Knepley + dm - The DM
33695080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3370b21d0597SMatthew G Knepley 
3371b21d0597SMatthew G Knepley   Level: intermediate
3372b21d0597SMatthew G Knepley 
3373b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3374b21d0597SMatthew G Knepley 
3375b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3376b21d0597SMatthew G Knepley @*/
33770adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
33780adebc6cSBarry Smith {
3379b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3380b21d0597SMatthew G Knepley 
3381b21d0597SMatthew G Knepley   PetscFunctionBegin;
3382b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
33835080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33841d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3385b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3386b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3387507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3388f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3389507e4973SMatthew G. Knepley #endif
3390b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3391b21d0597SMatthew G Knepley }
3392b21d0597SMatthew G Knepley 
3393b21d0597SMatthew G Knepley #undef __FUNCT__
339488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
339588ed4aceSMatthew G Knepley /*@
339688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
339788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
339888ed4aceSMatthew G Knepley 
339988ed4aceSMatthew G Knepley   Input Parameter:
340088ed4aceSMatthew G Knepley . dm - The DM
340188ed4aceSMatthew G Knepley 
340288ed4aceSMatthew G Knepley   Output Parameter:
340388ed4aceSMatthew G Knepley . sf - The PetscSF
340488ed4aceSMatthew G Knepley 
340588ed4aceSMatthew G Knepley   Level: intermediate
340688ed4aceSMatthew G Knepley 
340788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
340888ed4aceSMatthew G Knepley 
340988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
341088ed4aceSMatthew G Knepley @*/
34110adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
34120adebc6cSBarry Smith {
341388ed4aceSMatthew G Knepley   PetscInt       nroots;
341488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
341588ed4aceSMatthew G Knepley 
341688ed4aceSMatthew G Knepley   PetscFunctionBegin;
341788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
341888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
34190298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
342088ed4aceSMatthew G Knepley   if (nroots < 0) {
342188ed4aceSMatthew G Knepley     PetscSection section, gSection;
342288ed4aceSMatthew G Knepley 
342388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
342431ea6d37SMatthew G Knepley     if (section) {
342588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
342688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
342731ea6d37SMatthew G Knepley     } else {
34280298fd71SBarry Smith       *sf = NULL;
342931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
343031ea6d37SMatthew G Knepley     }
343188ed4aceSMatthew G Knepley   }
343288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
343388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
343488ed4aceSMatthew G Knepley }
343588ed4aceSMatthew G Knepley 
343688ed4aceSMatthew G Knepley #undef __FUNCT__
343788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
343888ed4aceSMatthew G Knepley /*@
343988ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
344088ed4aceSMatthew G Knepley 
344188ed4aceSMatthew G Knepley   Input Parameters:
344288ed4aceSMatthew G Knepley + dm - The DM
344388ed4aceSMatthew G Knepley - sf - The PetscSF
344488ed4aceSMatthew G Knepley 
344588ed4aceSMatthew G Knepley   Level: intermediate
344688ed4aceSMatthew G Knepley 
344788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
344888ed4aceSMatthew G Knepley 
344988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
345088ed4aceSMatthew G Knepley @*/
34510adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
34520adebc6cSBarry Smith {
345388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
345488ed4aceSMatthew G Knepley 
345588ed4aceSMatthew G Knepley   PetscFunctionBegin;
345688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
345788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
345888ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
345988ed4aceSMatthew G Knepley   dm->defaultSF = sf;
346088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
346188ed4aceSMatthew G Knepley }
346288ed4aceSMatthew G Knepley 
346388ed4aceSMatthew G Knepley #undef __FUNCT__
346488ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
346588ed4aceSMatthew G Knepley /*@C
346688ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
346788ed4aceSMatthew G Knepley   describing the data layout.
346888ed4aceSMatthew G Knepley 
346988ed4aceSMatthew G Knepley   Input Parameters:
347088ed4aceSMatthew G Knepley + dm - The DM
347188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
347288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
347388ed4aceSMatthew G Knepley 
347488ed4aceSMatthew G Knepley   Level: intermediate
347588ed4aceSMatthew G Knepley 
347688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
347788ed4aceSMatthew G Knepley @*/
347888ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
347988ed4aceSMatthew G Knepley {
348082f516ccSBarry Smith   MPI_Comm       comm;
348188ed4aceSMatthew G Knepley   PetscLayout    layout;
348288ed4aceSMatthew G Knepley   const PetscInt *ranges;
348388ed4aceSMatthew G Knepley   PetscInt       *local;
348488ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3485ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
348688ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
348788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
348888ed4aceSMatthew G Knepley 
348988ed4aceSMatthew G Knepley   PetscFunctionBegin;
349082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
349188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
349288ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
349388ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
349488ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
349588ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
349688ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
349788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
349888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
349988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
350088ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3501ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
35026636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
350388ed4aceSMatthew G Knepley 
35046636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35056636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3506235fbf56SMatthew 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));
35076636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
350888ed4aceSMatthew G Knepley   }
3509785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3510785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
351188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
35121f588964SMatthew G Knepley     const PetscInt *cind;
35136636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
351488ed4aceSMatthew G Knepley 
351588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
351688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
351788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
351888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
351988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
35206636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
352188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
35226636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
35236636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
35246636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3525057b4bcdSMatthew 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);
35266636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
35276636e97aSMatthew G Knepley     }
352888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
352988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
353088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
353188ed4aceSMatthew G Knepley     }
353288ed4aceSMatthew G Knepley     if (gdof < 0) {
35336636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
353488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
353588ed4aceSMatthew G Knepley 
353605376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
353731d3f06eSJed Brown         if (r < 0) r = -(r+2);
353805376888SMatthew 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);
353988ed4aceSMatthew G Knepley         remote[l].rank  = r;
354088ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
354188ed4aceSMatthew G Knepley       }
354288ed4aceSMatthew G Knepley     } else {
35436636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
354488ed4aceSMatthew G Knepley         remote[l].rank  = rank;
354588ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
354688ed4aceSMatthew G Knepley       }
354788ed4aceSMatthew G Knepley     }
354888ed4aceSMatthew G Knepley   }
35496636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
355088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
355188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
355288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
355388ed4aceSMatthew G Knepley }
3554af122d2aSMatthew G Knepley 
3555af122d2aSMatthew G Knepley #undef __FUNCT__
3556b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3557b21d0597SMatthew G Knepley /*@
3558b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3559b21d0597SMatthew G Knepley 
3560b21d0597SMatthew G Knepley   Input Parameter:
3561b21d0597SMatthew G Knepley . dm - The DM
3562b21d0597SMatthew G Knepley 
3563b21d0597SMatthew G Knepley   Output Parameter:
3564b21d0597SMatthew G Knepley . sf - The PetscSF
3565b21d0597SMatthew G Knepley 
3566b21d0597SMatthew G Knepley   Level: intermediate
3567b21d0597SMatthew G Knepley 
3568b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3569b21d0597SMatthew G Knepley 
3570057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3571b21d0597SMatthew G Knepley @*/
35720adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
35730adebc6cSBarry Smith {
3574b21d0597SMatthew G Knepley   PetscFunctionBegin;
3575b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3576b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3577b21d0597SMatthew G Knepley   *sf = dm->sf;
3578b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3579b21d0597SMatthew G Knepley }
3580b21d0597SMatthew G Knepley 
3581b21d0597SMatthew G Knepley #undef __FUNCT__
3582057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3583057b4bcdSMatthew G Knepley /*@
3584057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3585057b4bcdSMatthew G Knepley 
3586057b4bcdSMatthew G Knepley   Input Parameters:
3587057b4bcdSMatthew G Knepley + dm - The DM
3588057b4bcdSMatthew G Knepley - sf - The PetscSF
3589057b4bcdSMatthew G Knepley 
3590057b4bcdSMatthew G Knepley   Level: intermediate
3591057b4bcdSMatthew G Knepley 
3592057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3593057b4bcdSMatthew G Knepley @*/
35940adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
35950adebc6cSBarry Smith {
3596057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3597057b4bcdSMatthew G Knepley 
3598057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3599057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3600057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3601057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3602057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3603057b4bcdSMatthew G Knepley   dm->sf = sf;
3604057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3605057b4bcdSMatthew G Knepley }
3606057b4bcdSMatthew G Knepley 
3607057b4bcdSMatthew G Knepley #undef __FUNCT__
36082764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
36092764a2aaSMatthew G. Knepley /*@
36102764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
36112764a2aaSMatthew G. Knepley 
36122764a2aaSMatthew G. Knepley   Input Parameter:
36132764a2aaSMatthew G. Knepley . dm - The DM
36142764a2aaSMatthew G. Knepley 
36152764a2aaSMatthew G. Knepley   Output Parameter:
36162764a2aaSMatthew G. Knepley . prob - The PetscDS
36172764a2aaSMatthew G. Knepley 
36182764a2aaSMatthew G. Knepley   Level: developer
36192764a2aaSMatthew G. Knepley 
36202764a2aaSMatthew G. Knepley .seealso: DMSetDS()
36212764a2aaSMatthew G. Knepley @*/
36222764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3623af122d2aSMatthew G Knepley {
3624af122d2aSMatthew G Knepley   PetscFunctionBegin;
3625af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36260f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
36270f21e855SMatthew G. Knepley   *prob = dm->prob;
36280f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36290f21e855SMatthew G. Knepley }
36300f21e855SMatthew G. Knepley 
36310f21e855SMatthew G. Knepley #undef __FUNCT__
36322764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
36332764a2aaSMatthew G. Knepley /*@
36342764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
36352764a2aaSMatthew G. Knepley 
36362764a2aaSMatthew G. Knepley   Input Parameters:
36372764a2aaSMatthew G. Knepley + dm - The DM
36382764a2aaSMatthew G. Knepley - prob - The PetscDS
36392764a2aaSMatthew G. Knepley 
36402764a2aaSMatthew G. Knepley   Level: developer
36412764a2aaSMatthew G. Knepley 
36422764a2aaSMatthew G. Knepley .seealso: DMGetDS()
36432764a2aaSMatthew G. Knepley @*/
36442764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
36450f21e855SMatthew G. Knepley {
36460f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36470f21e855SMatthew G. Knepley 
36480f21e855SMatthew G. Knepley   PetscFunctionBegin;
36490f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36502764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
36512764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
36520f21e855SMatthew G. Knepley   dm->prob = prob;
36530f21e855SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->prob);CHKERRQ(ierr);
36540f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
36550f21e855SMatthew G. Knepley }
36560f21e855SMatthew G. Knepley 
36570f21e855SMatthew G. Knepley #undef __FUNCT__
36580f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
36590f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
36600f21e855SMatthew G. Knepley {
36610f21e855SMatthew G. Knepley   PetscErrorCode ierr;
36620f21e855SMatthew G. Knepley 
36630f21e855SMatthew G. Knepley   PetscFunctionBegin;
36640f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36652764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3666af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3667af122d2aSMatthew G Knepley }
3668af122d2aSMatthew G Knepley 
3669af122d2aSMatthew G Knepley #undef __FUNCT__
3670af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3671af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3672af122d2aSMatthew G Knepley {
36730f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3674af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3675af122d2aSMatthew G Knepley 
3676af122d2aSMatthew G Knepley   PetscFunctionBegin;
3677af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36782764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
36790f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
36800f21e855SMatthew G. Knepley     PetscContainer obj;
36810f21e855SMatthew G. Knepley 
36820f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
36832764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
36840f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3685af122d2aSMatthew G Knepley   }
3686af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3687af122d2aSMatthew G Knepley }
3688af122d2aSMatthew G Knepley 
3689af122d2aSMatthew G Knepley #undef __FUNCT__
3690af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3691c1929be8SMatthew G. Knepley /*@
3692c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3693c1929be8SMatthew G. Knepley 
3694c1929be8SMatthew G. Knepley   Not collective
3695c1929be8SMatthew G. Knepley 
3696c1929be8SMatthew G. Knepley   Input Parameters:
3697c1929be8SMatthew G. Knepley + dm - The DM
3698c1929be8SMatthew G. Knepley - f  - The field number
3699c1929be8SMatthew G. Knepley 
3700c1929be8SMatthew G. Knepley   Output Parameter:
3701c1929be8SMatthew G. Knepley . field - The discretization object
3702c1929be8SMatthew G. Knepley 
3703c1929be8SMatthew G. Knepley   Level: developer
3704c1929be8SMatthew G. Knepley 
3705c1929be8SMatthew G. Knepley .seealso: DMSetField()
3706c1929be8SMatthew G. Knepley @*/
3707af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3708af122d2aSMatthew G Knepley {
37090f21e855SMatthew G. Knepley   PetscErrorCode ierr;
37100f21e855SMatthew G. Knepley 
3711af122d2aSMatthew G Knepley   PetscFunctionBegin;
3712af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37132764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3714decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3715decb47aaSMatthew G. Knepley }
3716decb47aaSMatthew G. Knepley 
3717decb47aaSMatthew G. Knepley #undef __FUNCT__
3718decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3719c1929be8SMatthew G. Knepley /*@
3720c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3721c1929be8SMatthew G. Knepley 
3722c1929be8SMatthew G. Knepley   Logically collective on DM
3723c1929be8SMatthew G. Knepley 
3724c1929be8SMatthew G. Knepley   Input Parameters:
3725c1929be8SMatthew G. Knepley + dm - The DM
3726c1929be8SMatthew G. Knepley . f  - The field number
3727c1929be8SMatthew G. Knepley - field - The discretization object
3728c1929be8SMatthew G. Knepley 
3729c1929be8SMatthew G. Knepley   Level: developer
3730c1929be8SMatthew G. Knepley 
3731c1929be8SMatthew G. Knepley .seealso: DMGetField()
3732c1929be8SMatthew G. Knepley @*/
3733decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3734decb47aaSMatthew G. Knepley {
3735decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3736decb47aaSMatthew G. Knepley 
3737decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3738decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37392764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3740af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3741af122d2aSMatthew G Knepley }
37426636e97aSMatthew G Knepley 
37436636e97aSMatthew G Knepley #undef __FUNCT__
3744b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3745b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3746b64e0483SPeter Brune {
3747b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3748b64e0483SPeter Brune   PetscErrorCode ierr;
3749b64e0483SPeter Brune   Vec coords,ccoords;
37506dbf9973SLawrence Mitchell   Mat inject;
3751b64e0483SPeter Brune   PetscFunctionBegin;
3752b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3753b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3754b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3755b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3756b64e0483SPeter Brune   if (coords && !ccoords) {
3757b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
37586dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
37592adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
37606dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
3761b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
3762b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
3763b64e0483SPeter Brune   }
3764b64e0483SPeter Brune   PetscFunctionReturn(0);
3765b64e0483SPeter Brune }
3766b64e0483SPeter Brune 
3767b64e0483SPeter Brune #undef __FUNCT__
376803dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
376903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
377003dadc2fSPeter Brune {
377103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
377203dadc2fSPeter Brune   PetscErrorCode ierr;
377303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
377403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
377503dadc2fSPeter Brune   PetscFunctionBegin;
377603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
377703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
377803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
377903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
378003dadc2fSPeter Brune   if (coords && !ccoords) {
378103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
378203dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
378303dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
378403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
378803dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
378903dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
379003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
379103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
379203dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
379303dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
379403dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
379503dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
379603dadc2fSPeter Brune   }
379703dadc2fSPeter Brune   PetscFunctionReturn(0);
379803dadc2fSPeter Brune }
379903dadc2fSPeter Brune 
380003dadc2fSPeter Brune #undef __FUNCT__
3801c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
3802c73cfb54SMatthew G. Knepley /*@
3803c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
3804c73cfb54SMatthew G. Knepley 
3805c73cfb54SMatthew G. Knepley   Not collective
3806c73cfb54SMatthew G. Knepley 
3807c73cfb54SMatthew G. Knepley   Input Parameter:
3808c73cfb54SMatthew G. Knepley . dm - The DM
3809c73cfb54SMatthew G. Knepley 
3810c73cfb54SMatthew G. Knepley   Output Parameter:
3811c73cfb54SMatthew G. Knepley . dim - The topological dimension
3812c73cfb54SMatthew G. Knepley 
3813c73cfb54SMatthew G. Knepley   Level: beginner
3814c73cfb54SMatthew G. Knepley 
3815c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
3816c73cfb54SMatthew G. Knepley @*/
3817c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
3818c73cfb54SMatthew G. Knepley {
3819c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3820c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3821c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
3822c73cfb54SMatthew G. Knepley   *dim = dm->dim;
3823c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3824c73cfb54SMatthew G. Knepley }
3825c73cfb54SMatthew G. Knepley 
3826c73cfb54SMatthew G. Knepley #undef __FUNCT__
3827c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
3828c73cfb54SMatthew G. Knepley /*@
3829c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
3830c73cfb54SMatthew G. Knepley 
3831c73cfb54SMatthew G. Knepley   Collective on dm
3832c73cfb54SMatthew G. Knepley 
3833c73cfb54SMatthew G. Knepley   Input Parameters:
3834c73cfb54SMatthew G. Knepley + dm - The DM
3835c73cfb54SMatthew G. Knepley - dim - The topological dimension
3836c73cfb54SMatthew G. Knepley 
3837c73cfb54SMatthew G. Knepley   Level: beginner
3838c73cfb54SMatthew G. Knepley 
3839c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
3840c73cfb54SMatthew G. Knepley @*/
3841c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
3842c73cfb54SMatthew G. Knepley {
3843c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
3844c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3845c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
3846c73cfb54SMatthew G. Knepley   dm->dim = dim;
3847c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
3848c73cfb54SMatthew G. Knepley }
3849c73cfb54SMatthew G. Knepley 
3850793f3fe5SMatthew G. Knepley #undef __FUNCT__
3851793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
3852793f3fe5SMatthew G. Knepley /*@
3853793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
3854793f3fe5SMatthew G. Knepley 
3855793f3fe5SMatthew G. Knepley   Collective on DM
3856793f3fe5SMatthew G. Knepley 
3857793f3fe5SMatthew G. Knepley   Input Parameters:
3858793f3fe5SMatthew G. Knepley + dm - the DM
3859793f3fe5SMatthew G. Knepley - dim - the dimension
3860793f3fe5SMatthew G. Knepley 
3861793f3fe5SMatthew G. Knepley   Output Parameters:
3862793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
3863793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
3864793f3fe5SMatthew G. Knepley 
3865793f3fe5SMatthew G. Knepley   Note:
3866793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
3867793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
3868793f3fe5SMatthew G. Knepley   then the interval is empty.
3869793f3fe5SMatthew G. Knepley 
3870793f3fe5SMatthew G. Knepley   Level: intermediate
3871793f3fe5SMatthew G. Knepley 
3872793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
3873793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
3874793f3fe5SMatthew G. Knepley @*/
3875793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
3876793f3fe5SMatthew G. Knepley {
3877793f3fe5SMatthew G. Knepley   PetscInt       d;
3878793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
3879793f3fe5SMatthew G. Knepley 
3880793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
3881793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3882793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
3883793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
3884793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
3885793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
3886793f3fe5SMatthew G. Knepley }
3887793f3fe5SMatthew G. Knepley 
3888793f3fe5SMatthew G. Knepley #undef __FUNCT__
38896636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
38906636e97aSMatthew G Knepley /*@
38916636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
38926636e97aSMatthew G Knepley 
38936636e97aSMatthew G Knepley   Collective on DM
38946636e97aSMatthew G Knepley 
38956636e97aSMatthew G Knepley   Input Parameters:
38966636e97aSMatthew G Knepley + dm - the DM
38976636e97aSMatthew G Knepley - c - coordinate vector
38986636e97aSMatthew G Knepley 
38996636e97aSMatthew G Knepley   Note:
39006636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
39016636e97aSMatthew G Knepley 
39026636e97aSMatthew G Knepley   Level: intermediate
39036636e97aSMatthew G Knepley 
39046636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39056636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
39066636e97aSMatthew G Knepley @*/
39076636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
39086636e97aSMatthew G Knepley {
39096636e97aSMatthew G Knepley   PetscErrorCode ierr;
39106636e97aSMatthew G Knepley 
39116636e97aSMatthew G Knepley   PetscFunctionBegin;
39126636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39136636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39146636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39156636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39166636e97aSMatthew G Knepley   dm->coordinates = c;
39176636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
3918b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
391903dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
39206636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39216636e97aSMatthew G Knepley }
39226636e97aSMatthew G Knepley 
39236636e97aSMatthew G Knepley #undef __FUNCT__
39246636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
39256636e97aSMatthew G Knepley /*@
39266636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
39276636e97aSMatthew G Knepley 
39286636e97aSMatthew G Knepley   Collective on DM
39296636e97aSMatthew G Knepley 
39306636e97aSMatthew G Knepley    Input Parameters:
39316636e97aSMatthew G Knepley +  dm - the DM
39326636e97aSMatthew G Knepley -  c - coordinate vector
39336636e97aSMatthew G Knepley 
39346636e97aSMatthew G Knepley   Note:
39356636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
39366636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
39376636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
39386636e97aSMatthew G Knepley 
39396636e97aSMatthew G Knepley   Level: intermediate
39406636e97aSMatthew G Knepley 
39416636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39426636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
39436636e97aSMatthew G Knepley @*/
39446636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
39456636e97aSMatthew G Knepley {
39466636e97aSMatthew G Knepley   PetscErrorCode ierr;
39476636e97aSMatthew G Knepley 
39486636e97aSMatthew G Knepley   PetscFunctionBegin;
39496636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39506636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
39516636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
39526636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
39538865f1eaSKarl Rupp 
39546636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
39558865f1eaSKarl Rupp 
39566636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
39576636e97aSMatthew G Knepley   PetscFunctionReturn(0);
39586636e97aSMatthew G Knepley }
39596636e97aSMatthew G Knepley 
39606636e97aSMatthew G Knepley #undef __FUNCT__
39616636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
39626636e97aSMatthew G Knepley /*@
39636636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
39646636e97aSMatthew G Knepley 
39656636e97aSMatthew G Knepley   Not Collective
39666636e97aSMatthew G Knepley 
39676636e97aSMatthew G Knepley   Input Parameter:
39686636e97aSMatthew G Knepley . dm - the DM
39696636e97aSMatthew G Knepley 
39706636e97aSMatthew G Knepley   Output Parameter:
39716636e97aSMatthew G Knepley . c - global coordinate vector
39726636e97aSMatthew G Knepley 
39736636e97aSMatthew G Knepley   Note:
39746636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
39756636e97aSMatthew G Knepley 
39766636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
39776636e97aSMatthew G Knepley 
39786636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
39796636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
39806636e97aSMatthew G Knepley 
39816636e97aSMatthew G Knepley   Level: intermediate
39826636e97aSMatthew G Knepley 
39836636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
39846636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
39856636e97aSMatthew G Knepley @*/
39866636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
39876636e97aSMatthew G Knepley {
39886636e97aSMatthew G Knepley   PetscErrorCode ierr;
39896636e97aSMatthew G Knepley 
39906636e97aSMatthew G Knepley   PetscFunctionBegin;
39916636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
39926636e97aSMatthew G Knepley   PetscValidPointer(c,2);
39931f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
39940298fd71SBarry Smith     DM cdm = NULL;
39956636e97aSMatthew G Knepley 
39966636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
39976636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
39986636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
39996636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40006636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
40016636e97aSMatthew G Knepley   }
40026636e97aSMatthew G Knepley   *c = dm->coordinates;
40036636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40046636e97aSMatthew G Knepley }
40056636e97aSMatthew G Knepley 
40066636e97aSMatthew G Knepley #undef __FUNCT__
40076636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
40086636e97aSMatthew G Knepley /*@
40096636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
40106636e97aSMatthew G Knepley 
40116636e97aSMatthew G Knepley   Collective on DM
40126636e97aSMatthew G Knepley 
40136636e97aSMatthew G Knepley   Input Parameter:
40146636e97aSMatthew G Knepley . dm - the DM
40156636e97aSMatthew G Knepley 
40166636e97aSMatthew G Knepley   Output Parameter:
40176636e97aSMatthew G Knepley . c - coordinate vector
40186636e97aSMatthew G Knepley 
40196636e97aSMatthew G Knepley   Note:
40206636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
40216636e97aSMatthew G Knepley 
40226636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
40236636e97aSMatthew G Knepley 
40246636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
40256636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
40266636e97aSMatthew G Knepley 
40276636e97aSMatthew G Knepley   Level: intermediate
40286636e97aSMatthew G Knepley 
40296636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40306636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
40316636e97aSMatthew G Knepley @*/
40326636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
40336636e97aSMatthew G Knepley {
40346636e97aSMatthew G Knepley   PetscErrorCode ierr;
40356636e97aSMatthew G Knepley 
40366636e97aSMatthew G Knepley   PetscFunctionBegin;
40376636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40386636e97aSMatthew G Knepley   PetscValidPointer(c,2);
40391f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
40400298fd71SBarry Smith     DM cdm = NULL;
40416636e97aSMatthew G Knepley 
40426636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
40436636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
40446636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
40456636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40466636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
40476636e97aSMatthew G Knepley   }
40486636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
40496636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40506636e97aSMatthew G Knepley }
40516636e97aSMatthew G Knepley 
40526636e97aSMatthew G Knepley #undef __FUNCT__
40536636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
40546636e97aSMatthew G Knepley /*@
40551cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
40566636e97aSMatthew G Knepley 
40576636e97aSMatthew G Knepley   Collective on DM
40586636e97aSMatthew G Knepley 
40596636e97aSMatthew G Knepley   Input Parameter:
40606636e97aSMatthew G Knepley . dm - the DM
40616636e97aSMatthew G Knepley 
40626636e97aSMatthew G Knepley   Output Parameter:
40636636e97aSMatthew G Knepley . cdm - coordinate DM
40646636e97aSMatthew G Knepley 
40656636e97aSMatthew G Knepley   Level: intermediate
40666636e97aSMatthew G Knepley 
40676636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40681cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
40696636e97aSMatthew G Knepley @*/
40706636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
40716636e97aSMatthew G Knepley {
40726636e97aSMatthew G Knepley   PetscErrorCode ierr;
40736636e97aSMatthew G Knepley 
40746636e97aSMatthew G Knepley   PetscFunctionBegin;
40756636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
40766636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
40776636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
407882f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
40796636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
40806636e97aSMatthew G Knepley   }
40816636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
40826636e97aSMatthew G Knepley   PetscFunctionReturn(0);
40836636e97aSMatthew G Knepley }
4084e87bb0d3SMatthew G Knepley 
4085e87bb0d3SMatthew G Knepley #undef __FUNCT__
40861cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
40871cfe2091SMatthew G. Knepley /*@
40881cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
40891cfe2091SMatthew G. Knepley 
40901cfe2091SMatthew G. Knepley   Logically Collective on DM
40911cfe2091SMatthew G. Knepley 
40921cfe2091SMatthew G. Knepley   Input Parameters:
40931cfe2091SMatthew G. Knepley + dm - the DM
40941cfe2091SMatthew G. Knepley - cdm - coordinate DM
40951cfe2091SMatthew G. Knepley 
40961cfe2091SMatthew G. Knepley   Level: intermediate
40971cfe2091SMatthew G. Knepley 
40981cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
40991cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
41001cfe2091SMatthew G. Knepley @*/
41011cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
41021cfe2091SMatthew G. Knepley {
41031cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
41041cfe2091SMatthew G. Knepley 
41051cfe2091SMatthew G. Knepley   PetscFunctionBegin;
41061cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41071cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
41081cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
41091cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
41101cfe2091SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) dm->coordinateDM);CHKERRQ(ierr);
41111cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
41121cfe2091SMatthew G. Knepley }
41131cfe2091SMatthew G. Knepley 
41141cfe2091SMatthew G. Knepley #undef __FUNCT__
411546e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
411646e270d4SMatthew G. Knepley /*@
411746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
411846e270d4SMatthew G. Knepley 
411946e270d4SMatthew G. Knepley   Not Collective
412046e270d4SMatthew G. Knepley 
412146e270d4SMatthew G. Knepley   Input Parameter:
412246e270d4SMatthew G. Knepley . dm - The DM object
412346e270d4SMatthew G. Knepley 
412446e270d4SMatthew G. Knepley   Output Parameter:
412546e270d4SMatthew G. Knepley . dim - The embedding dimension
412646e270d4SMatthew G. Knepley 
412746e270d4SMatthew G. Knepley   Level: intermediate
412846e270d4SMatthew G. Knepley 
412946e270d4SMatthew G. Knepley .keywords: mesh, coordinates
413046e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
413146e270d4SMatthew G. Knepley @*/
413246e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
413346e270d4SMatthew G. Knepley {
413446e270d4SMatthew G. Knepley   PetscFunctionBegin;
413546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413646e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
41379a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
41389a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
41399a9a41abSToby Isaac   }
414046e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
414146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
414246e270d4SMatthew G. Knepley }
414346e270d4SMatthew G. Knepley 
414446e270d4SMatthew G. Knepley #undef __FUNCT__
414546e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
414646e270d4SMatthew G. Knepley /*@
414746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
414846e270d4SMatthew G. Knepley 
414946e270d4SMatthew G. Knepley   Not Collective
415046e270d4SMatthew G. Knepley 
415146e270d4SMatthew G. Knepley   Input Parameters:
415246e270d4SMatthew G. Knepley + dm  - The DM object
415346e270d4SMatthew G. Knepley - dim - The embedding dimension
415446e270d4SMatthew G. Knepley 
415546e270d4SMatthew G. Knepley   Level: intermediate
415646e270d4SMatthew G. Knepley 
415746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
415846e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
415946e270d4SMatthew G. Knepley @*/
416046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
416146e270d4SMatthew G. Knepley {
416246e270d4SMatthew G. Knepley   PetscFunctionBegin;
416346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
416446e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
416546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
416646e270d4SMatthew G. Knepley }
416746e270d4SMatthew G. Knepley 
416846e270d4SMatthew G. Knepley #undef __FUNCT__
4169e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4170e8abe2deSMatthew G. Knepley /*@
4171e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4172e8abe2deSMatthew G. Knepley 
4173e8abe2deSMatthew G. Knepley   Not Collective
4174e8abe2deSMatthew G. Knepley 
4175e8abe2deSMatthew G. Knepley   Input Parameter:
4176e8abe2deSMatthew G. Knepley . dm - The DM object
4177e8abe2deSMatthew G. Knepley 
4178e8abe2deSMatthew G. Knepley   Output Parameter:
4179e8abe2deSMatthew G. Knepley . section - The PetscSection object
4180e8abe2deSMatthew G. Knepley 
4181e8abe2deSMatthew G. Knepley   Level: intermediate
4182e8abe2deSMatthew G. Knepley 
4183e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4184e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4185e8abe2deSMatthew G. Knepley @*/
4186e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4187e8abe2deSMatthew G. Knepley {
4188e8abe2deSMatthew G. Knepley   DM             cdm;
4189e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4190e8abe2deSMatthew G. Knepley 
4191e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4192e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4193e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4194e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4195e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4196e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4197e8abe2deSMatthew G. Knepley }
4198e8abe2deSMatthew G. Knepley 
4199e8abe2deSMatthew G. Knepley #undef __FUNCT__
4200e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4201e8abe2deSMatthew G. Knepley /*@
4202e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4203e8abe2deSMatthew G. Knepley 
4204e8abe2deSMatthew G. Knepley   Not Collective
4205e8abe2deSMatthew G. Knepley 
4206e8abe2deSMatthew G. Knepley   Input Parameters:
4207e8abe2deSMatthew G. Knepley + dm      - The DM object
420846e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4209e8abe2deSMatthew G. Knepley - section - The PetscSection object
4210e8abe2deSMatthew G. Knepley 
4211e8abe2deSMatthew G. Knepley   Level: intermediate
4212e8abe2deSMatthew G. Knepley 
4213e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4214e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4215e8abe2deSMatthew G. Knepley @*/
421646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4217e8abe2deSMatthew G. Knepley {
4218e8abe2deSMatthew G. Knepley   DM             cdm;
4219e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4220e8abe2deSMatthew G. Knepley 
4221e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4222e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
422346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4224e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4225e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
422646e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
422746e270d4SMatthew G. Knepley     PetscInt d = dim;
422846e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
422946e270d4SMatthew G. Knepley 
423046e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
423146e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
423246e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
423346e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
423446e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
423546e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
423646e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
423746e270d4SMatthew G. Knepley     }
423846e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
423946e270d4SMatthew G. Knepley   }
4240e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4241e8abe2deSMatthew G. Knepley }
4242e8abe2deSMatthew G. Knepley 
4243e8abe2deSMatthew G. Knepley #undef __FUNCT__
4244c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
42455dc8c3f7SMatthew G. Knepley /*@C
42465dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42475dc8c3f7SMatthew G. Knepley 
42485dc8c3f7SMatthew G. Knepley   Input Parameters:
42495dc8c3f7SMatthew G. Knepley + dm      - The DM object
42505dc8c3f7SMatthew 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
42515dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42525dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42535dc8c3f7SMatthew G. Knepley 
42545dc8c3f7SMatthew G. Knepley   Level: developer
42555dc8c3f7SMatthew G. Knepley 
42565dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42575dc8c3f7SMatthew G. Knepley @*/
42585dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4259c6b900c6SMatthew G. Knepley {
4260c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4261c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4262c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4263c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
42645dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4265c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4266c6b900c6SMatthew G. Knepley }
4267c6b900c6SMatthew G. Knepley 
4268c6b900c6SMatthew G. Knepley #undef __FUNCT__
4269c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
42705dc8c3f7SMatthew G. Knepley /*@C
42715dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
42725dc8c3f7SMatthew G. Knepley 
42735dc8c3f7SMatthew G. Knepley   Input Parameters:
42745dc8c3f7SMatthew G. Knepley + dm      - The DM object
42755dc8c3f7SMatthew 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
42765dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
42775dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
42785dc8c3f7SMatthew G. Knepley 
42795dc8c3f7SMatthew G. Knepley   Level: developer
42805dc8c3f7SMatthew G. Knepley 
42815dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
42825dc8c3f7SMatthew G. Knepley @*/
42835dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4284c6b900c6SMatthew G. Knepley {
4285c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4286c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4287c6b900c6SMatthew G. Knepley 
4288c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4289c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42905dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
42915dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
42925dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
42935dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
42945dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4295c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4296c6b900c6SMatthew G. Knepley }
4297c6b900c6SMatthew G. Knepley 
4298c6b900c6SMatthew G. Knepley #undef __FUNCT__
4299e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4300e87bb0d3SMatthew G Knepley /*@
4301e87bb0d3SMatthew G Knepley   DMLocatePoints - Locate the points in v in the mesh and return an IS of the containing cells
4302e87bb0d3SMatthew G Knepley 
4303e87bb0d3SMatthew G Knepley   Not collective
4304e87bb0d3SMatthew G Knepley 
4305e87bb0d3SMatthew G Knepley   Input Parameters:
4306e87bb0d3SMatthew G Knepley + dm - The DM
4307e87bb0d3SMatthew G Knepley - v - The Vec of points
4308e87bb0d3SMatthew G Knepley 
430961e3bb9bSMatthew G Knepley   Output Parameter:
4310e87bb0d3SMatthew G Knepley . cells - The local cell numbers for cells which contain the points
4311e87bb0d3SMatthew G Knepley 
4312e87bb0d3SMatthew G Knepley   Level: developer
431361e3bb9bSMatthew G Knepley 
431461e3bb9bSMatthew G Knepley .keywords: point location, mesh
431561e3bb9bSMatthew G Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
431661e3bb9bSMatthew G Knepley @*/
4317e87bb0d3SMatthew G Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, IS *cells)
4318e87bb0d3SMatthew G Knepley {
4319735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4320735aa83eSMatthew G Knepley 
4321e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4322e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4323e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4324e87bb0d3SMatthew G Knepley   PetscValidPointer(cells,3);
432547a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4326735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
4327735aa83eSMatthew G Knepley     ierr = (*dm->ops->locatepoints)(dm,v,cells);CHKERRQ(ierr);
432882f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
432947a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4330e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4331e87bb0d3SMatthew G Knepley }
433214f150ffSMatthew G. Knepley 
433314f150ffSMatthew G. Knepley #undef __FUNCT__
433414f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4335f4d763aaSMatthew G. Knepley /*@
4336f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4337f4d763aaSMatthew G. Knepley 
4338f4d763aaSMatthew G. Knepley   Input Parameter:
4339f4d763aaSMatthew G. Knepley . dm - The original DM
4340f4d763aaSMatthew G. Knepley 
4341f4d763aaSMatthew G. Knepley   Output Parameter:
4342f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4343f4d763aaSMatthew G. Knepley 
4344f4d763aaSMatthew G. Knepley   Level: intermediate
4345f4d763aaSMatthew G. Knepley 
4346f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4347f4d763aaSMatthew G. Knepley @*/
434814f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
434914f150ffSMatthew G. Knepley {
4350c26acbdeSMatthew G. Knepley   PetscSection   section;
4351c26acbdeSMatthew G. Knepley   PetscBool      hasConstraints;
435214f150ffSMatthew G. Knepley   PetscErrorCode ierr;
435314f150ffSMatthew G. Knepley 
435414f150ffSMatthew G. Knepley   PetscFunctionBegin;
435514f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
435614f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4357c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4358c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4359c26acbdeSMatthew G. Knepley   if (!hasConstraints) {
4360c26acbdeSMatthew G. Knepley     *odm = dm;
4361c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4362c26acbdeSMatthew G. Knepley   }
436314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
4364c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
436514f150ffSMatthew G. Knepley     PetscSF      sf;
436614f150ffSMatthew G. Knepley 
436714f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
436814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
436914f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
437014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
437114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
437215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
437314f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
437414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
437514f150ffSMatthew G. Knepley   }
437614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
437714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
437814f150ffSMatthew G. Knepley }
4379f4d763aaSMatthew G. Knepley 
4380f4d763aaSMatthew G. Knepley #undef __FUNCT__
4381f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
4382f4d763aaSMatthew G. Knepley /*@
4383cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
4384f4d763aaSMatthew G. Knepley 
4385f4d763aaSMatthew G. Knepley   Input Parameter:
4386f4d763aaSMatthew G. Knepley . dm - The original DM
4387f4d763aaSMatthew G. Knepley 
4388cdb7a50dSMatthew G. Knepley   Output Parameters:
4389cdb7a50dSMatthew G. Knepley + num - The output sequence number
4390cdb7a50dSMatthew G. Knepley - val - The output sequence value
4391f4d763aaSMatthew G. Knepley 
4392f4d763aaSMatthew G. Knepley   Level: intermediate
4393f4d763aaSMatthew G. Knepley 
4394f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4395f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4396f4d763aaSMatthew G. Knepley 
4397f4d763aaSMatthew G. Knepley .seealso: VecView()
4398f4d763aaSMatthew G. Knepley @*/
4399cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
4400f4d763aaSMatthew G. Knepley {
4401f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4402f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4403cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
4404cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
4405f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4406f4d763aaSMatthew G. Knepley }
4407f4d763aaSMatthew G. Knepley 
4408f4d763aaSMatthew G. Knepley #undef __FUNCT__
4409f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
4410f4d763aaSMatthew G. Knepley /*@
4411cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
4412f4d763aaSMatthew G. Knepley 
4413f4d763aaSMatthew G. Knepley   Input Parameters:
4414f4d763aaSMatthew G. Knepley + dm - The original DM
4415cdb7a50dSMatthew G. Knepley . num - The output sequence number
4416cdb7a50dSMatthew G. Knepley - val - The output sequence value
4417f4d763aaSMatthew G. Knepley 
4418f4d763aaSMatthew G. Knepley   Level: intermediate
4419f4d763aaSMatthew G. Knepley 
4420f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4421f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4422f4d763aaSMatthew G. Knepley 
4423f4d763aaSMatthew G. Knepley .seealso: VecView()
4424f4d763aaSMatthew G. Knepley @*/
4425cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
4426f4d763aaSMatthew G. Knepley {
4427f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
4428f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4429f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
4430cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
4431cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
4432cdb7a50dSMatthew G. Knepley }
4433cdb7a50dSMatthew G. Knepley 
4434cdb7a50dSMatthew G. Knepley #undef __FUNCT__
4435cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
4436cdb7a50dSMatthew G. Knepley /*@C
4437cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
4438cdb7a50dSMatthew G. Knepley 
4439cdb7a50dSMatthew G. Knepley   Input Parameters:
4440cdb7a50dSMatthew G. Knepley + dm   - The original DM
4441cdb7a50dSMatthew G. Knepley . name - The sequence name
4442cdb7a50dSMatthew G. Knepley - num  - The output sequence number
4443cdb7a50dSMatthew G. Knepley 
4444cdb7a50dSMatthew G. Knepley   Output Parameter:
4445cdb7a50dSMatthew G. Knepley . val  - The output sequence value
4446cdb7a50dSMatthew G. Knepley 
4447cdb7a50dSMatthew G. Knepley   Level: intermediate
4448cdb7a50dSMatthew G. Knepley 
4449cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
4450cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
4451cdb7a50dSMatthew G. Knepley 
4452cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
4453cdb7a50dSMatthew G. Knepley @*/
4454cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
4455cdb7a50dSMatthew G. Knepley {
4456cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
4457cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
4458cdb7a50dSMatthew G. Knepley 
4459cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
4460cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4461cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
4462cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
4463cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
4464cdb7a50dSMatthew G. Knepley   if (ishdf5) {
4465cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
4466cdb7a50dSMatthew G. Knepley     PetscScalar value;
4467cdb7a50dSMatthew G. Knepley 
4468cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
44694aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
4470cdb7a50dSMatthew G. Knepley #endif
4471cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
4472f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
4473f4d763aaSMatthew G. Knepley }
44748e4ac7eaSMatthew G. Knepley 
44758e4ac7eaSMatthew G. Knepley #undef __FUNCT__
44768e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
44778e4ac7eaSMatthew G. Knepley /*@
44788e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
44798e4ac7eaSMatthew G. Knepley 
44808e4ac7eaSMatthew G. Knepley   Not collective
44818e4ac7eaSMatthew G. Knepley 
44828e4ac7eaSMatthew G. Knepley   Input Parameter:
44838e4ac7eaSMatthew G. Knepley . dm - The DM
44848e4ac7eaSMatthew G. Knepley 
44858e4ac7eaSMatthew G. Knepley   Output Parameter:
44868e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
44878e4ac7eaSMatthew G. Knepley 
44888e4ac7eaSMatthew G. Knepley   Level: beginner
44898e4ac7eaSMatthew G. Knepley 
44908e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
44918e4ac7eaSMatthew G. Knepley @*/
44928e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
44938e4ac7eaSMatthew G. Knepley {
44948e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
44958e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
44968e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
44978e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
44988e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
44998e4ac7eaSMatthew G. Knepley }
45008e4ac7eaSMatthew G. Knepley 
45018e4ac7eaSMatthew G. Knepley #undef __FUNCT__
45028e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
45038e4ac7eaSMatthew G. Knepley /*@
45048e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
45058e4ac7eaSMatthew G. Knepley 
45068e4ac7eaSMatthew G. Knepley   Collective on dm
45078e4ac7eaSMatthew G. Knepley 
45088e4ac7eaSMatthew G. Knepley   Input Parameters:
45098e4ac7eaSMatthew G. Knepley + dm - The DM
45108e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
45118e4ac7eaSMatthew G. Knepley 
45128e4ac7eaSMatthew G. Knepley   Level: beginner
45138e4ac7eaSMatthew G. Knepley 
45148e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
45158e4ac7eaSMatthew G. Knepley @*/
45168e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
45178e4ac7eaSMatthew G. Knepley {
45188e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
45198e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
45208e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
45218e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
45228e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
45238e4ac7eaSMatthew G. Knepley }
4524