xref: /petsc/src/dm/interface/dm.c (revision 7294143f724bbda737018fa45f3d267ffee35a9d)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
50c312b8eSJed Brown #include <petscsf.h>
62764a2aaSMatthew G. Knepley #include <petscds.h>
747c6ae99SBarry Smith 
8732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
91ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1067a56275SMatthew G Knepley 
11bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
12bff4a2f0SMatthew G. Knepley 
1347c6ae99SBarry Smith #undef __FUNCT__
14a4121054SBarry Smith #define __FUNCT__ "DMCreate"
15a4121054SBarry Smith /*@
16de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
17a4121054SBarry Smith 
18a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
19a4121054SBarry Smith    error when you try to use the vector.
20a4121054SBarry Smith 
21a4121054SBarry Smith   Collective on MPI_Comm
22a4121054SBarry Smith 
23a4121054SBarry Smith   Input Parameter:
24a4121054SBarry Smith . comm - The communicator for the DM object
25a4121054SBarry Smith 
26a4121054SBarry Smith   Output Parameter:
27a4121054SBarry Smith . dm - The DM object
28a4121054SBarry Smith 
29a4121054SBarry Smith   Level: beginner
30a4121054SBarry Smith 
318472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
32a4121054SBarry Smith @*/
337087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
34a4121054SBarry Smith {
35a4121054SBarry Smith   DM             v;
36a4121054SBarry Smith   PetscErrorCode ierr;
37a4121054SBarry Smith 
38a4121054SBarry Smith   PetscFunctionBegin;
391411c6eeSJed Brown   PetscValidPointer(dm,2);
400298fd71SBarry Smith   *dm = NULL;
418b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
42607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
43607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
44607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
45a4121054SBarry Smith 
4673107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
47e7c4fc90SDmitry Karpeev 
480298fd71SBarry Smith   v->ltogmap                  = NULL;
491411c6eeSJed Brown   v->bs                       = 1;
50171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5188ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
53c58f1c22SToby Isaac   v->labels                   = NULL;
54c58f1c22SToby Isaac   v->depthLabel               = NULL;
550298fd71SBarry Smith   v->defaultSection           = NULL;
560298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
57fba222abSToby Isaac   v->defaultConstraintSection = NULL;
58fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
59c6b900c6SMatthew G. Knepley   v->L                        = NULL;
60c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
615dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
629a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
63435a35e8SMatthew G Knepley   {
64435a35e8SMatthew G Knepley     PetscInt i;
65435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
660298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
67435a35e8SMatthew G Knepley     }
68435a35e8SMatthew G Knepley   }
692764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
7014f150ffSMatthew G. Knepley   v->dmBC = NULL;
71a8fb8f29SToby Isaac   v->coarseMesh = NULL;
72f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
73cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
74c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
75b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
76bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
77c58f1c22SToby Isaac   v->labels->refct = 1;
781411c6eeSJed Brown   *dm = v;
79a4121054SBarry Smith   PetscFunctionReturn(0);
80a4121054SBarry Smith }
81a4121054SBarry Smith 
82a4121054SBarry Smith #undef __FUNCT__
8338221697SMatthew G. Knepley #define __FUNCT__ "DMClone"
8438221697SMatthew G. Knepley /*@
8538221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
8638221697SMatthew G. Knepley 
8738221697SMatthew G. Knepley   Collective on MPI_Comm
8838221697SMatthew G. Knepley 
8938221697SMatthew G. Knepley   Input Parameter:
9038221697SMatthew G. Knepley . dm - The original DM object
9138221697SMatthew G. Knepley 
9238221697SMatthew G. Knepley   Output Parameter:
9338221697SMatthew G. Knepley . newdm  - The new DM object
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley   Level: beginner
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley .keywords: DM, topology, create
9838221697SMatthew G. Knepley @*/
9938221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
10038221697SMatthew G. Knepley {
10138221697SMatthew G. Knepley   PetscSF        sf;
10238221697SMatthew G. Knepley   Vec            coords;
10338221697SMatthew G. Knepley   void          *ctx;
1041de53e9aSMatthew G. Knepley   PetscInt       dim;
10538221697SMatthew G. Knepley   PetscErrorCode ierr;
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley   PetscFunctionBegin;
10838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
10938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
11038221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
111c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
112c58f1c22SToby Isaac   dm->labels->refct++;
113c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
114c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1151de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1161de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
11738221697SMatthew G. Knepley   if (dm->ops->clone) {
11838221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
11938221697SMatthew G. Knepley   }
1203f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
12138221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
12238221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
12338221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
12438221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
125be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
126be4c1c3eSMatthew G. Knepley     DM           ncdm;
127be4c1c3eSMatthew G. Knepley     PetscSection cs;
1285a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
129be4c1c3eSMatthew G. Knepley 
130be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
131be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1325a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1335a0206caSToby Isaac     if (pEndMax >= 0) {
134be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
13513bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
136a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
137be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
138be4c1c3eSMatthew G. Knepley     }
139be4c1c3eSMatthew G. Knepley   }
14038221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   if (coords) {
14238221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
14338221697SMatthew G. Knepley   } else {
14438221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
14538221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
14638221697SMatthew G. Knepley   }
147c6b900c6SMatthew G. Knepley   if (dm->maxCell) {
148c6b900c6SMatthew G. Knepley     const PetscReal *maxCell, *L;
1495dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
1505dc8c3f7SMatthew G. Knepley     ierr = DMGetPeriodicity(dm,     &maxCell, &L, &bd);CHKERRQ(ierr);
1515dc8c3f7SMatthew G. Knepley     ierr = DMSetPeriodicity(*newdm,  maxCell,  L,  bd);CHKERRQ(ierr);
152c6b900c6SMatthew G. Knepley   }
15338221697SMatthew G. Knepley   PetscFunctionReturn(0);
15438221697SMatthew G. Knepley }
15538221697SMatthew G. Knepley 
15638221697SMatthew G. Knepley #undef __FUNCT__
1579a42bb27SBarry Smith #define __FUNCT__ "DMSetVecType"
1589a42bb27SBarry Smith /*@C
159564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1609a42bb27SBarry Smith 
1618472ad0fSDave May    Logically Collective on DM
1629a42bb27SBarry Smith 
1639a42bb27SBarry Smith    Input Parameter:
1649a42bb27SBarry Smith +  da - initial distributed array
1658154be41SBarry Smith .  ctype - the vector type, currently either VECSTANDARD or VECCUSP
1669a42bb27SBarry Smith 
1679a42bb27SBarry Smith    Options Database:
168dd85299cSBarry Smith .   -dm_vec_type ctype
1699a42bb27SBarry Smith 
1709a42bb27SBarry Smith    Level: intermediate
1719a42bb27SBarry Smith 
1728472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1739a42bb27SBarry Smith @*/
17419fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1759a42bb27SBarry Smith {
1769a42bb27SBarry Smith   PetscErrorCode ierr;
1779a42bb27SBarry Smith 
1789a42bb27SBarry Smith   PetscFunctionBegin;
1799a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1809a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
18119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1829a42bb27SBarry Smith   PetscFunctionReturn(0);
1839a42bb27SBarry Smith }
1849a42bb27SBarry Smith 
1859a42bb27SBarry Smith #undef __FUNCT__
186c0dedaeaSBarry Smith #define __FUNCT__ "DMGetVecType"
187c0dedaeaSBarry Smith /*@C
188c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
189c0dedaeaSBarry Smith 
1908472ad0fSDave May    Logically Collective on DM
191c0dedaeaSBarry Smith 
192c0dedaeaSBarry Smith    Input Parameter:
193c0dedaeaSBarry Smith .  da - initial distributed array
194c0dedaeaSBarry Smith 
195c0dedaeaSBarry Smith    Output Parameter:
196c0dedaeaSBarry Smith .  ctype - the vector type
197c0dedaeaSBarry Smith 
198c0dedaeaSBarry Smith    Level: intermediate
199c0dedaeaSBarry Smith 
2008472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
201c0dedaeaSBarry Smith @*/
202c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
203c0dedaeaSBarry Smith {
204c0dedaeaSBarry Smith   PetscFunctionBegin;
205c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
206c0dedaeaSBarry Smith   *ctype = da->vectype;
207c0dedaeaSBarry Smith   PetscFunctionReturn(0);
208c0dedaeaSBarry Smith }
209c0dedaeaSBarry Smith 
210c0dedaeaSBarry Smith #undef __FUNCT__
2115f1ad066SMatthew G Knepley #define __FUNCT__ "VecGetDM"
2125f1ad066SMatthew G Knepley /*@
21334f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2145f1ad066SMatthew G Knepley 
2155f1ad066SMatthew G Knepley   Not collective
2165f1ad066SMatthew G Knepley 
2175f1ad066SMatthew G Knepley   Input Parameter:
2185f1ad066SMatthew G Knepley . v - The Vec
2195f1ad066SMatthew G Knepley 
2205f1ad066SMatthew G Knepley   Output Parameter:
2215f1ad066SMatthew G Knepley . dm - The DM
2225f1ad066SMatthew G Knepley 
2235f1ad066SMatthew G Knepley   Level: intermediate
2245f1ad066SMatthew G Knepley 
2255f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2265f1ad066SMatthew G Knepley @*/
2275f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2285f1ad066SMatthew G Knepley {
2295f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley   PetscFunctionBegin;
2325f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2335f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2345f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2355f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2365f1ad066SMatthew G Knepley }
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley #undef __FUNCT__
2395f1ad066SMatthew G Knepley #define __FUNCT__ "VecSetDM"
2405f1ad066SMatthew G Knepley /*@
241d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Not collective
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley   Input Parameters:
2465f1ad066SMatthew G Knepley + v - The Vec
2475f1ad066SMatthew G Knepley - dm - The DM
2485f1ad066SMatthew G Knepley 
249d9805387SMatthew 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.
250d9805387SMatthew G. Knepley 
2515f1ad066SMatthew G Knepley   Level: intermediate
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2545f1ad066SMatthew G Knepley @*/
2555f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2565f1ad066SMatthew G Knepley {
2575f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley   PetscFunctionBegin;
2605f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
261d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2625f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2635f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2645f1ad066SMatthew G Knepley }
2655f1ad066SMatthew G Knepley 
2665f1ad066SMatthew G Knepley #undef __FUNCT__
267521d9a4cSLisandro Dalcin #define __FUNCT__ "DMSetMatType"
268521d9a4cSLisandro Dalcin /*@C
269521d9a4cSLisandro Dalcin        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
270521d9a4cSLisandro Dalcin 
271521d9a4cSLisandro Dalcin    Logically Collective on DM
272521d9a4cSLisandro Dalcin 
273521d9a4cSLisandro Dalcin    Input Parameter:
274521d9a4cSLisandro Dalcin +  dm - the DM context
275a2b5a043SBarry Smith -  ctype - the matrix type
276521d9a4cSLisandro Dalcin 
277521d9a4cSLisandro Dalcin    Options Database:
278521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
279521d9a4cSLisandro Dalcin 
280521d9a4cSLisandro Dalcin    Level: intermediate
281521d9a4cSLisandro Dalcin 
282c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
283521d9a4cSLisandro Dalcin @*/
28419fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
285521d9a4cSLisandro Dalcin {
286521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
28788f0584fSBarry Smith 
288521d9a4cSLisandro Dalcin   PetscFunctionBegin;
289521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
290521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
29119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
292521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
293521d9a4cSLisandro Dalcin }
294521d9a4cSLisandro Dalcin 
295521d9a4cSLisandro Dalcin #undef __FUNCT__
296c0dedaeaSBarry Smith #define __FUNCT__ "DMGetMatType"
297c0dedaeaSBarry Smith /*@C
298c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
299c0dedaeaSBarry Smith 
300c0dedaeaSBarry Smith    Logically Collective on DM
301c0dedaeaSBarry Smith 
302c0dedaeaSBarry Smith    Input Parameter:
303c0dedaeaSBarry Smith .  dm - the DM context
304c0dedaeaSBarry Smith 
305c0dedaeaSBarry Smith    Output Parameter:
306c0dedaeaSBarry Smith .  ctype - the matrix type
307c0dedaeaSBarry Smith 
308c0dedaeaSBarry Smith    Options Database:
309c0dedaeaSBarry Smith .   -dm_mat_type ctype
310c0dedaeaSBarry Smith 
311c0dedaeaSBarry Smith    Level: intermediate
312c0dedaeaSBarry Smith 
313c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
314c0dedaeaSBarry Smith @*/
315c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
316c0dedaeaSBarry Smith {
317c0dedaeaSBarry Smith   PetscFunctionBegin;
318c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
319c0dedaeaSBarry Smith   *ctype = dm->mattype;
320c0dedaeaSBarry Smith   PetscFunctionReturn(0);
321c0dedaeaSBarry Smith }
322c0dedaeaSBarry Smith 
323c0dedaeaSBarry Smith #undef __FUNCT__
324c688c046SMatthew G Knepley #define __FUNCT__ "MatGetDM"
325c688c046SMatthew G Knepley /*@
32634f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
327c688c046SMatthew G Knepley 
328c688c046SMatthew G Knepley   Not collective
329c688c046SMatthew G Knepley 
330c688c046SMatthew G Knepley   Input Parameter:
331c688c046SMatthew G Knepley . A - The Mat
332c688c046SMatthew G Knepley 
333c688c046SMatthew G Knepley   Output Parameter:
334c688c046SMatthew G Knepley . dm - The DM
335c688c046SMatthew G Knepley 
336c688c046SMatthew G Knepley   Level: intermediate
337c688c046SMatthew G Knepley 
338c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
339c688c046SMatthew G Knepley @*/
340c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
341c688c046SMatthew G Knepley {
342c688c046SMatthew G Knepley   PetscErrorCode ierr;
343c688c046SMatthew G Knepley 
344c688c046SMatthew G Knepley   PetscFunctionBegin;
345c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
346c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
347c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
348c688c046SMatthew G Knepley   PetscFunctionReturn(0);
349c688c046SMatthew G Knepley }
350c688c046SMatthew G Knepley 
351c688c046SMatthew G Knepley #undef __FUNCT__
352c688c046SMatthew G Knepley #define __FUNCT__ "MatSetDM"
353c688c046SMatthew G Knepley /*@
354c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
355c688c046SMatthew G Knepley 
356c688c046SMatthew G Knepley   Not collective
357c688c046SMatthew G Knepley 
358c688c046SMatthew G Knepley   Input Parameters:
359c688c046SMatthew G Knepley + A - The Mat
360c688c046SMatthew G Knepley - dm - The DM
361c688c046SMatthew G Knepley 
362c688c046SMatthew G Knepley   Level: intermediate
363c688c046SMatthew G Knepley 
364c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
365c688c046SMatthew G Knepley @*/
366c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
367c688c046SMatthew G Knepley {
368c688c046SMatthew G Knepley   PetscErrorCode ierr;
369c688c046SMatthew G Knepley 
370c688c046SMatthew G Knepley   PetscFunctionBegin;
371c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
3728865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
373c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
374c688c046SMatthew G Knepley   PetscFunctionReturn(0);
375c688c046SMatthew G Knepley }
376c688c046SMatthew G Knepley 
377c688c046SMatthew G Knepley #undef __FUNCT__
3789a42bb27SBarry Smith #define __FUNCT__ "DMSetOptionsPrefix"
3799a42bb27SBarry Smith /*@C
3809a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
3816757b960SDave May    DM options in the database.
3829a42bb27SBarry Smith 
3838353ddbbSDave May    Logically Collective on DM
3849a42bb27SBarry Smith 
3859a42bb27SBarry Smith    Input Parameter:
3868353ddbbSDave May +  da - the DM context
3879a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
3889a42bb27SBarry Smith 
3899a42bb27SBarry Smith    Notes:
3909a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
3919a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
3929a42bb27SBarry Smith 
3939a42bb27SBarry Smith    Level: advanced
3949a42bb27SBarry Smith 
3958353ddbbSDave May .keywords: DM, set, options, prefix, database
3969a42bb27SBarry Smith 
3979a42bb27SBarry Smith .seealso: DMSetFromOptions()
3989a42bb27SBarry Smith @*/
3997087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4009a42bb27SBarry Smith {
4019a42bb27SBarry Smith   PetscErrorCode ierr;
4029a42bb27SBarry Smith 
4039a42bb27SBarry Smith   PetscFunctionBegin;
4049a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4059a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
406691be533SLawrence Mitchell   if (dm->sf) {
407691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
408691be533SLawrence Mitchell   }
409691be533SLawrence Mitchell   if (dm->defaultSF) {
410691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
411691be533SLawrence Mitchell   }
4129a42bb27SBarry Smith   PetscFunctionReturn(0);
4139a42bb27SBarry Smith }
4149a42bb27SBarry Smith 
4159a42bb27SBarry Smith #undef __FUNCT__
41631697293SDave May #define __FUNCT__ "DMAppendOptionsPrefix"
41731697293SDave May /*@C
41831697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
41931697293SDave May    DM options in the database.
42031697293SDave May 
42131697293SDave May    Logically Collective on DM
42231697293SDave May 
42331697293SDave May    Input Parameters:
42431697293SDave May +  dm - the DM context
42531697293SDave May -  prefix - the prefix string to prepend to all DM option requests
42631697293SDave May 
42731697293SDave May    Notes:
42831697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
42931697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
43031697293SDave May 
43131697293SDave May    Level: advanced
43231697293SDave May 
43331697293SDave May .keywords: DM, append, options, prefix, database
43431697293SDave May 
43531697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
43631697293SDave May @*/
43731697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
43831697293SDave May {
43931697293SDave May   PetscErrorCode ierr;
44031697293SDave May 
44131697293SDave May   PetscFunctionBegin;
44231697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44331697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
44431697293SDave May   PetscFunctionReturn(0);
44531697293SDave May }
44631697293SDave May 
44731697293SDave May #undef __FUNCT__
44831697293SDave May #define __FUNCT__ "DMGetOptionsPrefix"
44931697293SDave May /*@C
45031697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
45131697293SDave May    DM options in the database.
45231697293SDave May 
45331697293SDave May    Not Collective
45431697293SDave May 
45531697293SDave May    Input Parameters:
45631697293SDave May .  dm - the DM context
45731697293SDave May 
45831697293SDave May    Output Parameters:
45931697293SDave May .  prefix - pointer to the prefix string used is returned
46031697293SDave May 
46131697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
46231697293SDave May    sufficient length to hold the prefix.
46331697293SDave May 
46431697293SDave May    Level: advanced
46531697293SDave May 
46631697293SDave May .keywords: DM, set, options, prefix, database
46731697293SDave May 
46831697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
46931697293SDave May @*/
47031697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
47131697293SDave May {
47231697293SDave May   PetscErrorCode ierr;
47331697293SDave May 
47431697293SDave May   PetscFunctionBegin;
47531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47631697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
47731697293SDave May   PetscFunctionReturn(0);
47831697293SDave May }
47931697293SDave May 
48031697293SDave May #undef __FUNCT__
48188bdff64SToby Isaac #define __FUNCT__ "DMCountNonCyclicReferences"
48288bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
48388bdff64SToby Isaac {
48488bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
48588bdff64SToby Isaac   DMNamedVecLink nlink;
48688bdff64SToby Isaac   PetscErrorCode ierr;
48788bdff64SToby Isaac 
48888bdff64SToby Isaac   PetscFunctionBegin;
48988bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
49088bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
49188bdff64SToby Isaac     if (dm->localin[i])  refct--;
49288bdff64SToby Isaac     if (dm->globalin[i]) refct--;
49388bdff64SToby Isaac   }
49488bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
49588bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
49688bdff64SToby Isaac   if (dm->x) {
49788bdff64SToby Isaac     DM obj;
49888bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
49988bdff64SToby Isaac     if (obj == dm) refct--;
50088bdff64SToby Isaac   }
50188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
50288bdff64SToby Isaac     refct--;
50388bdff64SToby Isaac     if (recurseCoarse) {
50488bdff64SToby Isaac       PetscInt coarseCount;
50588bdff64SToby Isaac 
50688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
50788bdff64SToby Isaac       refct += coarseCount;
50888bdff64SToby Isaac     }
50988bdff64SToby Isaac   }
51088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
51188bdff64SToby Isaac     refct--;
51288bdff64SToby Isaac     if (recurseFine) {
51388bdff64SToby Isaac       PetscInt fineCount;
51488bdff64SToby Isaac 
51588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
51688bdff64SToby Isaac       refct += fineCount;
51788bdff64SToby Isaac     }
51888bdff64SToby Isaac   }
51988bdff64SToby Isaac   *ncrefct = refct;
52088bdff64SToby Isaac   PetscFunctionReturn(0);
52188bdff64SToby Isaac }
52288bdff64SToby Isaac 
52388bdff64SToby Isaac #undef __FUNCT__
524354557abSToby Isaac #define __FUNCT__ "DMDestroyLabelLinkList"
525354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
526354557abSToby Isaac {
527354557abSToby Isaac   PetscErrorCode ierr;
528354557abSToby Isaac 
529354557abSToby Isaac   PetscFunctionBegin;
530354557abSToby Isaac   if (!--(dm->labels->refct)) {
531354557abSToby Isaac     DMLabelLink next = dm->labels->next;
532354557abSToby Isaac 
533354557abSToby Isaac     /* destroy the labels */
534354557abSToby Isaac     while (next) {
535354557abSToby Isaac       DMLabelLink tmp = next->next;
536354557abSToby Isaac 
537354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
538354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
539354557abSToby Isaac       next = tmp;
540354557abSToby Isaac     }
541354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
542354557abSToby Isaac   }
543354557abSToby Isaac   PetscFunctionReturn(0);
544354557abSToby Isaac }
545354557abSToby Isaac 
546354557abSToby Isaac #undef __FUNCT__
54747c6ae99SBarry Smith #define __FUNCT__ "DMDestroy"
54847c6ae99SBarry Smith /*@
5498472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
55047c6ae99SBarry Smith 
55147c6ae99SBarry Smith     Collective on DM
55247c6ae99SBarry Smith 
55347c6ae99SBarry Smith     Input Parameter:
55447c6ae99SBarry Smith .   dm - the DM object to destroy
55547c6ae99SBarry Smith 
55647c6ae99SBarry Smith     Level: developer
55747c6ae99SBarry Smith 
558e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
55947c6ae99SBarry Smith 
56047c6ae99SBarry Smith @*/
561fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
56247c6ae99SBarry Smith {
56388bdff64SToby Isaac   PetscInt       i, cnt;
564dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
56547c6ae99SBarry Smith   PetscErrorCode ierr;
56647c6ae99SBarry Smith 
56747c6ae99SBarry Smith   PetscFunctionBegin;
5686bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
5696bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
57087e657c6SBarry Smith 
57188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
57288bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
57388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
57488bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
575732e2eb9SMatthew G Knepley   /*
576732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
577732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
578732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
579732e2eb9SMatthew G Knepley   */
5806bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
5816bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
582732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
5836bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
5846bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
585732e2eb9SMatthew G Knepley   }
586f490541aSPeter Brune   nnext=(*dm)->namedglobal;
5870298fd71SBarry Smith   (*dm)->namedglobal = NULL;
588f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
5892348bcf4SPeter Brune     nnext = nlink->next;
5902348bcf4SPeter 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);
5912348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
5922348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
5932348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
5942348bcf4SPeter Brune   }
595f490541aSPeter Brune   nnext=(*dm)->namedlocal;
5960298fd71SBarry Smith   (*dm)->namedlocal = NULL;
597f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
598f490541aSPeter Brune     nnext = nlink->next;
599f490541aSPeter 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);
600f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
601f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
602f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
603f490541aSPeter Brune   }
6042348bcf4SPeter Brune 
605b17ce1afSJed Brown   /* Destroy the list of hooks */
606c833c3b5SJed Brown   {
607c833c3b5SJed Brown     DMCoarsenHookLink link,next;
608b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
609b17ce1afSJed Brown       next = link->next;
610b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
611b17ce1afSJed Brown     }
6120298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
613c833c3b5SJed Brown   }
614c833c3b5SJed Brown   {
615c833c3b5SJed Brown     DMRefineHookLink link,next;
616c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
617c833c3b5SJed Brown       next = link->next;
618c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
619c833c3b5SJed Brown     }
6200298fd71SBarry Smith     (*dm)->refinehook = NULL;
621c833c3b5SJed Brown   }
622be081cd6SPeter Brune   {
623be081cd6SPeter Brune     DMSubDomainHookLink link,next;
624be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
625be081cd6SPeter Brune       next = link->next;
626be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
627be081cd6SPeter Brune     }
6280298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
629be081cd6SPeter Brune   }
630baf369e7SPeter Brune   {
631baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
632baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
633baf369e7SPeter Brune       next = link->next;
634baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
635baf369e7SPeter Brune     }
6360298fd71SBarry Smith     (*dm)->gtolhook = NULL;
637baf369e7SPeter Brune   }
638d4d07f1eSToby Isaac   {
639d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
640d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
641d4d07f1eSToby Isaac       next = link->next;
642d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
643d4d07f1eSToby Isaac     }
644d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
645d4d07f1eSToby Isaac   }
646aa1993deSMatthew G Knepley   /* Destroy the work arrays */
647aa1993deSMatthew G Knepley   {
648aa1993deSMatthew G Knepley     DMWorkLink link,next;
649aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
650aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
651aa1993deSMatthew G Knepley       next = link->next;
652aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
653aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
654aa1993deSMatthew G Knepley     }
6550298fd71SBarry Smith     (*dm)->workin = NULL;
656aa1993deSMatthew G Knepley   }
657c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
658c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
659c58f1c22SToby Isaac 
660c58f1c22SToby Isaac     /* destroy the labels */
661c58f1c22SToby Isaac     while (next) {
662c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
663c58f1c22SToby Isaac 
664c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
665c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
666c58f1c22SToby Isaac       next = tmp;
667c58f1c22SToby Isaac     }
668c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
669c58f1c22SToby Isaac   }
670e6f8dbb6SToby Isaac   {
671e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
672e6f8dbb6SToby Isaac     while (next) {
673e6f8dbb6SToby Isaac       DMBoundary b = next;
674e6f8dbb6SToby Isaac 
675e6f8dbb6SToby Isaac       next = b->next;
676e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
677e6f8dbb6SToby Isaac     }
678e6f8dbb6SToby Isaac   }
679b17ce1afSJed Brown 
68052536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
68152536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
68252536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
68352536dc3SBarry Smith 
6841a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
6851a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
6861a266240SBarry Smith   }
68787e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
68871cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
6894dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6906bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
6916bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
692073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
69388ed4aceSMatthew G Knepley 
69488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
69588ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
6968b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
697fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
698fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
69988ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
70088ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
7018e4ac7eaSMatthew G. Knepley   ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
702af122d2aSMatthew G Knepley 
70388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
70488bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
70588bdff64SToby Isaac   }
706a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
70788bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
70888bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
70988bdff64SToby Isaac   }
71088bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
7116636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7126636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7136636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7145dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7156636e97aSMatthew G Knepley 
7162764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
71714f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
718e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
719e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
720732e2eb9SMatthew G Knepley 
7216bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
722435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
723732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
72447c6ae99SBarry Smith   PetscFunctionReturn(0);
72547c6ae99SBarry Smith }
72647c6ae99SBarry Smith 
72747c6ae99SBarry Smith #undef __FUNCT__
728d7bf68aeSBarry Smith #define __FUNCT__ "DMSetUp"
729d7bf68aeSBarry Smith /*@
730d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
731d7bf68aeSBarry Smith 
732d7bf68aeSBarry Smith     Collective on DM
733d7bf68aeSBarry Smith 
734d7bf68aeSBarry Smith     Input Parameter:
735d7bf68aeSBarry Smith .   dm - the DM object to setup
736d7bf68aeSBarry Smith 
737d7bf68aeSBarry Smith     Level: developer
738d7bf68aeSBarry Smith 
739e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
740d7bf68aeSBarry Smith 
741d7bf68aeSBarry Smith @*/
7427087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
743d7bf68aeSBarry Smith {
744d7bf68aeSBarry Smith   PetscErrorCode ierr;
745d7bf68aeSBarry Smith 
746d7bf68aeSBarry Smith   PetscFunctionBegin;
747171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7488387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
749d7bf68aeSBarry Smith   if (dm->ops->setup) {
750d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
751d7bf68aeSBarry Smith   }
7528387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
753d7bf68aeSBarry Smith   PetscFunctionReturn(0);
754d7bf68aeSBarry Smith }
755d7bf68aeSBarry Smith 
756d7bf68aeSBarry Smith #undef __FUNCT__
757d7bf68aeSBarry Smith #define __FUNCT__ "DMSetFromOptions"
758d7bf68aeSBarry Smith /*@
759d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
760d7bf68aeSBarry Smith 
761d7bf68aeSBarry Smith     Collective on DM
762d7bf68aeSBarry Smith 
763d7bf68aeSBarry Smith     Input Parameter:
764d7bf68aeSBarry Smith .   dm - the DM object to set options for
765d7bf68aeSBarry Smith 
766732e2eb9SMatthew G Knepley     Options Database:
7674164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
7684164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
7694164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
7704164ae61SDominic Meiser -   -dm_coloring_type    - <global or ghosted>
771732e2eb9SMatthew G Knepley 
772d7bf68aeSBarry Smith     Level: developer
773d7bf68aeSBarry Smith 
774e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
775d7bf68aeSBarry Smith 
776d7bf68aeSBarry Smith @*/
7777087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
778d7bf68aeSBarry Smith {
7797781c08eSBarry Smith   char           typeName[256];
780ca266f36SBarry Smith   PetscBool      flg;
781d7bf68aeSBarry Smith   PetscErrorCode ierr;
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith   PetscFunctionBegin;
784171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
785f1fd5e65SToby Isaac   if (dm->prob) {
786f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
787f1fd5e65SToby Isaac   }
788691be533SLawrence Mitchell   if (dm->sf) {
789691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
790691be533SLawrence Mitchell   }
791691be533SLawrence Mitchell   if (dm->defaultSF) {
792691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
793691be533SLawrence Mitchell   }
7943194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
7950298fd71SBarry 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);
796a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
797f9ba7244SBarry Smith   if (flg) {
798f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
799f9ba7244SBarry Smith   }
800a264d7a6SBarry 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);
801073dac72SJed Brown   if (flg) {
802521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
803073dac72SJed Brown   }
8040298fd71SBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","ISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
805f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
806e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
807f9ba7244SBarry Smith   }
808f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8090633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
81082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
811d7bf68aeSBarry Smith   PetscFunctionReturn(0);
812d7bf68aeSBarry Smith }
813d7bf68aeSBarry Smith 
814d7bf68aeSBarry Smith #undef __FUNCT__
81547c6ae99SBarry Smith #define __FUNCT__ "DMView"
816fc9bc008SSatish Balay /*@C
817224748a4SBarry Smith     DMView - Views a DM
81847c6ae99SBarry Smith 
81947c6ae99SBarry Smith     Collective on DM
82047c6ae99SBarry Smith 
82147c6ae99SBarry Smith     Input Parameter:
82247c6ae99SBarry Smith +   dm - the DM object to view
82347c6ae99SBarry Smith -   v - the viewer
82447c6ae99SBarry Smith 
825224748a4SBarry Smith     Level: beginner
82647c6ae99SBarry Smith 
827e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
82847c6ae99SBarry Smith 
82947c6ae99SBarry Smith @*/
8307087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
83147c6ae99SBarry Smith {
83247c6ae99SBarry Smith   PetscErrorCode ierr;
83332c0f0efSBarry Smith   PetscBool      isbinary;
83447c6ae99SBarry Smith 
83547c6ae99SBarry Smith   PetscFunctionBegin;
836171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8373014e516SBarry Smith   if (!v) {
838ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8393014e516SBarry Smith   }
84098c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
84132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
84232c0f0efSBarry Smith   if (isbinary) {
84355849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
84432c0f0efSBarry Smith     char     type[256];
84532c0f0efSBarry Smith 
84632c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
84732c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
84832c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
84932c0f0efSBarry Smith   }
8500c010503SBarry Smith   if (dm->ops->view) {
8510c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
85247c6ae99SBarry Smith   }
85347c6ae99SBarry Smith   PetscFunctionReturn(0);
85447c6ae99SBarry Smith }
85547c6ae99SBarry Smith 
85647c6ae99SBarry Smith #undef __FUNCT__
85747c6ae99SBarry Smith #define __FUNCT__ "DMCreateGlobalVector"
85847c6ae99SBarry Smith /*@
8598472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith     Collective on DM
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith     Input Parameter:
86447c6ae99SBarry Smith .   dm - the DM object
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith     Output Parameter:
86747c6ae99SBarry Smith .   vec - the global vector
86847c6ae99SBarry Smith 
869073dac72SJed Brown     Level: beginner
87047c6ae99SBarry Smith 
871e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87247c6ae99SBarry Smith 
87347c6ae99SBarry Smith @*/
8747087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
87547c6ae99SBarry Smith {
87647c6ae99SBarry Smith   PetscErrorCode ierr;
87747c6ae99SBarry Smith 
87847c6ae99SBarry Smith   PetscFunctionBegin;
879171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
88047c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
88147c6ae99SBarry Smith   PetscFunctionReturn(0);
88247c6ae99SBarry Smith }
88347c6ae99SBarry Smith 
88447c6ae99SBarry Smith #undef __FUNCT__
88547c6ae99SBarry Smith #define __FUNCT__ "DMCreateLocalVector"
88647c6ae99SBarry Smith /*@
8878472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith     Not Collective
89047c6ae99SBarry Smith 
89147c6ae99SBarry Smith     Input Parameter:
89247c6ae99SBarry Smith .   dm - the DM object
89347c6ae99SBarry Smith 
89447c6ae99SBarry Smith     Output Parameter:
89547c6ae99SBarry Smith .   vec - the local vector
89647c6ae99SBarry Smith 
897073dac72SJed Brown     Level: beginner
89847c6ae99SBarry Smith 
899e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
90047c6ae99SBarry Smith 
90147c6ae99SBarry Smith @*/
9027087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
90347c6ae99SBarry Smith {
90447c6ae99SBarry Smith   PetscErrorCode ierr;
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith   PetscFunctionBegin;
907171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
90847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
90947c6ae99SBarry Smith   PetscFunctionReturn(0);
91047c6ae99SBarry Smith }
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith #undef __FUNCT__
9131411c6eeSJed Brown #define __FUNCT__ "DMGetLocalToGlobalMapping"
9141411c6eeSJed Brown /*@
9151411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9161411c6eeSJed Brown 
9171411c6eeSJed Brown    Collective on DM
9181411c6eeSJed Brown 
9191411c6eeSJed Brown    Input Parameter:
9201411c6eeSJed Brown .  dm - the DM that provides the mapping
9211411c6eeSJed Brown 
9221411c6eeSJed Brown    Output Parameter:
9231411c6eeSJed Brown .  ltog - the mapping
9241411c6eeSJed Brown 
9251411c6eeSJed Brown    Level: intermediate
9261411c6eeSJed Brown 
9271411c6eeSJed Brown    Notes:
9281411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9291411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9301411c6eeSJed Brown 
931fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9321411c6eeSJed Brown @*/
9337087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9341411c6eeSJed Brown {
935bff27382SMatthew G. Knepley   PetscInt       bs = -1, bsLocal, bsMin, bsMax;
9361411c6eeSJed Brown   PetscErrorCode ierr;
9371411c6eeSJed Brown 
9381411c6eeSJed Brown   PetscFunctionBegin;
9391411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9401411c6eeSJed Brown   PetscValidPointer(ltog,2);
9411411c6eeSJed Brown   if (!dm->ltogmap) {
94237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
94337d0c07bSMatthew G Knepley 
94437d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
94537d0c07bSMatthew G Knepley     if (section) {
946a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
94737d0c07bSMatthew G Knepley       PetscInt       *ltog;
94837d0c07bSMatthew G Knepley       PetscInt        pStart, pEnd, size, p, l;
94937d0c07bSMatthew G Knepley 
95037d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
95137d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
95237d0c07bSMatthew G Knepley       ierr = PetscSectionGetStorageSize(section, &size);CHKERRQ(ierr);
953785e854fSJed Brown       ierr = PetscMalloc1(size, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
95437d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
955a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
95637d0c07bSMatthew G Knepley 
95737d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
95837d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
959a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
960a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
96137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
9621a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
9631a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
9641a7dc684SMatthew G. Knepley         if (dof) {
965b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
966b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
9671a7dc684SMatthew G. Knepley         }
96837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
969a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
970a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
97137d0c07bSMatthew G Knepley         }
97237d0c07bSMatthew G Knepley       }
973bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
974bff27382SMatthew G. Knepley       bsLocal = bs;
975bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMax, 1, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
976bff27382SMatthew G. Knepley       bsLocal = bs < 0 ? bsMax : bs;
977bff27382SMatthew G. Knepley       ierr = MPIU_Allreduce(&bsLocal, &bsMin, 1, MPIU_INT, MPI_MIN, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
978bff27382SMatthew G. Knepley       if (bsMin != bsMax) {bs = 1;}
979bff27382SMatthew G. Knepley       else                {bs = bsMax;}
9807591dbb2SMatthew G. Knepley       bs   = bs < 0 ? 1 : bs;
9817591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
9827591dbb2SMatthew G. Knepley       if (bs > 1) for (l = 0; l < size; ++l) ltog[l] /= bs;
9837591dbb2SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, size, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
9843bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
98537d0c07bSMatthew G Knepley     } else {
986184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
987184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
9881411c6eeSJed Brown     }
98937d0c07bSMatthew G Knepley   }
9901411c6eeSJed Brown   *ltog = dm->ltogmap;
9911411c6eeSJed Brown   PetscFunctionReturn(0);
9921411c6eeSJed Brown }
9931411c6eeSJed Brown 
9941411c6eeSJed Brown #undef __FUNCT__
9951411c6eeSJed Brown #define __FUNCT__ "DMGetBlockSize"
9961411c6eeSJed Brown /*@
9971411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
9981411c6eeSJed Brown 
9991411c6eeSJed Brown    Not Collective
10001411c6eeSJed Brown 
10011411c6eeSJed Brown    Input Parameter:
10021411c6eeSJed Brown .  dm - the DM with block structure
10031411c6eeSJed Brown 
10041411c6eeSJed Brown    Output Parameter:
10051411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10061411c6eeSJed Brown 
10071411c6eeSJed Brown    Level: intermediate
10081411c6eeSJed Brown 
1009fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10101411c6eeSJed Brown @*/
10117087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10121411c6eeSJed Brown {
10131411c6eeSJed Brown   PetscFunctionBegin;
10141411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10151411c6eeSJed Brown   PetscValidPointer(bs,2);
10161411c6eeSJed 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");
10171411c6eeSJed Brown   *bs = dm->bs;
10181411c6eeSJed Brown   PetscFunctionReturn(0);
10191411c6eeSJed Brown }
10201411c6eeSJed Brown 
10211411c6eeSJed Brown #undef __FUNCT__
1022e727c939SJed Brown #define __FUNCT__ "DMCreateInterpolation"
102347c6ae99SBarry Smith /*@
10248472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
102547c6ae99SBarry Smith 
102647c6ae99SBarry Smith     Collective on DM
102747c6ae99SBarry Smith 
102847c6ae99SBarry Smith     Input Parameter:
102947c6ae99SBarry Smith +   dm1 - the DM object
103047c6ae99SBarry Smith -   dm2 - the second, finer DM object
103147c6ae99SBarry Smith 
103247c6ae99SBarry Smith     Output Parameter:
103347c6ae99SBarry Smith +  mat - the interpolation
103447c6ae99SBarry Smith -  vec - the scaling (optional)
103547c6ae99SBarry Smith 
103647c6ae99SBarry Smith     Level: developer
103747c6ae99SBarry Smith 
103885afcc9aSBarry 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
103985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1040d52bd9f3SBarry Smith 
10411f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1042d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
104385afcc9aSBarry Smith 
104485afcc9aSBarry Smith 
10453ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
104647c6ae99SBarry Smith 
104747c6ae99SBarry Smith @*/
1048e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
104947c6ae99SBarry Smith {
105047c6ae99SBarry Smith   PetscErrorCode ierr;
105147c6ae99SBarry Smith 
105247c6ae99SBarry Smith   PetscFunctionBegin;
1053171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1054171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1055cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
105625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1057cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
105847c6ae99SBarry Smith   PetscFunctionReturn(0);
105947c6ae99SBarry Smith }
106047c6ae99SBarry Smith 
106147c6ae99SBarry Smith #undef __FUNCT__
10623ad4599aSBarry Smith #define __FUNCT__ "DMCreateRestriction"
10633ad4599aSBarry Smith /*@
10643ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
10653ad4599aSBarry Smith 
10663ad4599aSBarry Smith     Collective on DM
10673ad4599aSBarry Smith 
10683ad4599aSBarry Smith     Input Parameter:
10693ad4599aSBarry Smith +   dm1 - the DM object
10703ad4599aSBarry Smith -   dm2 - the second, finer DM object
10713ad4599aSBarry Smith 
10723ad4599aSBarry Smith     Output Parameter:
10733ad4599aSBarry Smith .  mat - the restriction
10743ad4599aSBarry Smith 
10753ad4599aSBarry Smith 
10763ad4599aSBarry Smith     Level: developer
10773ad4599aSBarry Smith 
10783ad4599aSBarry 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
10793ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
10803ad4599aSBarry Smith 
10813ad4599aSBarry Smith 
10823ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
10833ad4599aSBarry Smith 
10843ad4599aSBarry Smith @*/
10853ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
10863ad4599aSBarry Smith {
10873ad4599aSBarry Smith   PetscErrorCode ierr;
10883ad4599aSBarry Smith 
10893ad4599aSBarry Smith   PetscFunctionBegin;
10903ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
10913ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
10923ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
1093*7294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
10943ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
10953ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
10963ad4599aSBarry Smith   PetscFunctionReturn(0);
10973ad4599aSBarry Smith }
10983ad4599aSBarry Smith 
10993ad4599aSBarry Smith #undef __FUNCT__
1100e727c939SJed Brown #define __FUNCT__ "DMCreateInjection"
110147c6ae99SBarry Smith /*@
11028472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
110347c6ae99SBarry Smith 
110447c6ae99SBarry Smith     Collective on DM
110547c6ae99SBarry Smith 
110647c6ae99SBarry Smith     Input Parameter:
110747c6ae99SBarry Smith +   dm1 - the DM object
110847c6ae99SBarry Smith -   dm2 - the second, finer DM object
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith     Output Parameter:
11116dbf9973SLawrence Mitchell .   mat - the injection
111247c6ae99SBarry Smith 
111347c6ae99SBarry Smith     Level: developer
111447c6ae99SBarry Smith 
111585afcc9aSBarry 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
111685afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
111785afcc9aSBarry Smith 
1118e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
111947c6ae99SBarry Smith 
112047c6ae99SBarry Smith @*/
11216dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
112247c6ae99SBarry Smith {
112347c6ae99SBarry Smith   PetscErrorCode ierr;
112447c6ae99SBarry Smith 
112547c6ae99SBarry Smith   PetscFunctionBegin;
1126171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1127171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11280c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11296dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
113047c6ae99SBarry Smith   PetscFunctionReturn(0);
113147c6ae99SBarry Smith }
113247c6ae99SBarry Smith 
113347c6ae99SBarry Smith #undef __FUNCT__
1134e727c939SJed Brown #define __FUNCT__ "DMCreateColoring"
1135b412c318SBarry Smith /*@
1136b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
113747c6ae99SBarry Smith 
113847c6ae99SBarry Smith     Collective on DM
113947c6ae99SBarry Smith 
114047c6ae99SBarry Smith     Input Parameter:
114147c6ae99SBarry Smith +   dm - the DM object
11425bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
114347c6ae99SBarry Smith 
114447c6ae99SBarry Smith     Output Parameter:
114547c6ae99SBarry Smith .   coloring - the coloring
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith     Level: developer
114847c6ae99SBarry Smith 
1149b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
115047c6ae99SBarry Smith 
1151aab9d709SJed Brown @*/
1152b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
115347c6ae99SBarry Smith {
115447c6ae99SBarry Smith   PetscErrorCode ierr;
115547c6ae99SBarry Smith 
115647c6ae99SBarry Smith   PetscFunctionBegin;
1157171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1158ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1159b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
116047c6ae99SBarry Smith   PetscFunctionReturn(0);
116147c6ae99SBarry Smith }
116247c6ae99SBarry Smith 
116347c6ae99SBarry Smith #undef __FUNCT__
1164950540a4SJed Brown #define __FUNCT__ "DMCreateMatrix"
1165b412c318SBarry Smith /*@
11668472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith     Collective on DM
116947c6ae99SBarry Smith 
117047c6ae99SBarry Smith     Input Parameter:
1171b412c318SBarry Smith .   dm - the DM object
117247c6ae99SBarry Smith 
117347c6ae99SBarry Smith     Output Parameter:
117447c6ae99SBarry Smith .   mat - the empty Jacobian
117547c6ae99SBarry Smith 
1176073dac72SJed Brown     Level: beginner
117747c6ae99SBarry Smith 
117894013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
117994013140SBarry Smith        do not need to do it yourself.
118094013140SBarry Smith 
118194013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
11827889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
118394013140SBarry Smith 
118494013140SBarry 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
118594013140SBarry Smith        internally by PETSc.
118694013140SBarry Smith 
118794013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1188aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
118994013140SBarry Smith 
1190b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
119147c6ae99SBarry Smith 
1192aab9d709SJed Brown @*/
1193b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
119447c6ae99SBarry Smith {
119547c6ae99SBarry Smith   PetscErrorCode ierr;
119647c6ae99SBarry Smith 
119747c6ae99SBarry Smith   PetscFunctionBegin;
1198171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1199607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1200c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1201c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1202b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
120347c6ae99SBarry Smith   PetscFunctionReturn(0);
120447c6ae99SBarry Smith }
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith #undef __FUNCT__
1207732e2eb9SMatthew G Knepley #define __FUNCT__ "DMSetMatrixPreallocateOnly"
1208732e2eb9SMatthew G Knepley /*@
1209950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1210732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1211732e2eb9SMatthew G Knepley 
12128472ad0fSDave May   Logically Collective on DM
1213732e2eb9SMatthew G Knepley 
1214732e2eb9SMatthew G Knepley   Input Parameter:
1215732e2eb9SMatthew G Knepley + dm - the DM
1216732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1217732e2eb9SMatthew G Knepley 
1218732e2eb9SMatthew G Knepley   Level: developer
1219950540a4SJed Brown .seealso DMCreateMatrix()
1220732e2eb9SMatthew G Knepley @*/
1221732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1222732e2eb9SMatthew G Knepley {
1223732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1224732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1225732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1226732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1227732e2eb9SMatthew G Knepley }
1228732e2eb9SMatthew G Knepley 
1229732e2eb9SMatthew G Knepley #undef __FUNCT__
1230a89ea682SMatthew G Knepley #define __FUNCT__ "DMGetWorkArray"
1231a89ea682SMatthew G Knepley /*@C
1232aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1233a89ea682SMatthew G Knepley 
1234a89ea682SMatthew G Knepley   Not Collective
1235a89ea682SMatthew G Knepley 
1236a89ea682SMatthew G Knepley   Input Parameters:
1237a89ea682SMatthew G Knepley + dm - the DM object
1238aa1993deSMatthew G Knepley . count - The minium size
1239aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1240a89ea682SMatthew G Knepley 
1241a89ea682SMatthew G Knepley   Output Parameter:
1242a89ea682SMatthew G Knepley . array - the work array
1243a89ea682SMatthew G Knepley 
1244a89ea682SMatthew G Knepley   Level: developer
1245a89ea682SMatthew G Knepley 
1246a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1247a89ea682SMatthew G Knepley @*/
1248aa1993deSMatthew G Knepley PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1249a89ea682SMatthew G Knepley {
1250a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1251aa1993deSMatthew G Knepley   DMWorkLink     link;
1252854ce69bSBarry Smith   size_t         dsize;
1253a89ea682SMatthew G Knepley 
1254a89ea682SMatthew G Knepley   PetscFunctionBegin;
1255a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1256aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1257aa1993deSMatthew G Knepley   if (dm->workin) {
1258aa1993deSMatthew G Knepley     link       = dm->workin;
1259aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1260aa1993deSMatthew G Knepley   } else {
1261b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1262a89ea682SMatthew G Knepley   }
1263854ce69bSBarry Smith   ierr = PetscDataTypeGetSize(dtype,&dsize);CHKERRQ(ierr);
1264854ce69bSBarry Smith   if (dsize*count > link->bytes) {
1265aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1266854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1267854ce69bSBarry Smith     link->bytes = dsize*count;
1268aa1993deSMatthew G Knepley   }
1269aa1993deSMatthew G Knepley   link->next   = dm->workout;
1270aa1993deSMatthew G Knepley   dm->workout  = link;
1271aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1272a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1273a89ea682SMatthew G Knepley }
1274a89ea682SMatthew G Knepley 
1275aa1993deSMatthew G Knepley #undef __FUNCT__
1276aa1993deSMatthew G Knepley #define __FUNCT__ "DMRestoreWorkArray"
1277aa1993deSMatthew G Knepley /*@C
1278aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1279aa1993deSMatthew G Knepley 
1280aa1993deSMatthew G Knepley   Not Collective
1281aa1993deSMatthew G Knepley 
1282aa1993deSMatthew G Knepley   Input Parameters:
1283aa1993deSMatthew G Knepley + dm - the DM object
1284aa1993deSMatthew G Knepley . count - The minium size
1285aa1993deSMatthew G Knepley - dtype - data type (PETSC_REAL, PETSC_SCALAR, PETSC_INT)
1286aa1993deSMatthew G Knepley 
1287aa1993deSMatthew G Knepley   Output Parameter:
1288aa1993deSMatthew G Knepley . array - the work array
1289aa1993deSMatthew G Knepley 
1290aa1993deSMatthew G Knepley   Level: developer
1291aa1993deSMatthew G Knepley 
1292aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1293aa1993deSMatthew G Knepley @*/
1294aa1993deSMatthew G Knepley PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,PetscDataType dtype,void *mem)
1295aa1993deSMatthew G Knepley {
1296aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1297aa1993deSMatthew G Knepley 
1298aa1993deSMatthew G Knepley   PetscFunctionBegin;
1299aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1300aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1301aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1302aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1303aa1993deSMatthew G Knepley       *p           = link->next;
1304aa1993deSMatthew G Knepley       link->next   = dm->workin;
1305aa1993deSMatthew G Knepley       dm->workin   = link;
13060298fd71SBarry Smith       *(void**)mem = NULL;
1307aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1308aa1993deSMatthew G Knepley     }
1309aa1993deSMatthew G Knepley   }
1310aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1311aa1993deSMatthew G Knepley }
1312e7c4fc90SDmitry Karpeev 
1313e7c4fc90SDmitry Karpeev #undef __FUNCT__
1314435a35e8SMatthew G Knepley #define __FUNCT__ "DMSetNullSpaceConstructor"
1315435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1316435a35e8SMatthew G Knepley {
1317435a35e8SMatthew G Knepley   PetscFunctionBegin;
1318435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
131982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1320435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1321435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1322435a35e8SMatthew G Knepley }
1323435a35e8SMatthew G Knepley 
1324435a35e8SMatthew G Knepley #undef __FUNCT__
13254d343eeaSMatthew G Knepley #define __FUNCT__ "DMCreateFieldIS"
13264f3b5142SJed Brown /*@C
13274d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
13284d343eeaSMatthew G Knepley 
13294d343eeaSMatthew G Knepley   Not collective
13304d343eeaSMatthew G Knepley 
13314d343eeaSMatthew G Knepley   Input Parameter:
13324d343eeaSMatthew G Knepley . dm - the DM object
13334d343eeaSMatthew G Knepley 
13344d343eeaSMatthew G Knepley   Output Parameters:
13350298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
13360298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
13370298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
13384d343eeaSMatthew G Knepley 
13394d343eeaSMatthew G Knepley   Level: intermediate
13404d343eeaSMatthew G Knepley 
134121c9b008SJed Brown   Notes:
134221c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
134321c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
134421c9b008SJed Brown   PetscFree().
134521c9b008SJed Brown 
13464d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
13474d343eeaSMatthew G Knepley @*/
134837d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
13494d343eeaSMatthew G Knepley {
135037d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
13514d343eeaSMatthew G Knepley   PetscErrorCode ierr;
13524d343eeaSMatthew G Knepley 
13534d343eeaSMatthew G Knepley   PetscFunctionBegin;
13544d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
135569ca1f37SDmitry Karpeev   if (numFields) {
135669ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
135769ca1f37SDmitry Karpeev     *numFields = 0;
135869ca1f37SDmitry Karpeev   }
135937d0c07bSMatthew G Knepley   if (fieldNames) {
136037d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
13610298fd71SBarry Smith     *fieldNames = NULL;
136269ca1f37SDmitry Karpeev   }
136369ca1f37SDmitry Karpeev   if (fields) {
136469ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
13650298fd71SBarry Smith     *fields = NULL;
136669ca1f37SDmitry Karpeev   }
136737d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
136837d0c07bSMatthew G Knepley   if (section) {
136937d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
137037d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
137137d0c07bSMatthew G Knepley 
137237d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
137337d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1374dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
137537d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
137637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
137737d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
137837d0c07bSMatthew G Knepley     }
137937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
138037d0c07bSMatthew G Knepley       PetscInt gdof;
138137d0c07bSMatthew G Knepley 
138237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
138337d0c07bSMatthew G Knepley       if (gdof > 0) {
138437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
138537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
138637d0c07bSMatthew G Knepley 
138737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
138837d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
138937d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
139037d0c07bSMatthew G Knepley         }
139137d0c07bSMatthew G Knepley       }
139237d0c07bSMatthew G Knepley     }
139337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1394785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
139537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
139637d0c07bSMatthew G Knepley     }
139737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
139837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
139937d0c07bSMatthew G Knepley 
140037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
140137d0c07bSMatthew G Knepley       if (gdof > 0) {
140237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
140337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
140437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
140537d0c07bSMatthew G Knepley 
140637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
140737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
140837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
140937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
141037d0c07bSMatthew G Knepley           }
141137d0c07bSMatthew G Knepley         }
141237d0c07bSMatthew G Knepley       }
141337d0c07bSMatthew G Knepley     }
14148865f1eaSKarl Rupp     if (numFields) *numFields = nF;
141537d0c07bSMatthew G Knepley     if (fieldNames) {
1416785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
141737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
141837d0c07bSMatthew G Knepley         const char *fieldName;
141937d0c07bSMatthew G Knepley 
142037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
142137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
142237d0c07bSMatthew G Knepley       }
142337d0c07bSMatthew G Knepley     }
142437d0c07bSMatthew G Knepley     if (fields) {
1425785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
142637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
142782f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
142837d0c07bSMatthew G Knepley       }
142937d0c07bSMatthew G Knepley     }
143037d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
14318865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
14328865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
143369ca1f37SDmitry Karpeev   }
14344d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
14354d343eeaSMatthew G Knepley }
14364d343eeaSMatthew G Knepley 
143716621825SDmitry Karpeev 
143816621825SDmitry Karpeev #undef __FUNCT__
143916621825SDmitry Karpeev #define __FUNCT__ "DMCreateFieldDecomposition"
144016621825SDmitry Karpeev /*@C
144116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
144216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
144316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1444e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1445e7c4fc90SDmitry Karpeev 
1446e7c4fc90SDmitry Karpeev   Not collective
1447e7c4fc90SDmitry Karpeev 
1448e7c4fc90SDmitry Karpeev   Input Parameter:
1449e7c4fc90SDmitry Karpeev . dm - the DM object
1450e7c4fc90SDmitry Karpeev 
1451e7c4fc90SDmitry Karpeev   Output Parameters:
14520298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
14530298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
14540298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
14550298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1456e7c4fc90SDmitry Karpeev 
1457e7c4fc90SDmitry Karpeev   Level: intermediate
1458e7c4fc90SDmitry Karpeev 
1459e7c4fc90SDmitry Karpeev   Notes:
1460e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1461e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1462e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1463e7c4fc90SDmitry Karpeev 
1464e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1465e7c4fc90SDmitry Karpeev @*/
146616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1467e7c4fc90SDmitry Karpeev {
1468e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1469e7c4fc90SDmitry Karpeev 
1470e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1471e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
14728865f1eaSKarl Rupp   if (len) {
14738865f1eaSKarl Rupp     PetscValidPointer(len,2);
14748865f1eaSKarl Rupp     *len = 0;
14758865f1eaSKarl Rupp   }
14768865f1eaSKarl Rupp   if (namelist) {
14778865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
14788865f1eaSKarl Rupp     *namelist = 0;
14798865f1eaSKarl Rupp   }
14808865f1eaSKarl Rupp   if (islist) {
14818865f1eaSKarl Rupp     PetscValidPointer(islist,4);
14828865f1eaSKarl Rupp     *islist = 0;
14838865f1eaSKarl Rupp   }
14848865f1eaSKarl Rupp   if (dmlist) {
14858865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
14868865f1eaSKarl Rupp     *dmlist = 0;
14878865f1eaSKarl Rupp   }
1488f3f0edfdSDmitry Karpeev   /*
1489f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1490f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1491f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1492f3f0edfdSDmitry Karpeev    */
1493ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
149416621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1495435a35e8SMatthew G Knepley     PetscSection section;
1496435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1497435a35e8SMatthew G Knepley 
1498435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1499435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1500435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1501f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
150203dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
150303dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
150403dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1505435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1506435a35e8SMatthew G Knepley         const char *fieldName;
1507435a35e8SMatthew G Knepley 
150803dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
150903dc3394SMatthew G. Knepley         if (namelist) {
1510435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1511435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1512435a35e8SMatthew G Knepley         }
151303dc3394SMatthew G. Knepley       }
1514435a35e8SMatthew G Knepley     } else {
151569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1516e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15170298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1518e7c4fc90SDmitry Karpeev     }
15198865f1eaSKarl Rupp   } else {
152016621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
152116621825SDmitry Karpeev   }
152216621825SDmitry Karpeev   PetscFunctionReturn(0);
152316621825SDmitry Karpeev }
152416621825SDmitry Karpeev 
152516621825SDmitry Karpeev #undef __FUNCT__
1526435a35e8SMatthew G Knepley #define __FUNCT__ "DMCreateSubDM"
1527564cec59SMatthew G. Knepley /*@
1528435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1529435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1530435a35e8SMatthew G Knepley 
1531435a35e8SMatthew G Knepley   Not collective
1532435a35e8SMatthew G Knepley 
1533435a35e8SMatthew G Knepley   Input Parameters:
1534435a35e8SMatthew G Knepley + dm - the DM object
1535435a35e8SMatthew G Knepley . numFields - number of fields in this subproblem
15360298fd71SBarry Smith - len       - The number of subproblems in the decomposition (or NULL if not requested)
1537435a35e8SMatthew G Knepley 
1538435a35e8SMatthew G Knepley   Output Parameters:
1539435a35e8SMatthew G Knepley . is - The global indices for the subproblem
1540435a35e8SMatthew G Knepley - dm - The DM for the subproblem
1541435a35e8SMatthew G Knepley 
1542435a35e8SMatthew G Knepley   Level: intermediate
1543435a35e8SMatthew G Knepley 
1544435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1545435a35e8SMatthew G Knepley @*/
1546435a35e8SMatthew G Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, PetscInt fields[], IS *is, DM *subdm)
1547435a35e8SMatthew G Knepley {
1548435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1549435a35e8SMatthew G Knepley 
1550435a35e8SMatthew G Knepley   PetscFunctionBegin;
1551435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1552435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
15538865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
15548865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1555435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1556435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
155782f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1558435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1559435a35e8SMatthew G Knepley }
1560435a35e8SMatthew G Knepley 
156116621825SDmitry Karpeev 
156216621825SDmitry Karpeev #undef __FUNCT__
156316621825SDmitry Karpeev #define __FUNCT__ "DMCreateDomainDecomposition"
156416621825SDmitry Karpeev /*@C
15658d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
15668d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
15678d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
15688d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
15698d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
157016621825SDmitry Karpeev 
157116621825SDmitry Karpeev   Not collective
157216621825SDmitry Karpeev 
157316621825SDmitry Karpeev   Input Parameter:
157416621825SDmitry Karpeev . dm - the DM object
157516621825SDmitry Karpeev 
157616621825SDmitry Karpeev   Output Parameters:
15770298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
15780298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
15790298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
15800298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
15810298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
158216621825SDmitry Karpeev 
158316621825SDmitry Karpeev   Level: intermediate
158416621825SDmitry Karpeev 
158516621825SDmitry Karpeev   Notes:
158616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
158716621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
158816621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
158916621825SDmitry Karpeev 
15908d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
159116621825SDmitry Karpeev @*/
15928d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
159316621825SDmitry Karpeev {
159416621825SDmitry Karpeev   PetscErrorCode      ierr;
1595be081cd6SPeter Brune   DMSubDomainHookLink link;
1596be081cd6SPeter Brune   PetscInt            i,l;
159716621825SDmitry Karpeev 
159816621825SDmitry Karpeev   PetscFunctionBegin;
159916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
160014a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
16010298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
16020298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
16030298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
16040298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1605f3f0edfdSDmitry Karpeev   /*
1606f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1607f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1608f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1609f3f0edfdSDmitry Karpeev    */
1610ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
161116621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1612be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
161314a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1614f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1615be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1616be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1617be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1618be081cd6SPeter Brune         }
1619648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1620e7c4fc90SDmitry Karpeev       }
162114a18fd3SPeter Brune     }
162214a18fd3SPeter Brune     if (len) *len = l;
162314a18fd3SPeter Brune   }
1624e30e807fSPeter Brune   PetscFunctionReturn(0);
1625e30e807fSPeter Brune }
1626e30e807fSPeter Brune 
1627e30e807fSPeter Brune 
1628e30e807fSPeter Brune #undef __FUNCT__
1629e30e807fSPeter Brune #define __FUNCT__ "DMCreateDomainDecompositionScatters"
1630e30e807fSPeter Brune /*@C
1631e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1632e30e807fSPeter Brune 
1633e30e807fSPeter Brune   Not collective
1634e30e807fSPeter Brune 
1635e30e807fSPeter Brune   Input Parameters:
1636e30e807fSPeter Brune + dm - the DM object
1637e30e807fSPeter Brune . n  - the number of subdomain scatters
1638e30e807fSPeter Brune - subdms - the local subdomains
1639e30e807fSPeter Brune 
1640e30e807fSPeter Brune   Output Parameters:
1641e30e807fSPeter Brune + n     - the number of scatters returned
1642e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1643e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1644e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1645e30e807fSPeter Brune 
1646e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1647e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1648e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1649e30e807fSPeter Brune   solution and residual data.
1650e30e807fSPeter Brune 
1651e30e807fSPeter Brune   Level: developer
1652e30e807fSPeter Brune 
1653e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1654e30e807fSPeter Brune @*/
1655e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1656e30e807fSPeter Brune {
1657e30e807fSPeter Brune   PetscErrorCode ierr;
1658e30e807fSPeter Brune 
1659e30e807fSPeter Brune   PetscFunctionBegin;
1660e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1661e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1662e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1663e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
16646668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1665e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1666e7c4fc90SDmitry Karpeev }
1667e7c4fc90SDmitry Karpeev 
1668731c8d9eSDmitry Karpeev #undef __FUNCT__
166947c6ae99SBarry Smith #define __FUNCT__ "DMRefine"
167047c6ae99SBarry Smith /*@
167147c6ae99SBarry Smith   DMRefine - Refines a DM object
167247c6ae99SBarry Smith 
167347c6ae99SBarry Smith   Collective on DM
167447c6ae99SBarry Smith 
167547c6ae99SBarry Smith   Input Parameter:
167647c6ae99SBarry Smith + dm   - the DM object
167791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
167847c6ae99SBarry Smith 
167947c6ae99SBarry Smith   Output Parameter:
16800298fd71SBarry Smith . dmf - the refined DM, or NULL
1681ae0a1c52SMatthew G Knepley 
16820298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
168347c6ae99SBarry Smith 
168447c6ae99SBarry Smith   Level: developer
168547c6ae99SBarry Smith 
1686e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
168747c6ae99SBarry Smith @*/
16887087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
168947c6ae99SBarry Smith {
169047c6ae99SBarry Smith   PetscErrorCode   ierr;
1691c833c3b5SJed Brown   DMRefineHookLink link;
169247c6ae99SBarry Smith 
169347c6ae99SBarry Smith   PetscFunctionBegin;
1694732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16951ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1696fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
169747c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
16984057135bSMatthew G Knepley   if (*dmf) {
169943842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
17008865f1eaSKarl Rupp 
17018cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
17028865f1eaSKarl Rupp 
1703644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
17040598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1705656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
17068865f1eaSKarl Rupp 
1707e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1708c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
17098865f1eaSKarl Rupp       if (link->refinehook) {
17108865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
17118865f1eaSKarl Rupp       }
1712c833c3b5SJed Brown     }
1713c833c3b5SJed Brown   }
17141ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1715c833c3b5SJed Brown   PetscFunctionReturn(0);
1716c833c3b5SJed Brown }
1717c833c3b5SJed Brown 
1718c833c3b5SJed Brown #undef __FUNCT__
1719c833c3b5SJed Brown #define __FUNCT__ "DMRefineHookAdd"
1720bb9467b5SJed Brown /*@C
1721c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1722c833c3b5SJed Brown 
1723c833c3b5SJed Brown    Logically Collective
1724c833c3b5SJed Brown 
1725c833c3b5SJed Brown    Input Arguments:
1726c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1727c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1728c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
17290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1730c833c3b5SJed Brown 
1731c833c3b5SJed Brown    Calling sequence of refinehook:
1732c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1733c833c3b5SJed Brown 
1734c833c3b5SJed Brown +  coarse - coarse level DM
1735c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1736c833c3b5SJed Brown -  ctx - optional user-defined function context
1737c833c3b5SJed Brown 
1738c833c3b5SJed Brown    Calling sequence for interphook:
1739c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1740c833c3b5SJed Brown 
1741c833c3b5SJed Brown +  coarse - coarse level DM
1742c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1743c833c3b5SJed Brown .  fine - fine level DM to update
1744c833c3b5SJed Brown -  ctx - optional user-defined function context
1745c833c3b5SJed Brown 
1746c833c3b5SJed Brown    Level: advanced
1747c833c3b5SJed Brown 
1748c833c3b5SJed Brown    Notes:
1749c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1750c833c3b5SJed Brown 
1751c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1752c833c3b5SJed Brown 
1753bb9467b5SJed Brown    This function is currently not available from Fortran.
1754bb9467b5SJed Brown 
1755c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1756c833c3b5SJed Brown @*/
1757c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1758c833c3b5SJed Brown {
1759c833c3b5SJed Brown   PetscErrorCode   ierr;
1760c833c3b5SJed Brown   DMRefineHookLink link,*p;
1761c833c3b5SJed Brown 
1762c833c3b5SJed Brown   PetscFunctionBegin;
1763c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
1764c833c3b5SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
176595dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1766c833c3b5SJed Brown   link->refinehook = refinehook;
1767c833c3b5SJed Brown   link->interphook = interphook;
1768c833c3b5SJed Brown   link->ctx        = ctx;
17690298fd71SBarry Smith   link->next       = NULL;
1770c833c3b5SJed Brown   *p               = link;
1771c833c3b5SJed Brown   PetscFunctionReturn(0);
1772c833c3b5SJed Brown }
1773c833c3b5SJed Brown 
1774c833c3b5SJed Brown #undef __FUNCT__
1775c833c3b5SJed Brown #define __FUNCT__ "DMInterpolate"
1776c833c3b5SJed Brown /*@
1777c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1778c833c3b5SJed Brown 
1779c833c3b5SJed Brown    Collective if any hooks are
1780c833c3b5SJed Brown 
1781c833c3b5SJed Brown    Input Arguments:
1782c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1783c833c3b5SJed Brown .  restrct - interpolation matrix, apply using MatInterpolate()
1784c833c3b5SJed Brown -  fine - finer DM to update
1785c833c3b5SJed Brown 
1786c833c3b5SJed Brown    Level: developer
1787c833c3b5SJed Brown 
1788c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1789c833c3b5SJed Brown @*/
1790c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1791c833c3b5SJed Brown {
1792c833c3b5SJed Brown   PetscErrorCode   ierr;
1793c833c3b5SJed Brown   DMRefineHookLink link;
1794c833c3b5SJed Brown 
1795c833c3b5SJed Brown   PetscFunctionBegin;
1796c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
17978865f1eaSKarl Rupp     if (link->interphook) {
17988865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
17998865f1eaSKarl Rupp     }
18004057135bSMatthew G Knepley   }
180147c6ae99SBarry Smith   PetscFunctionReturn(0);
180247c6ae99SBarry Smith }
180347c6ae99SBarry Smith 
180447c6ae99SBarry Smith #undef __FUNCT__
1805eb3f98d2SBarry Smith #define __FUNCT__ "DMGetRefineLevel"
1806eb3f98d2SBarry Smith /*@
1807eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1808eb3f98d2SBarry Smith 
1809eb3f98d2SBarry Smith     Not Collective
1810eb3f98d2SBarry Smith 
1811eb3f98d2SBarry Smith     Input Parameter:
1812eb3f98d2SBarry Smith .   dm - the DM object
1813eb3f98d2SBarry Smith 
1814eb3f98d2SBarry Smith     Output Parameter:
1815eb3f98d2SBarry Smith .   level - number of refinements
1816eb3f98d2SBarry Smith 
1817eb3f98d2SBarry Smith     Level: developer
1818eb3f98d2SBarry Smith 
18196a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1820eb3f98d2SBarry Smith 
1821eb3f98d2SBarry Smith @*/
1822eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1823eb3f98d2SBarry Smith {
1824eb3f98d2SBarry Smith   PetscFunctionBegin;
1825eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1826eb3f98d2SBarry Smith   *level = dm->levelup;
1827eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1828eb3f98d2SBarry Smith }
1829eb3f98d2SBarry Smith 
1830eb3f98d2SBarry Smith #undef __FUNCT__
1831fef3a512SBarry Smith #define __FUNCT__ "DMSetRefineLevel"
1832fef3a512SBarry Smith /*@
1833fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1834fef3a512SBarry Smith 
1835fef3a512SBarry Smith     Not Collective
1836fef3a512SBarry Smith 
1837fef3a512SBarry Smith     Input Parameter:
1838fef3a512SBarry Smith +   dm - the DM object
1839fef3a512SBarry Smith -   level - number of refinements
1840fef3a512SBarry Smith 
1841fef3a512SBarry Smith     Level: advanced
1842fef3a512SBarry Smith 
1843fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1844fef3a512SBarry Smith 
1845fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1846fef3a512SBarry Smith 
1847fef3a512SBarry Smith @*/
1848fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1849fef3a512SBarry Smith {
1850fef3a512SBarry Smith   PetscFunctionBegin;
1851fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1852fef3a512SBarry Smith   dm->levelup = level;
1853fef3a512SBarry Smith   PetscFunctionReturn(0);
1854fef3a512SBarry Smith }
1855fef3a512SBarry Smith 
1856fef3a512SBarry Smith #undef __FUNCT__
1857baf369e7SPeter Brune #define __FUNCT__ "DMGlobalToLocalHookAdd"
1858bb9467b5SJed Brown /*@C
1859baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1860baf369e7SPeter Brune 
1861baf369e7SPeter Brune    Logically Collective
1862baf369e7SPeter Brune 
1863baf369e7SPeter Brune    Input Arguments:
1864baf369e7SPeter Brune +  dm - the DM
1865baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1866baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
18670298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1868baf369e7SPeter Brune 
1869baf369e7SPeter Brune    Calling sequence for beginhook:
1870baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1871baf369e7SPeter Brune 
1872baf369e7SPeter Brune +  dm - global DM
1873baf369e7SPeter Brune .  g - global vector
1874baf369e7SPeter Brune .  mode - mode
1875baf369e7SPeter Brune .  l - local vector
1876baf369e7SPeter Brune -  ctx - optional user-defined function context
1877baf369e7SPeter Brune 
1878baf369e7SPeter Brune 
1879baf369e7SPeter Brune    Calling sequence for endhook:
1880ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
1881baf369e7SPeter Brune 
1882baf369e7SPeter Brune +  global - global DM
1883baf369e7SPeter Brune -  ctx - optional user-defined function context
1884baf369e7SPeter Brune 
1885baf369e7SPeter Brune    Level: advanced
1886baf369e7SPeter Brune 
1887baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1888baf369e7SPeter Brune @*/
1889baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
1890baf369e7SPeter Brune {
1891baf369e7SPeter Brune   PetscErrorCode          ierr;
1892baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
1893baf369e7SPeter Brune 
1894baf369e7SPeter Brune   PetscFunctionBegin;
1895baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1896baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
189795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
1898baf369e7SPeter Brune   link->beginhook = beginhook;
1899baf369e7SPeter Brune   link->endhook   = endhook;
1900baf369e7SPeter Brune   link->ctx       = ctx;
19010298fd71SBarry Smith   link->next      = NULL;
1902baf369e7SPeter Brune   *p              = link;
1903baf369e7SPeter Brune   PetscFunctionReturn(0);
1904baf369e7SPeter Brune }
1905baf369e7SPeter Brune 
1906baf369e7SPeter Brune #undef __FUNCT__
19074c274da1SToby Isaac #define __FUNCT__ "DMGlobalToLocalHook_Constraints"
19084c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
19094c274da1SToby Isaac {
19104c274da1SToby Isaac   Mat cMat;
19114c274da1SToby Isaac   Vec cVec;
19124c274da1SToby Isaac   PetscSection section, cSec;
19134c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
19144c274da1SToby Isaac   PetscErrorCode ierr;
19154c274da1SToby Isaac 
19164c274da1SToby Isaac   PetscFunctionBegin;
19174c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
19184c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
19194c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
19205db9a05bSToby Isaac     PetscInt nRows;
19215db9a05bSToby Isaac 
19225db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
19235db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
19244c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
19257711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
19264c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
19274c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
19284c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
19294c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
19304c274da1SToby Isaac       if (dof) {
19314c274da1SToby Isaac         PetscScalar *vals;
19324c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
19334c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
19344c274da1SToby Isaac       }
19354c274da1SToby Isaac     }
19364c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
19374c274da1SToby Isaac   }
19384c274da1SToby Isaac   PetscFunctionReturn(0);
19394c274da1SToby Isaac }
19404c274da1SToby Isaac 
19414c274da1SToby Isaac #undef __FUNCT__
194247c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalBegin"
194347c6ae99SBarry Smith /*@
194447c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
194547c6ae99SBarry Smith 
194647c6ae99SBarry Smith     Neighbor-wise Collective on DM
194747c6ae99SBarry Smith 
194847c6ae99SBarry Smith     Input Parameters:
194947c6ae99SBarry Smith +   dm - the DM object
195047c6ae99SBarry Smith .   g - the global vector
195147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
195247c6ae99SBarry Smith -   l - the local vector
195347c6ae99SBarry Smith 
195447c6ae99SBarry Smith 
195547c6ae99SBarry Smith     Level: beginner
195647c6ae99SBarry Smith 
1957e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
195847c6ae99SBarry Smith 
195947c6ae99SBarry Smith @*/
19607087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
196147c6ae99SBarry Smith {
19627128ae9fSMatthew G Knepley   PetscSF                 sf;
196347c6ae99SBarry Smith   PetscErrorCode          ierr;
1964baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
196547c6ae99SBarry Smith 
196647c6ae99SBarry Smith   PetscFunctionBegin;
1967171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1968baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
19698865f1eaSKarl Rupp     if (link->beginhook) {
19708865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
19718865f1eaSKarl Rupp     }
1972baf369e7SPeter Brune   }
19737128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
19747128ae9fSMatthew G Knepley   if (sf) {
1975ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
1976ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
19777128ae9fSMatthew G Knepley 
197882f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
19797128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
1980ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
19817128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
19827128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
1983ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
19847128ae9fSMatthew G Knepley   } else {
1985843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
19867128ae9fSMatthew G Knepley   }
198747c6ae99SBarry Smith   PetscFunctionReturn(0);
198847c6ae99SBarry Smith }
198947c6ae99SBarry Smith 
199047c6ae99SBarry Smith #undef __FUNCT__
199147c6ae99SBarry Smith #define __FUNCT__ "DMGlobalToLocalEnd"
199247c6ae99SBarry Smith /*@
199347c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
199447c6ae99SBarry Smith 
199547c6ae99SBarry Smith     Neighbor-wise Collective on DM
199647c6ae99SBarry Smith 
199747c6ae99SBarry Smith     Input Parameters:
199847c6ae99SBarry Smith +   dm - the DM object
199947c6ae99SBarry Smith .   g - the global vector
200047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
200147c6ae99SBarry Smith -   l - the local vector
200247c6ae99SBarry Smith 
200347c6ae99SBarry Smith 
200447c6ae99SBarry Smith     Level: beginner
200547c6ae99SBarry Smith 
2006e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
200747c6ae99SBarry Smith 
200847c6ae99SBarry Smith @*/
20097087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
201047c6ae99SBarry Smith {
20117128ae9fSMatthew G Knepley   PetscSF                 sf;
201247c6ae99SBarry Smith   PetscErrorCode          ierr;
2013ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2014ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2015baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
201647c6ae99SBarry Smith 
201747c6ae99SBarry Smith   PetscFunctionBegin;
2018171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
20197128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
20207128ae9fSMatthew G Knepley   if (sf) {
202182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
20227128ae9fSMatthew G Knepley 
20237128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2024ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
20257128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
20267128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2027ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
20287128ae9fSMatthew G Knepley   } else {
2029843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
20307128ae9fSMatthew G Knepley   }
20314c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2032baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2033baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2034baf369e7SPeter Brune   }
203547c6ae99SBarry Smith   PetscFunctionReturn(0);
203647c6ae99SBarry Smith }
203747c6ae99SBarry Smith 
203847c6ae99SBarry Smith #undef __FUNCT__
2039d4d07f1eSToby Isaac #define __FUNCT__ "DMLocalToGlobalHookAdd"
2040d4d07f1eSToby Isaac /*@C
2041d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2042d4d07f1eSToby Isaac 
2043d4d07f1eSToby Isaac    Logically Collective
2044d4d07f1eSToby Isaac 
2045d4d07f1eSToby Isaac    Input Arguments:
2046d4d07f1eSToby Isaac +  dm - the DM
2047d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2048d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2049d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2050d4d07f1eSToby Isaac 
2051d4d07f1eSToby Isaac    Calling sequence for beginhook:
2052d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2053d4d07f1eSToby Isaac 
2054d4d07f1eSToby Isaac +  dm - global DM
2055d4d07f1eSToby Isaac .  l - local vector
2056d4d07f1eSToby Isaac .  mode - mode
2057d4d07f1eSToby Isaac .  g - global vector
2058d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2059d4d07f1eSToby Isaac 
2060d4d07f1eSToby Isaac 
2061d4d07f1eSToby Isaac    Calling sequence for endhook:
2062d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2063d4d07f1eSToby Isaac 
2064d4d07f1eSToby Isaac +  global - global DM
2065d4d07f1eSToby Isaac .  l - local vector
2066d4d07f1eSToby Isaac .  mode - mode
2067d4d07f1eSToby Isaac .  g - global vector
2068d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2069d4d07f1eSToby Isaac 
2070d4d07f1eSToby Isaac    Level: advanced
2071d4d07f1eSToby Isaac 
2072d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2073d4d07f1eSToby Isaac @*/
2074d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2075d4d07f1eSToby Isaac {
2076d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2077d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2078d4d07f1eSToby Isaac 
2079d4d07f1eSToby Isaac   PetscFunctionBegin;
2080d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2081d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
208295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2083d4d07f1eSToby Isaac   link->beginhook = beginhook;
2084d4d07f1eSToby Isaac   link->endhook   = endhook;
2085d4d07f1eSToby Isaac   link->ctx       = ctx;
2086d4d07f1eSToby Isaac   link->next      = NULL;
2087d4d07f1eSToby Isaac   *p              = link;
2088d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2089d4d07f1eSToby Isaac }
2090d4d07f1eSToby Isaac 
2091d4d07f1eSToby Isaac #undef __FUNCT__
20924c274da1SToby Isaac #define __FUNCT__ "DMLocalToGlobalHook_Constraints"
20934c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
20944c274da1SToby Isaac {
20954c274da1SToby Isaac   Mat cMat;
20964c274da1SToby Isaac   Vec cVec;
20974c274da1SToby Isaac   PetscSection section, cSec;
20984c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20994c274da1SToby Isaac   PetscErrorCode ierr;
21004c274da1SToby Isaac 
21014c274da1SToby Isaac   PetscFunctionBegin;
21024c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21034c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21044c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
21055db9a05bSToby Isaac     PetscInt nRows;
21065db9a05bSToby Isaac 
21075db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21085db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
21094c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
21107711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21114c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21124c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21134c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21144c274da1SToby Isaac       if (dof) {
21154c274da1SToby Isaac         PetscInt d;
21164c274da1SToby Isaac         PetscScalar *vals;
21174c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
21184c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
21194c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
21204c274da1SToby Isaac          * we just extracted */
21214c274da1SToby Isaac         for (d = 0; d < dof; d++) {
21224c274da1SToby Isaac           vals[d] = 0.;
21234c274da1SToby Isaac         }
21244c274da1SToby Isaac       }
21254c274da1SToby Isaac     }
21264c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
21274c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21284c274da1SToby Isaac   }
21294c274da1SToby Isaac   PetscFunctionReturn(0);
21304c274da1SToby Isaac }
21314c274da1SToby Isaac 
21324c274da1SToby Isaac #undef __FUNCT__
21339a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalBegin"
213447c6ae99SBarry Smith /*@
21359a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
21369a42bb27SBarry Smith 
21379a42bb27SBarry Smith     Neighbor-wise Collective on DM
21389a42bb27SBarry Smith 
21399a42bb27SBarry Smith     Input Parameters:
21409a42bb27SBarry Smith +   dm - the DM object
2141f6813fd5SJed Brown .   l - the local vector
21421eb28f2eSBarry 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.
21431eb28f2eSBarry Smith -   g - the global vector
21449a42bb27SBarry Smith 
2145d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
214684330215SMatthew 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.
21479a42bb27SBarry Smith 
21489a42bb27SBarry Smith     Level: beginner
21499a42bb27SBarry Smith 
2150e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
21519a42bb27SBarry Smith 
21529a42bb27SBarry Smith @*/
21537087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
21549a42bb27SBarry Smith {
21557128ae9fSMatthew G Knepley   PetscSF                 sf;
215684330215SMatthew G. Knepley   PetscSection            s, gs;
2157d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2158ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2159ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
216084330215SMatthew G. Knepley   PetscBool               isInsert;
216184330215SMatthew G. Knepley   PetscErrorCode          ierr;
21629a42bb27SBarry Smith 
21639a42bb27SBarry Smith   PetscFunctionBegin;
2164171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2165d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2166d4d07f1eSToby Isaac     if (link->beginhook) {
2167d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2168d4d07f1eSToby Isaac     }
2169d4d07f1eSToby Isaac   }
21704c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
21717128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
217284330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
21737128ae9fSMatthew G Knepley   switch (mode) {
21747128ae9fSMatthew G Knepley   case INSERT_VALUES:
21757128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2176304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
217784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
21787128ae9fSMatthew G Knepley   case ADD_VALUES:
21797128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2180304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
218184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
21827128ae9fSMatthew G Knepley   default:
218382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21847128ae9fSMatthew G Knepley   }
218584330215SMatthew G. Knepley   if (sf && !isInsert) {
2186ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
21877128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2188a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2189ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
219084330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
219184330215SMatthew G. Knepley   } else if (s && isInsert) {
219284330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
219384330215SMatthew G. Knepley 
219484330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
219584330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
219684330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2197ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
219884330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
219984330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2200b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
220184330215SMatthew G. Knepley 
220284330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
220303442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
220484330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2205b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
220684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
220784330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2208b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
220903442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2210b3b16f48SMatthew 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);
2211b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2212b3b16f48SMatthew G. Knepley       if (!gcdof) {
221384330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2214b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2215b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
221684330215SMatthew G. Knepley         const PetscInt *cdofs;
221784330215SMatthew G. Knepley         PetscInt        cind = 0;
221884330215SMatthew G. Knepley 
221984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2220b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
222184330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2222b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
222384330215SMatthew G. Knepley         }
2224b3b16f48SMatthew 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);
222584330215SMatthew G. Knepley     }
2226ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22277128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
22287128ae9fSMatthew G Knepley   } else {
2229843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22307128ae9fSMatthew G Knepley   }
22319a42bb27SBarry Smith   PetscFunctionReturn(0);
22329a42bb27SBarry Smith }
22339a42bb27SBarry Smith 
22349a42bb27SBarry Smith #undef __FUNCT__
22359a42bb27SBarry Smith #define __FUNCT__ "DMLocalToGlobalEnd"
22369a42bb27SBarry Smith /*@
22379a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
223847c6ae99SBarry Smith 
223947c6ae99SBarry Smith     Neighbor-wise Collective on DM
224047c6ae99SBarry Smith 
224147c6ae99SBarry Smith     Input Parameters:
224247c6ae99SBarry Smith +   dm - the DM object
2243f6813fd5SJed Brown .   l - the local vector
224447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2245f6813fd5SJed Brown -   g - the global vector
224647c6ae99SBarry Smith 
224747c6ae99SBarry Smith 
224847c6ae99SBarry Smith     Level: beginner
224947c6ae99SBarry Smith 
2250e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
225147c6ae99SBarry Smith 
225247c6ae99SBarry Smith @*/
22537087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
225447c6ae99SBarry Smith {
22557128ae9fSMatthew G Knepley   PetscSF                 sf;
225684330215SMatthew G. Knepley   PetscSection            s;
2257d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
225884330215SMatthew G. Knepley   PetscBool               isInsert;
225984330215SMatthew G. Knepley   PetscErrorCode          ierr;
226047c6ae99SBarry Smith 
226147c6ae99SBarry Smith   PetscFunctionBegin;
2262171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22637128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
226484330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
22657128ae9fSMatthew G Knepley   switch (mode) {
22667128ae9fSMatthew G Knepley   case INSERT_VALUES:
22677128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
226884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22697128ae9fSMatthew G Knepley   case ADD_VALUES:
22707128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
227184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
22727128ae9fSMatthew G Knepley   default:
227382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22747128ae9fSMatthew G Knepley   }
227584330215SMatthew G. Knepley   if (sf && !isInsert) {
2276ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2277ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
227884330215SMatthew G. Knepley 
2279ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
22807128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2281a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2282ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
22837128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
228484330215SMatthew G. Knepley   } else if (s && isInsert) {
22857128ae9fSMatthew G Knepley   } else {
2286843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
22877128ae9fSMatthew G Knepley   }
2288d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2289d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2290d4d07f1eSToby Isaac   }
229147c6ae99SBarry Smith   PetscFunctionReturn(0);
229247c6ae99SBarry Smith }
229347c6ae99SBarry Smith 
229447c6ae99SBarry Smith #undef __FUNCT__
2295f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalBegin"
2296f089877aSRichard Tran Mills /*@
2297bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2298bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2299d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2300f089877aSRichard Tran Mills 
2301bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2302f089877aSRichard Tran Mills 
2303f089877aSRichard Tran Mills    Input Parameters:
2304f089877aSRichard Tran Mills +  dm - the DM object
2305bc0a1609SRichard Tran Mills .  g - the original local vector
2306bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2307f089877aSRichard Tran Mills 
2308bc0a1609SRichard Tran Mills    Output Parameter:
2309bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2310f089877aSRichard Tran Mills 
2311f089877aSRichard Tran Mills    Level: intermediate
2312f089877aSRichard Tran Mills 
2313bc0a1609SRichard Tran Mills    Notes:
2314bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2315bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2316bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2317bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2318bc0a1609SRichard Tran Mills 
2319bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2320bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2321f089877aSRichard Tran Mills 
2322f089877aSRichard Tran Mills @*/
2323f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2324f089877aSRichard Tran Mills {
2325f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2326f089877aSRichard Tran Mills 
2327f089877aSRichard Tran Mills   PetscFunctionBegin;
2328f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2329f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2330f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2331f089877aSRichard Tran Mills }
2332f089877aSRichard Tran Mills 
2333f089877aSRichard Tran Mills #undef __FUNCT__
2334f089877aSRichard Tran Mills #define __FUNCT__ "DMLocalToLocalEnd"
2335f089877aSRichard Tran Mills /*@
2336bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2337bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2338d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2339f089877aSRichard Tran Mills 
2340bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2341f089877aSRichard Tran Mills 
2342f089877aSRichard Tran Mills    Input Parameters:
2343bc0a1609SRichard Tran Mills +  da - the DM object
2344bc0a1609SRichard Tran Mills .  g - the original local vector
2345bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2346f089877aSRichard Tran Mills 
2347bc0a1609SRichard Tran Mills    Output Parameter:
2348bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2349f089877aSRichard Tran Mills 
2350f089877aSRichard Tran Mills    Level: intermediate
2351f089877aSRichard Tran Mills 
2352bc0a1609SRichard Tran Mills    Notes:
2353bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2354bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2355bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2356bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2357bc0a1609SRichard Tran Mills 
2358bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2359bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2360f089877aSRichard Tran Mills 
2361f089877aSRichard Tran Mills @*/
2362f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2363f089877aSRichard Tran Mills {
2364f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2365f089877aSRichard Tran Mills 
2366f089877aSRichard Tran Mills   PetscFunctionBegin;
2367f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2368f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2369f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2370f089877aSRichard Tran Mills }
2371f089877aSRichard Tran Mills 
2372f089877aSRichard Tran Mills 
2373f089877aSRichard Tran Mills #undef __FUNCT__
237447c6ae99SBarry Smith #define __FUNCT__ "DMCoarsen"
237547c6ae99SBarry Smith /*@
237647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
237747c6ae99SBarry Smith 
237847c6ae99SBarry Smith     Collective on DM
237947c6ae99SBarry Smith 
238047c6ae99SBarry Smith     Input Parameter:
238147c6ae99SBarry Smith +   dm - the DM object
238291d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
238347c6ae99SBarry Smith 
238447c6ae99SBarry Smith     Output Parameter:
238547c6ae99SBarry Smith .   dmc - the coarsened DM
238647c6ae99SBarry Smith 
238747c6ae99SBarry Smith     Level: developer
238847c6ae99SBarry Smith 
2389e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
239047c6ae99SBarry Smith 
239147c6ae99SBarry Smith @*/
23927087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
239347c6ae99SBarry Smith {
239447c6ae99SBarry Smith   PetscErrorCode    ierr;
2395b17ce1afSJed Brown   DMCoarsenHookLink link;
239647c6ae99SBarry Smith 
239747c6ae99SBarry Smith   PetscFunctionBegin;
2398171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2399fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
240047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
240147c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
24028892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2403a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
240443842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
24058cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2406644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
24070598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2408656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2409e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2410b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2411b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2412b17ce1afSJed Brown   }
241347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2414b17ce1afSJed Brown   PetscFunctionReturn(0);
2415b17ce1afSJed Brown }
2416b17ce1afSJed Brown 
2417b17ce1afSJed Brown #undef __FUNCT__
2418b17ce1afSJed Brown #define __FUNCT__ "DMCoarsenHookAdd"
2419bb9467b5SJed Brown /*@C
2420b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2421b17ce1afSJed Brown 
2422b17ce1afSJed Brown    Logically Collective
2423b17ce1afSJed Brown 
2424b17ce1afSJed Brown    Input Arguments:
2425b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2426b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2427b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
24280298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2429b17ce1afSJed Brown 
2430b17ce1afSJed Brown    Calling sequence of coarsenhook:
2431b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2432b17ce1afSJed Brown 
2433b17ce1afSJed Brown +  fine - fine level DM
2434b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2435b17ce1afSJed Brown -  ctx - optional user-defined function context
2436b17ce1afSJed Brown 
2437b17ce1afSJed Brown    Calling sequence for restricthook:
2438c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2439b17ce1afSJed Brown 
2440b17ce1afSJed Brown +  fine - fine level DM
2441b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2442c833c3b5SJed Brown .  rscale - scaling vector for restriction
2443c833c3b5SJed Brown .  inject - matrix restricting by injection
2444b17ce1afSJed Brown .  coarse - coarse level DM to update
2445b17ce1afSJed Brown -  ctx - optional user-defined function context
2446b17ce1afSJed Brown 
2447b17ce1afSJed Brown    Level: advanced
2448b17ce1afSJed Brown 
2449b17ce1afSJed Brown    Notes:
2450b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2451b17ce1afSJed Brown 
2452b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2453b17ce1afSJed Brown 
2454b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2455b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2456b17ce1afSJed Brown 
2457bb9467b5SJed Brown    This function is currently not available from Fortran.
2458bb9467b5SJed Brown 
2459c833c3b5SJed Brown .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2460b17ce1afSJed Brown @*/
2461b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2462b17ce1afSJed Brown {
2463b17ce1afSJed Brown   PetscErrorCode    ierr;
2464b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2465b17ce1afSJed Brown 
2466b17ce1afSJed Brown   PetscFunctionBegin;
2467b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
24686bfea28cSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
246995dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2470b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2471b17ce1afSJed Brown   link->restricthook = restricthook;
2472b17ce1afSJed Brown   link->ctx          = ctx;
24730298fd71SBarry Smith   link->next         = NULL;
2474b17ce1afSJed Brown   *p                 = link;
2475b17ce1afSJed Brown   PetscFunctionReturn(0);
2476b17ce1afSJed Brown }
2477b17ce1afSJed Brown 
2478b17ce1afSJed Brown #undef __FUNCT__
2479b17ce1afSJed Brown #define __FUNCT__ "DMRestrict"
2480b17ce1afSJed Brown /*@
2481b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2482b17ce1afSJed Brown 
2483b17ce1afSJed Brown    Collective if any hooks are
2484b17ce1afSJed Brown 
2485b17ce1afSJed Brown    Input Arguments:
2486b17ce1afSJed Brown +  fine - finer DM to use as a base
2487b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2488b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2489b17ce1afSJed Brown -  coarse - coarer DM to update
2490b17ce1afSJed Brown 
2491b17ce1afSJed Brown    Level: developer
2492b17ce1afSJed Brown 
2493b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2494b17ce1afSJed Brown @*/
2495b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2496b17ce1afSJed Brown {
2497b17ce1afSJed Brown   PetscErrorCode    ierr;
2498b17ce1afSJed Brown   DMCoarsenHookLink link;
2499b17ce1afSJed Brown 
2500b17ce1afSJed Brown   PetscFunctionBegin;
2501b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
25028865f1eaSKarl Rupp     if (link->restricthook) {
25038865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
25048865f1eaSKarl Rupp     }
2505b17ce1afSJed Brown   }
250647c6ae99SBarry Smith   PetscFunctionReturn(0);
250747c6ae99SBarry Smith }
250847c6ae99SBarry Smith 
250947c6ae99SBarry Smith #undef __FUNCT__
2510be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainHookAdd"
2511bb9467b5SJed Brown /*@C
2512be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
25135dbd56e3SPeter Brune 
25145dbd56e3SPeter Brune    Logically Collective
25155dbd56e3SPeter Brune 
25165dbd56e3SPeter Brune    Input Arguments:
25175dbd56e3SPeter Brune +  global - global DM
2518ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
25195dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
25200298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
25215dbd56e3SPeter Brune 
2522ec4806b8SPeter Brune 
2523ec4806b8SPeter Brune    Calling sequence for ddhook:
2524ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2525ec4806b8SPeter Brune 
2526ec4806b8SPeter Brune +  global - global DM
2527ec4806b8SPeter Brune .  block  - block DM
2528ec4806b8SPeter Brune -  ctx - optional user-defined function context
2529ec4806b8SPeter Brune 
25305dbd56e3SPeter Brune    Calling sequence for restricthook:
2531ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
25325dbd56e3SPeter Brune 
25335dbd56e3SPeter Brune +  global - global DM
25345dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
25355dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2536ec4806b8SPeter Brune .  block  - block DM
25375dbd56e3SPeter Brune -  ctx - optional user-defined function context
25385dbd56e3SPeter Brune 
25395dbd56e3SPeter Brune    Level: advanced
25405dbd56e3SPeter Brune 
25415dbd56e3SPeter Brune    Notes:
2542ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
25435dbd56e3SPeter Brune 
25445dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
25455dbd56e3SPeter Brune 
25465dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2547ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
25485dbd56e3SPeter Brune 
2549bb9467b5SJed Brown    This function is currently not available from Fortran.
2550bb9467b5SJed Brown 
25515dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
25525dbd56e3SPeter Brune @*/
2553be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
25545dbd56e3SPeter Brune {
25555dbd56e3SPeter Brune   PetscErrorCode      ierr;
2556be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
25575dbd56e3SPeter Brune 
25585dbd56e3SPeter Brune   PetscFunctionBegin;
25595dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2560be081cd6SPeter Brune   for (p=&global->subdomainhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
256195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
25625dbd56e3SPeter Brune   link->restricthook = restricthook;
2563be081cd6SPeter Brune   link->ddhook       = ddhook;
25645dbd56e3SPeter Brune   link->ctx          = ctx;
25650298fd71SBarry Smith   link->next         = NULL;
25665dbd56e3SPeter Brune   *p                 = link;
25675dbd56e3SPeter Brune   PetscFunctionReturn(0);
25685dbd56e3SPeter Brune }
25695dbd56e3SPeter Brune 
25705dbd56e3SPeter Brune #undef __FUNCT__
2571be081cd6SPeter Brune #define __FUNCT__ "DMSubDomainRestrict"
25725dbd56e3SPeter Brune /*@
2573be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
25745dbd56e3SPeter Brune 
25755dbd56e3SPeter Brune    Collective if any hooks are
25765dbd56e3SPeter Brune 
25775dbd56e3SPeter Brune    Input Arguments:
25785dbd56e3SPeter Brune +  fine - finer DM to use as a base
2579be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2580be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
25815dbd56e3SPeter Brune -  coarse - coarer DM to update
25825dbd56e3SPeter Brune 
25835dbd56e3SPeter Brune    Level: developer
25845dbd56e3SPeter Brune 
25855dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
25865dbd56e3SPeter Brune @*/
2587be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
25885dbd56e3SPeter Brune {
25895dbd56e3SPeter Brune   PetscErrorCode      ierr;
2590be081cd6SPeter Brune   DMSubDomainHookLink link;
25915dbd56e3SPeter Brune 
25925dbd56e3SPeter Brune   PetscFunctionBegin;
2593be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
25948865f1eaSKarl Rupp     if (link->restricthook) {
25958865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
25968865f1eaSKarl Rupp     }
25975dbd56e3SPeter Brune   }
25985dbd56e3SPeter Brune   PetscFunctionReturn(0);
25995dbd56e3SPeter Brune }
26005dbd56e3SPeter Brune 
26015dbd56e3SPeter Brune #undef __FUNCT__
26025fe1f584SPeter Brune #define __FUNCT__ "DMGetCoarsenLevel"
26035fe1f584SPeter Brune /*@
26046a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
26055fe1f584SPeter Brune 
26065fe1f584SPeter Brune     Not Collective
26075fe1f584SPeter Brune 
26085fe1f584SPeter Brune     Input Parameter:
26095fe1f584SPeter Brune .   dm - the DM object
26105fe1f584SPeter Brune 
26115fe1f584SPeter Brune     Output Parameter:
26126a7d9d85SPeter Brune .   level - number of coarsenings
26135fe1f584SPeter Brune 
26145fe1f584SPeter Brune     Level: developer
26155fe1f584SPeter Brune 
26166a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
26175fe1f584SPeter Brune 
26185fe1f584SPeter Brune @*/
26195fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
26205fe1f584SPeter Brune {
26215fe1f584SPeter Brune   PetscFunctionBegin;
26225fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26235fe1f584SPeter Brune   *level = dm->leveldown;
26245fe1f584SPeter Brune   PetscFunctionReturn(0);
26255fe1f584SPeter Brune }
26265fe1f584SPeter Brune 
26275fe1f584SPeter Brune 
26285fe1f584SPeter Brune 
26295fe1f584SPeter Brune #undef __FUNCT__
263047c6ae99SBarry Smith #define __FUNCT__ "DMRefineHierarchy"
263147c6ae99SBarry Smith /*@C
263247c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
263347c6ae99SBarry Smith 
263447c6ae99SBarry Smith     Collective on DM
263547c6ae99SBarry Smith 
263647c6ae99SBarry Smith     Input Parameter:
263747c6ae99SBarry Smith +   dm - the DM object
263847c6ae99SBarry Smith -   nlevels - the number of levels of refinement
263947c6ae99SBarry Smith 
264047c6ae99SBarry Smith     Output Parameter:
264147c6ae99SBarry Smith .   dmf - the refined DM hierarchy
264247c6ae99SBarry Smith 
264347c6ae99SBarry Smith     Level: developer
264447c6ae99SBarry Smith 
2645e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
264647c6ae99SBarry Smith 
264747c6ae99SBarry Smith @*/
26487087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
264947c6ae99SBarry Smith {
265047c6ae99SBarry Smith   PetscErrorCode ierr;
265147c6ae99SBarry Smith 
265247c6ae99SBarry Smith   PetscFunctionBegin;
2653171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2654ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
265547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
265647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
265747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
265847c6ae99SBarry Smith   } else if (dm->ops->refine) {
265947c6ae99SBarry Smith     PetscInt i;
266047c6ae99SBarry Smith 
2661ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
266247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2663ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
266447c6ae99SBarry Smith     }
2665ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
266647c6ae99SBarry Smith   PetscFunctionReturn(0);
266747c6ae99SBarry Smith }
266847c6ae99SBarry Smith 
266947c6ae99SBarry Smith #undef __FUNCT__
267047c6ae99SBarry Smith #define __FUNCT__ "DMCoarsenHierarchy"
267147c6ae99SBarry Smith /*@C
267247c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
267347c6ae99SBarry Smith 
267447c6ae99SBarry Smith     Collective on DM
267547c6ae99SBarry Smith 
267647c6ae99SBarry Smith     Input Parameter:
267747c6ae99SBarry Smith +   dm - the DM object
267847c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
267947c6ae99SBarry Smith 
268047c6ae99SBarry Smith     Output Parameter:
268147c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
268247c6ae99SBarry Smith 
268347c6ae99SBarry Smith     Level: developer
268447c6ae99SBarry Smith 
2685e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
268647c6ae99SBarry Smith 
268747c6ae99SBarry Smith @*/
26887087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
268947c6ae99SBarry Smith {
269047c6ae99SBarry Smith   PetscErrorCode ierr;
269147c6ae99SBarry Smith 
269247c6ae99SBarry Smith   PetscFunctionBegin;
2693171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2694ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
269547c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
269647c6ae99SBarry Smith   PetscValidPointer(dmc,3);
269747c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
269847c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
269947c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
270047c6ae99SBarry Smith     PetscInt i;
270147c6ae99SBarry Smith 
2702ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
270347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2704ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
270547c6ae99SBarry Smith     }
2706ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
270747c6ae99SBarry Smith   PetscFunctionReturn(0);
270847c6ae99SBarry Smith }
270947c6ae99SBarry Smith 
271047c6ae99SBarry Smith #undef __FUNCT__
2711e727c939SJed Brown #define __FUNCT__ "DMCreateAggregates"
271247c6ae99SBarry Smith /*@
2713e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
271447c6ae99SBarry Smith    grids associated with two DMs.
271547c6ae99SBarry Smith 
271647c6ae99SBarry Smith    Collective on DM
271747c6ae99SBarry Smith 
271847c6ae99SBarry Smith    Input Parameters:
271947c6ae99SBarry Smith +  dmc - the coarse grid DM
272047c6ae99SBarry Smith -  dmf - the fine grid DM
272147c6ae99SBarry Smith 
272247c6ae99SBarry Smith    Output Parameters:
272347c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
272447c6ae99SBarry Smith 
272547c6ae99SBarry Smith    Level: intermediate
272647c6ae99SBarry Smith 
272747c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
272847c6ae99SBarry Smith 
2729e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
273047c6ae99SBarry Smith @*/
2731e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
273247c6ae99SBarry Smith {
273347c6ae99SBarry Smith   PetscErrorCode ierr;
273447c6ae99SBarry Smith 
273547c6ae99SBarry Smith   PetscFunctionBegin;
2736171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2737171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
273847c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
273947c6ae99SBarry Smith   PetscFunctionReturn(0);
274047c6ae99SBarry Smith }
274147c6ae99SBarry Smith 
274247c6ae99SBarry Smith #undef __FUNCT__
27431a266240SBarry Smith #define __FUNCT__ "DMSetApplicationContextDestroy"
27441a266240SBarry Smith /*@C
27451a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
27461a266240SBarry Smith 
27471a266240SBarry Smith     Not Collective
27481a266240SBarry Smith 
27491a266240SBarry Smith     Input Parameters:
27501a266240SBarry Smith +   dm - the DM object
27511a266240SBarry Smith -   destroy - the destroy function
27521a266240SBarry Smith 
27531a266240SBarry Smith     Level: intermediate
27541a266240SBarry Smith 
2755e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
27561a266240SBarry Smith 
2757f07f9ceaSJed Brown @*/
27581a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
27591a266240SBarry Smith {
27601a266240SBarry Smith   PetscFunctionBegin;
2761171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
27621a266240SBarry Smith   dm->ctxdestroy = destroy;
27631a266240SBarry Smith   PetscFunctionReturn(0);
27641a266240SBarry Smith }
27651a266240SBarry Smith 
27661a266240SBarry Smith #undef __FUNCT__
27671b2093e4SBarry Smith #define __FUNCT__ "DMSetApplicationContext"
2768b07ff414SBarry Smith /*@
27691b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
277047c6ae99SBarry Smith 
277147c6ae99SBarry Smith     Not Collective
277247c6ae99SBarry Smith 
277347c6ae99SBarry Smith     Input Parameters:
277447c6ae99SBarry Smith +   dm - the DM object
277547c6ae99SBarry Smith -   ctx - the user context
277647c6ae99SBarry Smith 
277747c6ae99SBarry Smith     Level: intermediate
277847c6ae99SBarry Smith 
2779e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
278047c6ae99SBarry Smith 
278147c6ae99SBarry Smith @*/
27821b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
278347c6ae99SBarry Smith {
278447c6ae99SBarry Smith   PetscFunctionBegin;
2785171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
278647c6ae99SBarry Smith   dm->ctx = ctx;
278747c6ae99SBarry Smith   PetscFunctionReturn(0);
278847c6ae99SBarry Smith }
278947c6ae99SBarry Smith 
279047c6ae99SBarry Smith #undef __FUNCT__
27911b2093e4SBarry Smith #define __FUNCT__ "DMGetApplicationContext"
279247c6ae99SBarry Smith /*@
27931b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
279447c6ae99SBarry Smith 
279547c6ae99SBarry Smith     Not Collective
279647c6ae99SBarry Smith 
279747c6ae99SBarry Smith     Input Parameter:
279847c6ae99SBarry Smith .   dm - the DM object
279947c6ae99SBarry Smith 
280047c6ae99SBarry Smith     Output Parameter:
280147c6ae99SBarry Smith .   ctx - the user context
280247c6ae99SBarry Smith 
280347c6ae99SBarry Smith     Level: intermediate
280447c6ae99SBarry Smith 
2805e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
280647c6ae99SBarry Smith 
280747c6ae99SBarry Smith @*/
28081b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
280947c6ae99SBarry Smith {
281047c6ae99SBarry Smith   PetscFunctionBegin;
2811171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28121b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
281347c6ae99SBarry Smith   PetscFunctionReturn(0);
281447c6ae99SBarry Smith }
281547c6ae99SBarry Smith 
281647c6ae99SBarry Smith #undef __FUNCT__
281708da532bSDmitry Karpeev #define __FUNCT__ "DMSetVariableBounds"
281808da532bSDmitry Karpeev /*@C
2819df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
282008da532bSDmitry Karpeev 
282108da532bSDmitry Karpeev     Logically Collective on DM
282208da532bSDmitry Karpeev 
282308da532bSDmitry Karpeev     Input Parameter:
282408da532bSDmitry Karpeev +   dm - the DM object
28250298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
282608da532bSDmitry Karpeev 
282708da532bSDmitry Karpeev     Level: intermediate
282808da532bSDmitry Karpeev 
2829835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
283008da532bSDmitry Karpeev          DMSetJacobian()
283108da532bSDmitry Karpeev 
283208da532bSDmitry Karpeev @*/
283308da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
283408da532bSDmitry Karpeev {
283508da532bSDmitry Karpeev   PetscFunctionBegin;
283608da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
283708da532bSDmitry Karpeev   PetscFunctionReturn(0);
283808da532bSDmitry Karpeev }
283908da532bSDmitry Karpeev 
284008da532bSDmitry Karpeev #undef __FUNCT__
284108da532bSDmitry Karpeev #define __FUNCT__ "DMHasVariableBounds"
284208da532bSDmitry Karpeev /*@
284308da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
284408da532bSDmitry Karpeev 
284508da532bSDmitry Karpeev     Not Collective
284608da532bSDmitry Karpeev 
284708da532bSDmitry Karpeev     Input Parameter:
284808da532bSDmitry Karpeev .   dm - the DM object to destroy
284908da532bSDmitry Karpeev 
285008da532bSDmitry Karpeev     Output Parameter:
285108da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
285208da532bSDmitry Karpeev 
285308da532bSDmitry Karpeev     Level: developer
285408da532bSDmitry Karpeev 
285574e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
285608da532bSDmitry Karpeev 
285708da532bSDmitry Karpeev @*/
285808da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
285908da532bSDmitry Karpeev {
286008da532bSDmitry Karpeev   PetscFunctionBegin;
286108da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
286208da532bSDmitry Karpeev   PetscFunctionReturn(0);
286308da532bSDmitry Karpeev }
286408da532bSDmitry Karpeev 
286508da532bSDmitry Karpeev #undef __FUNCT__
286608da532bSDmitry Karpeev #define __FUNCT__ "DMComputeVariableBounds"
286708da532bSDmitry Karpeev /*@C
286808da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
286908da532bSDmitry Karpeev 
287008da532bSDmitry Karpeev     Logically Collective on DM
287108da532bSDmitry Karpeev 
287208da532bSDmitry Karpeev     Input Parameters:
2873907376e6SBarry Smith .   dm - the DM object
287408da532bSDmitry Karpeev 
287508da532bSDmitry Karpeev     Output parameters:
287608da532bSDmitry Karpeev +   xl - lower bound
287708da532bSDmitry Karpeev -   xu - upper bound
287808da532bSDmitry Karpeev 
2879907376e6SBarry Smith     Level: advanced
2880907376e6SBarry Smith 
2881907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
288208da532bSDmitry Karpeev 
288374e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
288408da532bSDmitry Karpeev 
288508da532bSDmitry Karpeev @*/
288608da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
288708da532bSDmitry Karpeev {
288808da532bSDmitry Karpeev   PetscErrorCode ierr;
28895fd66863SKarl Rupp 
289008da532bSDmitry Karpeev   PetscFunctionBegin;
289108da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
289208da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
289308da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
289408da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
28958865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
289608da532bSDmitry Karpeev   PetscFunctionReturn(0);
289708da532bSDmitry Karpeev }
289808da532bSDmitry Karpeev 
289908da532bSDmitry Karpeev #undef __FUNCT__
2900b0ae01b7SPeter Brune #define __FUNCT__ "DMHasColoring"
2901b0ae01b7SPeter Brune /*@
2902b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
2903b0ae01b7SPeter Brune 
2904b0ae01b7SPeter Brune     Not Collective
2905b0ae01b7SPeter Brune 
2906b0ae01b7SPeter Brune     Input Parameter:
2907b0ae01b7SPeter Brune .   dm - the DM object
2908b0ae01b7SPeter Brune 
2909b0ae01b7SPeter Brune     Output Parameter:
2910b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
2911b0ae01b7SPeter Brune 
2912b0ae01b7SPeter Brune     Level: developer
2913b0ae01b7SPeter Brune 
2914b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
2915b0ae01b7SPeter Brune 
2916b0ae01b7SPeter Brune @*/
2917b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
2918b0ae01b7SPeter Brune {
2919b0ae01b7SPeter Brune   PetscFunctionBegin;
2920b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
2921b0ae01b7SPeter Brune   PetscFunctionReturn(0);
2922b0ae01b7SPeter Brune }
2923b0ae01b7SPeter Brune 
2924b0ae01b7SPeter Brune #undef __FUNCT__
29253ad4599aSBarry Smith #define __FUNCT__ "DMHasCreateRestriction"
29263ad4599aSBarry Smith /*@
29273ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
29283ad4599aSBarry Smith 
29293ad4599aSBarry Smith     Not Collective
29303ad4599aSBarry Smith 
29313ad4599aSBarry Smith     Input Parameter:
29323ad4599aSBarry Smith .   dm - the DM object
29333ad4599aSBarry Smith 
29343ad4599aSBarry Smith     Output Parameter:
29353ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
29363ad4599aSBarry Smith 
29373ad4599aSBarry Smith     Level: developer
29383ad4599aSBarry Smith 
29393ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
29403ad4599aSBarry Smith 
29413ad4599aSBarry Smith @*/
29423ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
29433ad4599aSBarry Smith {
29443ad4599aSBarry Smith   PetscFunctionBegin;
29453ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
29463ad4599aSBarry Smith   PetscFunctionReturn(0);
29473ad4599aSBarry Smith }
29483ad4599aSBarry Smith 
29493ad4599aSBarry Smith #undef  __FUNCT__
295008da532bSDmitry Karpeev #define __FUNCT__ "DMSetVec"
2951748fac09SDmitry Karpeev /*@C
295208da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
295308da532bSDmitry Karpeev 
295408da532bSDmitry Karpeev     Collective on DM
295508da532bSDmitry Karpeev 
295608da532bSDmitry Karpeev     Input Parameter:
295708da532bSDmitry Karpeev +   dm - the DM object
29580298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
295908da532bSDmitry Karpeev 
296008da532bSDmitry Karpeev     Level: developer
296108da532bSDmitry Karpeev 
296274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
296308da532bSDmitry Karpeev 
296408da532bSDmitry Karpeev @*/
296508da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
296608da532bSDmitry Karpeev {
296708da532bSDmitry Karpeev   PetscErrorCode ierr;
29685fd66863SKarl Rupp 
296908da532bSDmitry Karpeev   PetscFunctionBegin;
297008da532bSDmitry Karpeev   if (x) {
297108da532bSDmitry Karpeev     if (!dm->x) {
297208da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
297308da532bSDmitry Karpeev     }
297408da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
29758865f1eaSKarl Rupp   } else if (dm->x) {
297608da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
297708da532bSDmitry Karpeev   }
297808da532bSDmitry Karpeev   PetscFunctionReturn(0);
297908da532bSDmitry Karpeev }
298008da532bSDmitry Karpeev 
29810298fd71SBarry Smith PetscFunctionList DMList              = NULL;
2982264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
2983264ace61SBarry Smith 
2984264ace61SBarry Smith #undef __FUNCT__
2985264ace61SBarry Smith #define __FUNCT__ "DMSetType"
2986264ace61SBarry Smith /*@C
2987264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
2988264ace61SBarry Smith 
2989264ace61SBarry Smith   Collective on DM
2990264ace61SBarry Smith 
2991264ace61SBarry Smith   Input Parameters:
2992264ace61SBarry Smith + dm     - The DM object
2993264ace61SBarry Smith - method - The name of the DM type
2994264ace61SBarry Smith 
2995264ace61SBarry Smith   Options Database Key:
2996264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
2997264ace61SBarry Smith 
2998264ace61SBarry Smith   Notes:
2999e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3000264ace61SBarry Smith 
3001264ace61SBarry Smith   Level: intermediate
3002264ace61SBarry Smith 
3003264ace61SBarry Smith .keywords: DM, set, type
3004264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3005264ace61SBarry Smith @*/
300619fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3007264ace61SBarry Smith {
3008264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3009264ace61SBarry Smith   PetscBool      match;
3010264ace61SBarry Smith   PetscErrorCode ierr;
3011264ace61SBarry Smith 
3012264ace61SBarry Smith   PetscFunctionBegin;
3013264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3014251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3015264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3016264ace61SBarry Smith 
30170f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
30181c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3019ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3020264ace61SBarry Smith 
3021264ace61SBarry Smith   if (dm->ops->destroy) {
3022264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
30230298fd71SBarry Smith     dm->ops->destroy = NULL;
3024264ace61SBarry Smith   }
3025264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3026264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3027264ace61SBarry Smith   PetscFunctionReturn(0);
3028264ace61SBarry Smith }
3029264ace61SBarry Smith 
3030264ace61SBarry Smith #undef __FUNCT__
3031264ace61SBarry Smith #define __FUNCT__ "DMGetType"
3032264ace61SBarry Smith /*@C
3033264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3034264ace61SBarry Smith 
3035264ace61SBarry Smith   Not Collective
3036264ace61SBarry Smith 
3037264ace61SBarry Smith   Input Parameter:
3038264ace61SBarry Smith . dm  - The DM
3039264ace61SBarry Smith 
3040264ace61SBarry Smith   Output Parameter:
3041264ace61SBarry Smith . type - The DM type name
3042264ace61SBarry Smith 
3043264ace61SBarry Smith   Level: intermediate
3044264ace61SBarry Smith 
3045264ace61SBarry Smith .keywords: DM, get, type, name
3046264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3047264ace61SBarry Smith @*/
304819fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3049264ace61SBarry Smith {
3050264ace61SBarry Smith   PetscErrorCode ierr;
3051264ace61SBarry Smith 
3052264ace61SBarry Smith   PetscFunctionBegin;
3053264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3054c959eef4SJed Brown   PetscValidPointer(type,2);
3055607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3056264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3057264ace61SBarry Smith   PetscFunctionReturn(0);
3058264ace61SBarry Smith }
3059264ace61SBarry Smith 
306067a56275SMatthew G Knepley #undef __FUNCT__
306167a56275SMatthew G Knepley #define __FUNCT__ "DMConvert"
306267a56275SMatthew G Knepley /*@C
306367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
306467a56275SMatthew G Knepley 
306567a56275SMatthew G Knepley   Collective on DM
306667a56275SMatthew G Knepley 
306767a56275SMatthew G Knepley   Input Parameters:
306867a56275SMatthew G Knepley + dm - the DM
306967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
307067a56275SMatthew G Knepley 
307167a56275SMatthew G Knepley   Output Parameter:
307267a56275SMatthew G Knepley . M - pointer to new DM
307367a56275SMatthew G Knepley 
307467a56275SMatthew G Knepley   Notes:
307567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
307667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
307767a56275SMatthew G Knepley   of the input DM.
307867a56275SMatthew G Knepley 
307967a56275SMatthew G Knepley   Level: intermediate
308067a56275SMatthew G Knepley 
308167a56275SMatthew G Knepley .seealso: DMCreate()
308267a56275SMatthew G Knepley @*/
308319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
308467a56275SMatthew G Knepley {
308567a56275SMatthew G Knepley   DM             B;
308667a56275SMatthew G Knepley   char           convname[256];
3087c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
308867a56275SMatthew G Knepley   PetscErrorCode ierr;
308967a56275SMatthew G Knepley 
309067a56275SMatthew G Knepley   PetscFunctionBegin;
309167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
309267a56275SMatthew G Knepley   PetscValidType(dm,1);
309367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3094251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3095c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3096c067b6caSMatthew G. Knepley   if (sametype) {
3097c067b6caSMatthew G. Knepley     *M   = dm;
3098c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3099c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3100c067b6caSMatthew G. Knepley   } else {
31010298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
310267a56275SMatthew G Knepley 
310367a56275SMatthew G Knepley     /*
310467a56275SMatthew G Knepley        Order of precedence:
310567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
310667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
310767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
310867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
310967a56275SMatthew G Knepley        5) Use a really basic converter.
311067a56275SMatthew G Knepley     */
311167a56275SMatthew G Knepley 
311267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
311367a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
311467a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
311567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
311667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
311767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31180005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
311967a56275SMatthew G Knepley     if (conv) goto foundconv;
312067a56275SMatthew G Knepley 
312167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
312282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
312367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
312467a56275SMatthew G Knepley     ierr = PetscStrcpy(convname,"DMConvert_");CHKERRQ(ierr);
312567a56275SMatthew G Knepley     ierr = PetscStrcat(convname,((PetscObject) dm)->type_name);CHKERRQ(ierr);
312667a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_");CHKERRQ(ierr);
312767a56275SMatthew G Knepley     ierr = PetscStrcat(convname,newtype);CHKERRQ(ierr);
312867a56275SMatthew G Knepley     ierr = PetscStrcat(convname,"_C");CHKERRQ(ierr);
31290005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
313067a56275SMatthew G Knepley     if (conv) {
3131fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
313267a56275SMatthew G Knepley       goto foundconv;
313367a56275SMatthew G Knepley     }
313467a56275SMatthew G Knepley 
313567a56275SMatthew G Knepley #if 0
313667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
313767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3138fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
313967a56275SMatthew G Knepley     if (conv) goto foundconv;
314067a56275SMatthew G Knepley 
314167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
314267a56275SMatthew G Knepley     if (dm->ops->convert) {
314367a56275SMatthew G Knepley       conv = dm->ops->convert;
314467a56275SMatthew G Knepley     }
314567a56275SMatthew G Knepley     if (conv) goto foundconv;
314667a56275SMatthew G Knepley #endif
314767a56275SMatthew G Knepley 
314867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
314982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
315067a56275SMatthew G Knepley 
315167a56275SMatthew G Knepley foundconv:
315267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
315367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
315412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
315512fa691eSMatthew G. Knepley     if (dm->maxCell) {
315612fa691eSMatthew G. Knepley       const PetscReal *maxCell, *L;
315712fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
315812fa691eSMatthew G. Knepley       ierr = DMGetPeriodicity(dm, &maxCell, &L, &bd);CHKERRQ(ierr);
315912fa691eSMatthew G. Knepley       ierr = DMSetPeriodicity(*M,  maxCell,  L,  bd);CHKERRQ(ierr);
316012fa691eSMatthew G. Knepley     }
316167a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
316267a56275SMatthew G Knepley   }
316367a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
316467a56275SMatthew G Knepley   PetscFunctionReturn(0);
316567a56275SMatthew G Knepley }
3166264ace61SBarry Smith 
3167264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3168264ace61SBarry Smith 
3169264ace61SBarry Smith #undef __FUNCT__
3170264ace61SBarry Smith #define __FUNCT__ "DMRegister"
3171264ace61SBarry Smith /*@C
31721c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
31731c84c290SBarry Smith 
31741c84c290SBarry Smith   Not Collective
31751c84c290SBarry Smith 
31761c84c290SBarry Smith   Input Parameters:
31771c84c290SBarry Smith + name        - The name of a new user-defined creation routine
31781c84c290SBarry Smith - create_func - The creation routine itself
31791c84c290SBarry Smith 
31801c84c290SBarry Smith   Notes:
31811c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
31821c84c290SBarry Smith 
31831c84c290SBarry Smith 
31841c84c290SBarry Smith   Sample usage:
31851c84c290SBarry Smith .vb
3186bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
31871c84c290SBarry Smith .ve
31881c84c290SBarry Smith 
31891c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
31901c84c290SBarry Smith .vb
31911c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
31921c84c290SBarry Smith     DMSetType(DM,"my_da");
31931c84c290SBarry Smith .ve
31941c84c290SBarry Smith    or at runtime via the option
31951c84c290SBarry Smith .vb
31961c84c290SBarry Smith     -da_type my_da
31971c84c290SBarry Smith .ve
3198264ace61SBarry Smith 
3199264ace61SBarry Smith   Level: advanced
32001c84c290SBarry Smith 
32011c84c290SBarry Smith .keywords: DM, register
3202bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
32031c84c290SBarry Smith 
3204264ace61SBarry Smith @*/
3205bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3206264ace61SBarry Smith {
3207264ace61SBarry Smith   PetscErrorCode ierr;
3208264ace61SBarry Smith 
3209264ace61SBarry Smith   PetscFunctionBegin;
3210a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3211264ace61SBarry Smith   PetscFunctionReturn(0);
3212264ace61SBarry Smith }
3213264ace61SBarry Smith 
3214b859378eSBarry Smith #undef __FUNCT__
3215b859378eSBarry Smith #define __FUNCT__ "DMLoad"
3216b859378eSBarry Smith /*@C
321755849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3218b859378eSBarry Smith 
3219b859378eSBarry Smith   Collective on PetscViewer
3220b859378eSBarry Smith 
3221b859378eSBarry Smith   Input Parameters:
3222b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3223b859378eSBarry Smith            some related function before a call to DMLoad().
3224b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3225b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3226b859378eSBarry Smith 
3227b859378eSBarry Smith    Level: intermediate
3228b859378eSBarry Smith 
3229b859378eSBarry Smith   Notes:
323055849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3231b859378eSBarry Smith 
3232b859378eSBarry Smith   Notes for advanced users:
3233b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3234b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3235b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3236b859378eSBarry Smith   format is
3237b859378eSBarry Smith .vb
3238b859378eSBarry Smith      has not yet been determined
3239b859378eSBarry Smith .ve
3240b859378eSBarry Smith 
3241b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3242b859378eSBarry Smith @*/
3243b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3244b859378eSBarry Smith {
32459331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3246b859378eSBarry Smith   PetscErrorCode ierr;
3247b859378eSBarry Smith 
3248b859378eSBarry Smith   PetscFunctionBegin;
3249b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3250b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
325132c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
32529331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
32539331c7a4SMatthew G. Knepley   if (isbinary) {
32549331c7a4SMatthew G. Knepley     PetscInt classid;
32559331c7a4SMatthew G. Knepley     char     type[256];
3256b859378eSBarry Smith 
3257060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
32589200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3259060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
326032c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
32619331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32629331c7a4SMatthew G. Knepley   } else if (ishdf5) {
32639331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
32649331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3265b859378eSBarry Smith   PetscFunctionReturn(0);
3266b859378eSBarry Smith }
3267b859378eSBarry Smith 
32687da65231SMatthew G Knepley /******************************** FEM Support **********************************/
32697da65231SMatthew G Knepley 
32707da65231SMatthew G Knepley #undef __FUNCT__
32717da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellVector"
3272a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3273a6dfd86eSKarl Rupp {
32741d47ebbbSSatish Balay   PetscInt       f;
32751b30c384SMatthew G Knepley   PetscErrorCode ierr;
32761b30c384SMatthew G Knepley 
32777da65231SMatthew G Knepley   PetscFunctionBegin;
327874778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32791d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
328057622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
32817da65231SMatthew G Knepley   }
32827da65231SMatthew G Knepley   PetscFunctionReturn(0);
32837da65231SMatthew G Knepley }
32847da65231SMatthew G Knepley 
32857da65231SMatthew G Knepley #undef __FUNCT__
32867da65231SMatthew G Knepley #define __FUNCT__ "DMPrintCellMatrix"
3287a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3288a6dfd86eSKarl Rupp {
32891b30c384SMatthew G Knepley   PetscInt       f, g;
32907da65231SMatthew G Knepley   PetscErrorCode ierr;
32917da65231SMatthew G Knepley 
32927da65231SMatthew G Knepley   PetscFunctionBegin;
329374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
32941d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
329574778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
32961d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3297e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
32987da65231SMatthew G Knepley     }
329974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
33007da65231SMatthew G Knepley   }
33017da65231SMatthew G Knepley   PetscFunctionReturn(0);
33027da65231SMatthew G Knepley }
3303e7c4fc90SDmitry Karpeev 
3304970e74d5SMatthew G Knepley #undef __FUNCT__
3305e759306cSMatthew G. Knepley #define __FUNCT__ "DMPrintLocalVec"
33066113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3307e759306cSMatthew G. Knepley {
3308e759306cSMatthew G. Knepley   PetscMPIInt    rank, numProcs;
3309e759306cSMatthew G. Knepley   PetscInt       p;
3310e759306cSMatthew G. Knepley   PetscErrorCode ierr;
3311e759306cSMatthew G. Knepley 
3312e759306cSMatthew G. Knepley   PetscFunctionBegin;
3313e759306cSMatthew G. Knepley   ierr = MPI_Comm_rank(PetscObjectComm((PetscObject) dm), &rank);CHKERRQ(ierr);
3314e759306cSMatthew G. Knepley   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm), &numProcs);CHKERRQ(ierr);
3315e759306cSMatthew G. Knepley   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm), "%s:\n", name);CHKERRQ(ierr);
3316e759306cSMatthew G. Knepley   for (p = 0; p < numProcs; ++p) {
3317e759306cSMatthew G. Knepley     if (p == rank) {
3318e759306cSMatthew G. Knepley       Vec x;
3319e759306cSMatthew G. Knepley 
3320e759306cSMatthew G. Knepley       ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3321e759306cSMatthew G. Knepley       ierr = VecCopy(X, x);CHKERRQ(ierr);
33226113b454SMatthew G. Knepley       ierr = VecChop(x, tol);CHKERRQ(ierr);
3323e759306cSMatthew G. Knepley       ierr = VecView(x, PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3324e759306cSMatthew G. Knepley       ierr = VecDestroy(&x);CHKERRQ(ierr);
3325e759306cSMatthew G. Knepley       ierr = PetscViewerFlush(PETSC_VIEWER_STDOUT_SELF);CHKERRQ(ierr);
3326e759306cSMatthew G. Knepley     }
3327e759306cSMatthew G. Knepley     ierr = PetscBarrier((PetscObject) dm);CHKERRQ(ierr);
3328e759306cSMatthew G. Knepley   }
3329e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3330e759306cSMatthew G. Knepley }
3331e759306cSMatthew G. Knepley 
3332e759306cSMatthew G. Knepley #undef __FUNCT__
333388ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSection"
333488ed4aceSMatthew G Knepley /*@
333588ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
333688ed4aceSMatthew G Knepley 
333788ed4aceSMatthew G Knepley   Input Parameter:
333888ed4aceSMatthew G Knepley . dm - The DM
333988ed4aceSMatthew G Knepley 
334088ed4aceSMatthew G Knepley   Output Parameter:
334188ed4aceSMatthew G Knepley . section - The PetscSection
334288ed4aceSMatthew G Knepley 
334388ed4aceSMatthew G Knepley   Level: intermediate
334488ed4aceSMatthew G Knepley 
334588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
334688ed4aceSMatthew G Knepley 
334788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
334888ed4aceSMatthew G Knepley @*/
33490adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
33500adebc6cSBarry Smith {
3351fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3352fd59a867SMatthew G. Knepley 
335388ed4aceSMatthew G Knepley   PetscFunctionBegin;
335488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
335588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
33562f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
33572f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3358ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
33592f0f8703SMatthew G. Knepley   }
336088ed4aceSMatthew G Knepley   *section = dm->defaultSection;
336188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
336288ed4aceSMatthew G Knepley }
336388ed4aceSMatthew G Knepley 
336488ed4aceSMatthew G Knepley #undef __FUNCT__
336588ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSection"
336688ed4aceSMatthew G Knepley /*@
336788ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
336888ed4aceSMatthew G Knepley 
336988ed4aceSMatthew G Knepley   Input Parameters:
337088ed4aceSMatthew G Knepley + dm - The DM
337188ed4aceSMatthew G Knepley - section - The PetscSection
337288ed4aceSMatthew G Knepley 
337388ed4aceSMatthew G Knepley   Level: intermediate
337488ed4aceSMatthew G Knepley 
337588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
337688ed4aceSMatthew G Knepley 
337788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
337888ed4aceSMatthew G Knepley @*/
33790adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
33800adebc6cSBarry Smith {
3381c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3382af122d2aSMatthew G Knepley   PetscInt       f;
338388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
338488ed4aceSMatthew G Knepley 
338588ed4aceSMatthew G Knepley   PetscFunctionBegin;
338688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3387c473ab19SMatthew G. Knepley   if (section) {
33881d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
33891d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3390c473ab19SMatthew G. Knepley   }
339188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
339288ed4aceSMatthew G Knepley   dm->defaultSection = section;
3393c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3394af122d2aSMatthew G Knepley   if (numFields) {
3395af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3396af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
33970f21e855SMatthew G. Knepley       PetscObject disc;
3398af122d2aSMatthew G Knepley       const char *name;
3399af122d2aSMatthew G Knepley 
3400af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
34010f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
34020f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3403af122d2aSMatthew G Knepley     }
3404af122d2aSMatthew G Knepley   }
34051d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
34061d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
340788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
340888ed4aceSMatthew G Knepley }
340988ed4aceSMatthew G Knepley 
341088ed4aceSMatthew G Knepley #undef __FUNCT__
34119435951eSToby Isaac #define __FUNCT__ "DMGetDefaultConstraints"
34129435951eSToby Isaac /*@
34139435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
34149435951eSToby Isaac 
3415e228b242SToby Isaac   not collective
3416e228b242SToby Isaac 
34179435951eSToby Isaac   Input Parameter:
34189435951eSToby Isaac . dm - The DM
34199435951eSToby Isaac 
34209435951eSToby Isaac   Output Parameter:
34219435951eSToby 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.
34229435951eSToby 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.
34239435951eSToby Isaac 
34249435951eSToby Isaac   Level: advanced
34259435951eSToby Isaac 
34269435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
34279435951eSToby Isaac 
34289435951eSToby Isaac .seealso: DMSetDefaultConstraints()
34299435951eSToby Isaac @*/
34309435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
34319435951eSToby Isaac {
34329435951eSToby Isaac   PetscErrorCode ierr;
34339435951eSToby Isaac 
34349435951eSToby Isaac   PetscFunctionBegin;
34359435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
34369435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
343745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
343845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
34399435951eSToby Isaac   PetscFunctionReturn(0);
34409435951eSToby Isaac }
34419435951eSToby Isaac 
34429435951eSToby Isaac #undef __FUNCT__
34439435951eSToby Isaac #define __FUNCT__ "DMSetDefaultConstraints"
34449435951eSToby Isaac /*@
34459435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
34469435951eSToby Isaac 
34479435951eSToby 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().
34489435951eSToby Isaac 
34499435951eSToby 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.
34509435951eSToby Isaac 
3451e228b242SToby Isaac   collective on dm
3452e228b242SToby Isaac 
34539435951eSToby Isaac   Input Parameters:
34549435951eSToby Isaac + dm - The DM
3455e228b242SToby 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).
3456e228b242SToby 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).
34579435951eSToby Isaac 
34589435951eSToby Isaac   Level: advanced
34599435951eSToby Isaac 
34609435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
34619435951eSToby Isaac 
34629435951eSToby Isaac .seealso: DMGetDefaultConstraints()
34639435951eSToby Isaac @*/
34649435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
34659435951eSToby Isaac {
3466e228b242SToby Isaac   PetscMPIInt result;
34679435951eSToby Isaac   PetscErrorCode ierr;
34689435951eSToby Isaac 
34699435951eSToby Isaac   PetscFunctionBegin;
34709435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3471e228b242SToby Isaac   if (section) {
3472e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3473e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3474f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3475e228b242SToby Isaac   }
3476e228b242SToby Isaac   if (mat) {
3477e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3478e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3479f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3480e228b242SToby Isaac   }
34819435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
34829435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
34839435951eSToby Isaac   dm->defaultConstraintSection = section;
34849435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
34859435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
34869435951eSToby Isaac   dm->defaultConstraintMat = mat;
34879435951eSToby Isaac   PetscFunctionReturn(0);
34889435951eSToby Isaac }
34899435951eSToby Isaac 
3490f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
34919435951eSToby Isaac #undef __FUNCT__
3492284f5c8fSMatthew G. Knepley #define __FUNCT__ "DMDefaultSectionCheckConsistency_Internal"
3493507e4973SMatthew G. Knepley /*
3494507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3495507e4973SMatthew G. Knepley 
3496507e4973SMatthew G. Knepley   Input Parameters:
3497507e4973SMatthew G. Knepley + dm - The DM
3498507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3499507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3500507e4973SMatthew G. Knepley 
3501507e4973SMatthew G. Knepley   Level: intermediate
3502507e4973SMatthew G. Knepley 
3503507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3504507e4973SMatthew G. Knepley */
3505f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3506507e4973SMatthew G. Knepley {
3507507e4973SMatthew G. Knepley   MPI_Comm        comm;
3508507e4973SMatthew G. Knepley   PetscLayout     layout;
3509507e4973SMatthew G. Knepley   const PetscInt *ranges;
3510507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3511507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3512507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3513507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3514507e4973SMatthew G. Knepley 
3515507e4973SMatthew G. Knepley   PetscFunctionBegin;
3516507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3517507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3518507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3519507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3520507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3521507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3522507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3523507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3524507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3525507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3526507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3527507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3528f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3529507e4973SMatthew G. Knepley 
3530507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3531507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3532507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3533507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3534507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3535507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3536507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3537507e4973SMatthew 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;}
3538507e4973SMatthew 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;}
3539507e4973SMatthew G. Knepley     if (gdof < 0) {
3540507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3541507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3542507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3543507e4973SMatthew G. Knepley 
3544507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3545507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3546507e4973SMatthew 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;}
3547507e4973SMatthew G. Knepley       }
3548507e4973SMatthew G. Knepley     }
3549507e4973SMatthew G. Knepley   }
3550507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3551507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3552b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3553507e4973SMatthew G. Knepley   if (!gvalid) {
3554507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3555507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3556507e4973SMatthew G. Knepley   }
3557507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3558507e4973SMatthew G. Knepley }
3559f741bcd2SMatthew G. Knepley #endif
3560507e4973SMatthew G. Knepley 
3561507e4973SMatthew G. Knepley #undef __FUNCT__
356288ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultGlobalSection"
356388ed4aceSMatthew G Knepley /*@
356488ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
356588ed4aceSMatthew G Knepley 
35668b1ab98fSJed Brown   Collective on DM
35678b1ab98fSJed Brown 
356888ed4aceSMatthew G Knepley   Input Parameter:
356988ed4aceSMatthew G Knepley . dm - The DM
357088ed4aceSMatthew G Knepley 
357188ed4aceSMatthew G Knepley   Output Parameter:
357288ed4aceSMatthew G Knepley . section - The PetscSection
357388ed4aceSMatthew G Knepley 
357488ed4aceSMatthew G Knepley   Level: intermediate
357588ed4aceSMatthew G Knepley 
357688ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
357788ed4aceSMatthew G Knepley 
357888ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
357988ed4aceSMatthew G Knepley @*/
35800adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
35810adebc6cSBarry Smith {
358288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
358388ed4aceSMatthew G Knepley 
358488ed4aceSMatthew G Knepley   PetscFunctionBegin;
358588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
358688ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
358788ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3588fd59a867SMatthew G. Knepley     PetscSection s;
3589fd59a867SMatthew G. Knepley 
3590fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3591fd59a867SMatthew 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");
3592fd59a867SMatthew 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");
359315b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3594cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3595ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3596685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
359788ed4aceSMatthew G Knepley   }
359888ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
359988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
360088ed4aceSMatthew G Knepley }
360188ed4aceSMatthew G Knepley 
360288ed4aceSMatthew G Knepley #undef __FUNCT__
3603b21d0597SMatthew G Knepley #define __FUNCT__ "DMSetDefaultGlobalSection"
3604b21d0597SMatthew G Knepley /*@
3605b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3606b21d0597SMatthew G Knepley 
3607b21d0597SMatthew G Knepley   Input Parameters:
3608b21d0597SMatthew G Knepley + dm - The DM
36095080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3610b21d0597SMatthew G Knepley 
3611b21d0597SMatthew G Knepley   Level: intermediate
3612b21d0597SMatthew G Knepley 
3613b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3614b21d0597SMatthew G Knepley 
3615b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3616b21d0597SMatthew G Knepley @*/
36170adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
36180adebc6cSBarry Smith {
3619b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3620b21d0597SMatthew G Knepley 
3621b21d0597SMatthew G Knepley   PetscFunctionBegin;
3622b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36235080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36241d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3625b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3626b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3627507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3628f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3629507e4973SMatthew G. Knepley #endif
3630b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3631b21d0597SMatthew G Knepley }
3632b21d0597SMatthew G Knepley 
3633b21d0597SMatthew G Knepley #undef __FUNCT__
363488ed4aceSMatthew G Knepley #define __FUNCT__ "DMGetDefaultSF"
363588ed4aceSMatthew G Knepley /*@
363688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
363788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
363888ed4aceSMatthew G Knepley 
363988ed4aceSMatthew G Knepley   Input Parameter:
364088ed4aceSMatthew G Knepley . dm - The DM
364188ed4aceSMatthew G Knepley 
364288ed4aceSMatthew G Knepley   Output Parameter:
364388ed4aceSMatthew G Knepley . sf - The PetscSF
364488ed4aceSMatthew G Knepley 
364588ed4aceSMatthew G Knepley   Level: intermediate
364688ed4aceSMatthew G Knepley 
364788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
364888ed4aceSMatthew G Knepley 
364988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
365088ed4aceSMatthew G Knepley @*/
36510adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
36520adebc6cSBarry Smith {
365388ed4aceSMatthew G Knepley   PetscInt       nroots;
365488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
365588ed4aceSMatthew G Knepley 
365688ed4aceSMatthew G Knepley   PetscFunctionBegin;
365788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
365888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
36590298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
366088ed4aceSMatthew G Knepley   if (nroots < 0) {
366188ed4aceSMatthew G Knepley     PetscSection section, gSection;
366288ed4aceSMatthew G Knepley 
366388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
366431ea6d37SMatthew G Knepley     if (section) {
366588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
366688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
366731ea6d37SMatthew G Knepley     } else {
36680298fd71SBarry Smith       *sf = NULL;
366931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
367031ea6d37SMatthew G Knepley     }
367188ed4aceSMatthew G Knepley   }
367288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
367388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
367488ed4aceSMatthew G Knepley }
367588ed4aceSMatthew G Knepley 
367688ed4aceSMatthew G Knepley #undef __FUNCT__
367788ed4aceSMatthew G Knepley #define __FUNCT__ "DMSetDefaultSF"
367888ed4aceSMatthew G Knepley /*@
367988ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
368088ed4aceSMatthew G Knepley 
368188ed4aceSMatthew G Knepley   Input Parameters:
368288ed4aceSMatthew G Knepley + dm - The DM
368388ed4aceSMatthew G Knepley - sf - The PetscSF
368488ed4aceSMatthew G Knepley 
368588ed4aceSMatthew G Knepley   Level: intermediate
368688ed4aceSMatthew G Knepley 
368788ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
368888ed4aceSMatthew G Knepley 
368988ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
369088ed4aceSMatthew G Knepley @*/
36910adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
36920adebc6cSBarry Smith {
369388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
369488ed4aceSMatthew G Knepley 
369588ed4aceSMatthew G Knepley   PetscFunctionBegin;
369688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
369788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
369888ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
369988ed4aceSMatthew G Knepley   dm->defaultSF = sf;
370088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
370188ed4aceSMatthew G Knepley }
370288ed4aceSMatthew G Knepley 
370388ed4aceSMatthew G Knepley #undef __FUNCT__
370488ed4aceSMatthew G Knepley #define __FUNCT__ "DMCreateDefaultSF"
370588ed4aceSMatthew G Knepley /*@C
370688ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
370788ed4aceSMatthew G Knepley   describing the data layout.
370888ed4aceSMatthew G Knepley 
370988ed4aceSMatthew G Knepley   Input Parameters:
371088ed4aceSMatthew G Knepley + dm - The DM
371188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
371288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
371388ed4aceSMatthew G Knepley 
371488ed4aceSMatthew G Knepley   Level: intermediate
371588ed4aceSMatthew G Knepley 
371688ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
371788ed4aceSMatthew G Knepley @*/
371888ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
371988ed4aceSMatthew G Knepley {
372082f516ccSBarry Smith   MPI_Comm       comm;
372188ed4aceSMatthew G Knepley   PetscLayout    layout;
372288ed4aceSMatthew G Knepley   const PetscInt *ranges;
372388ed4aceSMatthew G Knepley   PetscInt       *local;
372488ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3725ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
372688ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
372788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
372888ed4aceSMatthew G Knepley 
372988ed4aceSMatthew G Knepley   PetscFunctionBegin;
373082f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
373188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
373288ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
373388ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
373488ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
373588ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
373688ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
373788ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
373888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
373988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
374088ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3741ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
37426636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
374388ed4aceSMatthew G Knepley 
37446636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37456636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3746235fbf56SMatthew 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));
37476636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
374888ed4aceSMatthew G Knepley   }
3749785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3750785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
375188ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
37521f588964SMatthew G Knepley     const PetscInt *cind;
37536636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
375488ed4aceSMatthew G Knepley 
375588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
375688ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
375788ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
375888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
375988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
37606636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
376188ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
37626636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
37636636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
37646636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3765057b4bcdSMatthew 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);
37666636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
37676636e97aSMatthew G Knepley     }
376888ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
376988ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
377088ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
377188ed4aceSMatthew G Knepley     }
377288ed4aceSMatthew G Knepley     if (gdof < 0) {
37736636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
377488ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
377588ed4aceSMatthew G Knepley 
377605376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
377731d3f06eSJed Brown         if (r < 0) r = -(r+2);
377805376888SMatthew 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);
377988ed4aceSMatthew G Knepley         remote[l].rank  = r;
378088ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
378188ed4aceSMatthew G Knepley       }
378288ed4aceSMatthew G Knepley     } else {
37836636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
378488ed4aceSMatthew G Knepley         remote[l].rank  = rank;
378588ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
378688ed4aceSMatthew G Knepley       }
378788ed4aceSMatthew G Knepley     }
378888ed4aceSMatthew G Knepley   }
37896636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
379088ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
379188ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
379288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
379388ed4aceSMatthew G Knepley }
3794af122d2aSMatthew G Knepley 
3795af122d2aSMatthew G Knepley #undef __FUNCT__
3796b21d0597SMatthew G Knepley #define __FUNCT__ "DMGetPointSF"
3797b21d0597SMatthew G Knepley /*@
3798b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3799b21d0597SMatthew G Knepley 
3800b21d0597SMatthew G Knepley   Input Parameter:
3801b21d0597SMatthew G Knepley . dm - The DM
3802b21d0597SMatthew G Knepley 
3803b21d0597SMatthew G Knepley   Output Parameter:
3804b21d0597SMatthew G Knepley . sf - The PetscSF
3805b21d0597SMatthew G Knepley 
3806b21d0597SMatthew G Knepley   Level: intermediate
3807b21d0597SMatthew G Knepley 
3808b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3809b21d0597SMatthew G Knepley 
3810057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3811b21d0597SMatthew G Knepley @*/
38120adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
38130adebc6cSBarry Smith {
3814b21d0597SMatthew G Knepley   PetscFunctionBegin;
3815b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3816b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3817b21d0597SMatthew G Knepley   *sf = dm->sf;
3818b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3819b21d0597SMatthew G Knepley }
3820b21d0597SMatthew G Knepley 
3821b21d0597SMatthew G Knepley #undef __FUNCT__
3822057b4bcdSMatthew G Knepley #define __FUNCT__ "DMSetPointSF"
3823057b4bcdSMatthew G Knepley /*@
3824057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3825057b4bcdSMatthew G Knepley 
3826057b4bcdSMatthew G Knepley   Input Parameters:
3827057b4bcdSMatthew G Knepley + dm - The DM
3828057b4bcdSMatthew G Knepley - sf - The PetscSF
3829057b4bcdSMatthew G Knepley 
3830057b4bcdSMatthew G Knepley   Level: intermediate
3831057b4bcdSMatthew G Knepley 
3832057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3833057b4bcdSMatthew G Knepley @*/
38340adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
38350adebc6cSBarry Smith {
3836057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3837057b4bcdSMatthew G Knepley 
3838057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3839057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3840057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3841057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3842057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3843057b4bcdSMatthew G Knepley   dm->sf = sf;
3844057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3845057b4bcdSMatthew G Knepley }
3846057b4bcdSMatthew G Knepley 
3847057b4bcdSMatthew G Knepley #undef __FUNCT__
38482764a2aaSMatthew G. Knepley #define __FUNCT__ "DMGetDS"
38492764a2aaSMatthew G. Knepley /*@
38502764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
38512764a2aaSMatthew G. Knepley 
38522764a2aaSMatthew G. Knepley   Input Parameter:
38532764a2aaSMatthew G. Knepley . dm - The DM
38542764a2aaSMatthew G. Knepley 
38552764a2aaSMatthew G. Knepley   Output Parameter:
38562764a2aaSMatthew G. Knepley . prob - The PetscDS
38572764a2aaSMatthew G. Knepley 
38582764a2aaSMatthew G. Knepley   Level: developer
38592764a2aaSMatthew G. Knepley 
38602764a2aaSMatthew G. Knepley .seealso: DMSetDS()
38612764a2aaSMatthew G. Knepley @*/
38622764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
3863af122d2aSMatthew G Knepley {
3864af122d2aSMatthew G Knepley   PetscFunctionBegin;
3865af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38660f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
38670f21e855SMatthew G. Knepley   *prob = dm->prob;
38680f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38690f21e855SMatthew G. Knepley }
38700f21e855SMatthew G. Knepley 
38710f21e855SMatthew G. Knepley #undef __FUNCT__
38722764a2aaSMatthew G. Knepley #define __FUNCT__ "DMSetDS"
38732764a2aaSMatthew G. Knepley /*@
38742764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
38752764a2aaSMatthew G. Knepley 
38762764a2aaSMatthew G. Knepley   Input Parameters:
38772764a2aaSMatthew G. Knepley + dm - The DM
38782764a2aaSMatthew G. Knepley - prob - The PetscDS
38792764a2aaSMatthew G. Knepley 
38802764a2aaSMatthew G. Knepley   Level: developer
38812764a2aaSMatthew G. Knepley 
38822764a2aaSMatthew G. Knepley .seealso: DMGetDS()
38832764a2aaSMatthew G. Knepley @*/
38842764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
38850f21e855SMatthew G. Knepley {
38860f21e855SMatthew G. Knepley   PetscErrorCode ierr;
38870f21e855SMatthew G. Knepley 
38880f21e855SMatthew G. Knepley   PetscFunctionBegin;
38890f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38902764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
389137316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
38922764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
38930f21e855SMatthew G. Knepley   dm->prob = prob;
38940f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
38950f21e855SMatthew G. Knepley }
38960f21e855SMatthew G. Knepley 
38970f21e855SMatthew G. Knepley #undef __FUNCT__
38980f21e855SMatthew G. Knepley #define __FUNCT__ "DMGetNumFields"
38990f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
39000f21e855SMatthew G. Knepley {
39010f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39020f21e855SMatthew G. Knepley 
39030f21e855SMatthew G. Knepley   PetscFunctionBegin;
39040f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39052764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
3906af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3907af122d2aSMatthew G Knepley }
3908af122d2aSMatthew G Knepley 
3909af122d2aSMatthew G Knepley #undef __FUNCT__
3910af122d2aSMatthew G Knepley #define __FUNCT__ "DMSetNumFields"
3911af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
3912af122d2aSMatthew G Knepley {
39130f21e855SMatthew G. Knepley   PetscInt       Nf, f;
3914af122d2aSMatthew G Knepley   PetscErrorCode ierr;
3915af122d2aSMatthew G Knepley 
3916af122d2aSMatthew G Knepley   PetscFunctionBegin;
3917af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39182764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
39190f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
39200f21e855SMatthew G. Knepley     PetscContainer obj;
39210f21e855SMatthew G. Knepley 
39220f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
39232764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
39240f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
3925af122d2aSMatthew G Knepley   }
3926af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3927af122d2aSMatthew G Knepley }
3928af122d2aSMatthew G Knepley 
3929af122d2aSMatthew G Knepley #undef __FUNCT__
3930af122d2aSMatthew G Knepley #define __FUNCT__ "DMGetField"
3931c1929be8SMatthew G. Knepley /*@
3932c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
3933c1929be8SMatthew G. Knepley 
3934c1929be8SMatthew G. Knepley   Not collective
3935c1929be8SMatthew G. Knepley 
3936c1929be8SMatthew G. Knepley   Input Parameters:
3937c1929be8SMatthew G. Knepley + dm - The DM
3938c1929be8SMatthew G. Knepley - f  - The field number
3939c1929be8SMatthew G. Knepley 
3940c1929be8SMatthew G. Knepley   Output Parameter:
3941c1929be8SMatthew G. Knepley . field - The discretization object
3942c1929be8SMatthew G. Knepley 
3943c1929be8SMatthew G. Knepley   Level: developer
3944c1929be8SMatthew G. Knepley 
3945c1929be8SMatthew G. Knepley .seealso: DMSetField()
3946c1929be8SMatthew G. Knepley @*/
3947af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
3948af122d2aSMatthew G Knepley {
39490f21e855SMatthew G. Knepley   PetscErrorCode ierr;
39500f21e855SMatthew G. Knepley 
3951af122d2aSMatthew G Knepley   PetscFunctionBegin;
3952af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39532764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3954decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
3955decb47aaSMatthew G. Knepley }
3956decb47aaSMatthew G. Knepley 
3957decb47aaSMatthew G. Knepley #undef __FUNCT__
3958decb47aaSMatthew G. Knepley #define __FUNCT__ "DMSetField"
3959c1929be8SMatthew G. Knepley /*@
3960c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
3961c1929be8SMatthew G. Knepley 
3962c1929be8SMatthew G. Knepley   Logically collective on DM
3963c1929be8SMatthew G. Knepley 
3964c1929be8SMatthew G. Knepley   Input Parameters:
3965c1929be8SMatthew G. Knepley + dm - The DM
3966c1929be8SMatthew G. Knepley . f  - The field number
3967c1929be8SMatthew G. Knepley - field - The discretization object
3968c1929be8SMatthew G. Knepley 
3969c1929be8SMatthew G. Knepley   Level: developer
3970c1929be8SMatthew G. Knepley 
3971c1929be8SMatthew G. Knepley .seealso: DMGetField()
3972c1929be8SMatthew G. Knepley @*/
3973decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
3974decb47aaSMatthew G. Knepley {
3975decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
3976decb47aaSMatthew G. Knepley 
3977decb47aaSMatthew G. Knepley   PetscFunctionBegin;
3978decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39792764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
3980af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
3981af122d2aSMatthew G Knepley }
39826636e97aSMatthew G Knepley 
39836636e97aSMatthew G Knepley #undef __FUNCT__
3984b64e0483SPeter Brune #define __FUNCT__ "DMRestrictHook_Coordinates"
3985b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
3986b64e0483SPeter Brune {
3987b64e0483SPeter Brune   DM dm_coord,dmc_coord;
3988b64e0483SPeter Brune   PetscErrorCode ierr;
3989b64e0483SPeter Brune   Vec coords,ccoords;
39906dbf9973SLawrence Mitchell   Mat inject;
3991b64e0483SPeter Brune   PetscFunctionBegin;
3992b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
3993b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
3994b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
3995b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
3996b64e0483SPeter Brune   if (coords && !ccoords) {
3997b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
39986668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
39996dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
40002adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
40016dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4002b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4003b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4004b64e0483SPeter Brune   }
4005b64e0483SPeter Brune   PetscFunctionReturn(0);
4006b64e0483SPeter Brune }
4007b64e0483SPeter Brune 
4008b64e0483SPeter Brune #undef __FUNCT__
400903dadc2fSPeter Brune #define __FUNCT__ "DMSubDomainHook_Coordinates"
401003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
401103dadc2fSPeter Brune {
401203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
401303dadc2fSPeter Brune   PetscErrorCode ierr;
401403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
401503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
401603dadc2fSPeter Brune   PetscFunctionBegin;
401703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
401803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
401903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
402003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
402103dadc2fSPeter Brune   if (coords && !ccoords) {
402203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
40236668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
402403dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
402524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
402603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
402703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
402803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
402903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
403003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
403103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
403203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
403303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
403403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
403503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
403603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
403703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
403803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
403903dadc2fSPeter Brune   }
404003dadc2fSPeter Brune   PetscFunctionReturn(0);
404103dadc2fSPeter Brune }
404203dadc2fSPeter Brune 
404303dadc2fSPeter Brune #undef __FUNCT__
4044c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMGetDimension"
4045c73cfb54SMatthew G. Knepley /*@
4046c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4047c73cfb54SMatthew G. Knepley 
4048c73cfb54SMatthew G. Knepley   Not collective
4049c73cfb54SMatthew G. Knepley 
4050c73cfb54SMatthew G. Knepley   Input Parameter:
4051c73cfb54SMatthew G. Knepley . dm - The DM
4052c73cfb54SMatthew G. Knepley 
4053c73cfb54SMatthew G. Knepley   Output Parameter:
4054c73cfb54SMatthew G. Knepley . dim - The topological dimension
4055c73cfb54SMatthew G. Knepley 
4056c73cfb54SMatthew G. Knepley   Level: beginner
4057c73cfb54SMatthew G. Knepley 
4058c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4059c73cfb54SMatthew G. Knepley @*/
4060c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4061c73cfb54SMatthew G. Knepley {
4062c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4063c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4064c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4065c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4066c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4067c73cfb54SMatthew G. Knepley }
4068c73cfb54SMatthew G. Knepley 
4069c73cfb54SMatthew G. Knepley #undef __FUNCT__
4070c73cfb54SMatthew G. Knepley #define __FUNCT__ "DMSetDimension"
4071c73cfb54SMatthew G. Knepley /*@
4072c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4073c73cfb54SMatthew G. Knepley 
4074c73cfb54SMatthew G. Knepley   Collective on dm
4075c73cfb54SMatthew G. Knepley 
4076c73cfb54SMatthew G. Knepley   Input Parameters:
4077c73cfb54SMatthew G. Knepley + dm - The DM
4078c73cfb54SMatthew G. Knepley - dim - The topological dimension
4079c73cfb54SMatthew G. Knepley 
4080c73cfb54SMatthew G. Knepley   Level: beginner
4081c73cfb54SMatthew G. Knepley 
4082c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4083c73cfb54SMatthew G. Knepley @*/
4084c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4085c73cfb54SMatthew G. Knepley {
4086c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4087c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4088c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4089c73cfb54SMatthew G. Knepley   dm->dim = dim;
4090c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4091c73cfb54SMatthew G. Knepley }
4092c73cfb54SMatthew G. Knepley 
4093793f3fe5SMatthew G. Knepley #undef __FUNCT__
4094793f3fe5SMatthew G. Knepley #define __FUNCT__ "DMGetDimPoints"
4095793f3fe5SMatthew G. Knepley /*@
4096793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4097793f3fe5SMatthew G. Knepley 
4098793f3fe5SMatthew G. Knepley   Collective on DM
4099793f3fe5SMatthew G. Knepley 
4100793f3fe5SMatthew G. Knepley   Input Parameters:
4101793f3fe5SMatthew G. Knepley + dm - the DM
4102793f3fe5SMatthew G. Knepley - dim - the dimension
4103793f3fe5SMatthew G. Knepley 
4104793f3fe5SMatthew G. Knepley   Output Parameters:
4105793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4106793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4107793f3fe5SMatthew G. Knepley 
4108793f3fe5SMatthew G. Knepley   Note:
4109793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4110793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4111793f3fe5SMatthew G. Knepley   then the interval is empty.
4112793f3fe5SMatthew G. Knepley 
4113793f3fe5SMatthew G. Knepley   Level: intermediate
4114793f3fe5SMatthew G. Knepley 
4115793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4116793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4117793f3fe5SMatthew G. Knepley @*/
4118793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4119793f3fe5SMatthew G. Knepley {
4120793f3fe5SMatthew G. Knepley   PetscInt       d;
4121793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4122793f3fe5SMatthew G. Knepley 
4123793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4124793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4125793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4126793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4127793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4128793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4129793f3fe5SMatthew G. Knepley }
4130793f3fe5SMatthew G. Knepley 
4131793f3fe5SMatthew G. Knepley #undef __FUNCT__
41326636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinates"
41336636e97aSMatthew G Knepley /*@
41346636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
41356636e97aSMatthew G Knepley 
41366636e97aSMatthew G Knepley   Collective on DM
41376636e97aSMatthew G Knepley 
41386636e97aSMatthew G Knepley   Input Parameters:
41396636e97aSMatthew G Knepley + dm - the DM
41406636e97aSMatthew G Knepley - c - coordinate vector
41416636e97aSMatthew G Knepley 
41426636e97aSMatthew G Knepley   Note:
41436636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
41446636e97aSMatthew G Knepley 
41456636e97aSMatthew G Knepley   Level: intermediate
41466636e97aSMatthew G Knepley 
41476636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41486636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
41496636e97aSMatthew G Knepley @*/
41506636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
41516636e97aSMatthew G Knepley {
41526636e97aSMatthew G Knepley   PetscErrorCode ierr;
41536636e97aSMatthew G Knepley 
41546636e97aSMatthew G Knepley   PetscFunctionBegin;
41556636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41566636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41576636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41586636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
41596636e97aSMatthew G Knepley   dm->coordinates = c;
41606636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4161b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
416203dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
41636636e97aSMatthew G Knepley   PetscFunctionReturn(0);
41646636e97aSMatthew G Knepley }
41656636e97aSMatthew G Knepley 
41666636e97aSMatthew G Knepley #undef __FUNCT__
41676636e97aSMatthew G Knepley #define __FUNCT__ "DMSetCoordinatesLocal"
41686636e97aSMatthew G Knepley /*@
41696636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
41706636e97aSMatthew G Knepley 
41716636e97aSMatthew G Knepley   Collective on DM
41726636e97aSMatthew G Knepley 
41736636e97aSMatthew G Knepley    Input Parameters:
41746636e97aSMatthew G Knepley +  dm - the DM
41756636e97aSMatthew G Knepley -  c - coordinate vector
41766636e97aSMatthew G Knepley 
41776636e97aSMatthew G Knepley   Note:
41786636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
41796636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
41806636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
41816636e97aSMatthew G Knepley 
41826636e97aSMatthew G Knepley   Level: intermediate
41836636e97aSMatthew G Knepley 
41846636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
41856636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
41866636e97aSMatthew G Knepley @*/
41876636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
41886636e97aSMatthew G Knepley {
41896636e97aSMatthew G Knepley   PetscErrorCode ierr;
41906636e97aSMatthew G Knepley 
41916636e97aSMatthew G Knepley   PetscFunctionBegin;
41926636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
41936636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
41946636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
41956636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
41968865f1eaSKarl Rupp 
41976636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
41988865f1eaSKarl Rupp 
41996636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42006636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42016636e97aSMatthew G Knepley }
42026636e97aSMatthew G Knepley 
42036636e97aSMatthew G Knepley #undef __FUNCT__
42046636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinates"
42056636e97aSMatthew G Knepley /*@
42066636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
42076636e97aSMatthew G Knepley 
42086636e97aSMatthew G Knepley   Not Collective
42096636e97aSMatthew G Knepley 
42106636e97aSMatthew G Knepley   Input Parameter:
42116636e97aSMatthew G Knepley . dm - the DM
42126636e97aSMatthew G Knepley 
42136636e97aSMatthew G Knepley   Output Parameter:
42146636e97aSMatthew G Knepley . c - global coordinate vector
42156636e97aSMatthew G Knepley 
42166636e97aSMatthew G Knepley   Note:
42176636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42186636e97aSMatthew G Knepley 
42196636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
42206636e97aSMatthew G Knepley 
42216636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42226636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42236636e97aSMatthew G Knepley 
42246636e97aSMatthew G Knepley   Level: intermediate
42256636e97aSMatthew G Knepley 
42266636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42276636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
42286636e97aSMatthew G Knepley @*/
42296636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
42306636e97aSMatthew G Knepley {
42316636e97aSMatthew G Knepley   PetscErrorCode ierr;
42326636e97aSMatthew G Knepley 
42336636e97aSMatthew G Knepley   PetscFunctionBegin;
42346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42356636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42361f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
42370298fd71SBarry Smith     DM cdm = NULL;
42386636e97aSMatthew G Knepley 
42396636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42406636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
42416636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
42426636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42436636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
42446636e97aSMatthew G Knepley   }
42456636e97aSMatthew G Knepley   *c = dm->coordinates;
42466636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42476636e97aSMatthew G Knepley }
42486636e97aSMatthew G Knepley 
42496636e97aSMatthew G Knepley #undef __FUNCT__
42506636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinatesLocal"
42516636e97aSMatthew G Knepley /*@
42526636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
42536636e97aSMatthew G Knepley 
42546636e97aSMatthew G Knepley   Collective on DM
42556636e97aSMatthew G Knepley 
42566636e97aSMatthew G Knepley   Input Parameter:
42576636e97aSMatthew G Knepley . dm - the DM
42586636e97aSMatthew G Knepley 
42596636e97aSMatthew G Knepley   Output Parameter:
42606636e97aSMatthew G Knepley . c - coordinate vector
42616636e97aSMatthew G Knepley 
42626636e97aSMatthew G Knepley   Note:
42636636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
42646636e97aSMatthew G Knepley 
42656636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
42666636e97aSMatthew G Knepley 
42676636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
42686636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
42696636e97aSMatthew G Knepley 
42706636e97aSMatthew G Knepley   Level: intermediate
42716636e97aSMatthew G Knepley 
42726636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42736636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
42746636e97aSMatthew G Knepley @*/
42756636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
42766636e97aSMatthew G Knepley {
42776636e97aSMatthew G Knepley   PetscErrorCode ierr;
42786636e97aSMatthew G Knepley 
42796636e97aSMatthew G Knepley   PetscFunctionBegin;
42806636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42816636e97aSMatthew G Knepley   PetscValidPointer(c,2);
42821f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
42830298fd71SBarry Smith     DM cdm = NULL;
42846636e97aSMatthew G Knepley 
42856636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
42866636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
42876636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
42886636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42896636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
42906636e97aSMatthew G Knepley   }
42916636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
42926636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42936636e97aSMatthew G Knepley }
42946636e97aSMatthew G Knepley 
42956636e97aSMatthew G Knepley #undef __FUNCT__
42966636e97aSMatthew G Knepley #define __FUNCT__ "DMGetCoordinateDM"
42976636e97aSMatthew G Knepley /*@
42981cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
42996636e97aSMatthew G Knepley 
43006636e97aSMatthew G Knepley   Collective on DM
43016636e97aSMatthew G Knepley 
43026636e97aSMatthew G Knepley   Input Parameter:
43036636e97aSMatthew G Knepley . dm - the DM
43046636e97aSMatthew G Knepley 
43056636e97aSMatthew G Knepley   Output Parameter:
43066636e97aSMatthew G Knepley . cdm - coordinate DM
43076636e97aSMatthew G Knepley 
43086636e97aSMatthew G Knepley   Level: intermediate
43096636e97aSMatthew G Knepley 
43106636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43111cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43126636e97aSMatthew G Knepley @*/
43136636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
43146636e97aSMatthew G Knepley {
43156636e97aSMatthew G Knepley   PetscErrorCode ierr;
43166636e97aSMatthew G Knepley 
43176636e97aSMatthew G Knepley   PetscFunctionBegin;
43186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43196636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
43206636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
432182f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
43226636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
43236636e97aSMatthew G Knepley   }
43246636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
43256636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43266636e97aSMatthew G Knepley }
4327e87bb0d3SMatthew G Knepley 
4328e87bb0d3SMatthew G Knepley #undef __FUNCT__
43291cfe2091SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDM"
43301cfe2091SMatthew G. Knepley /*@
43311cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
43321cfe2091SMatthew G. Knepley 
43331cfe2091SMatthew G. Knepley   Logically Collective on DM
43341cfe2091SMatthew G. Knepley 
43351cfe2091SMatthew G. Knepley   Input Parameters:
43361cfe2091SMatthew G. Knepley + dm - the DM
43371cfe2091SMatthew G. Knepley - cdm - coordinate DM
43381cfe2091SMatthew G. Knepley 
43391cfe2091SMatthew G. Knepley   Level: intermediate
43401cfe2091SMatthew G. Knepley 
43411cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43421cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
43431cfe2091SMatthew G. Knepley @*/
43441cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
43451cfe2091SMatthew G. Knepley {
43461cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
43471cfe2091SMatthew G. Knepley 
43481cfe2091SMatthew G. Knepley   PetscFunctionBegin;
43491cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43501cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4351f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
43521cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
43531cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
43541cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
43551cfe2091SMatthew G. Knepley }
43561cfe2091SMatthew G. Knepley 
43571cfe2091SMatthew G. Knepley #undef __FUNCT__
435846e270d4SMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateDim"
435946e270d4SMatthew G. Knepley /*@
436046e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
436146e270d4SMatthew G. Knepley 
436246e270d4SMatthew G. Knepley   Not Collective
436346e270d4SMatthew G. Knepley 
436446e270d4SMatthew G. Knepley   Input Parameter:
436546e270d4SMatthew G. Knepley . dm - The DM object
436646e270d4SMatthew G. Knepley 
436746e270d4SMatthew G. Knepley   Output Parameter:
436846e270d4SMatthew G. Knepley . dim - The embedding dimension
436946e270d4SMatthew G. Knepley 
437046e270d4SMatthew G. Knepley   Level: intermediate
437146e270d4SMatthew G. Knepley 
437246e270d4SMatthew G. Knepley .keywords: mesh, coordinates
437346e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
437446e270d4SMatthew G. Knepley @*/
437546e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
437646e270d4SMatthew G. Knepley {
437746e270d4SMatthew G. Knepley   PetscFunctionBegin;
437846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
437946e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
43809a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
43819a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
43829a9a41abSToby Isaac   }
438346e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
438446e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
438546e270d4SMatthew G. Knepley }
438646e270d4SMatthew G. Knepley 
438746e270d4SMatthew G. Knepley #undef __FUNCT__
438846e270d4SMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateDim"
438946e270d4SMatthew G. Knepley /*@
439046e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
439146e270d4SMatthew G. Knepley 
439246e270d4SMatthew G. Knepley   Not Collective
439346e270d4SMatthew G. Knepley 
439446e270d4SMatthew G. Knepley   Input Parameters:
439546e270d4SMatthew G. Knepley + dm  - The DM object
439646e270d4SMatthew G. Knepley - dim - The embedding dimension
439746e270d4SMatthew G. Knepley 
439846e270d4SMatthew G. Knepley   Level: intermediate
439946e270d4SMatthew G. Knepley 
440046e270d4SMatthew G. Knepley .keywords: mesh, coordinates
440146e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
440246e270d4SMatthew G. Knepley @*/
440346e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
440446e270d4SMatthew G. Knepley {
440546e270d4SMatthew G. Knepley   PetscFunctionBegin;
440646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
440746e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
440846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
440946e270d4SMatthew G. Knepley }
441046e270d4SMatthew G. Knepley 
441146e270d4SMatthew G. Knepley #undef __FUNCT__
4412e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMGetCoordinateSection"
4413e8abe2deSMatthew G. Knepley /*@
4414e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4415e8abe2deSMatthew G. Knepley 
4416e8abe2deSMatthew G. Knepley   Not Collective
4417e8abe2deSMatthew G. Knepley 
4418e8abe2deSMatthew G. Knepley   Input Parameter:
4419e8abe2deSMatthew G. Knepley . dm - The DM object
4420e8abe2deSMatthew G. Knepley 
4421e8abe2deSMatthew G. Knepley   Output Parameter:
4422e8abe2deSMatthew G. Knepley . section - The PetscSection object
4423e8abe2deSMatthew G. Knepley 
4424e8abe2deSMatthew G. Knepley   Level: intermediate
4425e8abe2deSMatthew G. Knepley 
4426e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4427e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4428e8abe2deSMatthew G. Knepley @*/
4429e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4430e8abe2deSMatthew G. Knepley {
4431e8abe2deSMatthew G. Knepley   DM             cdm;
4432e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4433e8abe2deSMatthew G. Knepley 
4434e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4435e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4436e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4437e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4438e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4439e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4440e8abe2deSMatthew G. Knepley }
4441e8abe2deSMatthew G. Knepley 
4442e8abe2deSMatthew G. Knepley #undef __FUNCT__
4443e8abe2deSMatthew G. Knepley #define __FUNCT__ "DMSetCoordinateSection"
4444e8abe2deSMatthew G. Knepley /*@
4445e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4446e8abe2deSMatthew G. Knepley 
4447e8abe2deSMatthew G. Knepley   Not Collective
4448e8abe2deSMatthew G. Knepley 
4449e8abe2deSMatthew G. Knepley   Input Parameters:
4450e8abe2deSMatthew G. Knepley + dm      - The DM object
445146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4452e8abe2deSMatthew G. Knepley - section - The PetscSection object
4453e8abe2deSMatthew G. Knepley 
4454e8abe2deSMatthew G. Knepley   Level: intermediate
4455e8abe2deSMatthew G. Knepley 
4456e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4457e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4458e8abe2deSMatthew G. Knepley @*/
445946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4460e8abe2deSMatthew G. Knepley {
4461e8abe2deSMatthew G. Knepley   DM             cdm;
4462e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4463e8abe2deSMatthew G. Knepley 
4464e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4465e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
446646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4467e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4468e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
446946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
447046e270d4SMatthew G. Knepley     PetscInt d = dim;
447146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
447246e270d4SMatthew G. Knepley 
447346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
447446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
447546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
447646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
447746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
447846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
447946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
448046e270d4SMatthew G. Knepley     }
44816be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
448246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
448346e270d4SMatthew G. Knepley   }
4484e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4485e8abe2deSMatthew G. Knepley }
4486e8abe2deSMatthew G. Knepley 
4487e8abe2deSMatthew G. Knepley #undef __FUNCT__
4488c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMGetPeriodicity"
44895dc8c3f7SMatthew G. Knepley /*@C
44905dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
44915dc8c3f7SMatthew G. Knepley 
44925dc8c3f7SMatthew G. Knepley   Input Parameters:
44935dc8c3f7SMatthew G. Knepley + dm      - The DM object
44945dc8c3f7SMatthew 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
44955dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
44965dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
44975dc8c3f7SMatthew G. Knepley 
44985dc8c3f7SMatthew G. Knepley   Level: developer
44995dc8c3f7SMatthew G. Knepley 
45005dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45015dc8c3f7SMatthew G. Knepley @*/
45025dc8c3f7SMatthew G. Knepley PetscErrorCode DMGetPeriodicity(DM dm, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4503c6b900c6SMatthew G. Knepley {
4504c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4505c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4506c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4507c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
45085dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4509c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4510c6b900c6SMatthew G. Knepley }
4511c6b900c6SMatthew G. Knepley 
4512c6b900c6SMatthew G. Knepley #undef __FUNCT__
4513c6b900c6SMatthew G. Knepley #define __FUNCT__ "DMSetPeriodicity"
45145dc8c3f7SMatthew G. Knepley /*@C
45155dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
45165dc8c3f7SMatthew G. Knepley 
45175dc8c3f7SMatthew G. Knepley   Input Parameters:
45185dc8c3f7SMatthew G. Knepley + dm      - The DM object
45195dc8c3f7SMatthew 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
45205dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
45215dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
45225dc8c3f7SMatthew G. Knepley 
45235dc8c3f7SMatthew G. Knepley   Level: developer
45245dc8c3f7SMatthew G. Knepley 
45255dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
45265dc8c3f7SMatthew G. Knepley @*/
45275dc8c3f7SMatthew G. Knepley PetscErrorCode DMSetPeriodicity(DM dm, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4528c6b900c6SMatthew G. Knepley {
4529c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4530c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4531c6b900c6SMatthew G. Knepley 
4532c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4533c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45345dc8c3f7SMatthew G. Knepley   PetscValidPointer(L,3);PetscValidPointer(maxCell,2);PetscValidPointer(bd,4);
45355dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
45365dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
45375dc8c3f7SMatthew G. Knepley   ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
45385dc8c3f7SMatthew G. Knepley   for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
4539c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4540c6b900c6SMatthew G. Knepley }
4541c6b900c6SMatthew G. Knepley 
4542c6b900c6SMatthew G. Knepley #undef __FUNCT__
45432e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate"
45442e17dfb7SMatthew G. Knepley /*@
45452e17dfb7SMatthew G. Knepley   DMLocalizeCoordinate - If a mesh is periodic (a torus with lengths L_i, some of which can be infinite), project the coordinate onto [0, L_i) in each dimension.
45462e17dfb7SMatthew G. Knepley 
45472e17dfb7SMatthew G. Knepley   Input Parameters:
45482e17dfb7SMatthew G. Knepley + dm     - The DM
45492e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45502e17dfb7SMatthew G. Knepley 
45512e17dfb7SMatthew G. Knepley   Output Parameter:
45522e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45532e17dfb7SMatthew G. Knepley 
45542e17dfb7SMatthew G. Knepley   Level: developer
45552e17dfb7SMatthew G. Knepley 
45562e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45572e17dfb7SMatthew G. Knepley @*/
45582e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscScalar out[])
45592e17dfb7SMatthew G. Knepley {
45602e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
45612e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
45622e17dfb7SMatthew G. Knepley 
45632e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
45642e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
45652e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
45662e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
45672e17dfb7SMatthew G. Knepley   } else {
45682e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
45692e17dfb7SMatthew G. Knepley       out[d] = in[d] - dm->L[d]*floor(PetscRealPart(in[d])/dm->L[d]);
45702e17dfb7SMatthew G. Knepley     }
45712e17dfb7SMatthew G. Knepley   }
45722e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
45732e17dfb7SMatthew G. Knepley }
45742e17dfb7SMatthew G. Knepley 
45752e17dfb7SMatthew G. Knepley #undef __FUNCT__
45762e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinate_Internal"
45772e17dfb7SMatthew G. Knepley /*
45782e17dfb7SMatthew G. Knepley   DMLocalizeCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
45792e17dfb7SMatthew G. Knepley 
45802e17dfb7SMatthew G. Knepley   Input Parameters:
45812e17dfb7SMatthew G. Knepley + dm     - The DM
45822e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
45832e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
45842e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
45852e17dfb7SMatthew G. Knepley 
45862e17dfb7SMatthew G. Knepley   Output Parameter:
45872e17dfb7SMatthew G. Knepley . out - The localized coordinate point
45882e17dfb7SMatthew G. Knepley 
45892e17dfb7SMatthew G. Knepley   Level: developer
45902e17dfb7SMatthew G. Knepley 
45912e17dfb7SMatthew G. Knepley   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
45922e17dfb7SMatthew G. Knepley 
45932e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
45942e17dfb7SMatthew G. Knepley */
45952e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
45962e17dfb7SMatthew G. Knepley {
45972e17dfb7SMatthew G. Knepley   PetscInt d;
45982e17dfb7SMatthew G. Knepley 
45992e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46002e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46012e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46022e17dfb7SMatthew G. Knepley   } else {
46032e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
46042e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
46052e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46062e17dfb7SMatthew G. Knepley       } else {
46072e17dfb7SMatthew G. Knepley         out[d] = in[d];
46082e17dfb7SMatthew G. Knepley       }
46092e17dfb7SMatthew G. Knepley     }
46102e17dfb7SMatthew G. Knepley   }
46112e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46122e17dfb7SMatthew G. Knepley }
46132e17dfb7SMatthew G. Knepley #undef __FUNCT__
46142e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinateReal_Internal"
46152e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
46162e17dfb7SMatthew G. Knepley {
46172e17dfb7SMatthew G. Knepley   PetscInt d;
46182e17dfb7SMatthew G. Knepley 
46192e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46202e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46212e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46222e17dfb7SMatthew G. Knepley   } else {
46232e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
46242e17dfb7SMatthew G. Knepley       if (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d]) {
46252e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
46262e17dfb7SMatthew G. Knepley       } else {
46272e17dfb7SMatthew G. Knepley         out[d] = in[d];
46282e17dfb7SMatthew G. Knepley       }
46292e17dfb7SMatthew G. Knepley     }
46302e17dfb7SMatthew G. Knepley   }
46312e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46322e17dfb7SMatthew G. Knepley }
46332e17dfb7SMatthew G. Knepley 
46342e17dfb7SMatthew G. Knepley #undef __FUNCT__
46352e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeAddCoordinate_Internal"
46362e17dfb7SMatthew G. Knepley /*
46372e17dfb7SMatthew G. Knepley   DMLocalizeAddCoordinate_Internal - If a mesh is periodic, and the input point is far from the anchor, pick the coordinate sheet of the torus which moves it closer.
46382e17dfb7SMatthew G. Knepley 
46392e17dfb7SMatthew G. Knepley   Input Parameters:
46402e17dfb7SMatthew G. Knepley + dm     - The DM
46412e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
46422e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
46432e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
46442e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
46452e17dfb7SMatthew G. Knepley 
46462e17dfb7SMatthew G. Knepley   Output Parameter:
46472e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
46482e17dfb7SMatthew G. Knepley 
46492e17dfb7SMatthew G. Knepley   Level: developer
46502e17dfb7SMatthew G. Knepley 
46512e17dfb7SMatthew G. Knepley   Note: This is meant to get a set of coordinates close to each other, as in a cell. The anchor is usually the one of the vertices on a containing cell
46522e17dfb7SMatthew G. Knepley 
46532e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
46542e17dfb7SMatthew G. Knepley */
46552e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
46562e17dfb7SMatthew G. Knepley {
46572e17dfb7SMatthew G. Knepley   PetscInt d;
46582e17dfb7SMatthew G. Knepley 
46592e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46602e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46612e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
46622e17dfb7SMatthew G. Knepley   } else {
46632e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
46642e17dfb7SMatthew G. Knepley       if (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d]) {
46652e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
46662e17dfb7SMatthew G. Knepley       } else {
46672e17dfb7SMatthew G. Knepley         out[d] += in[d];
46682e17dfb7SMatthew G. Knepley       }
46692e17dfb7SMatthew G. Knepley     }
46702e17dfb7SMatthew G. Knepley   }
46712e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
46722e17dfb7SMatthew G. Knepley }
46732e17dfb7SMatthew G. Knepley 
46742e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetDepthStratum(DM, PetscInt, PetscInt *, PetscInt *);
46752e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexGetHeightStratum(DM, PetscInt, PetscInt *, PetscInt *);
46762e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecGetClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
46772e17dfb7SMatthew G. Knepley PETSC_EXTERN PetscErrorCode DMPlexVecRestoreClosure(DM, PetscSection, Vec, PetscInt, PetscInt *, PetscScalar *[]);
46782e17dfb7SMatthew G. Knepley 
46792e17dfb7SMatthew G. Knepley #undef __FUNCT__
468036447a5eSToby Isaac #define __FUNCT__ "DMGetCoordinatesLocalized"
468136447a5eSToby Isaac /*@
468236447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
468336447a5eSToby Isaac 
468436447a5eSToby Isaac   Input Parameter:
468536447a5eSToby Isaac . dm - The DM
468636447a5eSToby Isaac 
468736447a5eSToby Isaac   Output Parameter:
468836447a5eSToby Isaac   areLocalized - True if localized
468936447a5eSToby Isaac 
469036447a5eSToby Isaac   Level: developer
469136447a5eSToby Isaac 
469236447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
469336447a5eSToby Isaac @*/
469436447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
469536447a5eSToby Isaac {
469636447a5eSToby Isaac   DM             cdm;
469736447a5eSToby Isaac   PetscSection   coordSection;
469836447a5eSToby Isaac   PetscInt       cStart, cEnd, c, sStart, sEnd, dof;
469936447a5eSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
470036447a5eSToby Isaac   PetscErrorCode ierr;
470136447a5eSToby Isaac 
470236447a5eSToby Isaac   PetscFunctionBegin;
470336447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47048b09590cSToby Isaac   if (!dm->maxCell) {
47058b09590cSToby Isaac     *areLocalized = PETSC_FALSE;
47068b09590cSToby Isaac     PetscFunctionReturn(0);
47078b09590cSToby Isaac   }
470836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
470936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
471036447a5eSToby Isaac   {
471136447a5eSToby Isaac     PetscBool isplex;
471236447a5eSToby Isaac 
471336447a5eSToby Isaac     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
471436447a5eSToby Isaac     if (isplex) {
471536447a5eSToby Isaac       ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
471636447a5eSToby Isaac     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
471736447a5eSToby Isaac   }
471836447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
471936447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47205a42012cSToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_FALSE;
472136447a5eSToby Isaac   for (c = cStart; c < cEnd; ++c) {
472236447a5eSToby Isaac     if (c < sStart || c >= sEnd) {
472336447a5eSToby Isaac       alreadyLocalized = PETSC_FALSE;
472436447a5eSToby Isaac       break;
472536447a5eSToby Isaac     }
472636447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
47275a42012cSToby Isaac     if (dof) {
47285a42012cSToby Isaac       alreadyLocalized = PETSC_TRUE;
472936447a5eSToby Isaac       break;
473036447a5eSToby Isaac     }
473136447a5eSToby Isaac   }
47325a42012cSToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
473336447a5eSToby Isaac   *areLocalized = alreadyLocalizedGlobal;
473436447a5eSToby Isaac   PetscFunctionReturn(0);
473536447a5eSToby Isaac }
473636447a5eSToby Isaac 
473736447a5eSToby Isaac 
473836447a5eSToby Isaac #undef __FUNCT__
47392e17dfb7SMatthew G. Knepley #define __FUNCT__ "DMLocalizeCoordinates"
47402e17dfb7SMatthew G. Knepley /*@
47412e17dfb7SMatthew G. Knepley   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for each cell
47422e17dfb7SMatthew G. Knepley 
47432e17dfb7SMatthew G. Knepley   Input Parameter:
47442e17dfb7SMatthew G. Knepley . dm - The DM
47452e17dfb7SMatthew G. Knepley 
47462e17dfb7SMatthew G. Knepley   Level: developer
47472e17dfb7SMatthew G. Knepley 
47482e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
47492e17dfb7SMatthew G. Knepley @*/
47502e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
47512e17dfb7SMatthew G. Knepley {
47522e17dfb7SMatthew G. Knepley   DM             cdm;
47532e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
47542e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
47553e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
47563e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4757e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
47583e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
47593e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
47602e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47612e17dfb7SMatthew G. Knepley 
47622e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47632e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47642e17dfb7SMatthew G. Knepley   if (!dm->maxCell) PetscFunctionReturn(0);
47652e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
47662e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
47672e17dfb7SMatthew G. Knepley   {
47682e17dfb7SMatthew G. Knepley     PetscBool isplex;
47692e17dfb7SMatthew G. Knepley 
47702e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
47712e17dfb7SMatthew G. Knepley     if (isplex) {
47722e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
47733e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
47743e922f36SToby Isaac       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
47753e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
47763e922f36SToby Isaac       newStart = vStart;
47773e922f36SToby Isaac       newEnd   = vEnd;
47783e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
47793e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
47803e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
47813e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
47823e922f36SToby Isaac       }
47832e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
47842e17dfb7SMatthew G. Knepley   }
47852e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
47862e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
47873e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4788e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
47893e922f36SToby Isaac 
47902e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
47912e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
47922e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
47932e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
47943e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
47953e922f36SToby Isaac 
47963e922f36SToby Isaac   ierr = DMGetWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
47973e922f36SToby Isaac   localized = &anchor[bs];
47983e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
47993e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48003e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48013e922f36SToby Isaac 
48023e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
48033e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
48043e922f36SToby Isaac       PetscInt     b;
48053e922f36SToby Isaac 
48063e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
48073e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48083e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48093e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
48103e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
48113e922f36SToby Isaac         for (b = 0; b < bs; b++) {
48123e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
48133e922f36SToby Isaac         }
48143e922f36SToby Isaac         if (b < bs) break;
48153e922f36SToby Isaac       }
48163e922f36SToby Isaac       if (d < dof/bs) {
48173e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
48183e922f36SToby Isaac           PetscInt cdof;
48193e922f36SToby Isaac 
48203e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
48213e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
48223e922f36SToby Isaac         }
48233e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
48243e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
48253e922f36SToby Isaac       }
48263e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48273e922f36SToby Isaac     }
48283e922f36SToby Isaac   }
48293e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
48303e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
48313e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
48323e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
48333e922f36SToby Isaac     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
48343e922f36SToby Isaac     PetscFunctionReturn(0);
48353e922f36SToby Isaac   }
48362e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48372e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48382e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
48392e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
48402e17dfb7SMatthew G. Knepley   }
48412e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
48422e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4843c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
48442e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
48452e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
48462e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
48472e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4848c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
48492e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
48502e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
48512e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
48522e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
48532e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
48542e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
48552e17dfb7SMatthew G. Knepley   }
48563e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
48573e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
48583e922f36SToby Isaac 
48592e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
48602e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
48613e922f36SToby Isaac       PetscInt     b, cdof;
48622e17dfb7SMatthew G. Knepley 
48633e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
48643e922f36SToby Isaac       if (!cdof) continue;
48652e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48662e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
48672e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
48682e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
48692e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
48702e17dfb7SMatthew G. Knepley     }
48713e922f36SToby Isaac   }
48723e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm, 2 * bs, PETSC_SCALAR, &anchor);CHKERRQ(ierr);
48733e922f36SToby Isaac   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),PETSC_INT,&pStart);CHKERRQ(ierr);
4874c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
48752e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
48762e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
48772e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
48782e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
48792e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
48802e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48812e17dfb7SMatthew G. Knepley }
48822e17dfb7SMatthew G. Knepley 
48832e17dfb7SMatthew G. Knepley #undef __FUNCT__
4884e87bb0d3SMatthew G Knepley #define __FUNCT__ "DMLocatePoints"
4885e87bb0d3SMatthew G Knepley /*@
48863a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
4887e87bb0d3SMatthew G Knepley 
48883a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
4889e87bb0d3SMatthew G Knepley 
4890e87bb0d3SMatthew G Knepley   Input Parameters:
4891e87bb0d3SMatthew G Knepley + dm - The DM
48923a93e3b7SToby Isaac . v - The Vec of points
489362a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
48943a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
4895e87bb0d3SMatthew G Knepley 
489661e3bb9bSMatthew G Knepley   Output Parameter:
489762a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
489862a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
48993a93e3b7SToby Isaac 
4900e87bb0d3SMatthew G Knepley 
4901e87bb0d3SMatthew G Knepley   Level: developer
490261e3bb9bSMatthew G Knepley 
490362a38674SMatthew G. Knepley   Notes:
49043a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
490562a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
49063a93e3b7SToby Isaac 
49073a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
490862a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
49093a93e3b7SToby Isaac 
49103a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
49113a93e3b7SToby Isaac 
491262a38674SMatthew G. Knepley $    const PetscSFNode *cells;
491362a38674SMatthew G. Knepley $    PetscInt           nFound;
491462a38674SMatthew G. Knepley $    const PetscSFNode *found;
491562a38674SMatthew G. Knepley $
491662a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
49173a93e3b7SToby Isaac 
49183a93e3b7SToby Isaac   Where cells[i].rank is the rank of the cell containing point found[i] (or i if found == NULL), and cells[i].index is
49193a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
49203a93e3b7SToby Isaac 
492161e3bb9bSMatthew G Knepley .keywords: point location, mesh
492262a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
492361e3bb9bSMatthew G Knepley @*/
492462a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
4925e87bb0d3SMatthew G Knepley {
4926735aa83eSMatthew G Knepley   PetscErrorCode ierr;
4927735aa83eSMatthew G Knepley 
4928e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
4929e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4930e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
4931e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
49323a93e3b7SToby Isaac   if (*cellSF) {
49333a93e3b7SToby Isaac     PetscMPIInt result;
49343a93e3b7SToby Isaac 
4935e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
49363a93e3b7SToby Isaac     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)cellSF),&result);CHKERRQ(ierr);
49373a93e3b7SToby Isaac     if (result != MPI_IDENT && result != MPI_CONGRUENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_INCOMP,"cellSF must have a communicator congruent to v's");
4938e0fc9d1bSMatthew G. Knepley   } else {
49393a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
49403a93e3b7SToby Isaac   }
494147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4942735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
494362a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
494482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
494547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
4946e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
4947e87bb0d3SMatthew G Knepley }
494814f150ffSMatthew G. Knepley 
494914f150ffSMatthew G. Knepley #undef __FUNCT__
495014f150ffSMatthew G. Knepley #define __FUNCT__ "DMGetOutputDM"
4951f4d763aaSMatthew G. Knepley /*@
4952f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
4953f4d763aaSMatthew G. Knepley 
4954f4d763aaSMatthew G. Knepley   Input Parameter:
4955f4d763aaSMatthew G. Knepley . dm - The original DM
4956f4d763aaSMatthew G. Knepley 
4957f4d763aaSMatthew G. Knepley   Output Parameter:
4958f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
4959f4d763aaSMatthew G. Knepley 
4960f4d763aaSMatthew G. Knepley   Level: intermediate
4961f4d763aaSMatthew G. Knepley 
4962f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
4963f4d763aaSMatthew G. Knepley @*/
496414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
496514f150ffSMatthew G. Knepley {
4966c26acbdeSMatthew G. Knepley   PetscSection   section;
49672d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
496814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
496914f150ffSMatthew G. Knepley 
497014f150ffSMatthew G. Knepley   PetscFunctionBegin;
497114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
497214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
4973c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
4974c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
4975127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
49762d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
4977c26acbdeSMatthew G. Knepley     *odm = dm;
4978c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
4979c26acbdeSMatthew G. Knepley   }
498014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
49810305aff6SMatthew G. Knepley     PetscDS      ds;
4982c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
498314f150ffSMatthew G. Knepley     PetscSF      sf;
498414f150ffSMatthew G. Knepley 
498514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
49860305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
49870305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
498814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
498914f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
499014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
499114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
499215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
499314f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
499414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
499514f150ffSMatthew G. Knepley   }
499614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
499714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
499814f150ffSMatthew G. Knepley }
4999f4d763aaSMatthew G. Knepley 
5000f4d763aaSMatthew G. Knepley #undef __FUNCT__
5001f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMGetOutputSequenceNumber"
5002f4d763aaSMatthew G. Knepley /*@
5003cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5004f4d763aaSMatthew G. Knepley 
5005f4d763aaSMatthew G. Knepley   Input Parameter:
5006f4d763aaSMatthew G. Knepley . dm - The original DM
5007f4d763aaSMatthew G. Knepley 
5008cdb7a50dSMatthew G. Knepley   Output Parameters:
5009cdb7a50dSMatthew G. Knepley + num - The output sequence number
5010cdb7a50dSMatthew G. Knepley - val - The output sequence value
5011f4d763aaSMatthew G. Knepley 
5012f4d763aaSMatthew G. Knepley   Level: intermediate
5013f4d763aaSMatthew G. Knepley 
5014f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5015f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5016f4d763aaSMatthew G. Knepley 
5017f4d763aaSMatthew G. Knepley .seealso: VecView()
5018f4d763aaSMatthew G. Knepley @*/
5019cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5020f4d763aaSMatthew G. Knepley {
5021f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5022f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5023cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5024cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5025f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5026f4d763aaSMatthew G. Knepley }
5027f4d763aaSMatthew G. Knepley 
5028f4d763aaSMatthew G. Knepley #undef __FUNCT__
5029f4d763aaSMatthew G. Knepley #define __FUNCT__ "DMSetOutputSequenceNumber"
5030f4d763aaSMatthew G. Knepley /*@
5031cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5032f4d763aaSMatthew G. Knepley 
5033f4d763aaSMatthew G. Knepley   Input Parameters:
5034f4d763aaSMatthew G. Knepley + dm - The original DM
5035cdb7a50dSMatthew G. Knepley . num - The output sequence number
5036cdb7a50dSMatthew G. Knepley - val - The output sequence value
5037f4d763aaSMatthew G. Knepley 
5038f4d763aaSMatthew G. Knepley   Level: intermediate
5039f4d763aaSMatthew G. Knepley 
5040f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5041f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5042f4d763aaSMatthew G. Knepley 
5043f4d763aaSMatthew G. Knepley .seealso: VecView()
5044f4d763aaSMatthew G. Knepley @*/
5045cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5046f4d763aaSMatthew G. Knepley {
5047f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5048f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5049f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5050cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5051cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5052cdb7a50dSMatthew G. Knepley }
5053cdb7a50dSMatthew G. Knepley 
5054cdb7a50dSMatthew G. Knepley #undef __FUNCT__
5055cdb7a50dSMatthew G. Knepley #define __FUNCT__ "DMOutputSequenceLoad"
5056cdb7a50dSMatthew G. Knepley /*@C
5057cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5058cdb7a50dSMatthew G. Knepley 
5059cdb7a50dSMatthew G. Knepley   Input Parameters:
5060cdb7a50dSMatthew G. Knepley + dm   - The original DM
5061cdb7a50dSMatthew G. Knepley . name - The sequence name
5062cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5063cdb7a50dSMatthew G. Knepley 
5064cdb7a50dSMatthew G. Knepley   Output Parameter:
5065cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5066cdb7a50dSMatthew G. Knepley 
5067cdb7a50dSMatthew G. Knepley   Level: intermediate
5068cdb7a50dSMatthew G. Knepley 
5069cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5070cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5071cdb7a50dSMatthew G. Knepley 
5072cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5073cdb7a50dSMatthew G. Knepley @*/
5074cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5075cdb7a50dSMatthew G. Knepley {
5076cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5077cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5078cdb7a50dSMatthew G. Knepley 
5079cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5080cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5081cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5082cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5083cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5084cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5085cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5086cdb7a50dSMatthew G. Knepley     PetscScalar value;
5087cdb7a50dSMatthew G. Knepley 
5088cdb7a50dSMatthew G. Knepley     ierr = DMSequenceLoad_HDF5(dm, name, num, &value, viewer);CHKERRQ(ierr);
50894aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5090cdb7a50dSMatthew G. Knepley #endif
5091cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5092f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5093f4d763aaSMatthew G. Knepley }
50948e4ac7eaSMatthew G. Knepley 
50958e4ac7eaSMatthew G. Knepley #undef __FUNCT__
50968e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMGetUseNatural"
50978e4ac7eaSMatthew G. Knepley /*@
50988e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
50998e4ac7eaSMatthew G. Knepley 
51008e4ac7eaSMatthew G. Knepley   Not collective
51018e4ac7eaSMatthew G. Knepley 
51028e4ac7eaSMatthew G. Knepley   Input Parameter:
51038e4ac7eaSMatthew G. Knepley . dm - The DM
51048e4ac7eaSMatthew G. Knepley 
51058e4ac7eaSMatthew G. Knepley   Output Parameter:
51068e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
51078e4ac7eaSMatthew G. Knepley 
51088e4ac7eaSMatthew G. Knepley   Level: beginner
51098e4ac7eaSMatthew G. Knepley 
51108e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
51118e4ac7eaSMatthew G. Knepley @*/
51128e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
51138e4ac7eaSMatthew G. Knepley {
51148e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51158e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51168e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
51178e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
51188e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51198e4ac7eaSMatthew G. Knepley }
51208e4ac7eaSMatthew G. Knepley 
51218e4ac7eaSMatthew G. Knepley #undef __FUNCT__
51228e4ac7eaSMatthew G. Knepley #define __FUNCT__ "DMSetUseNatural"
51238e4ac7eaSMatthew G. Knepley /*@
51248e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
51258e4ac7eaSMatthew G. Knepley 
51268e4ac7eaSMatthew G. Knepley   Collective on dm
51278e4ac7eaSMatthew G. Knepley 
51288e4ac7eaSMatthew G. Knepley   Input Parameters:
51298e4ac7eaSMatthew G. Knepley + dm - The DM
51308e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
51318e4ac7eaSMatthew G. Knepley 
51328e4ac7eaSMatthew G. Knepley   Level: beginner
51338e4ac7eaSMatthew G. Knepley 
51348e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
51358e4ac7eaSMatthew G. Knepley @*/
51368e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
51378e4ac7eaSMatthew G. Knepley {
51388e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
51398e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
51408e4ac7eaSMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, useNatural, 2);
51418e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
51428e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
51438e4ac7eaSMatthew G. Knepley }
5144c58f1c22SToby Isaac 
5145c58f1c22SToby Isaac #undef __FUNCT__
5146c58f1c22SToby Isaac #define __FUNCT__
5147c58f1c22SToby Isaac 
5148c58f1c22SToby Isaac #undef __FUNCT__
5149c58f1c22SToby Isaac #define __FUNCT__ "DMCreateLabel"
5150c58f1c22SToby Isaac /*@C
5151c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5152c58f1c22SToby Isaac 
5153c58f1c22SToby Isaac   Not Collective
5154c58f1c22SToby Isaac 
5155c58f1c22SToby Isaac   Input Parameters:
5156c58f1c22SToby Isaac + dm   - The DM object
5157c58f1c22SToby Isaac - name - The label name
5158c58f1c22SToby Isaac 
5159c58f1c22SToby Isaac   Level: intermediate
5160c58f1c22SToby Isaac 
5161c58f1c22SToby Isaac .keywords: mesh
5162c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5163c58f1c22SToby Isaac @*/
5164c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5165c58f1c22SToby Isaac {
5166c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5167c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5168c58f1c22SToby Isaac   PetscErrorCode ierr;
5169c58f1c22SToby Isaac 
5170c58f1c22SToby Isaac   PetscFunctionBegin;
5171c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5172c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5173c58f1c22SToby Isaac   while (next) {
5174c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5175c58f1c22SToby Isaac     if (flg) break;
5176c58f1c22SToby Isaac     next = next->next;
5177c58f1c22SToby Isaac   }
5178c58f1c22SToby Isaac   if (!flg) {
5179c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5180c58f1c22SToby Isaac 
5181c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5182c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5183c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5184c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5185c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5186c58f1c22SToby Isaac   }
5187c58f1c22SToby Isaac   PetscFunctionReturn(0);
5188c58f1c22SToby Isaac }
5189c58f1c22SToby Isaac 
5190c58f1c22SToby Isaac #undef __FUNCT__
5191c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelValue"
5192c58f1c22SToby Isaac /*@C
5193c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5194c58f1c22SToby Isaac 
5195c58f1c22SToby Isaac   Not Collective
5196c58f1c22SToby Isaac 
5197c58f1c22SToby Isaac   Input Parameters:
5198c58f1c22SToby Isaac + dm   - The DM object
5199c58f1c22SToby Isaac . name - The label name
5200c58f1c22SToby Isaac - point - The mesh point
5201c58f1c22SToby Isaac 
5202c58f1c22SToby Isaac   Output Parameter:
5203c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5204c58f1c22SToby Isaac 
5205c58f1c22SToby Isaac   Level: beginner
5206c58f1c22SToby Isaac 
5207c58f1c22SToby Isaac .keywords: mesh
5208c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5209c58f1c22SToby Isaac @*/
5210c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5211c58f1c22SToby Isaac {
5212c58f1c22SToby Isaac   DMLabel        label;
5213c58f1c22SToby Isaac   PetscErrorCode ierr;
5214c58f1c22SToby Isaac 
5215c58f1c22SToby Isaac   PetscFunctionBegin;
5216c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5217c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5218c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5219c58f1c22SToby Isaac   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);CHKERRQ(ierr);
5220c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5221c58f1c22SToby Isaac   PetscFunctionReturn(0);
5222c58f1c22SToby Isaac }
5223c58f1c22SToby Isaac 
5224c58f1c22SToby Isaac #undef __FUNCT__
5225c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelValue"
5226c58f1c22SToby Isaac /*@C
5227c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5228c58f1c22SToby Isaac 
5229c58f1c22SToby Isaac   Not Collective
5230c58f1c22SToby Isaac 
5231c58f1c22SToby Isaac   Input Parameters:
5232c58f1c22SToby Isaac + dm   - The DM object
5233c58f1c22SToby Isaac . name - The label name
5234c58f1c22SToby Isaac . point - The mesh point
5235c58f1c22SToby Isaac - value - The label value for this point
5236c58f1c22SToby Isaac 
5237c58f1c22SToby Isaac   Output Parameter:
5238c58f1c22SToby Isaac 
5239c58f1c22SToby Isaac   Level: beginner
5240c58f1c22SToby Isaac 
5241c58f1c22SToby Isaac .keywords: mesh
5242c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5243c58f1c22SToby Isaac @*/
5244c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5245c58f1c22SToby Isaac {
5246c58f1c22SToby Isaac   DMLabel        label;
5247c58f1c22SToby Isaac   PetscErrorCode ierr;
5248c58f1c22SToby Isaac 
5249c58f1c22SToby Isaac   PetscFunctionBegin;
5250c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5251c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5252c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5253c58f1c22SToby Isaac   if (!label) {
5254c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5255c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5256c58f1c22SToby Isaac   }
5257c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5258c58f1c22SToby Isaac   PetscFunctionReturn(0);
5259c58f1c22SToby Isaac }
5260c58f1c22SToby Isaac 
5261c58f1c22SToby Isaac #undef __FUNCT__
5262c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelValue"
5263c58f1c22SToby Isaac /*@C
5264c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5265c58f1c22SToby Isaac 
5266c58f1c22SToby Isaac   Not Collective
5267c58f1c22SToby Isaac 
5268c58f1c22SToby Isaac   Input Parameters:
5269c58f1c22SToby Isaac + dm   - The DM object
5270c58f1c22SToby Isaac . name - The label name
5271c58f1c22SToby Isaac . point - The mesh point
5272c58f1c22SToby Isaac - value - The label value for this point
5273c58f1c22SToby Isaac 
5274c58f1c22SToby Isaac   Output Parameter:
5275c58f1c22SToby Isaac 
5276c58f1c22SToby Isaac   Level: beginner
5277c58f1c22SToby Isaac 
5278c58f1c22SToby Isaac .keywords: mesh
5279c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5280c58f1c22SToby Isaac @*/
5281c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5282c58f1c22SToby Isaac {
5283c58f1c22SToby Isaac   DMLabel        label;
5284c58f1c22SToby Isaac   PetscErrorCode ierr;
5285c58f1c22SToby Isaac 
5286c58f1c22SToby Isaac   PetscFunctionBegin;
5287c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5288c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5289c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5290c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5291c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5292c58f1c22SToby Isaac   PetscFunctionReturn(0);
5293c58f1c22SToby Isaac }
5294c58f1c22SToby Isaac 
5295c58f1c22SToby Isaac #undef __FUNCT__
5296c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelSize"
5297c58f1c22SToby Isaac /*@C
5298c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5299c58f1c22SToby Isaac 
5300c58f1c22SToby Isaac   Not Collective
5301c58f1c22SToby Isaac 
5302c58f1c22SToby Isaac   Input Parameters:
5303c58f1c22SToby Isaac + dm   - The DM object
5304c58f1c22SToby Isaac - name - The label name
5305c58f1c22SToby Isaac 
5306c58f1c22SToby Isaac   Output Parameter:
5307c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5308c58f1c22SToby Isaac 
5309c58f1c22SToby Isaac   Level: beginner
5310c58f1c22SToby Isaac 
5311c58f1c22SToby Isaac .keywords: mesh
5312c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5313c58f1c22SToby Isaac @*/
5314c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5315c58f1c22SToby Isaac {
5316c58f1c22SToby Isaac   DMLabel        label;
5317c58f1c22SToby Isaac   PetscErrorCode ierr;
5318c58f1c22SToby Isaac 
5319c58f1c22SToby Isaac   PetscFunctionBegin;
5320c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5321c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5322c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5323c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5324c58f1c22SToby Isaac   *size = 0;
5325c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5326c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5327c58f1c22SToby Isaac   PetscFunctionReturn(0);
5328c58f1c22SToby Isaac }
5329c58f1c22SToby Isaac 
5330c58f1c22SToby Isaac #undef __FUNCT__
5331c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelIdIS"
5332c58f1c22SToby Isaac /*@C
5333c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5334c58f1c22SToby Isaac 
5335c58f1c22SToby Isaac   Not Collective
5336c58f1c22SToby Isaac 
5337c58f1c22SToby Isaac   Input Parameters:
5338c58f1c22SToby Isaac + mesh - The DM object
5339c58f1c22SToby Isaac - name - The label name
5340c58f1c22SToby Isaac 
5341c58f1c22SToby Isaac   Output Parameter:
5342c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5343c58f1c22SToby Isaac 
5344c58f1c22SToby Isaac   Level: beginner
5345c58f1c22SToby Isaac 
5346c58f1c22SToby Isaac .keywords: mesh
5347c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5348c58f1c22SToby Isaac @*/
5349c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5350c58f1c22SToby Isaac {
5351c58f1c22SToby Isaac   DMLabel        label;
5352c58f1c22SToby Isaac   PetscErrorCode ierr;
5353c58f1c22SToby Isaac 
5354c58f1c22SToby Isaac   PetscFunctionBegin;
5355c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5356c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5357c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5358c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5359c58f1c22SToby Isaac   *ids = NULL;
5360c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5361c58f1c22SToby Isaac   ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5362c58f1c22SToby Isaac   PetscFunctionReturn(0);
5363c58f1c22SToby Isaac }
5364c58f1c22SToby Isaac 
5365c58f1c22SToby Isaac #undef __FUNCT__
5366c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumSize"
5367c58f1c22SToby Isaac /*@C
5368c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5369c58f1c22SToby Isaac 
5370c58f1c22SToby Isaac   Not Collective
5371c58f1c22SToby Isaac 
5372c58f1c22SToby Isaac   Input Parameters:
5373c58f1c22SToby Isaac + dm - The DM object
5374c58f1c22SToby Isaac . name - The label name
5375c58f1c22SToby Isaac - value - The stratum value
5376c58f1c22SToby Isaac 
5377c58f1c22SToby Isaac   Output Parameter:
5378c58f1c22SToby Isaac . size - The stratum size
5379c58f1c22SToby Isaac 
5380c58f1c22SToby Isaac   Level: beginner
5381c58f1c22SToby Isaac 
5382c58f1c22SToby Isaac .keywords: mesh
5383c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5384c58f1c22SToby Isaac @*/
5385c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5386c58f1c22SToby Isaac {
5387c58f1c22SToby Isaac   DMLabel        label;
5388c58f1c22SToby Isaac   PetscErrorCode ierr;
5389c58f1c22SToby Isaac 
5390c58f1c22SToby Isaac   PetscFunctionBegin;
5391c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5392c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5393c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5394c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5395c58f1c22SToby Isaac   *size = 0;
5396c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5397c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5398c58f1c22SToby Isaac   PetscFunctionReturn(0);
5399c58f1c22SToby Isaac }
5400c58f1c22SToby Isaac 
5401c58f1c22SToby Isaac #undef __FUNCT__
5402c58f1c22SToby Isaac #define __FUNCT__ "DMGetStratumIS"
5403c58f1c22SToby Isaac /*@C
5404c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5405c58f1c22SToby Isaac 
5406c58f1c22SToby Isaac   Not Collective
5407c58f1c22SToby Isaac 
5408c58f1c22SToby Isaac   Input Parameters:
5409c58f1c22SToby Isaac + dm - The DM object
5410c58f1c22SToby Isaac . name - The label name
5411c58f1c22SToby Isaac - value - The stratum value
5412c58f1c22SToby Isaac 
5413c58f1c22SToby Isaac   Output Parameter:
5414c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5415c58f1c22SToby Isaac 
5416c58f1c22SToby Isaac   Level: beginner
5417c58f1c22SToby Isaac 
5418c58f1c22SToby Isaac .keywords: mesh
5419c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5420c58f1c22SToby Isaac @*/
5421c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5422c58f1c22SToby Isaac {
5423c58f1c22SToby Isaac   DMLabel        label;
5424c58f1c22SToby Isaac   PetscErrorCode ierr;
5425c58f1c22SToby Isaac 
5426c58f1c22SToby Isaac   PetscFunctionBegin;
5427c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5428c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5429c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5430c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5431c58f1c22SToby Isaac   *points = NULL;
5432c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5433c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5434c58f1c22SToby Isaac   PetscFunctionReturn(0);
5435c58f1c22SToby Isaac }
5436c58f1c22SToby Isaac 
5437c58f1c22SToby Isaac #undef __FUNCT__
54384de306b1SToby Isaac #define __FUNCT__ "DMSetStratumIS"
54394de306b1SToby Isaac /*@C
54404de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
54414de306b1SToby Isaac 
54424de306b1SToby Isaac   Not Collective
54434de306b1SToby Isaac 
54444de306b1SToby Isaac   Input Parameters:
54454de306b1SToby Isaac + dm - The DM object
54464de306b1SToby Isaac . name - The label name
54474de306b1SToby Isaac . value - The stratum value
54484de306b1SToby Isaac - points - The stratum points
54494de306b1SToby Isaac 
54504de306b1SToby Isaac   Level: beginner
54514de306b1SToby Isaac 
54524de306b1SToby Isaac .keywords: mesh
54534de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
54544de306b1SToby Isaac @*/
54554de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
54564de306b1SToby Isaac {
54574de306b1SToby Isaac   DMLabel        label;
54584de306b1SToby Isaac   PetscErrorCode ierr;
54594de306b1SToby Isaac 
54604de306b1SToby Isaac   PetscFunctionBegin;
54614de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54624de306b1SToby Isaac   PetscValidCharPointer(name, 2);
54634de306b1SToby Isaac   PetscValidPointer(points, 4);
54644de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
54654de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
54664de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
54674de306b1SToby Isaac   PetscFunctionReturn(0);
54684de306b1SToby Isaac }
54694de306b1SToby Isaac 
54704de306b1SToby Isaac #undef __FUNCT__
5471c58f1c22SToby Isaac #define __FUNCT__ "DMClearLabelStratum"
5472c58f1c22SToby Isaac /*@C
5473c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5474c58f1c22SToby Isaac 
5475c58f1c22SToby Isaac   Not Collective
5476c58f1c22SToby Isaac 
5477c58f1c22SToby Isaac   Input Parameters:
5478c58f1c22SToby Isaac + dm   - The DM object
5479c58f1c22SToby Isaac . name - The label name
5480c58f1c22SToby Isaac - value - The label value for this point
5481c58f1c22SToby Isaac 
5482c58f1c22SToby Isaac   Output Parameter:
5483c58f1c22SToby Isaac 
5484c58f1c22SToby Isaac   Level: beginner
5485c58f1c22SToby Isaac 
5486c58f1c22SToby Isaac .keywords: mesh
5487c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5488c58f1c22SToby Isaac @*/
5489c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5490c58f1c22SToby Isaac {
5491c58f1c22SToby Isaac   DMLabel        label;
5492c58f1c22SToby Isaac   PetscErrorCode ierr;
5493c58f1c22SToby Isaac 
5494c58f1c22SToby Isaac   PetscFunctionBegin;
5495c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5496c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5497c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5498c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5499c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5500c58f1c22SToby Isaac   PetscFunctionReturn(0);
5501c58f1c22SToby Isaac }
5502c58f1c22SToby Isaac 
5503c58f1c22SToby Isaac #undef __FUNCT__
5504c58f1c22SToby Isaac #define __FUNCT__ "DMGetNumLabels"
5505c58f1c22SToby Isaac /*@
5506c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5507c58f1c22SToby Isaac 
5508c58f1c22SToby Isaac   Not Collective
5509c58f1c22SToby Isaac 
5510c58f1c22SToby Isaac   Input Parameter:
5511c58f1c22SToby Isaac . dm   - The DM object
5512c58f1c22SToby Isaac 
5513c58f1c22SToby Isaac   Output Parameter:
5514c58f1c22SToby Isaac . numLabels - the number of Labels
5515c58f1c22SToby Isaac 
5516c58f1c22SToby Isaac   Level: intermediate
5517c58f1c22SToby Isaac 
5518c58f1c22SToby Isaac .keywords: mesh
5519c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5520c58f1c22SToby Isaac @*/
5521c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5522c58f1c22SToby Isaac {
5523c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5524c58f1c22SToby Isaac   PetscInt  n    = 0;
5525c58f1c22SToby Isaac 
5526c58f1c22SToby Isaac   PetscFunctionBegin;
5527c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5528c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5529c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5530c58f1c22SToby Isaac   *numLabels = n;
5531c58f1c22SToby Isaac   PetscFunctionReturn(0);
5532c58f1c22SToby Isaac }
5533c58f1c22SToby Isaac 
5534c58f1c22SToby Isaac #undef __FUNCT__
5535c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelName"
5536c58f1c22SToby Isaac /*@C
5537c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5538c58f1c22SToby Isaac 
5539c58f1c22SToby Isaac   Not Collective
5540c58f1c22SToby Isaac 
5541c58f1c22SToby Isaac   Input Parameters:
5542c58f1c22SToby Isaac + dm - The DM object
5543c58f1c22SToby Isaac - n  - the label number
5544c58f1c22SToby Isaac 
5545c58f1c22SToby Isaac   Output Parameter:
5546c58f1c22SToby Isaac . name - the label name
5547c58f1c22SToby Isaac 
5548c58f1c22SToby Isaac   Level: intermediate
5549c58f1c22SToby Isaac 
5550c58f1c22SToby Isaac .keywords: mesh
5551c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5552c58f1c22SToby Isaac @*/
5553c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5554c58f1c22SToby Isaac {
5555c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5556c58f1c22SToby Isaac   PetscInt  l    = 0;
5557c58f1c22SToby Isaac 
5558c58f1c22SToby Isaac   PetscFunctionBegin;
5559c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5560c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5561c58f1c22SToby Isaac   while (next) {
5562c58f1c22SToby Isaac     if (l == n) {
5563c58f1c22SToby Isaac       *name = next->label->name;
5564c58f1c22SToby Isaac       PetscFunctionReturn(0);
5565c58f1c22SToby Isaac     }
5566c58f1c22SToby Isaac     ++l;
5567c58f1c22SToby Isaac     next = next->next;
5568c58f1c22SToby Isaac   }
5569c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5570c58f1c22SToby Isaac }
5571c58f1c22SToby Isaac 
5572c58f1c22SToby Isaac #undef __FUNCT__
5573c58f1c22SToby Isaac #define __FUNCT__ "DMHasLabel"
5574c58f1c22SToby Isaac /*@C
5575c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Not Collective
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac   Input Parameters:
5580c58f1c22SToby Isaac + dm   - The DM object
5581c58f1c22SToby Isaac - name - The label name
5582c58f1c22SToby Isaac 
5583c58f1c22SToby Isaac   Output Parameter:
5584c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5585c58f1c22SToby Isaac 
5586c58f1c22SToby Isaac   Level: intermediate
5587c58f1c22SToby Isaac 
5588c58f1c22SToby Isaac .keywords: mesh
5589c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5590c58f1c22SToby Isaac @*/
5591c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5592c58f1c22SToby Isaac {
5593c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5594c58f1c22SToby Isaac   PetscErrorCode ierr;
5595c58f1c22SToby Isaac 
5596c58f1c22SToby Isaac   PetscFunctionBegin;
5597c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5598c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5599c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5600c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5601c58f1c22SToby Isaac   while (next) {
5602c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5603c58f1c22SToby Isaac     if (*hasLabel) break;
5604c58f1c22SToby Isaac     next = next->next;
5605c58f1c22SToby Isaac   }
5606c58f1c22SToby Isaac   PetscFunctionReturn(0);
5607c58f1c22SToby Isaac }
5608c58f1c22SToby Isaac 
5609c58f1c22SToby Isaac #undef __FUNCT__
5610c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabel"
5611c58f1c22SToby Isaac /*@C
5612c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5613c58f1c22SToby Isaac 
5614c58f1c22SToby Isaac   Not Collective
5615c58f1c22SToby Isaac 
5616c58f1c22SToby Isaac   Input Parameters:
5617c58f1c22SToby Isaac + dm   - The DM object
5618c58f1c22SToby Isaac - name - The label name
5619c58f1c22SToby Isaac 
5620c58f1c22SToby Isaac   Output Parameter:
5621c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5622c58f1c22SToby Isaac 
5623c58f1c22SToby Isaac   Level: intermediate
5624c58f1c22SToby Isaac 
5625c58f1c22SToby Isaac .keywords: mesh
5626c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5627c58f1c22SToby Isaac @*/
5628c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5629c58f1c22SToby Isaac {
5630c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5631c58f1c22SToby Isaac   PetscBool      hasLabel;
5632c58f1c22SToby Isaac   PetscErrorCode ierr;
5633c58f1c22SToby Isaac 
5634c58f1c22SToby Isaac   PetscFunctionBegin;
5635c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5636c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5637c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5638c58f1c22SToby Isaac   *label = NULL;
5639c58f1c22SToby Isaac   while (next) {
5640c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5641c58f1c22SToby Isaac     if (hasLabel) {
5642c58f1c22SToby Isaac       *label = next->label;
5643c58f1c22SToby Isaac       break;
5644c58f1c22SToby Isaac     }
5645c58f1c22SToby Isaac     next = next->next;
5646c58f1c22SToby Isaac   }
5647c58f1c22SToby Isaac   PetscFunctionReturn(0);
5648c58f1c22SToby Isaac }
5649c58f1c22SToby Isaac 
5650c58f1c22SToby Isaac #undef __FUNCT__
5651c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelByNum"
5652c58f1c22SToby Isaac /*@C
5653c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5654c58f1c22SToby Isaac 
5655c58f1c22SToby Isaac   Not Collective
5656c58f1c22SToby Isaac 
5657c58f1c22SToby Isaac   Input Parameters:
5658c58f1c22SToby Isaac + dm - The DM object
5659c58f1c22SToby Isaac - n  - the label number
5660c58f1c22SToby Isaac 
5661c58f1c22SToby Isaac   Output Parameter:
5662c58f1c22SToby Isaac . label - the label
5663c58f1c22SToby Isaac 
5664c58f1c22SToby Isaac   Level: intermediate
5665c58f1c22SToby Isaac 
5666c58f1c22SToby Isaac .keywords: mesh
5667c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5668c58f1c22SToby Isaac @*/
5669c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5670c58f1c22SToby Isaac {
5671c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5672c58f1c22SToby Isaac   PetscInt    l    = 0;
5673c58f1c22SToby Isaac 
5674c58f1c22SToby Isaac   PetscFunctionBegin;
5675c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5676c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5677c58f1c22SToby Isaac   while (next) {
5678c58f1c22SToby Isaac     if (l == n) {
5679c58f1c22SToby Isaac       *label = next->label;
5680c58f1c22SToby Isaac       PetscFunctionReturn(0);
5681c58f1c22SToby Isaac     }
5682c58f1c22SToby Isaac     ++l;
5683c58f1c22SToby Isaac     next = next->next;
5684c58f1c22SToby Isaac   }
5685c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5686c58f1c22SToby Isaac }
5687c58f1c22SToby Isaac 
5688c58f1c22SToby Isaac #undef __FUNCT__
5689c58f1c22SToby Isaac #define __FUNCT__ "DMAddLabel"
5690c58f1c22SToby Isaac /*@C
5691c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5692c58f1c22SToby Isaac 
5693c58f1c22SToby Isaac   Not Collective
5694c58f1c22SToby Isaac 
5695c58f1c22SToby Isaac   Input Parameters:
5696c58f1c22SToby Isaac + dm   - The DM object
5697c58f1c22SToby Isaac - label - The DMLabel
5698c58f1c22SToby Isaac 
5699c58f1c22SToby Isaac   Level: developer
5700c58f1c22SToby Isaac 
5701c58f1c22SToby Isaac .keywords: mesh
5702c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5703c58f1c22SToby Isaac @*/
5704c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5705c58f1c22SToby Isaac {
5706c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5707c58f1c22SToby Isaac   PetscBool      hasLabel;
5708c58f1c22SToby Isaac   PetscErrorCode ierr;
5709c58f1c22SToby Isaac 
5710c58f1c22SToby Isaac   PetscFunctionBegin;
5711c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5712c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5713c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5714c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5715c58f1c22SToby Isaac   tmpLabel->label  = label;
5716c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5717c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5718c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5719c58f1c22SToby Isaac   PetscFunctionReturn(0);
5720c58f1c22SToby Isaac }
5721c58f1c22SToby Isaac 
5722c58f1c22SToby Isaac #undef __FUNCT__
5723c58f1c22SToby Isaac #define __FUNCT__ "DMRemoveLabel"
5724c58f1c22SToby Isaac /*@C
5725c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5726c58f1c22SToby Isaac 
5727c58f1c22SToby Isaac   Not Collective
5728c58f1c22SToby Isaac 
5729c58f1c22SToby Isaac   Input Parameters:
5730c58f1c22SToby Isaac + dm   - The DM object
5731c58f1c22SToby Isaac - name - The label name
5732c58f1c22SToby Isaac 
5733c58f1c22SToby Isaac   Output Parameter:
5734c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5735c58f1c22SToby Isaac 
5736c58f1c22SToby Isaac   Level: developer
5737c58f1c22SToby Isaac 
5738c58f1c22SToby Isaac .keywords: mesh
5739c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5740c58f1c22SToby Isaac @*/
5741c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5742c58f1c22SToby Isaac {
5743c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5744c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5745c58f1c22SToby Isaac   PetscBool      hasLabel;
5746c58f1c22SToby Isaac   PetscErrorCode ierr;
5747c58f1c22SToby Isaac 
5748c58f1c22SToby Isaac   PetscFunctionBegin;
5749c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5750c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5751c58f1c22SToby Isaac   *label = NULL;
5752c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5753c58f1c22SToby Isaac   while (next) {
5754c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5755c58f1c22SToby Isaac     if (hasLabel) {
5756c58f1c22SToby Isaac       if (last) last->next       = next->next;
5757c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5758c58f1c22SToby Isaac       next->next = NULL;
5759c58f1c22SToby Isaac       *label     = next->label;
5760c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5761c58f1c22SToby Isaac       if (hasLabel) {
5762c58f1c22SToby Isaac         dm->depthLabel = NULL;
5763c58f1c22SToby Isaac       }
5764c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5765c58f1c22SToby Isaac       break;
5766c58f1c22SToby Isaac     }
5767c58f1c22SToby Isaac     last = next;
5768c58f1c22SToby Isaac     next = next->next;
5769c58f1c22SToby Isaac   }
5770c58f1c22SToby Isaac   PetscFunctionReturn(0);
5771c58f1c22SToby Isaac }
5772c58f1c22SToby Isaac 
5773c58f1c22SToby Isaac #undef __FUNCT__
5774c58f1c22SToby Isaac #define __FUNCT__ "DMGetLabelOutput"
5775c58f1c22SToby Isaac /*@C
5776c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5777c58f1c22SToby Isaac 
5778c58f1c22SToby Isaac   Not Collective
5779c58f1c22SToby Isaac 
5780c58f1c22SToby Isaac   Input Parameters:
5781c58f1c22SToby Isaac + dm   - The DM object
5782c58f1c22SToby Isaac - name - The label name
5783c58f1c22SToby Isaac 
5784c58f1c22SToby Isaac   Output Parameter:
5785c58f1c22SToby Isaac . output - The flag for output
5786c58f1c22SToby Isaac 
5787c58f1c22SToby Isaac   Level: developer
5788c58f1c22SToby Isaac 
5789c58f1c22SToby Isaac .keywords: mesh
5790c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5791c58f1c22SToby Isaac @*/
5792c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5793c58f1c22SToby Isaac {
5794c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5795c58f1c22SToby Isaac   PetscErrorCode ierr;
5796c58f1c22SToby Isaac 
5797c58f1c22SToby Isaac   PetscFunctionBegin;
5798c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5799c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5800c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5801c58f1c22SToby Isaac   while (next) {
5802c58f1c22SToby Isaac     PetscBool flg;
5803c58f1c22SToby Isaac 
5804c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5805c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5806c58f1c22SToby Isaac     next = next->next;
5807c58f1c22SToby Isaac   }
5808c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5809c58f1c22SToby Isaac }
5810c58f1c22SToby Isaac 
5811c58f1c22SToby Isaac #undef __FUNCT__
5812c58f1c22SToby Isaac #define __FUNCT__ "DMSetLabelOutput"
5813c58f1c22SToby Isaac /*@C
5814c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5815c58f1c22SToby Isaac 
5816c58f1c22SToby Isaac   Not Collective
5817c58f1c22SToby Isaac 
5818c58f1c22SToby Isaac   Input Parameters:
5819c58f1c22SToby Isaac + dm     - The DM object
5820c58f1c22SToby Isaac . name   - The label name
5821c58f1c22SToby Isaac - output - The flag for output
5822c58f1c22SToby Isaac 
5823c58f1c22SToby Isaac   Level: developer
5824c58f1c22SToby Isaac 
5825c58f1c22SToby Isaac .keywords: mesh
5826c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5827c58f1c22SToby Isaac @*/
5828c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5829c58f1c22SToby Isaac {
5830c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5831c58f1c22SToby Isaac   PetscErrorCode ierr;
5832c58f1c22SToby Isaac 
5833c58f1c22SToby Isaac   PetscFunctionBegin;
5834c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5835c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5836c58f1c22SToby Isaac   while (next) {
5837c58f1c22SToby Isaac     PetscBool flg;
5838c58f1c22SToby Isaac 
5839c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5840c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5841c58f1c22SToby Isaac     next = next->next;
5842c58f1c22SToby Isaac   }
5843c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5844c58f1c22SToby Isaac }
5845c58f1c22SToby Isaac 
5846c58f1c22SToby Isaac 
5847c58f1c22SToby Isaac #undef __FUNCT__
5848c58f1c22SToby Isaac #define __FUNCT__ "DMCopyLabels"
5849c58f1c22SToby Isaac /*@
5850c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5851c58f1c22SToby Isaac 
5852c58f1c22SToby Isaac   Collective on DM
5853c58f1c22SToby Isaac 
5854c58f1c22SToby Isaac   Input Parameter:
58552e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5856c58f1c22SToby Isaac 
5857c58f1c22SToby Isaac   Output Parameter:
58582e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5859c58f1c22SToby Isaac 
5860c58f1c22SToby Isaac   Level: intermediate
5861c58f1c22SToby Isaac 
5862c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5863c58f1c22SToby Isaac 
5864c58f1c22SToby Isaac .keywords: mesh
5865c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5866c58f1c22SToby Isaac @*/
5867c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5868c58f1c22SToby Isaac {
5869c58f1c22SToby Isaac   PetscInt       numLabels, l;
5870c58f1c22SToby Isaac   PetscErrorCode ierr;
5871c58f1c22SToby Isaac 
5872c58f1c22SToby Isaac   PetscFunctionBegin;
5873c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5874c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5875c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5876c58f1c22SToby Isaac     DMLabel     label, labelNew;
5877c58f1c22SToby Isaac     const char *name;
5878c58f1c22SToby Isaac     PetscBool   flg;
5879c58f1c22SToby Isaac 
5880c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5881c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5882c58f1c22SToby Isaac     if (flg) continue;
5883c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5884c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5885c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5886c58f1c22SToby Isaac   }
5887c58f1c22SToby Isaac   PetscFunctionReturn(0);
5888c58f1c22SToby Isaac }
5889a8fb8f29SToby Isaac 
5890a8fb8f29SToby Isaac #undef __FUNCT__
5891a8fb8f29SToby Isaac #define __FUNCT__ "DMGetCoarseDM"
5892a8fb8f29SToby Isaac /*@
5893a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5894a8fb8f29SToby Isaac 
5895a8fb8f29SToby Isaac   Input Parameter:
5896a8fb8f29SToby Isaac . dm - The DM object
5897a8fb8f29SToby Isaac 
5898a8fb8f29SToby Isaac   Output Parameter:
5899a8fb8f29SToby Isaac . cdm - The coarse DM
5900a8fb8f29SToby Isaac 
5901a8fb8f29SToby Isaac   Level: intermediate
5902a8fb8f29SToby Isaac 
5903a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5904a8fb8f29SToby Isaac @*/
5905a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5906a8fb8f29SToby Isaac {
5907a8fb8f29SToby Isaac   PetscFunctionBegin;
5908a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5909a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5910a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5911a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5912a8fb8f29SToby Isaac }
5913a8fb8f29SToby Isaac 
5914a8fb8f29SToby Isaac #undef __FUNCT__
5915a8fb8f29SToby Isaac #define __FUNCT__ "DMSetCoarseDM"
5916a8fb8f29SToby Isaac /*@
5917a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5918a8fb8f29SToby Isaac 
5919a8fb8f29SToby Isaac   Input Parameters:
5920a8fb8f29SToby Isaac + dm - The DM object
5921a8fb8f29SToby Isaac - cdm - The coarse DM
5922a8fb8f29SToby Isaac 
5923a8fb8f29SToby Isaac   Level: intermediate
5924a8fb8f29SToby Isaac 
5925a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5926a8fb8f29SToby Isaac @*/
5927a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5928a8fb8f29SToby Isaac {
5929a8fb8f29SToby Isaac   PetscErrorCode ierr;
5930a8fb8f29SToby Isaac 
5931a8fb8f29SToby Isaac   PetscFunctionBegin;
5932a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5933a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5934a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5935a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5936a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
5937a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5938a8fb8f29SToby Isaac }
5939a8fb8f29SToby Isaac 
594088bdff64SToby Isaac #undef __FUNCT__
594188bdff64SToby Isaac #define __FUNCT__ "DMGetFineDM"
594288bdff64SToby Isaac /*@
594388bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
594488bdff64SToby Isaac 
594588bdff64SToby Isaac   Input Parameter:
594688bdff64SToby Isaac . dm - The DM object
594788bdff64SToby Isaac 
594888bdff64SToby Isaac   Output Parameter:
594988bdff64SToby Isaac . fdm - The fine DM
595088bdff64SToby Isaac 
595188bdff64SToby Isaac   Level: intermediate
595288bdff64SToby Isaac 
595388bdff64SToby Isaac .seealso: DMSetFineDM()
595488bdff64SToby Isaac @*/
595588bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
595688bdff64SToby Isaac {
595788bdff64SToby Isaac   PetscFunctionBegin;
595888bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
595988bdff64SToby Isaac   PetscValidPointer(fdm, 2);
596088bdff64SToby Isaac   *fdm = dm->fineMesh;
596188bdff64SToby Isaac   PetscFunctionReturn(0);
596288bdff64SToby Isaac }
596388bdff64SToby Isaac 
596488bdff64SToby Isaac #undef __FUNCT__
596588bdff64SToby Isaac #define __FUNCT__ "DMSetFineDM"
596688bdff64SToby Isaac /*@
596788bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
596888bdff64SToby Isaac 
596988bdff64SToby Isaac   Input Parameters:
597088bdff64SToby Isaac + dm - The DM object
597188bdff64SToby Isaac - fdm - The fine DM
597288bdff64SToby Isaac 
597388bdff64SToby Isaac   Level: intermediate
597488bdff64SToby Isaac 
597588bdff64SToby Isaac .seealso: DMGetFineDM()
597688bdff64SToby Isaac @*/
597788bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
597888bdff64SToby Isaac {
597988bdff64SToby Isaac   PetscErrorCode ierr;
598088bdff64SToby Isaac 
598188bdff64SToby Isaac   PetscFunctionBegin;
598288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
598388bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
598488bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
598588bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
598688bdff64SToby Isaac   dm->fineMesh = fdm;
598788bdff64SToby Isaac   PetscFunctionReturn(0);
598888bdff64SToby Isaac }
598988bdff64SToby Isaac 
5990a6ba4734SToby Isaac /*=== DMBoundary code ===*/
5991a6ba4734SToby Isaac 
5992a6ba4734SToby Isaac #undef __FUNCT__
5993a6ba4734SToby Isaac #define __FUNCT__ "DMCopyBoundary"
5994a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
5995a6ba4734SToby Isaac {
5996a6ba4734SToby Isaac   PetscErrorCode ierr;
5997a6ba4734SToby Isaac 
5998a6ba4734SToby Isaac   PetscFunctionBegin;
5999e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6000a6ba4734SToby Isaac   PetscFunctionReturn(0);
6001a6ba4734SToby Isaac }
6002a6ba4734SToby Isaac 
6003a6ba4734SToby Isaac #undef __FUNCT__
6004a6ba4734SToby Isaac #define __FUNCT__ "DMAddBoundary"
6005a6ba4734SToby Isaac /*@C
6006a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6007a6ba4734SToby Isaac 
6008a6ba4734SToby Isaac   Input Parameters:
60094c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6010f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6011a6ba4734SToby Isaac . name        - The BC name
6012a6ba4734SToby Isaac . labelname   - The label defining constrained points
6013a6ba4734SToby Isaac . field       - The field to constrain
6014a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6015a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6016a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6017a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6018a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6019a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6020a6ba4734SToby Isaac 
6021a6ba4734SToby Isaac   Options Database Keys:
6022a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6023a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6024a6ba4734SToby Isaac 
6025a6ba4734SToby Isaac   Level: developer
6026a6ba4734SToby Isaac 
6027a6ba4734SToby Isaac .seealso: DMGetBoundary()
6028a6ba4734SToby Isaac @*/
6029f971fd6bSMatthew G. Knepley PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(), PetscInt numids, const PetscInt *ids, void *ctx)
6030a6ba4734SToby Isaac {
6031a6ba4734SToby Isaac   PetscErrorCode ierr;
6032a6ba4734SToby Isaac 
6033a6ba4734SToby Isaac   PetscFunctionBegin;
6034a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6035f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6036a6ba4734SToby Isaac   PetscFunctionReturn(0);
6037a6ba4734SToby Isaac }
6038a6ba4734SToby Isaac 
6039a6ba4734SToby Isaac #undef __FUNCT__
6040a6ba4734SToby Isaac #define __FUNCT__ "DMGetNumBoundary"
6041a6ba4734SToby Isaac /*@
6042a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6043a6ba4734SToby Isaac 
6044a6ba4734SToby Isaac   Input Parameters:
6045a6ba4734SToby Isaac . dm - The mesh object
6046a6ba4734SToby Isaac 
6047a6ba4734SToby Isaac   Output Parameters:
6048a6ba4734SToby Isaac . numBd - The number of BC
6049a6ba4734SToby Isaac 
6050a6ba4734SToby Isaac   Level: intermediate
6051a6ba4734SToby Isaac 
6052a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6053a6ba4734SToby Isaac @*/
6054a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6055a6ba4734SToby Isaac {
605658ebd649SToby Isaac   PetscErrorCode ierr;
6057a6ba4734SToby Isaac 
6058a6ba4734SToby Isaac   PetscFunctionBegin;
6059a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
606058ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6061a6ba4734SToby Isaac   PetscFunctionReturn(0);
6062a6ba4734SToby Isaac }
6063a6ba4734SToby Isaac 
6064a6ba4734SToby Isaac #undef __FUNCT__
6065a6ba4734SToby Isaac #define __FUNCT__ "DMGetBoundary"
6066a6ba4734SToby Isaac /*@C
6067a6ba4734SToby Isaac   DMGetBoundary - Add a boundary condition to the model
6068a6ba4734SToby Isaac 
6069a6ba4734SToby Isaac   Input Parameters:
6070a6ba4734SToby Isaac + dm          - The mesh object
6071a6ba4734SToby Isaac - bd          - The BC number
6072a6ba4734SToby Isaac 
6073a6ba4734SToby Isaac   Output Parameters:
6074f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6075a6ba4734SToby Isaac . name        - The BC name
6076a6ba4734SToby Isaac . labelname   - The label defining constrained points
6077a6ba4734SToby Isaac . field       - The field to constrain
6078a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6079a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6080a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6081a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6082a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6083a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6084a6ba4734SToby Isaac 
6085a6ba4734SToby Isaac   Options Database Keys:
6086a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6087a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6088a6ba4734SToby Isaac 
6089a6ba4734SToby Isaac   Level: developer
6090a6ba4734SToby Isaac 
6091a6ba4734SToby Isaac .seealso: DMAddBoundary()
6092a6ba4734SToby Isaac @*/
6093f971fd6bSMatthew G. Knepley PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(), PetscInt *numids, const PetscInt **ids, void **ctx)
6094a6ba4734SToby Isaac {
609558ebd649SToby Isaac   PetscErrorCode ierr;
6096a6ba4734SToby Isaac 
6097a6ba4734SToby Isaac   PetscFunctionBegin;
6098a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6099f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6100a6ba4734SToby Isaac   PetscFunctionReturn(0);
6101a6ba4734SToby Isaac }
6102a6ba4734SToby Isaac 
6103a6ba4734SToby Isaac #undef __FUNCT__
6104e6f8dbb6SToby Isaac #define __FUNCT__ "DMPopulateBoundary"
6105e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6106e6f8dbb6SToby Isaac {
6107dff059c6SToby Isaac   DMBoundary *lastnext;
6108e6f8dbb6SToby Isaac   DSBoundary dsbound;
6109e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6110e6f8dbb6SToby Isaac 
6111e6f8dbb6SToby Isaac   PetscFunctionBegin;
6112e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
611347a1f5adSToby Isaac   if (dm->boundary) {
611447a1f5adSToby Isaac     DMBoundary next = dm->boundary;
611547a1f5adSToby Isaac 
611647a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
611747a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
611847a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
611947a1f5adSToby Isaac     while (next) {
612047a1f5adSToby Isaac       DMBoundary b = next;
612147a1f5adSToby Isaac 
612247a1f5adSToby Isaac       next = b->next;
612347a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6124a6ba4734SToby Isaac     }
612547a1f5adSToby Isaac     dm->boundary = NULL;
6126a6ba4734SToby Isaac   }
612747a1f5adSToby Isaac 
6128dff059c6SToby Isaac   lastnext = &(dm->boundary);
6129e6f8dbb6SToby Isaac   while (dsbound) {
6130e6f8dbb6SToby Isaac     DMBoundary dmbound;
6131e6f8dbb6SToby Isaac 
6132e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6133e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6134e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6135e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
613647a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6137dff059c6SToby Isaac     *lastnext = dmbound;
6138dff059c6SToby Isaac     lastnext = &(dmbound->next);
6139dff059c6SToby Isaac     dsbound = dsbound->next;
6140a6ba4734SToby Isaac   }
6141a6ba4734SToby Isaac   PetscFunctionReturn(0);
6142a6ba4734SToby Isaac }
6143a6ba4734SToby Isaac 
6144a6ba4734SToby Isaac #undef __FUNCT__
6145a6ba4734SToby Isaac #define __FUNCT__ "DMIsBoundaryPoint"
6146a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6147a6ba4734SToby Isaac {
6148b95f2879SToby Isaac   DMBoundary     b;
6149a6ba4734SToby Isaac   PetscErrorCode ierr;
6150a6ba4734SToby Isaac 
6151a6ba4734SToby Isaac   PetscFunctionBegin;
6152a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6153a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6154a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6155e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6156b95f2879SToby Isaac   b = dm->boundary;
6157a6ba4734SToby Isaac   while (b && !(*isBd)) {
6158e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6159e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
61603424c85cSToby Isaac 
61613424c85cSToby Isaac     if (label) {
6162a6ba4734SToby Isaac       PetscInt i;
6163a6ba4734SToby Isaac 
6164e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6165e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6166a6ba4734SToby Isaac       }
6167a6ba4734SToby Isaac     }
6168a6ba4734SToby Isaac     b = b->next;
6169a6ba4734SToby Isaac   }
6170a6ba4734SToby Isaac   PetscFunctionReturn(0);
6171a6ba4734SToby Isaac }
61724d6f44ffSToby Isaac 
61734d6f44ffSToby Isaac #undef __FUNCT__
61744d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunction"
61754d6f44ffSToby Isaac /*@C
61764d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
61774d6f44ffSToby Isaac 
61784d6f44ffSToby Isaac   Input Parameters:
61794d6f44ffSToby Isaac + dm      - The DM
61800709b2feSToby Isaac . time    - The time
61814d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
61824d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
61834d6f44ffSToby Isaac - mode    - The insertion mode for values
61844d6f44ffSToby Isaac 
61854d6f44ffSToby Isaac   Output Parameter:
61864d6f44ffSToby Isaac . X - vector
61874d6f44ffSToby Isaac 
61884d6f44ffSToby Isaac    Calling sequence of func:
61890709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
61904d6f44ffSToby Isaac 
61914d6f44ffSToby Isaac +  dim - The spatial dimension
61924d6f44ffSToby Isaac .  x   - The coordinates
61934d6f44ffSToby Isaac .  Nf  - The number of fields
61944d6f44ffSToby Isaac .  u   - The output field values
61954d6f44ffSToby Isaac -  ctx - optional user-defined function context
61964d6f44ffSToby Isaac 
61974d6f44ffSToby Isaac   Level: developer
61984d6f44ffSToby Isaac 
61992716604bSToby Isaac .seealso: DMComputeL2Diff()
62004d6f44ffSToby Isaac @*/
62010709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
62024d6f44ffSToby Isaac {
62034d6f44ffSToby Isaac   Vec            localX;
62044d6f44ffSToby Isaac   PetscErrorCode ierr;
62054d6f44ffSToby Isaac 
62064d6f44ffSToby Isaac   PetscFunctionBegin;
62074d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62084d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62090709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62104d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62114d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62124d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62134d6f44ffSToby Isaac   PetscFunctionReturn(0);
62144d6f44ffSToby Isaac }
62154d6f44ffSToby Isaac 
62164d6f44ffSToby Isaac #undef __FUNCT__
62174d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLocal"
62180709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
62194d6f44ffSToby Isaac {
62204d6f44ffSToby Isaac   PetscErrorCode ierr;
62214d6f44ffSToby Isaac 
62224d6f44ffSToby Isaac   PetscFunctionBegin;
62234d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62244d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62254d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLocal",((PetscObject)dm)->type_name);
62260709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62274d6f44ffSToby Isaac   PetscFunctionReturn(0);
62284d6f44ffSToby Isaac }
62294d6f44ffSToby Isaac 
62304d6f44ffSToby Isaac #undef __FUNCT__
62314d6f44ffSToby Isaac #define __FUNCT__ "DMProjectFunctionLabelLocal"
62320709b2feSToby Isaac PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
62334d6f44ffSToby Isaac {
62344d6f44ffSToby Isaac   PetscErrorCode ierr;
62354d6f44ffSToby Isaac 
62364d6f44ffSToby Isaac   PetscFunctionBegin;
62374d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62384d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62394d6f44ffSToby Isaac   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
62400709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62414d6f44ffSToby Isaac   PetscFunctionReturn(0);
62424d6f44ffSToby Isaac }
62432716604bSToby Isaac 
62442716604bSToby Isaac #undef __FUNCT__
62458c6c5593SMatthew G. Knepley #define __FUNCT__ "DMProjectFieldLocal"
62468c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
62478c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
62488c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62498c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62508c6c5593SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscScalar[]),
62518c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
62528c6c5593SMatthew G. Knepley {
62538c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62548c6c5593SMatthew G. Knepley 
62558c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62568c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
62588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
62598c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
62608c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
62618c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62628c6c5593SMatthew G. Knepley }
62638c6c5593SMatthew G. Knepley 
62648c6c5593SMatthew G. Knepley #undef __FUNCT__
62658c6c5593SMatthew G. Knepley #define __FUNCT__ "DMProjectFieldLabelLocal"
62668c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], Vec localU,
62678c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
62688c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62698c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
62708c6c5593SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscScalar[]),
62718c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
62728c6c5593SMatthew G. Knepley {
62738c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
62748c6c5593SMatthew G. Knepley 
62758c6c5593SMatthew G. Knepley   PetscFunctionBegin;
62768c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62778c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
62788c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
62798c6c5593SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMProjectFieldLocal",((PetscObject)dm)->type_name);
62808c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, localU, funcs, mode, localX);CHKERRQ(ierr);
62818c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
62828c6c5593SMatthew G. Knepley }
62838c6c5593SMatthew G. Knepley 
62848c6c5593SMatthew G. Knepley #undef __FUNCT__
62852716604bSToby Isaac #define __FUNCT__ "DMComputeL2Diff"
62862716604bSToby Isaac /*@C
62872716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
62882716604bSToby Isaac 
62892716604bSToby Isaac   Input Parameters:
62902716604bSToby Isaac + dm    - The DM
62910709b2feSToby Isaac . time  - The time
62922716604bSToby Isaac . funcs - The functions to evaluate for each field component
62932716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
62942716604bSToby Isaac - X     - The coefficient vector u_h
62952716604bSToby Isaac 
62962716604bSToby Isaac   Output Parameter:
62972716604bSToby Isaac . diff - The diff ||u - u_h||_2
62982716604bSToby Isaac 
62992716604bSToby Isaac   Level: developer
63002716604bSToby Isaac 
63011189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63022716604bSToby Isaac @*/
63030709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
63042716604bSToby Isaac {
63052716604bSToby Isaac   PetscErrorCode ierr;
63062716604bSToby Isaac 
63072716604bSToby Isaac   PetscFunctionBegin;
63082716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6309b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63102716604bSToby Isaac   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2Diff",((PetscObject)dm)->type_name);
63110709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63122716604bSToby Isaac   PetscFunctionReturn(0);
63132716604bSToby Isaac }
6314b698f381SToby Isaac 
6315b698f381SToby Isaac #undef __FUNCT__
6316b698f381SToby Isaac #define __FUNCT__ "DMComputeL2GradientDiff"
6317b698f381SToby Isaac /*@C
6318b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6319b698f381SToby Isaac 
6320b698f381SToby Isaac   Input Parameters:
6321b698f381SToby Isaac + dm    - The DM
6322b698f381SToby Isaac , time  - The time
6323b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6324b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6325b698f381SToby Isaac . X     - The coefficient vector u_h
6326b698f381SToby Isaac - n     - The vector to project along
6327b698f381SToby Isaac 
6328b698f381SToby Isaac   Output Parameter:
6329b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6330b698f381SToby Isaac 
6331b698f381SToby Isaac   Level: developer
6332b698f381SToby Isaac 
6333b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6334b698f381SToby Isaac @*/
6335b698f381SToby Isaac PetscErrorCode DMComputeL2GradientDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], const PetscReal[], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, const PetscReal n[], PetscReal *diff)
6336b698f381SToby Isaac {
6337b698f381SToby Isaac   PetscErrorCode ierr;
6338b698f381SToby Isaac 
6339b698f381SToby Isaac   PetscFunctionBegin;
6340b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6341b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6342b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6343b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6344b698f381SToby Isaac   PetscFunctionReturn(0);
6345b698f381SToby Isaac }
6346b698f381SToby Isaac 
63472a16baeaSToby Isaac #undef __FUNCT__
63482a16baeaSToby Isaac #define __FUNCT__ "DMComputeL2FieldDiff"
63492a16baeaSToby Isaac /*@C
63502a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
63512a16baeaSToby Isaac 
63522a16baeaSToby Isaac   Input Parameters:
63532a16baeaSToby Isaac + dm    - The DM
63542a16baeaSToby Isaac . time  - The time
63552a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
63562a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
63572a16baeaSToby Isaac - X     - The coefficient vector u_h
63582a16baeaSToby Isaac 
63592a16baeaSToby Isaac   Output Parameter:
63602a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
63612a16baeaSToby Isaac 
63622a16baeaSToby Isaac   Level: developer
63632a16baeaSToby Isaac 
63641189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63652a16baeaSToby Isaac @*/
63661189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
63672a16baeaSToby Isaac {
63682a16baeaSToby Isaac   PetscErrorCode ierr;
63692a16baeaSToby Isaac 
63702a16baeaSToby Isaac   PetscFunctionBegin;
63712a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63722a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63732a16baeaSToby Isaac   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
63742a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63752a16baeaSToby Isaac   PetscFunctionReturn(0);
63762a16baeaSToby Isaac }
63772a16baeaSToby Isaac 
6378df0b854cSToby Isaac #undef __FUNCT__
6379df0b854cSToby Isaac #define __FUNCT__ "DMAdaptLabel"
6380df0b854cSToby Isaac /*@C
6381df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6382cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6383df0b854cSToby Isaac 
6384df0b854cSToby Isaac   Collective on dm
6385df0b854cSToby Isaac 
6386df0b854cSToby Isaac   Input parameters:
6387df0b854cSToby Isaac + dm - the pre-adaptation DM object
6388a1b0c543SToby Isaac - label - label with the flags
6389df0b854cSToby Isaac 
6390df0b854cSToby Isaac   Output parameters:
6391df0b854cSToby Isaac . adaptedDM - the adapted DM object: may be NULL if an adapted DM could not be produced.
6392df0b854cSToby Isaac 
6393df0b854cSToby Isaac   Level: intermediate
6394df0b854cSToby Isaac @*/
6395a1b0c543SToby Isaac PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *adaptedDM)
6396df0b854cSToby Isaac {
6397df0b854cSToby Isaac   PetscErrorCode ierr;
6398df0b854cSToby Isaac 
6399df0b854cSToby Isaac   PetscFunctionBegin;
6400df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6401a1b0c543SToby Isaac   PetscValidPointer(label,2);
6402df0b854cSToby Isaac   PetscValidPointer(adaptedDM,3);
6403df0b854cSToby Isaac   *adaptedDM = NULL;
6404a1b0c543SToby Isaac   ierr = PetscTryMethod((PetscObject)dm,"DMAdaptLabel_C",(DM,DMLabel, DM*),(dm,label,adaptedDM));CHKERRQ(ierr);
6405df0b854cSToby Isaac   PetscFunctionReturn(0);
6406df0b854cSToby Isaac }
6407c4088d22SMatthew G. Knepley 
6408c4088d22SMatthew G. Knepley #undef __FUNCT__
6409502a2867SDave May #define __FUNCT__ "DMGetNeighbors"
6410502a2867SDave May /*@C
6411502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6412502a2867SDave May 
6413502a2867SDave May  Not Collective
6414502a2867SDave May 
6415502a2867SDave May  Input Parameter:
6416502a2867SDave May  . dm    - The DM
6417502a2867SDave May 
6418502a2867SDave May  Output Parameter:
6419502a2867SDave May  . nranks - the number of neighbours
6420502a2867SDave May  . ranks - the neighbors ranks
6421502a2867SDave May 
6422502a2867SDave May  Notes:
6423502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6424502a2867SDave May 
6425502a2867SDave May  Level: beginner
6426502a2867SDave May 
6427dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6428502a2867SDave May @*/
6429502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6430502a2867SDave May {
6431502a2867SDave May   PetscErrorCode ierr;
6432502a2867SDave May 
6433502a2867SDave May   PetscFunctionBegin;
6434502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6435502a2867SDave May   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implemnt DMGetNeighbors",((PetscObject)dm)->type_name);
6436502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6437502a2867SDave May   PetscFunctionReturn(0);
6438502a2867SDave May }
6439502a2867SDave May 
6440531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6441531c7667SBarry Smith 
6442531c7667SBarry Smith #undef __FUNCT__
6443531c7667SBarry Smith #define __FUNCT__ "MatFDColoringApply_AIJDM"
6444531c7667SBarry Smith /*
6445531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6446531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6447531c7667SBarry Smith */
6448531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6449531c7667SBarry Smith {
6450531c7667SBarry Smith   PetscErrorCode ierr;
6451531c7667SBarry Smith 
6452531c7667SBarry Smith   PetscFunctionBegin;
6453531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6454531c7667SBarry Smith     Vec x1local;
6455531c7667SBarry Smith     DM  dm;
6456531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6457531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6458531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6459531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6460531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6461531c7667SBarry Smith     x1   = x1local;
6462531c7667SBarry Smith   }
6463531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6464531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6465531c7667SBarry Smith     DM  dm;
6466531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6467531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6468531c7667SBarry Smith   }
6469531c7667SBarry Smith   PetscFunctionReturn(0);
6470531c7667SBarry Smith }
6471531c7667SBarry Smith 
6472531c7667SBarry Smith #undef __FUNCT__
6473531c7667SBarry Smith #define __FUNCT__ "MatFDColoringUseDM"
6474531c7667SBarry Smith /*@
6475531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6476531c7667SBarry Smith 
6477531c7667SBarry Smith     Input Parameter:
6478531c7667SBarry Smith .    coloring - the MatFDColoring object
6479531c7667SBarry Smith 
6480531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6481531c7667SBarry Smith 
6482531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6483531c7667SBarry Smith @*/
6484531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6485531c7667SBarry Smith {
6486531c7667SBarry Smith   PetscFunctionBegin;
6487531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6488531c7667SBarry Smith   PetscFunctionReturn(0);
6489531c7667SBarry Smith }
6490