xref: /petsc/src/dm/interface/dm.c (revision f9d4088a99e796628d3bc9f37c9e81c8a18e0f51)
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>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
101ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1167a56275SMatthew G Knepley 
12e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
13bff4a2f0SMatthew G. Knepley 
144a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
154a7a4c06SLawrence Mitchell {
164a7a4c06SLawrence Mitchell   PetscFunctionBegin;
174a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
184a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
194a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
204a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
214a7a4c06SLawrence Mitchell }
224a7a4c06SLawrence Mitchell 
23a4121054SBarry Smith /*@
24de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
25a4121054SBarry Smith 
26a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
27a4121054SBarry Smith    error when you try to use the vector.
28a4121054SBarry Smith 
29a4121054SBarry Smith   Collective on MPI_Comm
30a4121054SBarry Smith 
31a4121054SBarry Smith   Input Parameter:
32a4121054SBarry Smith . comm - The communicator for the DM object
33a4121054SBarry Smith 
34a4121054SBarry Smith   Output Parameter:
35a4121054SBarry Smith . dm - The DM object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Level: beginner
38a4121054SBarry Smith 
398472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
40a4121054SBarry Smith @*/
417087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
42a4121054SBarry Smith {
43a4121054SBarry Smith   DM             v;
44a4121054SBarry Smith   PetscErrorCode ierr;
45a4121054SBarry Smith 
46a4121054SBarry Smith   PetscFunctionBegin;
471411c6eeSJed Brown   PetscValidPointer(dm,2);
480298fd71SBarry Smith   *dm = NULL;
498b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
50607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
51607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
52607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
53a4121054SBarry Smith 
5473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
55e7c4fc90SDmitry Karpeev 
560298fd71SBarry Smith   v->ltogmap                  = NULL;
571411c6eeSJed Brown   v->bs                       = 1;
58171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
6088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
61c58f1c22SToby Isaac   v->labels                   = NULL;
62c58f1c22SToby Isaac   v->depthLabel               = NULL;
630298fd71SBarry Smith   v->defaultSection           = NULL;
640298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
65fba222abSToby Isaac   v->defaultConstraintSection = NULL;
66fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
67c6b900c6SMatthew G. Knepley   v->L                        = NULL;
68c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
695dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
709a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7196173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
72435a35e8SMatthew G Knepley   {
73435a35e8SMatthew G Knepley     PetscInt i;
74435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
750298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
76*f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
77435a35e8SMatthew G Knepley     }
78435a35e8SMatthew G Knepley   }
792764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
8014f150ffSMatthew G. Knepley   v->dmBC = NULL;
81a8fb8f29SToby Isaac   v->coarseMesh = NULL;
82f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
83cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
84c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
85b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
86bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
87c58f1c22SToby Isaac   v->labels->refct = 1;
884a7a4c06SLawrence Mitchell 
894a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
904a7a4c06SLawrence Mitchell 
911411c6eeSJed Brown   *dm = v;
92a4121054SBarry Smith   PetscFunctionReturn(0);
93a4121054SBarry Smith }
94a4121054SBarry Smith 
9538221697SMatthew G. Knepley /*@
9638221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9738221697SMatthew G. Knepley 
9838221697SMatthew G. Knepley   Collective on MPI_Comm
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   Input Parameter:
10138221697SMatthew G. Knepley . dm - The original DM object
10238221697SMatthew G. Knepley 
10338221697SMatthew G. Knepley   Output Parameter:
10438221697SMatthew G. Knepley . newdm  - The new DM object
10538221697SMatthew G. Knepley 
10638221697SMatthew G. Knepley   Level: beginner
10738221697SMatthew G. Knepley 
10838221697SMatthew G. Knepley .keywords: DM, topology, create
10938221697SMatthew G. Knepley @*/
11038221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11138221697SMatthew G. Knepley {
11238221697SMatthew G. Knepley   PetscSF        sf;
11338221697SMatthew G. Knepley   Vec            coords;
11438221697SMatthew G. Knepley   void          *ctx;
115a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11638221697SMatthew G. Knepley   PetscErrorCode ierr;
11738221697SMatthew G. Knepley 
11838221697SMatthew G. Knepley   PetscFunctionBegin;
11938221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12038221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12138221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
122c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
123c58f1c22SToby Isaac   dm->labels->refct++;
124c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
125c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1261de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1271de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12838221697SMatthew G. Knepley   if (dm->ops->clone) {
12938221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13038221697SMatthew G. Knepley   }
1313f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13238221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
136be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
137be4c1c3eSMatthew G. Knepley     DM           ncdm;
138be4c1c3eSMatthew G. Knepley     PetscSection cs;
1395a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
140be4c1c3eSMatthew G. Knepley 
141e87a4003SBarry Smith     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
142be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1435a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1445a0206caSToby Isaac     if (pEndMax >= 0) {
145be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
146e87a4003SBarry Smith       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
147a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
148be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
149be4c1c3eSMatthew G. Knepley     }
150be4c1c3eSMatthew G. Knepley   }
151a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
152a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15438221697SMatthew G. Knepley   if (coords) {
15538221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15638221697SMatthew G. Knepley   } else {
15738221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15838221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15938221697SMatthew G. Knepley   }
16090b157c4SStefano Zampini   {
16190b157c4SStefano Zampini     PetscBool             isper;
162c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1635dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16490b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16590b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
166c6b900c6SMatthew G. Knepley   }
16738221697SMatthew G. Knepley   PetscFunctionReturn(0);
16838221697SMatthew G. Knepley }
16938221697SMatthew G. Knepley 
1709a42bb27SBarry Smith /*@C
171564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1729a42bb27SBarry Smith 
1738472ad0fSDave May    Logically Collective on DM
1749a42bb27SBarry Smith 
1759a42bb27SBarry Smith    Input Parameter:
1769a42bb27SBarry Smith +  da - initial distributed array
177e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1789a42bb27SBarry Smith 
1799a42bb27SBarry Smith    Options Database:
180dd85299cSBarry Smith .   -dm_vec_type ctype
1819a42bb27SBarry Smith 
1829a42bb27SBarry Smith    Level: intermediate
1839a42bb27SBarry Smith 
1848472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1859a42bb27SBarry Smith @*/
18619fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1879a42bb27SBarry Smith {
1889a42bb27SBarry Smith   PetscErrorCode ierr;
1899a42bb27SBarry Smith 
1909a42bb27SBarry Smith   PetscFunctionBegin;
1919a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1929a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19319fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1949a42bb27SBarry Smith   PetscFunctionReturn(0);
1959a42bb27SBarry Smith }
1969a42bb27SBarry Smith 
197c0dedaeaSBarry Smith /*@C
198c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
199c0dedaeaSBarry Smith 
2008472ad0fSDave May    Logically Collective on DM
201c0dedaeaSBarry Smith 
202c0dedaeaSBarry Smith    Input Parameter:
203c0dedaeaSBarry Smith .  da - initial distributed array
204c0dedaeaSBarry Smith 
205c0dedaeaSBarry Smith    Output Parameter:
206c0dedaeaSBarry Smith .  ctype - the vector type
207c0dedaeaSBarry Smith 
208c0dedaeaSBarry Smith    Level: intermediate
209c0dedaeaSBarry Smith 
2108472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
211c0dedaeaSBarry Smith @*/
212c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
213c0dedaeaSBarry Smith {
214c0dedaeaSBarry Smith   PetscFunctionBegin;
215c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
216c0dedaeaSBarry Smith   *ctype = da->vectype;
217c0dedaeaSBarry Smith   PetscFunctionReturn(0);
218c0dedaeaSBarry Smith }
219c0dedaeaSBarry Smith 
2205f1ad066SMatthew G Knepley /*@
22134f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2225f1ad066SMatthew G Knepley 
2235f1ad066SMatthew G Knepley   Not collective
2245f1ad066SMatthew G Knepley 
2255f1ad066SMatthew G Knepley   Input Parameter:
2265f1ad066SMatthew G Knepley . v - The Vec
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Output Parameter:
2295f1ad066SMatthew G Knepley . dm - The DM
2305f1ad066SMatthew G Knepley 
2315f1ad066SMatthew G Knepley   Level: intermediate
2325f1ad066SMatthew G Knepley 
2335f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2345f1ad066SMatthew G Knepley @*/
2355f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2365f1ad066SMatthew G Knepley {
2375f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2385f1ad066SMatthew G Knepley 
2395f1ad066SMatthew G Knepley   PetscFunctionBegin;
2405f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2415f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2425f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2435f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2445f1ad066SMatthew G Knepley }
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley /*@
247d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2485f1ad066SMatthew G Knepley 
2495f1ad066SMatthew G Knepley   Not collective
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   Input Parameters:
2525f1ad066SMatthew G Knepley + v - The Vec
2535f1ad066SMatthew G Knepley - dm - The DM
2545f1ad066SMatthew G Knepley 
255d9805387SMatthew 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.
256d9805387SMatthew G. Knepley 
2575f1ad066SMatthew G Knepley   Level: intermediate
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2605f1ad066SMatthew G Knepley @*/
2615f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2625f1ad066SMatthew G Knepley {
2635f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2645f1ad066SMatthew G Knepley 
2655f1ad066SMatthew G Knepley   PetscFunctionBegin;
2665f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
267d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2685f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2695f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2705f1ad066SMatthew G Knepley }
2715f1ad066SMatthew G Knepley 
272521d9a4cSLisandro Dalcin /*@C
2738f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2748f1509bcSBarry Smith 
2758f1509bcSBarry Smith    Logically Collective on DM
2768f1509bcSBarry Smith 
2778f1509bcSBarry Smith    Input Parameters:
2788f1509bcSBarry Smith +  dm - the DM context
2798f1509bcSBarry Smith -  ctype - the matrix type
2808f1509bcSBarry Smith 
2818f1509bcSBarry Smith    Options Database:
2828f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2838f1509bcSBarry Smith 
2848f1509bcSBarry Smith    Level: intermediate
2858f1509bcSBarry Smith 
2868f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2878f1509bcSBarry Smith           DMGetISColoringType()
2888f1509bcSBarry Smith @*/
2898f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2908f1509bcSBarry Smith {
2918f1509bcSBarry Smith   PetscFunctionBegin;
2928f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2938f1509bcSBarry Smith   dm->coloringtype = ctype;
2948f1509bcSBarry Smith   PetscFunctionReturn(0);
2958f1509bcSBarry Smith }
2968f1509bcSBarry Smith 
2978f1509bcSBarry Smith /*@C
2988f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
299521d9a4cSLisandro Dalcin 
300521d9a4cSLisandro Dalcin    Logically Collective on DM
301521d9a4cSLisandro Dalcin 
302521d9a4cSLisandro Dalcin    Input Parameter:
3038f1509bcSBarry Smith .  dm - the DM context
3048f1509bcSBarry Smith 
3058f1509bcSBarry Smith    Output Parameter:
3068f1509bcSBarry Smith .  ctype - the matrix type
3078f1509bcSBarry Smith 
3088f1509bcSBarry Smith    Options Database:
3098f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3108f1509bcSBarry Smith 
3118f1509bcSBarry Smith    Level: intermediate
3128f1509bcSBarry Smith 
3138f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3148f1509bcSBarry Smith           DMGetISColoringType()
3158f1509bcSBarry Smith @*/
3168f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3178f1509bcSBarry Smith {
3188f1509bcSBarry Smith   PetscFunctionBegin;
3198f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3208f1509bcSBarry Smith   *ctype = dm->coloringtype;
3218f1509bcSBarry Smith   PetscFunctionReturn(0);
3228f1509bcSBarry Smith }
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith /*@C
3258f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3268f1509bcSBarry Smith 
3278f1509bcSBarry Smith    Logically Collective on DM
3288f1509bcSBarry Smith 
3298f1509bcSBarry Smith    Input Parameters:
330521d9a4cSLisandro Dalcin +  dm - the DM context
331a2b5a043SBarry Smith -  ctype - the matrix type
332521d9a4cSLisandro Dalcin 
333521d9a4cSLisandro Dalcin    Options Database:
334521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
335521d9a4cSLisandro Dalcin 
336521d9a4cSLisandro Dalcin    Level: intermediate
337521d9a4cSLisandro Dalcin 
338c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
339521d9a4cSLisandro Dalcin @*/
34019fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
341521d9a4cSLisandro Dalcin {
342521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34388f0584fSBarry Smith 
344521d9a4cSLisandro Dalcin   PetscFunctionBegin;
345521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
346521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
34719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
348521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
349521d9a4cSLisandro Dalcin }
350521d9a4cSLisandro Dalcin 
351c0dedaeaSBarry Smith /*@C
352c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
353c0dedaeaSBarry Smith 
354c0dedaeaSBarry Smith    Logically Collective on DM
355c0dedaeaSBarry Smith 
356c0dedaeaSBarry Smith    Input Parameter:
357c0dedaeaSBarry Smith .  dm - the DM context
358c0dedaeaSBarry Smith 
359c0dedaeaSBarry Smith    Output Parameter:
360c0dedaeaSBarry Smith .  ctype - the matrix type
361c0dedaeaSBarry Smith 
362c0dedaeaSBarry Smith    Options Database:
363c0dedaeaSBarry Smith .   -dm_mat_type ctype
364c0dedaeaSBarry Smith 
365c0dedaeaSBarry Smith    Level: intermediate
366c0dedaeaSBarry Smith 
367c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
368c0dedaeaSBarry Smith @*/
369c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
370c0dedaeaSBarry Smith {
371c0dedaeaSBarry Smith   PetscFunctionBegin;
372c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
373c0dedaeaSBarry Smith   *ctype = dm->mattype;
374c0dedaeaSBarry Smith   PetscFunctionReturn(0);
375c0dedaeaSBarry Smith }
376c0dedaeaSBarry Smith 
377c688c046SMatthew G Knepley /*@
37834f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
379c688c046SMatthew G Knepley 
380c688c046SMatthew G Knepley   Not collective
381c688c046SMatthew G Knepley 
382c688c046SMatthew G Knepley   Input Parameter:
383c688c046SMatthew G Knepley . A - The Mat
384c688c046SMatthew G Knepley 
385c688c046SMatthew G Knepley   Output Parameter:
386c688c046SMatthew G Knepley . dm - The DM
387c688c046SMatthew G Knepley 
388c688c046SMatthew G Knepley   Level: intermediate
389c688c046SMatthew G Knepley 
3908f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3918f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3928f1509bcSBarry Smith 
393c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
394c688c046SMatthew G Knepley @*/
395c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
396c688c046SMatthew G Knepley {
397c688c046SMatthew G Knepley   PetscErrorCode ierr;
398c688c046SMatthew G Knepley 
399c688c046SMatthew G Knepley   PetscFunctionBegin;
400c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
401c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
402c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
403c688c046SMatthew G Knepley   PetscFunctionReturn(0);
404c688c046SMatthew G Knepley }
405c688c046SMatthew G Knepley 
406c688c046SMatthew G Knepley /*@
407c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
408c688c046SMatthew G Knepley 
409c688c046SMatthew G Knepley   Not collective
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley   Input Parameters:
412c688c046SMatthew G Knepley + A - The Mat
413c688c046SMatthew G Knepley - dm - The DM
414c688c046SMatthew G Knepley 
415c688c046SMatthew G Knepley   Level: intermediate
416c688c046SMatthew G Knepley 
4178f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4188f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4198f1509bcSBarry Smith 
4208f1509bcSBarry Smith 
421c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
422c688c046SMatthew G Knepley @*/
423c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
424c688c046SMatthew G Knepley {
425c688c046SMatthew G Knepley   PetscErrorCode ierr;
426c688c046SMatthew G Knepley 
427c688c046SMatthew G Knepley   PetscFunctionBegin;
428c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4298865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
430c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
431c688c046SMatthew G Knepley   PetscFunctionReturn(0);
432c688c046SMatthew G Knepley }
433c688c046SMatthew G Knepley 
4349a42bb27SBarry Smith /*@C
4359a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4366757b960SDave May    DM options in the database.
4379a42bb27SBarry Smith 
4388353ddbbSDave May    Logically Collective on DM
4399a42bb27SBarry Smith 
4409a42bb27SBarry Smith    Input Parameter:
4418353ddbbSDave May +  da - the DM context
4429a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4439a42bb27SBarry Smith 
4449a42bb27SBarry Smith    Notes:
4459a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4469a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4479a42bb27SBarry Smith 
4489a42bb27SBarry Smith    Level: advanced
4499a42bb27SBarry Smith 
4508353ddbbSDave May .keywords: DM, set, options, prefix, database
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith .seealso: DMSetFromOptions()
4539a42bb27SBarry Smith @*/
4547087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4559a42bb27SBarry Smith {
4569a42bb27SBarry Smith   PetscErrorCode ierr;
4579a42bb27SBarry Smith 
4589a42bb27SBarry Smith   PetscFunctionBegin;
4599a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4609a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
461691be533SLawrence Mitchell   if (dm->sf) {
462691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
463691be533SLawrence Mitchell   }
464691be533SLawrence Mitchell   if (dm->defaultSF) {
465691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
466691be533SLawrence Mitchell   }
4679a42bb27SBarry Smith   PetscFunctionReturn(0);
4689a42bb27SBarry Smith }
4699a42bb27SBarry Smith 
47031697293SDave May /*@C
47131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47231697293SDave May    DM options in the database.
47331697293SDave May 
47431697293SDave May    Logically Collective on DM
47531697293SDave May 
47631697293SDave May    Input Parameters:
47731697293SDave May +  dm - the DM context
47831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
47931697293SDave May 
48031697293SDave May    Notes:
48131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48331697293SDave May 
48431697293SDave May    Level: advanced
48531697293SDave May 
48631697293SDave May .keywords: DM, append, options, prefix, database
48731697293SDave May 
48831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
48931697293SDave May @*/
49031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49131697293SDave May {
49231697293SDave May   PetscErrorCode ierr;
49331697293SDave May 
49431697293SDave May   PetscFunctionBegin;
49531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49731697293SDave May   PetscFunctionReturn(0);
49831697293SDave May }
49931697293SDave May 
50031697293SDave May /*@C
50131697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50231697293SDave May    DM options in the database.
50331697293SDave May 
50431697293SDave May    Not Collective
50531697293SDave May 
50631697293SDave May    Input Parameters:
50731697293SDave May .  dm - the DM context
50831697293SDave May 
50931697293SDave May    Output Parameters:
51031697293SDave May .  prefix - pointer to the prefix string used is returned
51131697293SDave May 
51295452b02SPatrick Sanan    Notes:
51395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51431697293SDave May    sufficient length to hold the prefix.
51531697293SDave May 
51631697293SDave May    Level: advanced
51731697293SDave May 
51831697293SDave May .keywords: DM, set, options, prefix, database
51931697293SDave May 
52031697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52131697293SDave May @*/
52231697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52331697293SDave May {
52431697293SDave May   PetscErrorCode ierr;
52531697293SDave May 
52631697293SDave May   PetscFunctionBegin;
52731697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52831697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52931697293SDave May   PetscFunctionReturn(0);
53031697293SDave May }
53131697293SDave May 
53288bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53388bdff64SToby Isaac {
53488bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53588bdff64SToby Isaac   DMNamedVecLink nlink;
53688bdff64SToby Isaac   PetscErrorCode ierr;
53788bdff64SToby Isaac 
53888bdff64SToby Isaac   PetscFunctionBegin;
539aab5bcd8SJed Brown   *ncrefct = 0;
54088bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54188bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54288bdff64SToby Isaac     if (dm->localin[i])  refct--;
54388bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54488bdff64SToby Isaac   }
54588bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54688bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54788bdff64SToby Isaac   if (dm->x) {
54888bdff64SToby Isaac     DM obj;
54988bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
55088bdff64SToby Isaac     if (obj == dm) refct--;
55188bdff64SToby Isaac   }
55288bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55388bdff64SToby Isaac     refct--;
55488bdff64SToby Isaac     if (recurseCoarse) {
55588bdff64SToby Isaac       PetscInt coarseCount;
55688bdff64SToby Isaac 
55788bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55888bdff64SToby Isaac       refct += coarseCount;
55988bdff64SToby Isaac     }
56088bdff64SToby Isaac   }
56188bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56288bdff64SToby Isaac     refct--;
56388bdff64SToby Isaac     if (recurseFine) {
56488bdff64SToby Isaac       PetscInt fineCount;
56588bdff64SToby Isaac 
56688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56788bdff64SToby Isaac       refct += fineCount;
56888bdff64SToby Isaac     }
56988bdff64SToby Isaac   }
57088bdff64SToby Isaac   *ncrefct = refct;
57188bdff64SToby Isaac   PetscFunctionReturn(0);
57288bdff64SToby Isaac }
57388bdff64SToby Isaac 
574354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
575354557abSToby Isaac {
576354557abSToby Isaac   PetscErrorCode ierr;
577354557abSToby Isaac 
578354557abSToby Isaac   PetscFunctionBegin;
579354557abSToby Isaac   if (!--(dm->labels->refct)) {
580354557abSToby Isaac     DMLabelLink next = dm->labels->next;
581354557abSToby Isaac 
582354557abSToby Isaac     /* destroy the labels */
583354557abSToby Isaac     while (next) {
584354557abSToby Isaac       DMLabelLink tmp = next->next;
585354557abSToby Isaac 
586354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
587354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
588354557abSToby Isaac       next = tmp;
589354557abSToby Isaac     }
590354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
591354557abSToby Isaac   }
592354557abSToby Isaac   PetscFunctionReturn(0);
593354557abSToby Isaac }
594354557abSToby Isaac 
59547c6ae99SBarry Smith /*@
5968472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Collective on DM
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith     Input Parameter:
60147c6ae99SBarry Smith .   dm - the DM object to destroy
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith     Level: developer
60447c6ae99SBarry Smith 
605e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith @*/
608fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60947c6ae99SBarry Smith {
61088bdff64SToby Isaac   PetscInt       i, cnt;
611dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61247c6ae99SBarry Smith   PetscErrorCode ierr;
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith   PetscFunctionBegin;
6156bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6166bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61787e657c6SBarry Smith 
61888bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61988bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62088bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
62188bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
622732e2eb9SMatthew G Knepley   /*
623732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
624732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
625732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
626732e2eb9SMatthew G Knepley   */
6276bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6286bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
629732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6306bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6316bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
632732e2eb9SMatthew G Knepley   }
633f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6340298fd71SBarry Smith   (*dm)->namedglobal = NULL;
635f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6362348bcf4SPeter Brune     nnext = nlink->next;
6372348bcf4SPeter 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);
6382348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6392348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6402348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6412348bcf4SPeter Brune   }
642f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6430298fd71SBarry Smith   (*dm)->namedlocal = NULL;
644f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
645f490541aSPeter Brune     nnext = nlink->next;
646f490541aSPeter 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);
647f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
648f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
649f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
650f490541aSPeter Brune   }
6512348bcf4SPeter Brune 
652b17ce1afSJed Brown   /* Destroy the list of hooks */
653c833c3b5SJed Brown   {
654c833c3b5SJed Brown     DMCoarsenHookLink link,next;
655b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
656b17ce1afSJed Brown       next = link->next;
657b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
658b17ce1afSJed Brown     }
6590298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
660c833c3b5SJed Brown   }
661c833c3b5SJed Brown   {
662c833c3b5SJed Brown     DMRefineHookLink link,next;
663c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
664c833c3b5SJed Brown       next = link->next;
665c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
666c833c3b5SJed Brown     }
6670298fd71SBarry Smith     (*dm)->refinehook = NULL;
668c833c3b5SJed Brown   }
669be081cd6SPeter Brune   {
670be081cd6SPeter Brune     DMSubDomainHookLink link,next;
671be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
672be081cd6SPeter Brune       next = link->next;
673be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
674be081cd6SPeter Brune     }
6750298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
676be081cd6SPeter Brune   }
677baf369e7SPeter Brune   {
678baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
679baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
680baf369e7SPeter Brune       next = link->next;
681baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
682baf369e7SPeter Brune     }
6830298fd71SBarry Smith     (*dm)->gtolhook = NULL;
684baf369e7SPeter Brune   }
685d4d07f1eSToby Isaac   {
686d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
687d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
688d4d07f1eSToby Isaac       next = link->next;
689d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
690d4d07f1eSToby Isaac     }
691d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
692d4d07f1eSToby Isaac   }
693aa1993deSMatthew G Knepley   /* Destroy the work arrays */
694aa1993deSMatthew G Knepley   {
695aa1993deSMatthew G Knepley     DMWorkLink link,next;
696aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
697aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
698aa1993deSMatthew G Knepley       next = link->next;
699aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
700aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
701aa1993deSMatthew G Knepley     }
7020298fd71SBarry Smith     (*dm)->workin = NULL;
703aa1993deSMatthew G Knepley   }
704c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
705c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
706c58f1c22SToby Isaac 
707c58f1c22SToby Isaac     /* destroy the labels */
708c58f1c22SToby Isaac     while (next) {
709c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
710c58f1c22SToby Isaac 
711c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
712c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
713c58f1c22SToby Isaac       next = tmp;
714c58f1c22SToby Isaac     }
715c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
716c58f1c22SToby Isaac   }
717e6f8dbb6SToby Isaac   {
718e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
719e6f8dbb6SToby Isaac     while (next) {
720e6f8dbb6SToby Isaac       DMBoundary b = next;
721e6f8dbb6SToby Isaac 
722e6f8dbb6SToby Isaac       next = b->next;
723e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
724e6f8dbb6SToby Isaac     }
725e6f8dbb6SToby Isaac   }
726b17ce1afSJed Brown 
72752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
73052536dc3SBarry Smith 
7311a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7321a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7331a266240SBarry Smith   }
73487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
73571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7364dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7376bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7386bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
739073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
74088ed4aceSMatthew G Knepley 
74188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
74288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7438b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
744fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
745fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
74688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
74788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
748736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
749736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7508e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
751736995cdSBlaise Bourdin     }
752736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
753736995cdSBlaise Bourdin   }
75488bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75588bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75688bdff64SToby Isaac   }
757a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75988bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76088bdff64SToby Isaac   }
76188bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
762f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7636636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7646636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7656636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7665dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7676636e97aSMatthew G Knepley 
7682764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
76914f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
770e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
771e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
772732e2eb9SMatthew G Knepley 
773ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7746bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
775ed3c66a1SDave May   }
776435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
777732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
77847c6ae99SBarry Smith   PetscFunctionReturn(0);
77947c6ae99SBarry Smith }
78047c6ae99SBarry Smith 
781d7bf68aeSBarry Smith /*@
782d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
783d7bf68aeSBarry Smith 
784d7bf68aeSBarry Smith     Collective on DM
785d7bf68aeSBarry Smith 
786d7bf68aeSBarry Smith     Input Parameter:
787d7bf68aeSBarry Smith .   dm - the DM object to setup
788d7bf68aeSBarry Smith 
789d7bf68aeSBarry Smith     Level: developer
790d7bf68aeSBarry Smith 
791e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith @*/
7947087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
795d7bf68aeSBarry Smith {
796d7bf68aeSBarry Smith   PetscErrorCode ierr;
797d7bf68aeSBarry Smith 
798d7bf68aeSBarry Smith   PetscFunctionBegin;
799171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8008387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
801d7bf68aeSBarry Smith   if (dm->ops->setup) {
802d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
803d7bf68aeSBarry Smith   }
8048387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
805d7bf68aeSBarry Smith   PetscFunctionReturn(0);
806d7bf68aeSBarry Smith }
807d7bf68aeSBarry Smith 
808d7bf68aeSBarry Smith /*@
809d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
810d7bf68aeSBarry Smith 
811d7bf68aeSBarry Smith     Collective on DM
812d7bf68aeSBarry Smith 
813d7bf68aeSBarry Smith     Input Parameter:
814d7bf68aeSBarry Smith .   dm - the DM object to set options for
815d7bf68aeSBarry Smith 
816732e2eb9SMatthew G Knepley     Options Database:
8174164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8184164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8194164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8208f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
821732e2eb9SMatthew G Knepley 
822d7bf68aeSBarry Smith     Level: developer
823d7bf68aeSBarry Smith 
824e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
825d7bf68aeSBarry Smith 
826d7bf68aeSBarry Smith @*/
8277087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
828d7bf68aeSBarry Smith {
8297781c08eSBarry Smith   char           typeName[256];
830ca266f36SBarry Smith   PetscBool      flg;
831d7bf68aeSBarry Smith   PetscErrorCode ierr;
832d7bf68aeSBarry Smith 
833d7bf68aeSBarry Smith   PetscFunctionBegin;
834171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
835f1fd5e65SToby Isaac   if (dm->prob) {
836f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
837f1fd5e65SToby Isaac   }
838691be533SLawrence Mitchell   if (dm->sf) {
839691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
840691be533SLawrence Mitchell   }
841691be533SLawrence Mitchell   if (dm->defaultSF) {
842691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
843691be533SLawrence Mitchell   }
8443194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8450298fd71SBarry 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);
846a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
847f9ba7244SBarry Smith   if (flg) {
848f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
849f9ba7244SBarry Smith   }
850a264d7a6SBarry 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);
851073dac72SJed Brown   if (flg) {
852521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
853073dac72SJed Brown   }
8548f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
855f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
856e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
857f9ba7244SBarry Smith   }
858f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8590633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
86082fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
861d7bf68aeSBarry Smith   PetscFunctionReturn(0);
862d7bf68aeSBarry Smith }
863d7bf68aeSBarry Smith 
864fc9bc008SSatish Balay /*@C
865224748a4SBarry Smith     DMView - Views a DM
86647c6ae99SBarry Smith 
86747c6ae99SBarry Smith     Collective on DM
86847c6ae99SBarry Smith 
86947c6ae99SBarry Smith     Input Parameter:
87047c6ae99SBarry Smith +   dm - the DM object to view
87147c6ae99SBarry Smith -   v - the viewer
87247c6ae99SBarry Smith 
873224748a4SBarry Smith     Level: beginner
87447c6ae99SBarry Smith 
875e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith @*/
8787087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
87947c6ae99SBarry Smith {
88047c6ae99SBarry Smith   PetscErrorCode    ierr;
88132c0f0efSBarry Smith   PetscBool         isbinary;
88276a8abe0SBarry Smith   PetscMPIInt       size;
88376a8abe0SBarry Smith   PetscViewerFormat format;
88447c6ae99SBarry Smith 
88547c6ae99SBarry Smith   PetscFunctionBegin;
886171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8873014e516SBarry Smith   if (!v) {
888ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8893014e516SBarry Smith   }
89076a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
89176a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
89276a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
89398c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
89532c0f0efSBarry Smith   if (isbinary) {
89655849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
89732c0f0efSBarry Smith     char     type[256];
89832c0f0efSBarry Smith 
89932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
90032c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
90132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
90232c0f0efSBarry Smith   }
9030c010503SBarry Smith   if (dm->ops->view) {
9040c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
90547c6ae99SBarry Smith   }
90647c6ae99SBarry Smith   PetscFunctionReturn(0);
90747c6ae99SBarry Smith }
90847c6ae99SBarry Smith 
90947c6ae99SBarry Smith /*@
9108472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
91147c6ae99SBarry Smith 
91247c6ae99SBarry Smith     Collective on DM
91347c6ae99SBarry Smith 
91447c6ae99SBarry Smith     Input Parameter:
91547c6ae99SBarry Smith .   dm - the DM object
91647c6ae99SBarry Smith 
91747c6ae99SBarry Smith     Output Parameter:
91847c6ae99SBarry Smith .   vec - the global vector
91947c6ae99SBarry Smith 
920073dac72SJed Brown     Level: beginner
92147c6ae99SBarry Smith 
922e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
92347c6ae99SBarry Smith 
92447c6ae99SBarry Smith @*/
9257087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92647c6ae99SBarry Smith {
92747c6ae99SBarry Smith   PetscErrorCode ierr;
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith   PetscFunctionBegin;
930171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93147c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93247c6ae99SBarry Smith   PetscFunctionReturn(0);
93347c6ae99SBarry Smith }
93447c6ae99SBarry Smith 
93547c6ae99SBarry Smith /*@
9368472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith     Not Collective
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith     Input Parameter:
94147c6ae99SBarry Smith .   dm - the DM object
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith     Output Parameter:
94447c6ae99SBarry Smith .   vec - the local vector
94547c6ae99SBarry Smith 
946073dac72SJed Brown     Level: beginner
94747c6ae99SBarry Smith 
948e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94947c6ae99SBarry Smith 
95047c6ae99SBarry Smith @*/
9517087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95247c6ae99SBarry Smith {
95347c6ae99SBarry Smith   PetscErrorCode ierr;
95447c6ae99SBarry Smith 
95547c6ae99SBarry Smith   PetscFunctionBegin;
956171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
95747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
95847c6ae99SBarry Smith   PetscFunctionReturn(0);
95947c6ae99SBarry Smith }
96047c6ae99SBarry Smith 
9611411c6eeSJed Brown /*@
9621411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9631411c6eeSJed Brown 
9641411c6eeSJed Brown    Collective on DM
9651411c6eeSJed Brown 
9661411c6eeSJed Brown    Input Parameter:
9671411c6eeSJed Brown .  dm - the DM that provides the mapping
9681411c6eeSJed Brown 
9691411c6eeSJed Brown    Output Parameter:
9701411c6eeSJed Brown .  ltog - the mapping
9711411c6eeSJed Brown 
9721411c6eeSJed Brown    Level: intermediate
9731411c6eeSJed Brown 
9741411c6eeSJed Brown    Notes:
9751411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9761411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9771411c6eeSJed Brown 
978fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9791411c6eeSJed Brown @*/
9807087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9811411c6eeSJed Brown {
9820be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9831411c6eeSJed Brown   PetscErrorCode ierr;
9841411c6eeSJed Brown 
9851411c6eeSJed Brown   PetscFunctionBegin;
9861411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9871411c6eeSJed Brown   PetscValidPointer(ltog,2);
9881411c6eeSJed Brown   if (!dm->ltogmap) {
98937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
99037d0c07bSMatthew G Knepley 
991e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
99237d0c07bSMatthew G Knepley     if (section) {
993a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
99437d0c07bSMatthew G Knepley       PetscInt       *ltog;
995ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
99637d0c07bSMatthew G Knepley 
997e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
99837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
999ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1000ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
100137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1002a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
100337d0c07bSMatthew G Knepley 
100437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
100537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1006a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1007a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
100837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10091a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10101a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10111a7dc684SMatthew G. Knepley         if (dof) {
1012b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1013b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10141a7dc684SMatthew G. Knepley         }
101537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1016a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1017a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
101837d0c07bSMatthew G Knepley         }
101937d0c07bSMatthew G Knepley       }
1020bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10210be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10220be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10230be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10240be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10257591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10267591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1027ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1028ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1029ccf3bd66SMatthew G. Knepley         n /= bs;
1030ccf3bd66SMatthew G. Knepley       }
1031ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10323bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
103337d0c07bSMatthew G Knepley     } else {
1034184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1035184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10361411c6eeSJed Brown     }
103737d0c07bSMatthew G Knepley   }
10381411c6eeSJed Brown   *ltog = dm->ltogmap;
10391411c6eeSJed Brown   PetscFunctionReturn(0);
10401411c6eeSJed Brown }
10411411c6eeSJed Brown 
10421411c6eeSJed Brown /*@
10431411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10441411c6eeSJed Brown 
10451411c6eeSJed Brown    Not Collective
10461411c6eeSJed Brown 
10471411c6eeSJed Brown    Input Parameter:
10481411c6eeSJed Brown .  dm - the DM with block structure
10491411c6eeSJed Brown 
10501411c6eeSJed Brown    Output Parameter:
10511411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10521411c6eeSJed Brown 
10531411c6eeSJed Brown    Level: intermediate
10541411c6eeSJed Brown 
1055fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10561411c6eeSJed Brown @*/
10577087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10581411c6eeSJed Brown {
10591411c6eeSJed Brown   PetscFunctionBegin;
10601411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10611411c6eeSJed Brown   PetscValidPointer(bs,2);
10621411c6eeSJed 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");
10631411c6eeSJed Brown   *bs = dm->bs;
10641411c6eeSJed Brown   PetscFunctionReturn(0);
10651411c6eeSJed Brown }
10661411c6eeSJed Brown 
106747c6ae99SBarry Smith /*@
10688472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith     Collective on DM
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith     Input Parameter:
107347c6ae99SBarry Smith +   dm1 - the DM object
107447c6ae99SBarry Smith -   dm2 - the second, finer DM object
107547c6ae99SBarry Smith 
107647c6ae99SBarry Smith     Output Parameter:
107747c6ae99SBarry Smith +  mat - the interpolation
107847c6ae99SBarry Smith -  vec - the scaling (optional)
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith     Level: developer
108147c6ae99SBarry Smith 
108295452b02SPatrick Sanan     Notes:
108395452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
108485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1085d52bd9f3SBarry Smith 
10861f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1087d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
108885afcc9aSBarry Smith 
108985afcc9aSBarry Smith 
10903ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith @*/
1093e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
109447c6ae99SBarry Smith {
109547c6ae99SBarry Smith   PetscErrorCode ierr;
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith   PetscFunctionBegin;
1098171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1099171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1100cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110125296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1102cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110347c6ae99SBarry Smith   PetscFunctionReturn(0);
110447c6ae99SBarry Smith }
110547c6ae99SBarry Smith 
11063ad4599aSBarry Smith /*@
11073ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11083ad4599aSBarry Smith 
11093ad4599aSBarry Smith     Collective on DM
11103ad4599aSBarry Smith 
11113ad4599aSBarry Smith     Input Parameter:
11123ad4599aSBarry Smith +   dm1 - the DM object
11133ad4599aSBarry Smith -   dm2 - the second, finer DM object
11143ad4599aSBarry Smith 
11153ad4599aSBarry Smith     Output Parameter:
11163ad4599aSBarry Smith .  mat - the restriction
11173ad4599aSBarry Smith 
11183ad4599aSBarry Smith 
11193ad4599aSBarry Smith     Level: developer
11203ad4599aSBarry Smith 
112195452b02SPatrick Sanan     Notes:
112295452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
11233ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11243ad4599aSBarry Smith 
11253ad4599aSBarry Smith 
11263ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11273ad4599aSBarry Smith 
11283ad4599aSBarry Smith @*/
11293ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11303ad4599aSBarry Smith {
11313ad4599aSBarry Smith   PetscErrorCode ierr;
11323ad4599aSBarry Smith 
11333ad4599aSBarry Smith   PetscFunctionBegin;
11343ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11353ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11363ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11377294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11383ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11393ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11403ad4599aSBarry Smith   PetscFunctionReturn(0);
11413ad4599aSBarry Smith }
11423ad4599aSBarry Smith 
114347c6ae99SBarry Smith /*@
11448472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114547c6ae99SBarry Smith 
114647c6ae99SBarry Smith     Collective on DM
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith     Input Parameter:
114947c6ae99SBarry Smith +   dm1 - the DM object
115047c6ae99SBarry Smith -   dm2 - the second, finer DM object
115147c6ae99SBarry Smith 
115247c6ae99SBarry Smith     Output Parameter:
11536dbf9973SLawrence Mitchell .   mat - the injection
115447c6ae99SBarry Smith 
115547c6ae99SBarry Smith     Level: developer
115647c6ae99SBarry Smith 
115795452b02SPatrick Sanan    Notes:
115895452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
115985afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
116085afcc9aSBarry Smith 
1161e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
116247c6ae99SBarry Smith 
116347c6ae99SBarry Smith @*/
11646dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116547c6ae99SBarry Smith {
116647c6ae99SBarry Smith   PetscErrorCode ierr;
116747c6ae99SBarry Smith 
116847c6ae99SBarry Smith   PetscFunctionBegin;
1169171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1170171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11710c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11726dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
117347c6ae99SBarry Smith   PetscFunctionReturn(0);
117447c6ae99SBarry Smith }
117547c6ae99SBarry Smith 
1176b412c318SBarry Smith /*@
1177bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1178bd041c0cSMatthew G. Knepley 
1179bd041c0cSMatthew G. Knepley   Collective on DM
1180bd041c0cSMatthew G. Knepley 
1181bd041c0cSMatthew G. Knepley   Input Parameter:
1182bd041c0cSMatthew G. Knepley + dm1 - the DM object
1183bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1184bd041c0cSMatthew G. Knepley 
1185bd041c0cSMatthew G. Knepley   Output Parameter:
1186bd041c0cSMatthew G. Knepley . mat - the interpolation
1187bd041c0cSMatthew G. Knepley 
1188bd041c0cSMatthew G. Knepley   Level: developer
1189bd041c0cSMatthew G. Knepley 
1190bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1191bd041c0cSMatthew G. Knepley @*/
1192bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1193bd041c0cSMatthew G. Knepley {
1194bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1195bd041c0cSMatthew G. Knepley 
1196bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1197bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1198bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1199bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1200bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1201bd041c0cSMatthew G. Knepley }
1202bd041c0cSMatthew G. Knepley 
1203bd041c0cSMatthew G. Knepley /*@
1204b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith     Collective on DM
120747c6ae99SBarry Smith 
120847c6ae99SBarry Smith     Input Parameter:
120947c6ae99SBarry Smith +   dm - the DM object
12105bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
121147c6ae99SBarry Smith 
121247c6ae99SBarry Smith     Output Parameter:
121347c6ae99SBarry Smith .   coloring - the coloring
121447c6ae99SBarry Smith 
121547c6ae99SBarry Smith     Level: developer
121647c6ae99SBarry Smith 
1217b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
121847c6ae99SBarry Smith 
1219aab9d709SJed Brown @*/
1220b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
122147c6ae99SBarry Smith {
122247c6ae99SBarry Smith   PetscErrorCode ierr;
122347c6ae99SBarry Smith 
122447c6ae99SBarry Smith   PetscFunctionBegin;
1225171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1226ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1227b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
122847c6ae99SBarry Smith   PetscFunctionReturn(0);
122947c6ae99SBarry Smith }
123047c6ae99SBarry Smith 
1231b412c318SBarry Smith /*@
12328472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith     Collective on DM
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith     Input Parameter:
1237b412c318SBarry Smith .   dm - the DM object
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith     Output Parameter:
124047c6ae99SBarry Smith .   mat - the empty Jacobian
124147c6ae99SBarry Smith 
1242073dac72SJed Brown     Level: beginner
124347c6ae99SBarry Smith 
124495452b02SPatrick Sanan     Notes:
124595452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
124694013140SBarry Smith        do not need to do it yourself.
124794013140SBarry Smith 
124894013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12497889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
125094013140SBarry Smith 
125194013140SBarry 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
125294013140SBarry Smith        internally by PETSc.
125394013140SBarry Smith 
125494013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1255aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
125694013140SBarry Smith 
1257b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
125847c6ae99SBarry Smith 
1259aab9d709SJed Brown @*/
1260b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
126147c6ae99SBarry Smith {
126247c6ae99SBarry Smith   PetscErrorCode ierr;
126347c6ae99SBarry Smith 
126447c6ae99SBarry Smith   PetscFunctionBegin;
1265171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1266607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1267c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1268c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1269b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
127047c6ae99SBarry Smith   PetscFunctionReturn(0);
127147c6ae99SBarry Smith }
127247c6ae99SBarry Smith 
1273732e2eb9SMatthew G Knepley /*@
1274950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1275732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1276732e2eb9SMatthew G Knepley 
12778472ad0fSDave May   Logically Collective on DM
1278732e2eb9SMatthew G Knepley 
1279732e2eb9SMatthew G Knepley   Input Parameter:
1280732e2eb9SMatthew G Knepley + dm - the DM
1281732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1282732e2eb9SMatthew G Knepley 
1283732e2eb9SMatthew G Knepley   Level: developer
1284b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1285732e2eb9SMatthew G Knepley @*/
1286732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1287732e2eb9SMatthew G Knepley {
1288732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1289732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1290732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1291732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1292732e2eb9SMatthew G Knepley }
1293732e2eb9SMatthew G Knepley 
1294b06ff27eSHong Zhang /*@
1295b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1296b06ff27eSHong Zhang     but the array for values will not be allocated.
1297b06ff27eSHong Zhang 
1298b06ff27eSHong Zhang   Logically Collective on DM
1299b06ff27eSHong Zhang 
1300b06ff27eSHong Zhang   Input Parameter:
1301b06ff27eSHong Zhang + dm - the DM
1302b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1303b06ff27eSHong Zhang 
1304b06ff27eSHong Zhang   Level: developer
1305b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1306b06ff27eSHong Zhang @*/
1307b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1308b06ff27eSHong Zhang {
1309b06ff27eSHong Zhang   PetscFunctionBegin;
1310b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1311b06ff27eSHong Zhang   dm->structure_only = only;
1312b06ff27eSHong Zhang   PetscFunctionReturn(0);
1313b06ff27eSHong Zhang }
1314b06ff27eSHong Zhang 
1315a89ea682SMatthew G Knepley /*@C
1316aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1317a89ea682SMatthew G Knepley 
1318a89ea682SMatthew G Knepley   Not Collective
1319a89ea682SMatthew G Knepley 
1320a89ea682SMatthew G Knepley   Input Parameters:
1321a89ea682SMatthew G Knepley + dm - the DM object
1322aa1993deSMatthew G Knepley . count - The minium size
132369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1324a89ea682SMatthew G Knepley 
1325a89ea682SMatthew G Knepley   Output Parameter:
1326a89ea682SMatthew G Knepley . array - the work array
1327a89ea682SMatthew G Knepley 
1328a89ea682SMatthew G Knepley   Level: developer
1329a89ea682SMatthew G Knepley 
1330a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1331a89ea682SMatthew G Knepley @*/
133269291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1333a89ea682SMatthew G Knepley {
1334a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1335aa1993deSMatthew G Knepley   DMWorkLink     link;
133669291d52SBarry Smith   PetscMPIInt    dsize;
1337a89ea682SMatthew G Knepley 
1338a89ea682SMatthew G Knepley   PetscFunctionBegin;
1339a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1340aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1341aa1993deSMatthew G Knepley   if (dm->workin) {
1342aa1993deSMatthew G Knepley     link       = dm->workin;
1343aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1344aa1993deSMatthew G Knepley   } else {
1345b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1346a89ea682SMatthew G Knepley   }
134769291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13485056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1349aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1350854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1351854ce69bSBarry Smith     link->bytes = dsize*count;
1352aa1993deSMatthew G Knepley   }
1353aa1993deSMatthew G Knepley   link->next   = dm->workout;
1354aa1993deSMatthew G Knepley   dm->workout  = link;
1355aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1356a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1357a89ea682SMatthew G Knepley }
1358a89ea682SMatthew G Knepley 
1359aa1993deSMatthew G Knepley /*@C
1360aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1361aa1993deSMatthew G Knepley 
1362aa1993deSMatthew G Knepley   Not Collective
1363aa1993deSMatthew G Knepley 
1364aa1993deSMatthew G Knepley   Input Parameters:
1365aa1993deSMatthew G Knepley + dm - the DM object
1366aa1993deSMatthew G Knepley . count - The minium size
136769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1368aa1993deSMatthew G Knepley 
1369aa1993deSMatthew G Knepley   Output Parameter:
1370aa1993deSMatthew G Knepley . array - the work array
1371aa1993deSMatthew G Knepley 
1372aa1993deSMatthew G Knepley   Level: developer
1373aa1993deSMatthew G Knepley 
137495452b02SPatrick Sanan   Developer Notes:
137595452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1376aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1377aa1993deSMatthew G Knepley @*/
137869291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1379aa1993deSMatthew G Knepley {
1380aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1381aa1993deSMatthew G Knepley 
1382aa1993deSMatthew G Knepley   PetscFunctionBegin;
1383aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1384aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1385aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1386aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1387aa1993deSMatthew G Knepley       *p           = link->next;
1388aa1993deSMatthew G Knepley       link->next   = dm->workin;
1389aa1993deSMatthew G Knepley       dm->workin   = link;
13900298fd71SBarry Smith       *(void**)mem = NULL;
1391aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1392aa1993deSMatthew G Knepley     }
1393aa1993deSMatthew G Knepley   }
1394aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1395aa1993deSMatthew G Knepley }
1396e7c4fc90SDmitry Karpeev 
1397435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1398435a35e8SMatthew G Knepley {
1399435a35e8SMatthew G Knepley   PetscFunctionBegin;
1400435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
140182f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1402435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1403435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1404435a35e8SMatthew G Knepley }
1405435a35e8SMatthew G Knepley 
1406*f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1407*f9d4088aSMatthew G. Knepley {
1408*f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1409*f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1410*f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1411*f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1412*f9d4088aSMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
1413*f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1414*f9d4088aSMatthew G. Knepley }
1415*f9d4088aSMatthew G. Knepley 
1416*f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1417*f9d4088aSMatthew G. Knepley {
1418*f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1419*f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1420*f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1421*f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1422*f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1423*f9d4088aSMatthew G. Knepley }
1424*f9d4088aSMatthew G. Knepley 
1425*f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1426*f9d4088aSMatthew G. Knepley {
1427*f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1428*f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1429*f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1430*f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1431*f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1432*f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1433*f9d4088aSMatthew G. Knepley }
1434*f9d4088aSMatthew G. Knepley 
14354f3b5142SJed Brown /*@C
14364d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14374d343eeaSMatthew G Knepley 
14384d343eeaSMatthew G Knepley   Not collective
14394d343eeaSMatthew G Knepley 
14404d343eeaSMatthew G Knepley   Input Parameter:
14414d343eeaSMatthew G Knepley . dm - the DM object
14424d343eeaSMatthew G Knepley 
14434d343eeaSMatthew G Knepley   Output Parameters:
14440298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14450298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14460298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14474d343eeaSMatthew G Knepley 
14484d343eeaSMatthew G Knepley   Level: intermediate
14494d343eeaSMatthew G Knepley 
145021c9b008SJed Brown   Notes:
145121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
145221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
145321c9b008SJed Brown   PetscFree().
145421c9b008SJed Brown 
14554d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14564d343eeaSMatthew G Knepley @*/
145737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14584d343eeaSMatthew G Knepley {
145937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14604d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14614d343eeaSMatthew G Knepley 
14624d343eeaSMatthew G Knepley   PetscFunctionBegin;
14634d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
146469ca1f37SDmitry Karpeev   if (numFields) {
146569ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
146669ca1f37SDmitry Karpeev     *numFields = 0;
146769ca1f37SDmitry Karpeev   }
146837d0c07bSMatthew G Knepley   if (fieldNames) {
146937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14700298fd71SBarry Smith     *fieldNames = NULL;
147169ca1f37SDmitry Karpeev   }
147269ca1f37SDmitry Karpeev   if (fields) {
147369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14740298fd71SBarry Smith     *fields = NULL;
147569ca1f37SDmitry Karpeev   }
1476e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
147737d0c07bSMatthew G Knepley   if (section) {
147837d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
147937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
148037d0c07bSMatthew G Knepley 
1481e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
148237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1483dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
148437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
148537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
148637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
148737d0c07bSMatthew G Knepley     }
148837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
148937d0c07bSMatthew G Knepley       PetscInt gdof;
149037d0c07bSMatthew G Knepley 
149137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
149237d0c07bSMatthew G Knepley       if (gdof > 0) {
149337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
149437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
149537d0c07bSMatthew G Knepley 
149637d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
149737d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
149837d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
149937d0c07bSMatthew G Knepley         }
150037d0c07bSMatthew G Knepley       }
150137d0c07bSMatthew G Knepley     }
150237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1503785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
150437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
150537d0c07bSMatthew G Knepley     }
150637d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
150737d0c07bSMatthew G Knepley       PetscInt gdof, goff;
150837d0c07bSMatthew G Knepley 
150937d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
151037d0c07bSMatthew G Knepley       if (gdof > 0) {
151137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
151237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
151337d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
151437d0c07bSMatthew G Knepley 
151537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
151637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
151737d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
151837d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
151937d0c07bSMatthew G Knepley           }
152037d0c07bSMatthew G Knepley         }
152137d0c07bSMatthew G Knepley       }
152237d0c07bSMatthew G Knepley     }
15238865f1eaSKarl Rupp     if (numFields) *numFields = nF;
152437d0c07bSMatthew G Knepley     if (fieldNames) {
1525785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
152637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
152737d0c07bSMatthew G Knepley         const char *fieldName;
152837d0c07bSMatthew G Knepley 
152937d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
153037d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
153137d0c07bSMatthew G Knepley       }
153237d0c07bSMatthew G Knepley     }
153337d0c07bSMatthew G Knepley     if (fields) {
1534785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
153537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
153682f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
153737d0c07bSMatthew G Knepley       }
153837d0c07bSMatthew G Knepley     }
153937d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
15408865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15418865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
154269ca1f37SDmitry Karpeev   }
15434d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15444d343eeaSMatthew G Knepley }
15454d343eeaSMatthew G Knepley 
154616621825SDmitry Karpeev 
154716621825SDmitry Karpeev /*@C
154816621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
154916621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
155016621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1551e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1552e7c4fc90SDmitry Karpeev 
1553e7c4fc90SDmitry Karpeev   Not collective
1554e7c4fc90SDmitry Karpeev 
1555e7c4fc90SDmitry Karpeev   Input Parameter:
1556e7c4fc90SDmitry Karpeev . dm - the DM object
1557e7c4fc90SDmitry Karpeev 
1558e7c4fc90SDmitry Karpeev   Output Parameters:
15590298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15600298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15610298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15620298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1563e7c4fc90SDmitry Karpeev 
1564e7c4fc90SDmitry Karpeev   Level: intermediate
1565e7c4fc90SDmitry Karpeev 
1566e7c4fc90SDmitry Karpeev   Notes:
1567e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1568e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1569e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1570e7c4fc90SDmitry Karpeev 
1571e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1572e7c4fc90SDmitry Karpeev @*/
157316621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1574e7c4fc90SDmitry Karpeev {
1575e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1576e7c4fc90SDmitry Karpeev 
1577e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1578e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15798865f1eaSKarl Rupp   if (len) {
15808865f1eaSKarl Rupp     PetscValidPointer(len,2);
15818865f1eaSKarl Rupp     *len = 0;
15828865f1eaSKarl Rupp   }
15838865f1eaSKarl Rupp   if (namelist) {
15848865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
15858865f1eaSKarl Rupp     *namelist = 0;
15868865f1eaSKarl Rupp   }
15878865f1eaSKarl Rupp   if (islist) {
15888865f1eaSKarl Rupp     PetscValidPointer(islist,4);
15898865f1eaSKarl Rupp     *islist = 0;
15908865f1eaSKarl Rupp   }
15918865f1eaSKarl Rupp   if (dmlist) {
15928865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
15938865f1eaSKarl Rupp     *dmlist = 0;
15948865f1eaSKarl Rupp   }
1595f3f0edfdSDmitry Karpeev   /*
1596f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1597f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1598f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1599f3f0edfdSDmitry Karpeev    */
1600ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
160116621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1602435a35e8SMatthew G Knepley     PetscSection section;
1603435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1604435a35e8SMatthew G Knepley 
1605e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1606435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1607435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1608f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
160903dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
161003dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
161103dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1612435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1613435a35e8SMatthew G Knepley         const char *fieldName;
1614435a35e8SMatthew G Knepley 
161503dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
161603dc3394SMatthew G. Knepley         if (namelist) {
1617435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1618435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1619435a35e8SMatthew G Knepley         }
162003dc3394SMatthew G. Knepley       }
1621435a35e8SMatthew G Knepley     } else {
162269ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1623e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16240298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1625e7c4fc90SDmitry Karpeev     }
16268865f1eaSKarl Rupp   } else {
162716621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
162816621825SDmitry Karpeev   }
162916621825SDmitry Karpeev   PetscFunctionReturn(0);
163016621825SDmitry Karpeev }
163116621825SDmitry Karpeev 
1632564cec59SMatthew G. Knepley /*@
1633435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1634435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1635435a35e8SMatthew G Knepley 
1636435a35e8SMatthew G Knepley   Not collective
1637435a35e8SMatthew G Knepley 
1638435a35e8SMatthew G Knepley   Input Parameters:
16392adcc780SMatthew G. Knepley + dm        - The DM object
16402adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16412adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1642435a35e8SMatthew G Knepley 
1643435a35e8SMatthew G Knepley   Output Parameters:
16442adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16452adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1646435a35e8SMatthew G Knepley 
1647435a35e8SMatthew G Knepley   Level: intermediate
1648435a35e8SMatthew G Knepley 
1649435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1650435a35e8SMatthew G Knepley @*/
165137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1652435a35e8SMatthew G Knepley {
1653435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1654435a35e8SMatthew G Knepley 
1655435a35e8SMatthew G Knepley   PetscFunctionBegin;
1656435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1657435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16588865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16598865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1660435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1661435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
166282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1663435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1664435a35e8SMatthew G Knepley }
1665435a35e8SMatthew G Knepley 
16662adcc780SMatthew G. Knepley /*@C
16672adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
16682adcc780SMatthew G. Knepley 
16692adcc780SMatthew G. Knepley   Not collective
16702adcc780SMatthew G. Knepley 
16712adcc780SMatthew G. Knepley   Input Parameter:
16722adcc780SMatthew G. Knepley + dms - The DM objects
16732adcc780SMatthew G. Knepley - len - The number of DMs
16742adcc780SMatthew G. Knepley 
16752adcc780SMatthew G. Knepley   Output Parameters:
16762adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16772adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
16782adcc780SMatthew G. Knepley 
16792adcc780SMatthew G. Knepley   Level: intermediate
16802adcc780SMatthew G. Knepley 
16812adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
16822adcc780SMatthew G. Knepley @*/
16832adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
16842adcc780SMatthew G. Knepley {
16852adcc780SMatthew G. Knepley   PetscInt       i;
16862adcc780SMatthew G. Knepley   PetscErrorCode ierr;
16872adcc780SMatthew G. Knepley 
16882adcc780SMatthew G. Knepley   PetscFunctionBegin;
16892adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
16902adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
16912adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
16922adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
16932adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
16942adcc780SMatthew G. Knepley   if (len) {
16952adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
16962adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
16972adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
16982adcc780SMatthew G. Knepley   }
16992adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17002adcc780SMatthew G. Knepley }
17012adcc780SMatthew G. Knepley 
170216621825SDmitry Karpeev 
170316621825SDmitry Karpeev /*@C
17048d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17058d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17068d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17078d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17088d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
170916621825SDmitry Karpeev 
171016621825SDmitry Karpeev   Not collective
171116621825SDmitry Karpeev 
171216621825SDmitry Karpeev   Input Parameter:
171316621825SDmitry Karpeev . dm - the DM object
171416621825SDmitry Karpeev 
171516621825SDmitry Karpeev   Output Parameters:
17160298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17170298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17180298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17190298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17200298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
172116621825SDmitry Karpeev 
172216621825SDmitry Karpeev   Level: intermediate
172316621825SDmitry Karpeev 
172416621825SDmitry Karpeev   Notes:
172516621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
172616621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
172716621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
172816621825SDmitry Karpeev 
17298d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
173016621825SDmitry Karpeev @*/
17318d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
173216621825SDmitry Karpeev {
173316621825SDmitry Karpeev   PetscErrorCode      ierr;
1734be081cd6SPeter Brune   DMSubDomainHookLink link;
1735be081cd6SPeter Brune   PetscInt            i,l;
173616621825SDmitry Karpeev 
173716621825SDmitry Karpeev   PetscFunctionBegin;
173816621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
173914a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17400298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17410298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17420298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17430298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1744f3f0edfdSDmitry Karpeev   /*
1745f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1746f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1747f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1748f3f0edfdSDmitry Karpeev    */
1749ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
175016621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1751be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
175214a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1753f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1754be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1755be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1756be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1757be081cd6SPeter Brune         }
1758648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1759e7c4fc90SDmitry Karpeev       }
176014a18fd3SPeter Brune     }
176114a18fd3SPeter Brune     if (len) *len = l;
176214a18fd3SPeter Brune   }
1763e30e807fSPeter Brune   PetscFunctionReturn(0);
1764e30e807fSPeter Brune }
1765e30e807fSPeter Brune 
1766e30e807fSPeter Brune 
1767e30e807fSPeter Brune /*@C
1768e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1769e30e807fSPeter Brune 
1770e30e807fSPeter Brune   Not collective
1771e30e807fSPeter Brune 
1772e30e807fSPeter Brune   Input Parameters:
1773e30e807fSPeter Brune + dm - the DM object
1774e30e807fSPeter Brune . n  - the number of subdomain scatters
1775e30e807fSPeter Brune - subdms - the local subdomains
1776e30e807fSPeter Brune 
1777e30e807fSPeter Brune   Output Parameters:
1778e30e807fSPeter Brune + n     - the number of scatters returned
1779e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1780e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1781e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1782e30e807fSPeter Brune 
178395452b02SPatrick Sanan   Notes:
178495452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1785e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1786e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1787e30e807fSPeter Brune   solution and residual data.
1788e30e807fSPeter Brune 
1789e30e807fSPeter Brune   Level: developer
1790e30e807fSPeter Brune 
1791e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1792e30e807fSPeter Brune @*/
1793e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1794e30e807fSPeter Brune {
1795e30e807fSPeter Brune   PetscErrorCode ierr;
1796e30e807fSPeter Brune 
1797e30e807fSPeter Brune   PetscFunctionBegin;
1798e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1799e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1800e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1801e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18026668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1803e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1804e7c4fc90SDmitry Karpeev }
1805e7c4fc90SDmitry Karpeev 
180647c6ae99SBarry Smith /*@
180747c6ae99SBarry Smith   DMRefine - Refines a DM object
180847c6ae99SBarry Smith 
180947c6ae99SBarry Smith   Collective on DM
181047c6ae99SBarry Smith 
181147c6ae99SBarry Smith   Input Parameter:
181247c6ae99SBarry Smith + dm   - the DM object
181391d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
181447c6ae99SBarry Smith 
181547c6ae99SBarry Smith   Output Parameter:
18160298fd71SBarry Smith . dmf - the refined DM, or NULL
1817ae0a1c52SMatthew G Knepley 
18180298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
181947c6ae99SBarry Smith 
182047c6ae99SBarry Smith   Level: developer
182147c6ae99SBarry Smith 
1822e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
182347c6ae99SBarry Smith @*/
18247087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
182547c6ae99SBarry Smith {
182647c6ae99SBarry Smith   PetscErrorCode   ierr;
1827c833c3b5SJed Brown   DMRefineHookLink link;
182847c6ae99SBarry Smith 
182947c6ae99SBarry Smith   PetscFunctionBegin;
1830732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18311ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1832fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
183347c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18344057135bSMatthew G Knepley   if (*dmf) {
183543842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18368865f1eaSKarl Rupp 
18378cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18388865f1eaSKarl Rupp 
1839644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18400598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1841656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18428865f1eaSKarl Rupp 
1843e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1844c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18458865f1eaSKarl Rupp       if (link->refinehook) {
18468865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18478865f1eaSKarl Rupp       }
1848c833c3b5SJed Brown     }
1849c833c3b5SJed Brown   }
18501ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1851c833c3b5SJed Brown   PetscFunctionReturn(0);
1852c833c3b5SJed Brown }
1853c833c3b5SJed Brown 
1854bb9467b5SJed Brown /*@C
1855c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1856c833c3b5SJed Brown 
1857c833c3b5SJed Brown    Logically Collective
1858c833c3b5SJed Brown 
1859c833c3b5SJed Brown    Input Arguments:
1860c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1861c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1862c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18630298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1864c833c3b5SJed Brown 
1865c833c3b5SJed Brown    Calling sequence of refinehook:
1866c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1867c833c3b5SJed Brown 
1868c833c3b5SJed Brown +  coarse - coarse level DM
1869c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1870c833c3b5SJed Brown -  ctx - optional user-defined function context
1871c833c3b5SJed Brown 
1872c833c3b5SJed Brown    Calling sequence for interphook:
1873c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1874c833c3b5SJed Brown 
1875c833c3b5SJed Brown +  coarse - coarse level DM
1876c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1877c833c3b5SJed Brown .  fine - fine level DM to update
1878c833c3b5SJed Brown -  ctx - optional user-defined function context
1879c833c3b5SJed Brown 
1880c833c3b5SJed Brown    Level: advanced
1881c833c3b5SJed Brown 
1882c833c3b5SJed Brown    Notes:
1883c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1884c833c3b5SJed Brown 
1885c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1886c833c3b5SJed Brown 
1887bb9467b5SJed Brown    This function is currently not available from Fortran.
1888bb9467b5SJed Brown 
1889c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1890c833c3b5SJed Brown @*/
1891c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1892c833c3b5SJed Brown {
1893c833c3b5SJed Brown   PetscErrorCode   ierr;
1894c833c3b5SJed Brown   DMRefineHookLink link,*p;
1895c833c3b5SJed Brown 
1896c833c3b5SJed Brown   PetscFunctionBegin;
1897c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18983d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
18993d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19003d8e3701SJed Brown   }
190195dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1902c833c3b5SJed Brown   link->refinehook = refinehook;
1903c833c3b5SJed Brown   link->interphook = interphook;
1904c833c3b5SJed Brown   link->ctx        = ctx;
19050298fd71SBarry Smith   link->next       = NULL;
1906c833c3b5SJed Brown   *p               = link;
1907c833c3b5SJed Brown   PetscFunctionReturn(0);
1908c833c3b5SJed Brown }
1909c833c3b5SJed Brown 
19103d8e3701SJed Brown /*@C
19113d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19123d8e3701SJed Brown 
19133d8e3701SJed Brown    Logically Collective
19143d8e3701SJed Brown 
19153d8e3701SJed Brown    Input Arguments:
19163d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19173d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19183d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19193d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19203d8e3701SJed Brown 
19213d8e3701SJed Brown    Level: advanced
19223d8e3701SJed Brown 
19233d8e3701SJed Brown    Notes:
19243d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19253d8e3701SJed Brown 
19263d8e3701SJed Brown    This function is currently not available from Fortran.
19273d8e3701SJed Brown 
19283d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19293d8e3701SJed Brown @*/
19303d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19313d8e3701SJed Brown {
19323d8e3701SJed Brown   PetscErrorCode   ierr;
19333d8e3701SJed Brown   DMRefineHookLink link,*p;
19343d8e3701SJed Brown 
19353d8e3701SJed Brown   PetscFunctionBegin;
19363d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19373d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19383d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19393d8e3701SJed Brown       link = *p;
19403d8e3701SJed Brown       *p = link->next;
19413d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19423d8e3701SJed Brown       break;
19433d8e3701SJed Brown     }
19443d8e3701SJed Brown   }
19453d8e3701SJed Brown   PetscFunctionReturn(0);
19463d8e3701SJed Brown }
19473d8e3701SJed Brown 
1948c833c3b5SJed Brown /*@
1949c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1950c833c3b5SJed Brown 
1951c833c3b5SJed Brown    Collective if any hooks are
1952c833c3b5SJed Brown 
1953c833c3b5SJed Brown    Input Arguments:
1954c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1955e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1956c833c3b5SJed Brown -  fine - finer DM to update
1957c833c3b5SJed Brown 
1958c833c3b5SJed Brown    Level: developer
1959c833c3b5SJed Brown 
1960c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1961c833c3b5SJed Brown @*/
1962c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1963c833c3b5SJed Brown {
1964c833c3b5SJed Brown   PetscErrorCode   ierr;
1965c833c3b5SJed Brown   DMRefineHookLink link;
1966c833c3b5SJed Brown 
1967c833c3b5SJed Brown   PetscFunctionBegin;
1968c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
19698865f1eaSKarl Rupp     if (link->interphook) {
19708865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
19718865f1eaSKarl Rupp     }
19724057135bSMatthew G Knepley   }
197347c6ae99SBarry Smith   PetscFunctionReturn(0);
197447c6ae99SBarry Smith }
197547c6ae99SBarry Smith 
1976eb3f98d2SBarry Smith /*@
1977eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1978eb3f98d2SBarry Smith 
1979eb3f98d2SBarry Smith     Not Collective
1980eb3f98d2SBarry Smith 
1981eb3f98d2SBarry Smith     Input Parameter:
1982eb3f98d2SBarry Smith .   dm - the DM object
1983eb3f98d2SBarry Smith 
1984eb3f98d2SBarry Smith     Output Parameter:
1985eb3f98d2SBarry Smith .   level - number of refinements
1986eb3f98d2SBarry Smith 
1987eb3f98d2SBarry Smith     Level: developer
1988eb3f98d2SBarry Smith 
19896a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1990eb3f98d2SBarry Smith 
1991eb3f98d2SBarry Smith @*/
1992eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1993eb3f98d2SBarry Smith {
1994eb3f98d2SBarry Smith   PetscFunctionBegin;
1995eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1996eb3f98d2SBarry Smith   *level = dm->levelup;
1997eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1998eb3f98d2SBarry Smith }
1999eb3f98d2SBarry Smith 
2000fef3a512SBarry Smith /*@
2001fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2002fef3a512SBarry Smith 
2003fef3a512SBarry Smith     Not Collective
2004fef3a512SBarry Smith 
2005fef3a512SBarry Smith     Input Parameter:
2006fef3a512SBarry Smith +   dm - the DM object
2007fef3a512SBarry Smith -   level - number of refinements
2008fef3a512SBarry Smith 
2009fef3a512SBarry Smith     Level: advanced
2010fef3a512SBarry Smith 
201195452b02SPatrick Sanan     Notes:
201295452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2013fef3a512SBarry Smith 
2014fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2015fef3a512SBarry Smith 
2016fef3a512SBarry Smith @*/
2017fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2018fef3a512SBarry Smith {
2019fef3a512SBarry Smith   PetscFunctionBegin;
2020fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2021fef3a512SBarry Smith   dm->levelup = level;
2022fef3a512SBarry Smith   PetscFunctionReturn(0);
2023fef3a512SBarry Smith }
2024fef3a512SBarry Smith 
2025bb9467b5SJed Brown /*@C
2026baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2027baf369e7SPeter Brune 
2028baf369e7SPeter Brune    Logically Collective
2029baf369e7SPeter Brune 
2030baf369e7SPeter Brune    Input Arguments:
2031baf369e7SPeter Brune +  dm - the DM
2032baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2033baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20340298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2035baf369e7SPeter Brune 
2036baf369e7SPeter Brune    Calling sequence for beginhook:
2037baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2038baf369e7SPeter Brune 
2039baf369e7SPeter Brune +  dm - global DM
2040baf369e7SPeter Brune .  g - global vector
2041baf369e7SPeter Brune .  mode - mode
2042baf369e7SPeter Brune .  l - local vector
2043baf369e7SPeter Brune -  ctx - optional user-defined function context
2044baf369e7SPeter Brune 
2045baf369e7SPeter Brune 
2046baf369e7SPeter Brune    Calling sequence for endhook:
2047ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2048baf369e7SPeter Brune 
2049baf369e7SPeter Brune +  global - global DM
2050baf369e7SPeter Brune -  ctx - optional user-defined function context
2051baf369e7SPeter Brune 
2052baf369e7SPeter Brune    Level: advanced
2053baf369e7SPeter Brune 
2054baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2055baf369e7SPeter Brune @*/
2056baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2057baf369e7SPeter Brune {
2058baf369e7SPeter Brune   PetscErrorCode          ierr;
2059baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2060baf369e7SPeter Brune 
2061baf369e7SPeter Brune   PetscFunctionBegin;
2062baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2063baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
206495dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2065baf369e7SPeter Brune   link->beginhook = beginhook;
2066baf369e7SPeter Brune   link->endhook   = endhook;
2067baf369e7SPeter Brune   link->ctx       = ctx;
20680298fd71SBarry Smith   link->next      = NULL;
2069baf369e7SPeter Brune   *p              = link;
2070baf369e7SPeter Brune   PetscFunctionReturn(0);
2071baf369e7SPeter Brune }
2072baf369e7SPeter Brune 
20734c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
20744c274da1SToby Isaac {
20754c274da1SToby Isaac   Mat cMat;
20764c274da1SToby Isaac   Vec cVec;
20774c274da1SToby Isaac   PetscSection section, cSec;
20784c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20794c274da1SToby Isaac   PetscErrorCode ierr;
20804c274da1SToby Isaac 
20814c274da1SToby Isaac   PetscFunctionBegin;
20824c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20834c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20844c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
20855db9a05bSToby Isaac     PetscInt nRows;
20865db9a05bSToby Isaac 
20875db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
20885db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2089e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
20907711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20914c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
20924c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20934c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20944c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20954c274da1SToby Isaac       if (dof) {
20964c274da1SToby Isaac         PetscScalar *vals;
20974c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
20984c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
20994c274da1SToby Isaac       }
21004c274da1SToby Isaac     }
21014c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21024c274da1SToby Isaac   }
21034c274da1SToby Isaac   PetscFunctionReturn(0);
21044c274da1SToby Isaac }
21054c274da1SToby Isaac 
210647c6ae99SBarry Smith /*@
210747c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
210847c6ae99SBarry Smith 
210947c6ae99SBarry Smith     Neighbor-wise Collective on DM
211047c6ae99SBarry Smith 
211147c6ae99SBarry Smith     Input Parameters:
211247c6ae99SBarry Smith +   dm - the DM object
211347c6ae99SBarry Smith .   g - the global vector
211447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
211547c6ae99SBarry Smith -   l - the local vector
211647c6ae99SBarry Smith 
211747c6ae99SBarry Smith 
211847c6ae99SBarry Smith     Level: beginner
211947c6ae99SBarry Smith 
2120e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
212147c6ae99SBarry Smith 
212247c6ae99SBarry Smith @*/
21237087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
212447c6ae99SBarry Smith {
21257128ae9fSMatthew G Knepley   PetscSF                 sf;
212647c6ae99SBarry Smith   PetscErrorCode          ierr;
2127baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
212847c6ae99SBarry Smith 
212947c6ae99SBarry Smith   PetscFunctionBegin;
2130171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2131baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
21328865f1eaSKarl Rupp     if (link->beginhook) {
21338865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
21348865f1eaSKarl Rupp     }
2135baf369e7SPeter Brune   }
21367128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21377128ae9fSMatthew G Knepley   if (sf) {
2138ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2139ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
21407128ae9fSMatthew G Knepley 
214182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21427128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2143ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21447128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21457128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2146ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21477128ae9fSMatthew G Knepley   } else {
2148843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21497128ae9fSMatthew G Knepley   }
215047c6ae99SBarry Smith   PetscFunctionReturn(0);
215147c6ae99SBarry Smith }
215247c6ae99SBarry Smith 
215347c6ae99SBarry Smith /*@
215447c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
215547c6ae99SBarry Smith 
215647c6ae99SBarry Smith     Neighbor-wise Collective on DM
215747c6ae99SBarry Smith 
215847c6ae99SBarry Smith     Input Parameters:
215947c6ae99SBarry Smith +   dm - the DM object
216047c6ae99SBarry Smith .   g - the global vector
216147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
216247c6ae99SBarry Smith -   l - the local vector
216347c6ae99SBarry Smith 
216447c6ae99SBarry Smith 
216547c6ae99SBarry Smith     Level: beginner
216647c6ae99SBarry Smith 
2167e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
216847c6ae99SBarry Smith 
216947c6ae99SBarry Smith @*/
21707087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
217147c6ae99SBarry Smith {
21727128ae9fSMatthew G Knepley   PetscSF                 sf;
217347c6ae99SBarry Smith   PetscErrorCode          ierr;
2174ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2175ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2176baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
217747c6ae99SBarry Smith 
217847c6ae99SBarry Smith   PetscFunctionBegin;
2179171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21807128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21817128ae9fSMatthew G Knepley   if (sf) {
218282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21837128ae9fSMatthew G Knepley 
21847128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2185ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21867128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21877128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2188ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21897128ae9fSMatthew G Knepley   } else {
2190843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21917128ae9fSMatthew G Knepley   }
21924c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2193baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2194baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2195baf369e7SPeter Brune   }
219647c6ae99SBarry Smith   PetscFunctionReturn(0);
219747c6ae99SBarry Smith }
219847c6ae99SBarry Smith 
2199d4d07f1eSToby Isaac /*@C
2200d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2201d4d07f1eSToby Isaac 
2202d4d07f1eSToby Isaac    Logically Collective
2203d4d07f1eSToby Isaac 
2204d4d07f1eSToby Isaac    Input Arguments:
2205d4d07f1eSToby Isaac +  dm - the DM
2206d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2207d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2208d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2209d4d07f1eSToby Isaac 
2210d4d07f1eSToby Isaac    Calling sequence for beginhook:
2211d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2212d4d07f1eSToby Isaac 
2213d4d07f1eSToby Isaac +  dm - global DM
2214d4d07f1eSToby Isaac .  l - local vector
2215d4d07f1eSToby Isaac .  mode - mode
2216d4d07f1eSToby Isaac .  g - global vector
2217d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2218d4d07f1eSToby Isaac 
2219d4d07f1eSToby Isaac 
2220d4d07f1eSToby Isaac    Calling sequence for endhook:
2221d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2222d4d07f1eSToby Isaac 
2223d4d07f1eSToby Isaac +  global - global DM
2224d4d07f1eSToby Isaac .  l - local vector
2225d4d07f1eSToby Isaac .  mode - mode
2226d4d07f1eSToby Isaac .  g - global vector
2227d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2228d4d07f1eSToby Isaac 
2229d4d07f1eSToby Isaac    Level: advanced
2230d4d07f1eSToby Isaac 
2231d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2232d4d07f1eSToby Isaac @*/
2233d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2234d4d07f1eSToby Isaac {
2235d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2236d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2237d4d07f1eSToby Isaac 
2238d4d07f1eSToby Isaac   PetscFunctionBegin;
2239d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2240d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
224195dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2242d4d07f1eSToby Isaac   link->beginhook = beginhook;
2243d4d07f1eSToby Isaac   link->endhook   = endhook;
2244d4d07f1eSToby Isaac   link->ctx       = ctx;
2245d4d07f1eSToby Isaac   link->next      = NULL;
2246d4d07f1eSToby Isaac   *p              = link;
2247d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2248d4d07f1eSToby Isaac }
2249d4d07f1eSToby Isaac 
22504c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
22514c274da1SToby Isaac {
22524c274da1SToby Isaac   Mat cMat;
22534c274da1SToby Isaac   Vec cVec;
22544c274da1SToby Isaac   PetscSection section, cSec;
22554c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22564c274da1SToby Isaac   PetscErrorCode ierr;
22574c274da1SToby Isaac 
22584c274da1SToby Isaac   PetscFunctionBegin;
22594c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22604c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22614c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
22625db9a05bSToby Isaac     PetscInt nRows;
22635db9a05bSToby Isaac 
22645db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22655db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2266e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
22677711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22684c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22694c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22704c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22714c274da1SToby Isaac       if (dof) {
22724c274da1SToby Isaac         PetscInt d;
22734c274da1SToby Isaac         PetscScalar *vals;
22744c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
22754c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
22764c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
22774c274da1SToby Isaac          * we just extracted */
22784c274da1SToby Isaac         for (d = 0; d < dof; d++) {
22794c274da1SToby Isaac           vals[d] = 0.;
22804c274da1SToby Isaac         }
22814c274da1SToby Isaac       }
22824c274da1SToby Isaac     }
22834c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
22844c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22854c274da1SToby Isaac   }
22864c274da1SToby Isaac   PetscFunctionReturn(0);
22874c274da1SToby Isaac }
22884c274da1SToby Isaac 
228947c6ae99SBarry Smith /*@
22909a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
22919a42bb27SBarry Smith 
22929a42bb27SBarry Smith     Neighbor-wise Collective on DM
22939a42bb27SBarry Smith 
22949a42bb27SBarry Smith     Input Parameters:
22959a42bb27SBarry Smith +   dm - the DM object
2296f6813fd5SJed Brown .   l - the local vector
22971eb28f2eSBarry 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.
22981eb28f2eSBarry Smith -   g - the global vector
22999a42bb27SBarry Smith 
230095452b02SPatrick Sanan     Notes:
230195452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
230284330215SMatthew 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.
23039a42bb27SBarry Smith 
23049a42bb27SBarry Smith     Level: beginner
23059a42bb27SBarry Smith 
2306e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
23079a42bb27SBarry Smith 
23089a42bb27SBarry Smith @*/
23097087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
23109a42bb27SBarry Smith {
23117128ae9fSMatthew G Knepley   PetscSF                 sf;
231284330215SMatthew G. Knepley   PetscSection            s, gs;
2313d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2314ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2315ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
231684330215SMatthew G. Knepley   PetscBool               isInsert;
231784330215SMatthew G. Knepley   PetscErrorCode          ierr;
23189a42bb27SBarry Smith 
23199a42bb27SBarry Smith   PetscFunctionBegin;
2320171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2321d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2322d4d07f1eSToby Isaac     if (link->beginhook) {
2323d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2324d4d07f1eSToby Isaac     }
2325d4d07f1eSToby Isaac   }
23264c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
23277128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2328e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
23297128ae9fSMatthew G Knepley   switch (mode) {
23307128ae9fSMatthew G Knepley   case INSERT_VALUES:
23317128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2332304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
233384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23347128ae9fSMatthew G Knepley   case ADD_VALUES:
23357128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2336304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
233784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23387128ae9fSMatthew G Knepley   default:
233982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23407128ae9fSMatthew G Knepley   }
234184330215SMatthew G. Knepley   if (sf && !isInsert) {
2342ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23437128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2344a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2345ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
234684330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
234784330215SMatthew G. Knepley   } else if (s && isInsert) {
234884330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
234984330215SMatthew G. Knepley 
2350e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
235184330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
235284330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2353ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
235484330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
235584330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2356b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
235784330215SMatthew G. Knepley 
235884330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
235903442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
236084330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2361b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
236284330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
236384330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2364b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
236503442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2366b3b16f48SMatthew 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);
2367b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2368b3b16f48SMatthew G. Knepley       if (!gcdof) {
236984330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2370b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2371b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
237284330215SMatthew G. Knepley         const PetscInt *cdofs;
237384330215SMatthew G. Knepley         PetscInt        cind = 0;
237484330215SMatthew G. Knepley 
237584330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2376b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
237784330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2378b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
237984330215SMatthew G. Knepley         }
2380b3b16f48SMatthew 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);
238184330215SMatthew G. Knepley     }
2382ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23837128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
23847128ae9fSMatthew G Knepley   } else {
2385843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23867128ae9fSMatthew G Knepley   }
23879a42bb27SBarry Smith   PetscFunctionReturn(0);
23889a42bb27SBarry Smith }
23899a42bb27SBarry Smith 
23909a42bb27SBarry Smith /*@
23919a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
239247c6ae99SBarry Smith 
239347c6ae99SBarry Smith     Neighbor-wise Collective on DM
239447c6ae99SBarry Smith 
239547c6ae99SBarry Smith     Input Parameters:
239647c6ae99SBarry Smith +   dm - the DM object
2397f6813fd5SJed Brown .   l - the local vector
239847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2399f6813fd5SJed Brown -   g - the global vector
240047c6ae99SBarry Smith 
240147c6ae99SBarry Smith 
240247c6ae99SBarry Smith     Level: beginner
240347c6ae99SBarry Smith 
2404e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
240547c6ae99SBarry Smith 
240647c6ae99SBarry Smith @*/
24077087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
240847c6ae99SBarry Smith {
24097128ae9fSMatthew G Knepley   PetscSF                 sf;
241084330215SMatthew G. Knepley   PetscSection            s;
2411d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
241284330215SMatthew G. Knepley   PetscBool               isInsert;
241384330215SMatthew G. Knepley   PetscErrorCode          ierr;
241447c6ae99SBarry Smith 
241547c6ae99SBarry Smith   PetscFunctionBegin;
2416171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
24177128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2418e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24197128ae9fSMatthew G Knepley   switch (mode) {
24207128ae9fSMatthew G Knepley   case INSERT_VALUES:
24217128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
242284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24237128ae9fSMatthew G Knepley   case ADD_VALUES:
24247128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
242584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24267128ae9fSMatthew G Knepley   default:
242782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24287128ae9fSMatthew G Knepley   }
242984330215SMatthew G. Knepley   if (sf && !isInsert) {
2430ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2431ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
243284330215SMatthew G. Knepley 
2433ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24347128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2435a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2436ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24377128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
243884330215SMatthew G. Knepley   } else if (s && isInsert) {
24397128ae9fSMatthew G Knepley   } else {
2440843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24417128ae9fSMatthew G Knepley   }
2442d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2443d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2444d4d07f1eSToby Isaac   }
244547c6ae99SBarry Smith   PetscFunctionReturn(0);
244647c6ae99SBarry Smith }
244747c6ae99SBarry Smith 
2448f089877aSRichard Tran Mills /*@
2449bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2450bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2451d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2452f089877aSRichard Tran Mills 
2453bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2454f089877aSRichard Tran Mills 
2455f089877aSRichard Tran Mills    Input Parameters:
2456f089877aSRichard Tran Mills +  dm - the DM object
2457bc0a1609SRichard Tran Mills .  g - the original local vector
2458bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2459f089877aSRichard Tran Mills 
2460bc0a1609SRichard Tran Mills    Output Parameter:
2461bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2462f089877aSRichard Tran Mills 
2463f089877aSRichard Tran Mills    Level: intermediate
2464f089877aSRichard Tran Mills 
2465bc0a1609SRichard Tran Mills    Notes:
2466bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2467bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2468bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2469bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2470bc0a1609SRichard Tran Mills 
2471bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2472bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2473f089877aSRichard Tran Mills 
2474f089877aSRichard Tran Mills @*/
2475f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2476f089877aSRichard Tran Mills {
2477f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2478f089877aSRichard Tran Mills 
2479f089877aSRichard Tran Mills   PetscFunctionBegin;
2480f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2481bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2482f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2483f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2484f089877aSRichard Tran Mills }
2485f089877aSRichard Tran Mills 
2486f089877aSRichard Tran Mills /*@
2487bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2488bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2489d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2490f089877aSRichard Tran Mills 
2491bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2492f089877aSRichard Tran Mills 
2493f089877aSRichard Tran Mills    Input Parameters:
2494bc0a1609SRichard Tran Mills +  da - the DM object
2495bc0a1609SRichard Tran Mills .  g - the original local vector
2496bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2497f089877aSRichard Tran Mills 
2498bc0a1609SRichard Tran Mills    Output Parameter:
2499bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2500f089877aSRichard Tran Mills 
2501f089877aSRichard Tran Mills    Level: intermediate
2502f089877aSRichard Tran Mills 
2503bc0a1609SRichard Tran Mills    Notes:
2504bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2505bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2506bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2507bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2508bc0a1609SRichard Tran Mills 
2509bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2510bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2511f089877aSRichard Tran Mills 
2512f089877aSRichard Tran Mills @*/
2513f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2514f089877aSRichard Tran Mills {
2515f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2516f089877aSRichard Tran Mills 
2517f089877aSRichard Tran Mills   PetscFunctionBegin;
2518f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2519bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2520f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2521f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2522f089877aSRichard Tran Mills }
2523f089877aSRichard Tran Mills 
2524f089877aSRichard Tran Mills 
252547c6ae99SBarry Smith /*@
252647c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
252747c6ae99SBarry Smith 
252847c6ae99SBarry Smith     Collective on DM
252947c6ae99SBarry Smith 
253047c6ae99SBarry Smith     Input Parameter:
253147c6ae99SBarry Smith +   dm - the DM object
253291d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
253347c6ae99SBarry Smith 
253447c6ae99SBarry Smith     Output Parameter:
253547c6ae99SBarry Smith .   dmc - the coarsened DM
253647c6ae99SBarry Smith 
253747c6ae99SBarry Smith     Level: developer
253847c6ae99SBarry Smith 
2539e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
254047c6ae99SBarry Smith 
254147c6ae99SBarry Smith @*/
25427087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
254347c6ae99SBarry Smith {
254447c6ae99SBarry Smith   PetscErrorCode    ierr;
2545b17ce1afSJed Brown   DMCoarsenHookLink link;
254647c6ae99SBarry Smith 
254747c6ae99SBarry Smith   PetscFunctionBegin;
2548171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2549fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
255047a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
255147c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
25528892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2553a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
255443842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
25558cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2556644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
25570598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2558656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2559e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2560b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2561b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2562b17ce1afSJed Brown   }
256347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2564b17ce1afSJed Brown   PetscFunctionReturn(0);
2565b17ce1afSJed Brown }
2566b17ce1afSJed Brown 
2567bb9467b5SJed Brown /*@C
2568b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2569b17ce1afSJed Brown 
2570b17ce1afSJed Brown    Logically Collective
2571b17ce1afSJed Brown 
2572b17ce1afSJed Brown    Input Arguments:
2573b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2574b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2575b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
25760298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2577b17ce1afSJed Brown 
2578b17ce1afSJed Brown    Calling sequence of coarsenhook:
2579b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2580b17ce1afSJed Brown 
2581b17ce1afSJed Brown +  fine - fine level DM
2582b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2583b17ce1afSJed Brown -  ctx - optional user-defined function context
2584b17ce1afSJed Brown 
2585b17ce1afSJed Brown    Calling sequence for restricthook:
2586c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2587b17ce1afSJed Brown 
2588b17ce1afSJed Brown +  fine - fine level DM
2589b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2590c833c3b5SJed Brown .  rscale - scaling vector for restriction
2591c833c3b5SJed Brown .  inject - matrix restricting by injection
2592b17ce1afSJed Brown .  coarse - coarse level DM to update
2593b17ce1afSJed Brown -  ctx - optional user-defined function context
2594b17ce1afSJed Brown 
2595b17ce1afSJed Brown    Level: advanced
2596b17ce1afSJed Brown 
2597b17ce1afSJed Brown    Notes:
2598b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2599b17ce1afSJed Brown 
2600b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2601b17ce1afSJed Brown 
2602b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2603b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2604b17ce1afSJed Brown 
2605bb9467b5SJed Brown    This function is currently not available from Fortran.
2606bb9467b5SJed Brown 
2607dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2608b17ce1afSJed Brown @*/
2609b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2610b17ce1afSJed Brown {
2611b17ce1afSJed Brown   PetscErrorCode    ierr;
2612b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2613b17ce1afSJed Brown 
2614b17ce1afSJed Brown   PetscFunctionBegin;
2615b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
26161e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
26171e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
26181e3d8eccSJed Brown   }
261995dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2620b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2621b17ce1afSJed Brown   link->restricthook = restricthook;
2622b17ce1afSJed Brown   link->ctx          = ctx;
26230298fd71SBarry Smith   link->next         = NULL;
2624b17ce1afSJed Brown   *p                 = link;
2625b17ce1afSJed Brown   PetscFunctionReturn(0);
2626b17ce1afSJed Brown }
2627b17ce1afSJed Brown 
2628dc822a44SJed Brown /*@C
2629dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2630dc822a44SJed Brown 
2631dc822a44SJed Brown    Logically Collective
2632dc822a44SJed Brown 
2633dc822a44SJed Brown    Input Arguments:
2634dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2635dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2636dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2637dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2638dc822a44SJed Brown 
2639dc822a44SJed Brown    Level: advanced
2640dc822a44SJed Brown 
2641dc822a44SJed Brown    Notes:
2642dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2643dc822a44SJed Brown 
2644dc822a44SJed Brown    This function is currently not available from Fortran.
2645dc822a44SJed Brown 
2646dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2647dc822a44SJed Brown @*/
2648dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2649dc822a44SJed Brown {
2650dc822a44SJed Brown   PetscErrorCode    ierr;
2651dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2652dc822a44SJed Brown 
2653dc822a44SJed Brown   PetscFunctionBegin;
2654dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2655dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2656dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2657dc822a44SJed Brown       link = *p;
2658dc822a44SJed Brown       *p = link->next;
2659dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2660dc822a44SJed Brown       break;
2661dc822a44SJed Brown     }
2662dc822a44SJed Brown   }
2663dc822a44SJed Brown   PetscFunctionReturn(0);
2664dc822a44SJed Brown }
2665dc822a44SJed Brown 
2666dc822a44SJed Brown 
2667b17ce1afSJed Brown /*@
2668b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2669b17ce1afSJed Brown 
2670b17ce1afSJed Brown    Collective if any hooks are
2671b17ce1afSJed Brown 
2672b17ce1afSJed Brown    Input Arguments:
2673b17ce1afSJed Brown +  fine - finer DM to use as a base
2674b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2675e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2676b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2677e91eccc2SStefano Zampini -  coarse - coarser DM to update
2678b17ce1afSJed Brown 
2679b17ce1afSJed Brown    Level: developer
2680b17ce1afSJed Brown 
2681b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2682b17ce1afSJed Brown @*/
2683b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2684b17ce1afSJed Brown {
2685b17ce1afSJed Brown   PetscErrorCode    ierr;
2686b17ce1afSJed Brown   DMCoarsenHookLink link;
2687b17ce1afSJed Brown 
2688b17ce1afSJed Brown   PetscFunctionBegin;
2689b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
26908865f1eaSKarl Rupp     if (link->restricthook) {
26918865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
26928865f1eaSKarl Rupp     }
2693b17ce1afSJed Brown   }
269447c6ae99SBarry Smith   PetscFunctionReturn(0);
269547c6ae99SBarry Smith }
269647c6ae99SBarry Smith 
2697bb9467b5SJed Brown /*@C
2698be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
26995dbd56e3SPeter Brune 
27005dbd56e3SPeter Brune    Logically Collective
27015dbd56e3SPeter Brune 
27025dbd56e3SPeter Brune    Input Arguments:
27035dbd56e3SPeter Brune +  global - global DM
2704ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
27055dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
27060298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
27075dbd56e3SPeter Brune 
2708ec4806b8SPeter Brune 
2709ec4806b8SPeter Brune    Calling sequence for ddhook:
2710ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2711ec4806b8SPeter Brune 
2712ec4806b8SPeter Brune +  global - global DM
2713ec4806b8SPeter Brune .  block  - block DM
2714ec4806b8SPeter Brune -  ctx - optional user-defined function context
2715ec4806b8SPeter Brune 
27165dbd56e3SPeter Brune    Calling sequence for restricthook:
2717ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
27185dbd56e3SPeter Brune 
27195dbd56e3SPeter Brune +  global - global DM
27205dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
27215dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2722ec4806b8SPeter Brune .  block  - block DM
27235dbd56e3SPeter Brune -  ctx - optional user-defined function context
27245dbd56e3SPeter Brune 
27255dbd56e3SPeter Brune    Level: advanced
27265dbd56e3SPeter Brune 
27275dbd56e3SPeter Brune    Notes:
2728ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
27295dbd56e3SPeter Brune 
27305dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
27315dbd56e3SPeter Brune 
27325dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2733ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
27345dbd56e3SPeter Brune 
2735bb9467b5SJed Brown    This function is currently not available from Fortran.
2736bb9467b5SJed Brown 
27375dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
27385dbd56e3SPeter Brune @*/
2739be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
27405dbd56e3SPeter Brune {
27415dbd56e3SPeter Brune   PetscErrorCode      ierr;
2742be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
27435dbd56e3SPeter Brune 
27445dbd56e3SPeter Brune   PetscFunctionBegin;
27455dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2746b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2747b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2748b3a6b972SJed Brown   }
274995dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
27505dbd56e3SPeter Brune   link->restricthook = restricthook;
2751be081cd6SPeter Brune   link->ddhook       = ddhook;
27525dbd56e3SPeter Brune   link->ctx          = ctx;
27530298fd71SBarry Smith   link->next         = NULL;
27545dbd56e3SPeter Brune   *p                 = link;
27555dbd56e3SPeter Brune   PetscFunctionReturn(0);
27565dbd56e3SPeter Brune }
27575dbd56e3SPeter Brune 
2758b3a6b972SJed Brown /*@C
2759b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2760b3a6b972SJed Brown 
2761b3a6b972SJed Brown    Logically Collective
2762b3a6b972SJed Brown 
2763b3a6b972SJed Brown    Input Arguments:
2764b3a6b972SJed Brown +  global - global DM
2765b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2766b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2767b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2768b3a6b972SJed Brown 
2769b3a6b972SJed Brown    Level: advanced
2770b3a6b972SJed Brown 
2771b3a6b972SJed Brown    Notes:
2772b3a6b972SJed Brown 
2773b3a6b972SJed Brown    This function is currently not available from Fortran.
2774b3a6b972SJed Brown 
2775b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2776b3a6b972SJed Brown @*/
2777b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2778b3a6b972SJed Brown {
2779b3a6b972SJed Brown   PetscErrorCode      ierr;
2780b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2781b3a6b972SJed Brown 
2782b3a6b972SJed Brown   PetscFunctionBegin;
2783b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2784b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2785b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2786b3a6b972SJed Brown       link = *p;
2787b3a6b972SJed Brown       *p = link->next;
2788b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2789b3a6b972SJed Brown       break;
2790b3a6b972SJed Brown     }
2791b3a6b972SJed Brown   }
2792b3a6b972SJed Brown   PetscFunctionReturn(0);
2793b3a6b972SJed Brown }
2794b3a6b972SJed Brown 
27955dbd56e3SPeter Brune /*@
2796be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
27975dbd56e3SPeter Brune 
27985dbd56e3SPeter Brune    Collective if any hooks are
27995dbd56e3SPeter Brune 
28005dbd56e3SPeter Brune    Input Arguments:
28015dbd56e3SPeter Brune +  fine - finer DM to use as a base
2802be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2803be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
28045dbd56e3SPeter Brune -  coarse - coarer DM to update
28055dbd56e3SPeter Brune 
28065dbd56e3SPeter Brune    Level: developer
28075dbd56e3SPeter Brune 
28085dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
28095dbd56e3SPeter Brune @*/
2810be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
28115dbd56e3SPeter Brune {
28125dbd56e3SPeter Brune   PetscErrorCode      ierr;
2813be081cd6SPeter Brune   DMSubDomainHookLink link;
28145dbd56e3SPeter Brune 
28155dbd56e3SPeter Brune   PetscFunctionBegin;
2816be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
28178865f1eaSKarl Rupp     if (link->restricthook) {
28188865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
28198865f1eaSKarl Rupp     }
28205dbd56e3SPeter Brune   }
28215dbd56e3SPeter Brune   PetscFunctionReturn(0);
28225dbd56e3SPeter Brune }
28235dbd56e3SPeter Brune 
28245fe1f584SPeter Brune /*@
28256a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
28265fe1f584SPeter Brune 
28275fe1f584SPeter Brune     Not Collective
28285fe1f584SPeter Brune 
28295fe1f584SPeter Brune     Input Parameter:
28305fe1f584SPeter Brune .   dm - the DM object
28315fe1f584SPeter Brune 
28325fe1f584SPeter Brune     Output Parameter:
28336a7d9d85SPeter Brune .   level - number of coarsenings
28345fe1f584SPeter Brune 
28355fe1f584SPeter Brune     Level: developer
28365fe1f584SPeter Brune 
28376a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
28385fe1f584SPeter Brune 
28395fe1f584SPeter Brune @*/
28405fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
28415fe1f584SPeter Brune {
28425fe1f584SPeter Brune   PetscFunctionBegin;
28435fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28445fe1f584SPeter Brune   *level = dm->leveldown;
28455fe1f584SPeter Brune   PetscFunctionReturn(0);
28465fe1f584SPeter Brune }
28475fe1f584SPeter Brune 
28485fe1f584SPeter Brune 
28495fe1f584SPeter Brune 
285047c6ae99SBarry Smith /*@C
285147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
285247c6ae99SBarry Smith 
285347c6ae99SBarry Smith     Collective on DM
285447c6ae99SBarry Smith 
285547c6ae99SBarry Smith     Input Parameter:
285647c6ae99SBarry Smith +   dm - the DM object
285747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
285847c6ae99SBarry Smith 
285947c6ae99SBarry Smith     Output Parameter:
286047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
286147c6ae99SBarry Smith 
286247c6ae99SBarry Smith     Level: developer
286347c6ae99SBarry Smith 
2864e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
286547c6ae99SBarry Smith 
286647c6ae99SBarry Smith @*/
28677087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
286847c6ae99SBarry Smith {
286947c6ae99SBarry Smith   PetscErrorCode ierr;
287047c6ae99SBarry Smith 
287147c6ae99SBarry Smith   PetscFunctionBegin;
2872171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2873ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
287447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
287547c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
287647c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
287747c6ae99SBarry Smith   } else if (dm->ops->refine) {
287847c6ae99SBarry Smith     PetscInt i;
287947c6ae99SBarry Smith 
2880ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
288147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2882ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
288347c6ae99SBarry Smith     }
2884ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
288547c6ae99SBarry Smith   PetscFunctionReturn(0);
288647c6ae99SBarry Smith }
288747c6ae99SBarry Smith 
288847c6ae99SBarry Smith /*@C
288947c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
289047c6ae99SBarry Smith 
289147c6ae99SBarry Smith     Collective on DM
289247c6ae99SBarry Smith 
289347c6ae99SBarry Smith     Input Parameter:
289447c6ae99SBarry Smith +   dm - the DM object
289547c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
289647c6ae99SBarry Smith 
289747c6ae99SBarry Smith     Output Parameter:
289847c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
289947c6ae99SBarry Smith 
290047c6ae99SBarry Smith     Level: developer
290147c6ae99SBarry Smith 
2902e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
290347c6ae99SBarry Smith 
290447c6ae99SBarry Smith @*/
29057087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
290647c6ae99SBarry Smith {
290747c6ae99SBarry Smith   PetscErrorCode ierr;
290847c6ae99SBarry Smith 
290947c6ae99SBarry Smith   PetscFunctionBegin;
2910171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2911ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
291247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
291347c6ae99SBarry Smith   PetscValidPointer(dmc,3);
291447c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
291547c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
291647c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
291747c6ae99SBarry Smith     PetscInt i;
291847c6ae99SBarry Smith 
2919ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
292047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2921ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
292247c6ae99SBarry Smith     }
2923ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
292447c6ae99SBarry Smith   PetscFunctionReturn(0);
292547c6ae99SBarry Smith }
292647c6ae99SBarry Smith 
292747c6ae99SBarry Smith /*@
2928e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
292947c6ae99SBarry Smith    grids associated with two DMs.
293047c6ae99SBarry Smith 
293147c6ae99SBarry Smith    Collective on DM
293247c6ae99SBarry Smith 
293347c6ae99SBarry Smith    Input Parameters:
293447c6ae99SBarry Smith +  dmc - the coarse grid DM
293547c6ae99SBarry Smith -  dmf - the fine grid DM
293647c6ae99SBarry Smith 
293747c6ae99SBarry Smith    Output Parameters:
293847c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
293947c6ae99SBarry Smith 
294047c6ae99SBarry Smith    Level: intermediate
294147c6ae99SBarry Smith 
294247c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
294347c6ae99SBarry Smith 
2944e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
294547c6ae99SBarry Smith @*/
2946e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
294747c6ae99SBarry Smith {
294847c6ae99SBarry Smith   PetscErrorCode ierr;
294947c6ae99SBarry Smith 
295047c6ae99SBarry Smith   PetscFunctionBegin;
2951171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2952171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
295347c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
295447c6ae99SBarry Smith   PetscFunctionReturn(0);
295547c6ae99SBarry Smith }
295647c6ae99SBarry Smith 
29571a266240SBarry Smith /*@C
29581a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
29591a266240SBarry Smith 
29601a266240SBarry Smith     Not Collective
29611a266240SBarry Smith 
29621a266240SBarry Smith     Input Parameters:
29631a266240SBarry Smith +   dm - the DM object
29641a266240SBarry Smith -   destroy - the destroy function
29651a266240SBarry Smith 
29661a266240SBarry Smith     Level: intermediate
29671a266240SBarry Smith 
2968e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
29691a266240SBarry Smith 
2970f07f9ceaSJed Brown @*/
29711a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
29721a266240SBarry Smith {
29731a266240SBarry Smith   PetscFunctionBegin;
2974171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29751a266240SBarry Smith   dm->ctxdestroy = destroy;
29761a266240SBarry Smith   PetscFunctionReturn(0);
29771a266240SBarry Smith }
29781a266240SBarry Smith 
2979b07ff414SBarry Smith /*@
29801b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
298147c6ae99SBarry Smith 
298247c6ae99SBarry Smith     Not Collective
298347c6ae99SBarry Smith 
298447c6ae99SBarry Smith     Input Parameters:
298547c6ae99SBarry Smith +   dm - the DM object
298647c6ae99SBarry Smith -   ctx - the user context
298747c6ae99SBarry Smith 
298847c6ae99SBarry Smith     Level: intermediate
298947c6ae99SBarry Smith 
2990e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
299147c6ae99SBarry Smith 
299247c6ae99SBarry Smith @*/
29931b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
299447c6ae99SBarry Smith {
299547c6ae99SBarry Smith   PetscFunctionBegin;
2996171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
299747c6ae99SBarry Smith   dm->ctx = ctx;
299847c6ae99SBarry Smith   PetscFunctionReturn(0);
299947c6ae99SBarry Smith }
300047c6ae99SBarry Smith 
300147c6ae99SBarry Smith /*@
30021b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
300347c6ae99SBarry Smith 
300447c6ae99SBarry Smith     Not Collective
300547c6ae99SBarry Smith 
300647c6ae99SBarry Smith     Input Parameter:
300747c6ae99SBarry Smith .   dm - the DM object
300847c6ae99SBarry Smith 
300947c6ae99SBarry Smith     Output Parameter:
301047c6ae99SBarry Smith .   ctx - the user context
301147c6ae99SBarry Smith 
301247c6ae99SBarry Smith     Level: intermediate
301347c6ae99SBarry Smith 
3014e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
301547c6ae99SBarry Smith 
301647c6ae99SBarry Smith @*/
30171b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
301847c6ae99SBarry Smith {
301947c6ae99SBarry Smith   PetscFunctionBegin;
3020171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30211b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
302247c6ae99SBarry Smith   PetscFunctionReturn(0);
302347c6ae99SBarry Smith }
302447c6ae99SBarry Smith 
302508da532bSDmitry Karpeev /*@C
3026df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
302708da532bSDmitry Karpeev 
302808da532bSDmitry Karpeev     Logically Collective on DM
302908da532bSDmitry Karpeev 
303008da532bSDmitry Karpeev     Input Parameter:
303108da532bSDmitry Karpeev +   dm - the DM object
30320298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
303308da532bSDmitry Karpeev 
303408da532bSDmitry Karpeev     Level: intermediate
303508da532bSDmitry Karpeev 
3036835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
303708da532bSDmitry Karpeev          DMSetJacobian()
303808da532bSDmitry Karpeev 
303908da532bSDmitry Karpeev @*/
304008da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
304108da532bSDmitry Karpeev {
304208da532bSDmitry Karpeev   PetscFunctionBegin;
304308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
304408da532bSDmitry Karpeev   PetscFunctionReturn(0);
304508da532bSDmitry Karpeev }
304608da532bSDmitry Karpeev 
304708da532bSDmitry Karpeev /*@
304808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
304908da532bSDmitry Karpeev 
305008da532bSDmitry Karpeev     Not Collective
305108da532bSDmitry Karpeev 
305208da532bSDmitry Karpeev     Input Parameter:
305308da532bSDmitry Karpeev .   dm - the DM object to destroy
305408da532bSDmitry Karpeev 
305508da532bSDmitry Karpeev     Output Parameter:
305608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
305708da532bSDmitry Karpeev 
305808da532bSDmitry Karpeev     Level: developer
305908da532bSDmitry Karpeev 
306074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
306108da532bSDmitry Karpeev 
306208da532bSDmitry Karpeev @*/
306308da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
306408da532bSDmitry Karpeev {
306508da532bSDmitry Karpeev   PetscFunctionBegin;
306608da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
306708da532bSDmitry Karpeev   PetscFunctionReturn(0);
306808da532bSDmitry Karpeev }
306908da532bSDmitry Karpeev 
307008da532bSDmitry Karpeev /*@C
307108da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
307208da532bSDmitry Karpeev 
307308da532bSDmitry Karpeev     Logically Collective on DM
307408da532bSDmitry Karpeev 
307508da532bSDmitry Karpeev     Input Parameters:
3076907376e6SBarry Smith .   dm - the DM object
307708da532bSDmitry Karpeev 
307808da532bSDmitry Karpeev     Output parameters:
307908da532bSDmitry Karpeev +   xl - lower bound
308008da532bSDmitry Karpeev -   xu - upper bound
308108da532bSDmitry Karpeev 
3082907376e6SBarry Smith     Level: advanced
3083907376e6SBarry Smith 
308495452b02SPatrick Sanan     Notes:
308595452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
308608da532bSDmitry Karpeev 
308774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
308808da532bSDmitry Karpeev 
308908da532bSDmitry Karpeev @*/
309008da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
309108da532bSDmitry Karpeev {
309208da532bSDmitry Karpeev   PetscErrorCode ierr;
30935fd66863SKarl Rupp 
309408da532bSDmitry Karpeev   PetscFunctionBegin;
309508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
309608da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
309708da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
309808da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
30998865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
310008da532bSDmitry Karpeev   PetscFunctionReturn(0);
310108da532bSDmitry Karpeev }
310208da532bSDmitry Karpeev 
3103b0ae01b7SPeter Brune /*@
3104b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3105b0ae01b7SPeter Brune 
3106b0ae01b7SPeter Brune     Not Collective
3107b0ae01b7SPeter Brune 
3108b0ae01b7SPeter Brune     Input Parameter:
3109b0ae01b7SPeter Brune .   dm - the DM object
3110b0ae01b7SPeter Brune 
3111b0ae01b7SPeter Brune     Output Parameter:
3112b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3113b0ae01b7SPeter Brune 
3114b0ae01b7SPeter Brune     Level: developer
3115b0ae01b7SPeter Brune 
3116b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3117b0ae01b7SPeter Brune 
3118b0ae01b7SPeter Brune @*/
3119b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3120b0ae01b7SPeter Brune {
3121b0ae01b7SPeter Brune   PetscFunctionBegin;
3122b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3123b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3124b0ae01b7SPeter Brune }
3125b0ae01b7SPeter Brune 
31263ad4599aSBarry Smith /*@
31273ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
31283ad4599aSBarry Smith 
31293ad4599aSBarry Smith     Not Collective
31303ad4599aSBarry Smith 
31313ad4599aSBarry Smith     Input Parameter:
31323ad4599aSBarry Smith .   dm - the DM object
31333ad4599aSBarry Smith 
31343ad4599aSBarry Smith     Output Parameter:
31353ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
31363ad4599aSBarry Smith 
31373ad4599aSBarry Smith     Level: developer
31383ad4599aSBarry Smith 
31393ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
31403ad4599aSBarry Smith 
31413ad4599aSBarry Smith @*/
31423ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
31433ad4599aSBarry Smith {
31443ad4599aSBarry Smith   PetscFunctionBegin;
31453ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
31463ad4599aSBarry Smith   PetscFunctionReturn(0);
31473ad4599aSBarry Smith }
31483ad4599aSBarry Smith 
3149a7058e45SLawrence Mitchell 
3150a7058e45SLawrence Mitchell /*@
3151a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3152a7058e45SLawrence Mitchell 
3153a7058e45SLawrence Mitchell     Not Collective
3154a7058e45SLawrence Mitchell 
3155a7058e45SLawrence Mitchell     Input Parameter:
3156a7058e45SLawrence Mitchell .   dm - the DM object
3157a7058e45SLawrence Mitchell 
3158a7058e45SLawrence Mitchell     Output Parameter:
3159a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3160a7058e45SLawrence Mitchell 
3161a7058e45SLawrence Mitchell     Level: developer
3162a7058e45SLawrence Mitchell 
3163a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3164a7058e45SLawrence Mitchell 
3165a7058e45SLawrence Mitchell @*/
3166a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3167a7058e45SLawrence Mitchell {
31684a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3169a7058e45SLawrence Mitchell   PetscFunctionBegin;
31704a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
31714a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3172a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3173a7058e45SLawrence Mitchell }
3174a7058e45SLawrence Mitchell 
3175748fac09SDmitry Karpeev /*@C
317608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
317708da532bSDmitry Karpeev 
317808da532bSDmitry Karpeev     Collective on DM
317908da532bSDmitry Karpeev 
318008da532bSDmitry Karpeev     Input Parameter:
318108da532bSDmitry Karpeev +   dm - the DM object
31820298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
318308da532bSDmitry Karpeev 
318408da532bSDmitry Karpeev     Level: developer
318508da532bSDmitry Karpeev 
318674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
318708da532bSDmitry Karpeev 
318808da532bSDmitry Karpeev @*/
318908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
319008da532bSDmitry Karpeev {
319108da532bSDmitry Karpeev   PetscErrorCode ierr;
31925fd66863SKarl Rupp 
319308da532bSDmitry Karpeev   PetscFunctionBegin;
319408da532bSDmitry Karpeev   if (x) {
319508da532bSDmitry Karpeev     if (!dm->x) {
319608da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
319708da532bSDmitry Karpeev     }
319808da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
31998865f1eaSKarl Rupp   } else if (dm->x) {
320008da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
320108da532bSDmitry Karpeev   }
320208da532bSDmitry Karpeev   PetscFunctionReturn(0);
320308da532bSDmitry Karpeev }
320408da532bSDmitry Karpeev 
32050298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3206264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3207264ace61SBarry Smith 
3208264ace61SBarry Smith /*@C
3209264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3210264ace61SBarry Smith 
3211264ace61SBarry Smith   Collective on DM
3212264ace61SBarry Smith 
3213264ace61SBarry Smith   Input Parameters:
3214264ace61SBarry Smith + dm     - The DM object
3215264ace61SBarry Smith - method - The name of the DM type
3216264ace61SBarry Smith 
3217264ace61SBarry Smith   Options Database Key:
3218264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3219264ace61SBarry Smith 
3220264ace61SBarry Smith   Notes:
3221e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3222264ace61SBarry Smith 
3223264ace61SBarry Smith   Level: intermediate
3224264ace61SBarry Smith 
3225264ace61SBarry Smith .keywords: DM, set, type
3226264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3227264ace61SBarry Smith @*/
322819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3229264ace61SBarry Smith {
3230264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3231264ace61SBarry Smith   PetscBool      match;
3232264ace61SBarry Smith   PetscErrorCode ierr;
3233264ace61SBarry Smith 
3234264ace61SBarry Smith   PetscFunctionBegin;
3235264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3236251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3237264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3238264ace61SBarry Smith 
32390f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
32401c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3241ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3242264ace61SBarry Smith 
3243264ace61SBarry Smith   if (dm->ops->destroy) {
3244264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
32450298fd71SBarry Smith     dm->ops->destroy = NULL;
3246264ace61SBarry Smith   }
3247264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3248264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3249264ace61SBarry Smith   PetscFunctionReturn(0);
3250264ace61SBarry Smith }
3251264ace61SBarry Smith 
3252264ace61SBarry Smith /*@C
3253264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3254264ace61SBarry Smith 
3255264ace61SBarry Smith   Not Collective
3256264ace61SBarry Smith 
3257264ace61SBarry Smith   Input Parameter:
3258264ace61SBarry Smith . dm  - The DM
3259264ace61SBarry Smith 
3260264ace61SBarry Smith   Output Parameter:
3261264ace61SBarry Smith . type - The DM type name
3262264ace61SBarry Smith 
3263264ace61SBarry Smith   Level: intermediate
3264264ace61SBarry Smith 
3265264ace61SBarry Smith .keywords: DM, get, type, name
3266264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3267264ace61SBarry Smith @*/
326819fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3269264ace61SBarry Smith {
3270264ace61SBarry Smith   PetscErrorCode ierr;
3271264ace61SBarry Smith 
3272264ace61SBarry Smith   PetscFunctionBegin;
3273264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3274c959eef4SJed Brown   PetscValidPointer(type,2);
3275607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3276264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3277264ace61SBarry Smith   PetscFunctionReturn(0);
3278264ace61SBarry Smith }
3279264ace61SBarry Smith 
328067a56275SMatthew G Knepley /*@C
328167a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
328267a56275SMatthew G Knepley 
328367a56275SMatthew G Knepley   Collective on DM
328467a56275SMatthew G Knepley 
328567a56275SMatthew G Knepley   Input Parameters:
328667a56275SMatthew G Knepley + dm - the DM
328767a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
328867a56275SMatthew G Knepley 
328967a56275SMatthew G Knepley   Output Parameter:
329067a56275SMatthew G Knepley . M - pointer to new DM
329167a56275SMatthew G Knepley 
329267a56275SMatthew G Knepley   Notes:
329367a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
329467a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
329567a56275SMatthew G Knepley   of the input DM.
329667a56275SMatthew G Knepley 
329767a56275SMatthew G Knepley   Level: intermediate
329867a56275SMatthew G Knepley 
329967a56275SMatthew G Knepley .seealso: DMCreate()
330067a56275SMatthew G Knepley @*/
330119fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
330267a56275SMatthew G Knepley {
330367a56275SMatthew G Knepley   DM             B;
330467a56275SMatthew G Knepley   char           convname[256];
3305c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
330667a56275SMatthew G Knepley   PetscErrorCode ierr;
330767a56275SMatthew G Knepley 
330867a56275SMatthew G Knepley   PetscFunctionBegin;
330967a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
331067a56275SMatthew G Knepley   PetscValidType(dm,1);
331167a56275SMatthew G Knepley   PetscValidPointer(M,3);
3312251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3313c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3314c067b6caSMatthew G. Knepley   if (sametype) {
3315c067b6caSMatthew G. Knepley     *M   = dm;
3316c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3317c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3318c067b6caSMatthew G. Knepley   } else {
33190298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
332067a56275SMatthew G Knepley 
332167a56275SMatthew G Knepley     /*
332267a56275SMatthew G Knepley        Order of precedence:
332367a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
332467a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
332567a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
332667a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
332767a56275SMatthew G Knepley        5) Use a really basic converter.
332867a56275SMatthew G Knepley     */
332967a56275SMatthew G Knepley 
333067a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3331a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3332a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3333a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3334a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3335a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
33360005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
333767a56275SMatthew G Knepley     if (conv) goto foundconv;
333867a56275SMatthew G Knepley 
333967a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
334082f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
334167a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3342a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3343a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3344a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3345a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3346a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
33470005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
334867a56275SMatthew G Knepley     if (conv) {
3349fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
335067a56275SMatthew G Knepley       goto foundconv;
335167a56275SMatthew G Knepley     }
335267a56275SMatthew G Knepley 
335367a56275SMatthew G Knepley #if 0
335467a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
335567a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3356fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
335767a56275SMatthew G Knepley     if (conv) goto foundconv;
335867a56275SMatthew G Knepley 
335967a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
336067a56275SMatthew G Knepley     if (dm->ops->convert) {
336167a56275SMatthew G Knepley       conv = dm->ops->convert;
336267a56275SMatthew G Knepley     }
336367a56275SMatthew G Knepley     if (conv) goto foundconv;
336467a56275SMatthew G Knepley #endif
336567a56275SMatthew G Knepley 
336667a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
336782f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
336867a56275SMatthew G Knepley 
336967a56275SMatthew G Knepley foundconv:
337067a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
337167a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
337212fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
337390b157c4SStefano Zampini     {
337490b157c4SStefano Zampini       PetscBool             isper;
337512fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
337612fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
337790b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
337890b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
337912fa691eSMatthew G. Knepley     }
338067a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
338167a56275SMatthew G Knepley   }
338267a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
338367a56275SMatthew G Knepley   PetscFunctionReturn(0);
338467a56275SMatthew G Knepley }
3385264ace61SBarry Smith 
3386264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3387264ace61SBarry Smith 
3388264ace61SBarry Smith /*@C
33891c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
33901c84c290SBarry Smith 
33911c84c290SBarry Smith   Not Collective
33921c84c290SBarry Smith 
33931c84c290SBarry Smith   Input Parameters:
33941c84c290SBarry Smith + name        - The name of a new user-defined creation routine
33951c84c290SBarry Smith - create_func - The creation routine itself
33961c84c290SBarry Smith 
33971c84c290SBarry Smith   Notes:
33981c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
33991c84c290SBarry Smith 
34001c84c290SBarry Smith 
34011c84c290SBarry Smith   Sample usage:
34021c84c290SBarry Smith .vb
3403bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
34041c84c290SBarry Smith .ve
34051c84c290SBarry Smith 
34061c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
34071c84c290SBarry Smith .vb
34081c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
34091c84c290SBarry Smith     DMSetType(DM,"my_da");
34101c84c290SBarry Smith .ve
34111c84c290SBarry Smith    or at runtime via the option
34121c84c290SBarry Smith .vb
34131c84c290SBarry Smith     -da_type my_da
34141c84c290SBarry Smith .ve
3415264ace61SBarry Smith 
3416264ace61SBarry Smith   Level: advanced
34171c84c290SBarry Smith 
34181c84c290SBarry Smith .keywords: DM, register
3419bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
34201c84c290SBarry Smith 
3421264ace61SBarry Smith @*/
3422bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3423264ace61SBarry Smith {
3424264ace61SBarry Smith   PetscErrorCode ierr;
3425264ace61SBarry Smith 
3426264ace61SBarry Smith   PetscFunctionBegin;
3427a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3428264ace61SBarry Smith   PetscFunctionReturn(0);
3429264ace61SBarry Smith }
3430264ace61SBarry Smith 
3431b859378eSBarry Smith /*@C
343255849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3433b859378eSBarry Smith 
3434b859378eSBarry Smith   Collective on PetscViewer
3435b859378eSBarry Smith 
3436b859378eSBarry Smith   Input Parameters:
3437b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3438b859378eSBarry Smith            some related function before a call to DMLoad().
3439b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3440b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3441b859378eSBarry Smith 
3442b859378eSBarry Smith    Level: intermediate
3443b859378eSBarry Smith 
3444b859378eSBarry Smith   Notes:
344555849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3446b859378eSBarry Smith 
3447b859378eSBarry Smith   Notes for advanced users:
3448b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3449b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3450b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3451b859378eSBarry Smith   format is
3452b859378eSBarry Smith .vb
3453b859378eSBarry Smith      has not yet been determined
3454b859378eSBarry Smith .ve
3455b859378eSBarry Smith 
3456b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3457b859378eSBarry Smith @*/
3458b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3459b859378eSBarry Smith {
34609331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3461b859378eSBarry Smith   PetscErrorCode ierr;
3462b859378eSBarry Smith 
3463b859378eSBarry Smith   PetscFunctionBegin;
3464b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3465b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
346632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
34679331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
34689331c7a4SMatthew G. Knepley   if (isbinary) {
34699331c7a4SMatthew G. Knepley     PetscInt classid;
34709331c7a4SMatthew G. Knepley     char     type[256];
3471b859378eSBarry Smith 
3472060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
34739200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3474060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
347532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
34769331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
34779331c7a4SMatthew G. Knepley   } else if (ishdf5) {
34789331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
34799331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3480b859378eSBarry Smith   PetscFunctionReturn(0);
3481b859378eSBarry Smith }
3482b859378eSBarry Smith 
34837da65231SMatthew G Knepley /******************************** FEM Support **********************************/
34847da65231SMatthew G Knepley 
3485a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3486a6dfd86eSKarl Rupp {
34871d47ebbbSSatish Balay   PetscInt       f;
34881b30c384SMatthew G Knepley   PetscErrorCode ierr;
34891b30c384SMatthew G Knepley 
34907da65231SMatthew G Knepley   PetscFunctionBegin;
349174778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
34921d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
349357622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
34947da65231SMatthew G Knepley   }
34957da65231SMatthew G Knepley   PetscFunctionReturn(0);
34967da65231SMatthew G Knepley }
34977da65231SMatthew G Knepley 
3498a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3499a6dfd86eSKarl Rupp {
35001b30c384SMatthew G Knepley   PetscInt       f, g;
35017da65231SMatthew G Knepley   PetscErrorCode ierr;
35027da65231SMatthew G Knepley 
35037da65231SMatthew G Knepley   PetscFunctionBegin;
350474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35051d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
350674778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
35071d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3508e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
35097da65231SMatthew G Knepley     }
351074778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
35117da65231SMatthew G Knepley   }
35127da65231SMatthew G Knepley   PetscFunctionReturn(0);
35137da65231SMatthew G Knepley }
3514e7c4fc90SDmitry Karpeev 
35156113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3516e759306cSMatthew G. Knepley {
35170c5b8624SToby Isaac   PetscInt          localSize, bs;
35180c5b8624SToby Isaac   PetscMPIInt       size;
35190c5b8624SToby Isaac   Vec               x, xglob;
35200c5b8624SToby Isaac   const PetscScalar *xarray;
3521e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3522e759306cSMatthew G. Knepley 
3523e759306cSMatthew G. Knepley   PetscFunctionBegin;
35249852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3525e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3526e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
35276113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
35280c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
35290c5b8624SToby Isaac   if (size > 1) {
35300c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
35310c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
35320c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
35330c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
35340c5b8624SToby Isaac   } else {
35350c5b8624SToby Isaac     xglob = x;
35360c5b8624SToby Isaac   }
35370c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
35380c5b8624SToby Isaac   if (size > 1) {
35390c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
35400c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
35410c5b8624SToby Isaac   }
3542e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3543e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3544e759306cSMatthew G. Knepley }
3545e759306cSMatthew G. Knepley 
354688ed4aceSMatthew G Knepley /*@
3547e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
354888ed4aceSMatthew G Knepley 
354988ed4aceSMatthew G Knepley   Input Parameter:
355088ed4aceSMatthew G Knepley . dm - The DM
355188ed4aceSMatthew G Knepley 
355288ed4aceSMatthew G Knepley   Output Parameter:
355388ed4aceSMatthew G Knepley . section - The PetscSection
355488ed4aceSMatthew G Knepley 
355588ed4aceSMatthew G Knepley   Level: intermediate
355688ed4aceSMatthew G Knepley 
355788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
355888ed4aceSMatthew G Knepley 
3559e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
356088ed4aceSMatthew G Knepley @*/
3561e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
35620adebc6cSBarry Smith {
3563fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3564fd59a867SMatthew G. Knepley 
356588ed4aceSMatthew G Knepley   PetscFunctionBegin;
356688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
356788ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
35682f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
35692f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3570ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
35712f0f8703SMatthew G. Knepley   }
357288ed4aceSMatthew G Knepley   *section = dm->defaultSection;
357388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
357488ed4aceSMatthew G Knepley }
357588ed4aceSMatthew G Knepley 
357688ed4aceSMatthew G Knepley /*@
3577e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
357888ed4aceSMatthew G Knepley 
357988ed4aceSMatthew G Knepley   Input Parameters:
358088ed4aceSMatthew G Knepley + dm - The DM
358188ed4aceSMatthew G Knepley - section - The PetscSection
358288ed4aceSMatthew G Knepley 
358388ed4aceSMatthew G Knepley   Level: intermediate
358488ed4aceSMatthew G Knepley 
358588ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
358688ed4aceSMatthew G Knepley 
3587e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
358888ed4aceSMatthew G Knepley @*/
3589e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
35900adebc6cSBarry Smith {
3591c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3592af122d2aSMatthew G Knepley   PetscInt       f;
359388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
359488ed4aceSMatthew G Knepley 
359588ed4aceSMatthew G Knepley   PetscFunctionBegin;
359688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3597c473ab19SMatthew G. Knepley   if (section) {
35981d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
35991d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3600c473ab19SMatthew G. Knepley   }
360188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
360288ed4aceSMatthew G Knepley   dm->defaultSection = section;
3603c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3604af122d2aSMatthew G Knepley   if (numFields) {
3605af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3606af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
36070f21e855SMatthew G. Knepley       PetscObject disc;
3608af122d2aSMatthew G Knepley       const char *name;
3609af122d2aSMatthew G Knepley 
3610af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
36110f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
36120f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3613af122d2aSMatthew G Knepley     }
3614af122d2aSMatthew G Knepley   }
3615e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
36161d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
361788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
361888ed4aceSMatthew G Knepley }
361988ed4aceSMatthew G Knepley 
36209435951eSToby Isaac /*@
36219435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
36229435951eSToby Isaac 
3623e228b242SToby Isaac   not collective
3624e228b242SToby Isaac 
36259435951eSToby Isaac   Input Parameter:
36269435951eSToby Isaac . dm - The DM
36279435951eSToby Isaac 
36289435951eSToby Isaac   Output Parameter:
36299435951eSToby 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.
36309435951eSToby 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.
36319435951eSToby Isaac 
36329435951eSToby Isaac   Level: advanced
36339435951eSToby Isaac 
36349435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
36359435951eSToby Isaac 
36369435951eSToby Isaac .seealso: DMSetDefaultConstraints()
36379435951eSToby Isaac @*/
36389435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
36399435951eSToby Isaac {
36409435951eSToby Isaac   PetscErrorCode ierr;
36419435951eSToby Isaac 
36429435951eSToby Isaac   PetscFunctionBegin;
36439435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36449435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
364545a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
364645a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
36479435951eSToby Isaac   PetscFunctionReturn(0);
36489435951eSToby Isaac }
36499435951eSToby Isaac 
36509435951eSToby Isaac /*@
36519435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
36529435951eSToby Isaac 
36539435951eSToby 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().
36549435951eSToby Isaac 
36559435951eSToby 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.
36569435951eSToby Isaac 
3657e228b242SToby Isaac   collective on dm
3658e228b242SToby Isaac 
36599435951eSToby Isaac   Input Parameters:
36609435951eSToby Isaac + dm - The DM
3661e228b242SToby 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).
3662e228b242SToby 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).
36639435951eSToby Isaac 
36649435951eSToby Isaac   Level: advanced
36659435951eSToby Isaac 
36669435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
36679435951eSToby Isaac 
36689435951eSToby Isaac .seealso: DMGetDefaultConstraints()
36699435951eSToby Isaac @*/
36709435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
36719435951eSToby Isaac {
3672e228b242SToby Isaac   PetscMPIInt result;
36739435951eSToby Isaac   PetscErrorCode ierr;
36749435951eSToby Isaac 
36759435951eSToby Isaac   PetscFunctionBegin;
36769435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3677e228b242SToby Isaac   if (section) {
3678e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3679e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3680f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3681e228b242SToby Isaac   }
3682e228b242SToby Isaac   if (mat) {
3683e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3684e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3685f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3686e228b242SToby Isaac   }
36879435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
36889435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
36899435951eSToby Isaac   dm->defaultConstraintSection = section;
36909435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
36919435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
36929435951eSToby Isaac   dm->defaultConstraintMat = mat;
36939435951eSToby Isaac   PetscFunctionReturn(0);
36949435951eSToby Isaac }
36959435951eSToby Isaac 
3696497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3697507e4973SMatthew G. Knepley /*
3698507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3699507e4973SMatthew G. Knepley 
3700507e4973SMatthew G. Knepley   Input Parameters:
3701507e4973SMatthew G. Knepley + dm - The DM
3702507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3703507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3704507e4973SMatthew G. Knepley 
3705507e4973SMatthew G. Knepley   Level: intermediate
3706507e4973SMatthew G. Knepley 
3707507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3708507e4973SMatthew G. Knepley */
3709f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3710507e4973SMatthew G. Knepley {
3711507e4973SMatthew G. Knepley   MPI_Comm        comm;
3712507e4973SMatthew G. Knepley   PetscLayout     layout;
3713507e4973SMatthew G. Knepley   const PetscInt *ranges;
3714507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3715507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3716507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3717507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3718507e4973SMatthew G. Knepley 
3719507e4973SMatthew G. Knepley   PetscFunctionBegin;
3720507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3721507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3722507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3723507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3724507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3725507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3726507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3727507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3728507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3729507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3730507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3731507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3732f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3733507e4973SMatthew G. Knepley 
3734507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3735507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3736507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3737507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3738507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3739507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3740507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3741507e4973SMatthew 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;}
3742507e4973SMatthew 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;}
3743507e4973SMatthew G. Knepley     if (gdof < 0) {
3744507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3745507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3746507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3747507e4973SMatthew G. Knepley 
3748507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3749507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3750507e4973SMatthew 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;}
3751507e4973SMatthew G. Knepley       }
3752507e4973SMatthew G. Knepley     }
3753507e4973SMatthew G. Knepley   }
3754507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3755507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3756b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3757507e4973SMatthew G. Knepley   if (!gvalid) {
3758507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3759507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3760507e4973SMatthew G. Knepley   }
3761507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3762507e4973SMatthew G. Knepley }
3763f741bcd2SMatthew G. Knepley #endif
3764507e4973SMatthew G. Knepley 
376588ed4aceSMatthew G Knepley /*@
3766e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
376788ed4aceSMatthew G Knepley 
37688b1ab98fSJed Brown   Collective on DM
37698b1ab98fSJed Brown 
377088ed4aceSMatthew G Knepley   Input Parameter:
377188ed4aceSMatthew G Knepley . dm - The DM
377288ed4aceSMatthew G Knepley 
377388ed4aceSMatthew G Knepley   Output Parameter:
377488ed4aceSMatthew G Knepley . section - The PetscSection
377588ed4aceSMatthew G Knepley 
377688ed4aceSMatthew G Knepley   Level: intermediate
377788ed4aceSMatthew G Knepley 
377888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
377988ed4aceSMatthew G Knepley 
3780e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
378188ed4aceSMatthew G Knepley @*/
3782e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
37830adebc6cSBarry Smith {
378488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
378588ed4aceSMatthew G Knepley 
378688ed4aceSMatthew G Knepley   PetscFunctionBegin;
378788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
378888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
378988ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3790fd59a867SMatthew G. Knepley     PetscSection s;
3791fd59a867SMatthew G. Knepley 
3792e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3793fd59a867SMatthew 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");
3794fd59a867SMatthew 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");
379515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3796cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3797ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3798685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
379988ed4aceSMatthew G Knepley   }
380088ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
380188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
380288ed4aceSMatthew G Knepley }
380388ed4aceSMatthew G Knepley 
3804b21d0597SMatthew G Knepley /*@
3805e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3806b21d0597SMatthew G Knepley 
3807b21d0597SMatthew G Knepley   Input Parameters:
3808b21d0597SMatthew G Knepley + dm - The DM
38095080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3810b21d0597SMatthew G Knepley 
3811b21d0597SMatthew G Knepley   Level: intermediate
3812b21d0597SMatthew G Knepley 
3813b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3814b21d0597SMatthew G Knepley 
3815e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3816b21d0597SMatthew G Knepley @*/
3817e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
38180adebc6cSBarry Smith {
3819b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3820b21d0597SMatthew G Knepley 
3821b21d0597SMatthew G Knepley   PetscFunctionBegin;
3822b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
38235080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
38241d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3825b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3826b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3827497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3828f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3829507e4973SMatthew G. Knepley #endif
3830b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3831b21d0597SMatthew G Knepley }
3832b21d0597SMatthew G Knepley 
383388ed4aceSMatthew G Knepley /*@
383488ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
383588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
383688ed4aceSMatthew G Knepley 
383788ed4aceSMatthew G Knepley   Input Parameter:
383888ed4aceSMatthew G Knepley . dm - The DM
383988ed4aceSMatthew G Knepley 
384088ed4aceSMatthew G Knepley   Output Parameter:
384188ed4aceSMatthew G Knepley . sf - The PetscSF
384288ed4aceSMatthew G Knepley 
384388ed4aceSMatthew G Knepley   Level: intermediate
384488ed4aceSMatthew G Knepley 
384588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
384688ed4aceSMatthew G Knepley 
384788ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
384888ed4aceSMatthew G Knepley @*/
38490adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
38500adebc6cSBarry Smith {
385188ed4aceSMatthew G Knepley   PetscInt       nroots;
385288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
385388ed4aceSMatthew G Knepley 
385488ed4aceSMatthew G Knepley   PetscFunctionBegin;
385588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
385688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
38570298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
385888ed4aceSMatthew G Knepley   if (nroots < 0) {
385988ed4aceSMatthew G Knepley     PetscSection section, gSection;
386088ed4aceSMatthew G Knepley 
3861e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
386231ea6d37SMatthew G Knepley     if (section) {
3863e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
386488ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
386531ea6d37SMatthew G Knepley     } else {
38660298fd71SBarry Smith       *sf = NULL;
386731ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
386831ea6d37SMatthew G Knepley     }
386988ed4aceSMatthew G Knepley   }
387088ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
387188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
387288ed4aceSMatthew G Knepley }
387388ed4aceSMatthew G Knepley 
387488ed4aceSMatthew G Knepley /*@
387588ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
387688ed4aceSMatthew G Knepley 
387788ed4aceSMatthew G Knepley   Input Parameters:
387888ed4aceSMatthew G Knepley + dm - The DM
387988ed4aceSMatthew G Knepley - sf - The PetscSF
388088ed4aceSMatthew G Knepley 
388188ed4aceSMatthew G Knepley   Level: intermediate
388288ed4aceSMatthew G Knepley 
388388ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
388488ed4aceSMatthew G Knepley 
388588ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
388688ed4aceSMatthew G Knepley @*/
38870adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
38880adebc6cSBarry Smith {
388988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
389088ed4aceSMatthew G Knepley 
389188ed4aceSMatthew G Knepley   PetscFunctionBegin;
389288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
389388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
389488ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
389588ed4aceSMatthew G Knepley   dm->defaultSF = sf;
389688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
389788ed4aceSMatthew G Knepley }
389888ed4aceSMatthew G Knepley 
389988ed4aceSMatthew G Knepley /*@C
390088ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
390188ed4aceSMatthew G Knepley   describing the data layout.
390288ed4aceSMatthew G Knepley 
390388ed4aceSMatthew G Knepley   Input Parameters:
390488ed4aceSMatthew G Knepley + dm - The DM
390588ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
390688ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
390788ed4aceSMatthew G Knepley 
390888ed4aceSMatthew G Knepley   Level: intermediate
390988ed4aceSMatthew G Knepley 
391088ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
391188ed4aceSMatthew G Knepley @*/
391288ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
391388ed4aceSMatthew G Knepley {
391482f516ccSBarry Smith   MPI_Comm       comm;
391588ed4aceSMatthew G Knepley   PetscLayout    layout;
391688ed4aceSMatthew G Knepley   const PetscInt *ranges;
391788ed4aceSMatthew G Knepley   PetscInt       *local;
391888ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3919ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
392088ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
392188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
392288ed4aceSMatthew G Knepley 
392388ed4aceSMatthew G Knepley   PetscFunctionBegin;
392482f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
392588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
392688ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
392788ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
392888ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
392988ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
393088ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
393188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
393288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
393388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
393488ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3935ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
39366636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
393788ed4aceSMatthew G Knepley 
39386636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39396636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3940235fbf56SMatthew 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));
39416636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
394288ed4aceSMatthew G Knepley   }
3943785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3944785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
394588ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
39461f588964SMatthew G Knepley     const PetscInt *cind;
39476636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
394888ed4aceSMatthew G Knepley 
394988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
395088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
395188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
395288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
395388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39546636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
395588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
39566636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
39576636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
39586636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3959057b4bcdSMatthew 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);
39606636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
39616636e97aSMatthew G Knepley     }
396288ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
396388ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
396488ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
396588ed4aceSMatthew G Knepley     }
396688ed4aceSMatthew G Knepley     if (gdof < 0) {
39676636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
396888ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
396988ed4aceSMatthew G Knepley 
397005376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
397131d3f06eSJed Brown         if (r < 0) r = -(r+2);
397205376888SMatthew 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);
397388ed4aceSMatthew G Knepley         remote[l].rank  = r;
397488ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
397588ed4aceSMatthew G Knepley       }
397688ed4aceSMatthew G Knepley     } else {
39776636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
397888ed4aceSMatthew G Knepley         remote[l].rank  = rank;
397988ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
398088ed4aceSMatthew G Knepley       }
398188ed4aceSMatthew G Knepley     }
398288ed4aceSMatthew G Knepley   }
39836636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
398488ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
398588ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
398688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
398788ed4aceSMatthew G Knepley }
3988af122d2aSMatthew G Knepley 
3989b21d0597SMatthew G Knepley /*@
3990b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3991b21d0597SMatthew G Knepley 
3992b21d0597SMatthew G Knepley   Input Parameter:
3993b21d0597SMatthew G Knepley . dm - The DM
3994b21d0597SMatthew G Knepley 
3995b21d0597SMatthew G Knepley   Output Parameter:
3996b21d0597SMatthew G Knepley . sf - The PetscSF
3997b21d0597SMatthew G Knepley 
3998b21d0597SMatthew G Knepley   Level: intermediate
3999b21d0597SMatthew G Knepley 
4000b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4001b21d0597SMatthew G Knepley 
4002057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4003b21d0597SMatthew G Knepley @*/
40040adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
40050adebc6cSBarry Smith {
4006b21d0597SMatthew G Knepley   PetscFunctionBegin;
4007b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4008b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4009b21d0597SMatthew G Knepley   *sf = dm->sf;
4010b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4011b21d0597SMatthew G Knepley }
4012b21d0597SMatthew G Knepley 
4013057b4bcdSMatthew G Knepley /*@
4014057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4015057b4bcdSMatthew G Knepley 
4016057b4bcdSMatthew G Knepley   Input Parameters:
4017057b4bcdSMatthew G Knepley + dm - The DM
4018057b4bcdSMatthew G Knepley - sf - The PetscSF
4019057b4bcdSMatthew G Knepley 
4020057b4bcdSMatthew G Knepley   Level: intermediate
4021057b4bcdSMatthew G Knepley 
4022057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4023057b4bcdSMatthew G Knepley @*/
40240adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
40250adebc6cSBarry Smith {
4026057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4027057b4bcdSMatthew G Knepley 
4028057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4029057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4030057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
4031057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4032057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4033057b4bcdSMatthew G Knepley   dm->sf = sf;
4034057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4035057b4bcdSMatthew G Knepley }
4036057b4bcdSMatthew G Knepley 
40372764a2aaSMatthew G. Knepley /*@
40382764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
40392764a2aaSMatthew G. Knepley 
40402764a2aaSMatthew G. Knepley   Input Parameter:
40412764a2aaSMatthew G. Knepley . dm - The DM
40422764a2aaSMatthew G. Knepley 
40432764a2aaSMatthew G. Knepley   Output Parameter:
40442764a2aaSMatthew G. Knepley . prob - The PetscDS
40452764a2aaSMatthew G. Knepley 
40462764a2aaSMatthew G. Knepley   Level: developer
40472764a2aaSMatthew G. Knepley 
40482764a2aaSMatthew G. Knepley .seealso: DMSetDS()
40492764a2aaSMatthew G. Knepley @*/
40502764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4051af122d2aSMatthew G Knepley {
4052af122d2aSMatthew G Knepley   PetscFunctionBegin;
4053af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40540f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
40550f21e855SMatthew G. Knepley   *prob = dm->prob;
40560f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
40570f21e855SMatthew G. Knepley }
40580f21e855SMatthew G. Knepley 
40592764a2aaSMatthew G. Knepley /*@
40602764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
40612764a2aaSMatthew G. Knepley 
40622764a2aaSMatthew G. Knepley   Input Parameters:
40632764a2aaSMatthew G. Knepley + dm - The DM
40642764a2aaSMatthew G. Knepley - prob - The PetscDS
40652764a2aaSMatthew G. Knepley 
40662764a2aaSMatthew G. Knepley   Level: developer
40672764a2aaSMatthew G. Knepley 
40682764a2aaSMatthew G. Knepley .seealso: DMGetDS()
40692764a2aaSMatthew G. Knepley @*/
40702764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
40710f21e855SMatthew G. Knepley {
4072f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
40730f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40740f21e855SMatthew G. Knepley 
40750f21e855SMatthew G. Knepley   PetscFunctionBegin;
40760f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40772764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
407837316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
40792764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
40800f21e855SMatthew G. Knepley   dm->prob = prob;
4081f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4082f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
40830f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
40840f21e855SMatthew G. Knepley }
40850f21e855SMatthew G. Knepley 
40860f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
40870f21e855SMatthew G. Knepley {
40880f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40890f21e855SMatthew G. Knepley 
40900f21e855SMatthew G. Knepley   PetscFunctionBegin;
40910f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40922764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4093af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4094af122d2aSMatthew G Knepley }
4095af122d2aSMatthew G Knepley 
4096af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4097af122d2aSMatthew G Knepley {
40980f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4099af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4100af122d2aSMatthew G Knepley 
4101af122d2aSMatthew G Knepley   PetscFunctionBegin;
4102af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41032764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
41040f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
41050f21e855SMatthew G. Knepley     PetscContainer obj;
41060f21e855SMatthew G. Knepley 
41070f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
41082764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
41090f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4110af122d2aSMatthew G Knepley   }
4111af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4112af122d2aSMatthew G Knepley }
4113af122d2aSMatthew G Knepley 
4114c1929be8SMatthew G. Knepley /*@
4115c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4116c1929be8SMatthew G. Knepley 
4117c1929be8SMatthew G. Knepley   Not collective
4118c1929be8SMatthew G. Knepley 
4119c1929be8SMatthew G. Knepley   Input Parameters:
4120c1929be8SMatthew G. Knepley + dm - The DM
4121c1929be8SMatthew G. Knepley - f  - The field number
4122c1929be8SMatthew G. Knepley 
4123c1929be8SMatthew G. Knepley   Output Parameter:
4124c1929be8SMatthew G. Knepley . field - The discretization object
4125c1929be8SMatthew G. Knepley 
4126c1929be8SMatthew G. Knepley   Level: developer
4127c1929be8SMatthew G. Knepley 
4128c1929be8SMatthew G. Knepley .seealso: DMSetField()
4129c1929be8SMatthew G. Knepley @*/
4130af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4131af122d2aSMatthew G Knepley {
41320f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41330f21e855SMatthew G. Knepley 
4134af122d2aSMatthew G Knepley   PetscFunctionBegin;
4135af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41362764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4137decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4138decb47aaSMatthew G. Knepley }
4139decb47aaSMatthew G. Knepley 
4140c1929be8SMatthew G. Knepley /*@
4141c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4142c1929be8SMatthew G. Knepley 
4143c1929be8SMatthew G. Knepley   Logically collective on DM
4144c1929be8SMatthew G. Knepley 
4145c1929be8SMatthew G. Knepley   Input Parameters:
4146c1929be8SMatthew G. Knepley + dm - The DM
4147c1929be8SMatthew G. Knepley . f  - The field number
4148c1929be8SMatthew G. Knepley - field - The discretization object
4149c1929be8SMatthew G. Knepley 
4150c1929be8SMatthew G. Knepley   Level: developer
4151c1929be8SMatthew G. Knepley 
4152c1929be8SMatthew G. Knepley .seealso: DMGetField()
4153c1929be8SMatthew G. Knepley @*/
4154decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4155decb47aaSMatthew G. Knepley {
4156decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4157decb47aaSMatthew G. Knepley 
4158decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4159decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41602764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4161af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4162af122d2aSMatthew G Knepley }
41636636e97aSMatthew G Knepley 
4164b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4165b64e0483SPeter Brune {
4166b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4167b64e0483SPeter Brune   PetscErrorCode ierr;
4168b64e0483SPeter Brune   Vec coords,ccoords;
41696dbf9973SLawrence Mitchell   Mat inject;
4170b64e0483SPeter Brune   PetscFunctionBegin;
4171b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4172b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4173b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4174b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4175b64e0483SPeter Brune   if (coords && !ccoords) {
4176b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
41776668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
41786dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
41792adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
41806dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4181b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4182b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4183b64e0483SPeter Brune   }
4184b64e0483SPeter Brune   PetscFunctionReturn(0);
4185b64e0483SPeter Brune }
4186b64e0483SPeter Brune 
418703dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
418803dadc2fSPeter Brune {
418903dadc2fSPeter Brune   DM dm_coord,subdm_coord;
419003dadc2fSPeter Brune   PetscErrorCode ierr;
419103dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
419203dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
419303dadc2fSPeter Brune   PetscFunctionBegin;
419403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
419503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
419603dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
419703dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
419803dadc2fSPeter Brune   if (coords && !ccoords) {
419903dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
42006668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
420103dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
420224640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
420303dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
420403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
420503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
420603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
420703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
420803dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
420903dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
421003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
421103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
421203dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
421303dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
421403dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
421503dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
421603dadc2fSPeter Brune   }
421703dadc2fSPeter Brune   PetscFunctionReturn(0);
421803dadc2fSPeter Brune }
421903dadc2fSPeter Brune 
4220c73cfb54SMatthew G. Knepley /*@
4221c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4222c73cfb54SMatthew G. Knepley 
4223c73cfb54SMatthew G. Knepley   Not collective
4224c73cfb54SMatthew G. Knepley 
4225c73cfb54SMatthew G. Knepley   Input Parameter:
4226c73cfb54SMatthew G. Knepley . dm - The DM
4227c73cfb54SMatthew G. Knepley 
4228c73cfb54SMatthew G. Knepley   Output Parameter:
4229c73cfb54SMatthew G. Knepley . dim - The topological dimension
4230c73cfb54SMatthew G. Knepley 
4231c73cfb54SMatthew G. Knepley   Level: beginner
4232c73cfb54SMatthew G. Knepley 
4233c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4234c73cfb54SMatthew G. Knepley @*/
4235c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4236c73cfb54SMatthew G. Knepley {
4237c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4238c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4239c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4240c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4241c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4242c73cfb54SMatthew G. Knepley }
4243c73cfb54SMatthew G. Knepley 
4244c73cfb54SMatthew G. Knepley /*@
4245c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4246c73cfb54SMatthew G. Knepley 
4247c73cfb54SMatthew G. Knepley   Collective on dm
4248c73cfb54SMatthew G. Knepley 
4249c73cfb54SMatthew G. Knepley   Input Parameters:
4250c73cfb54SMatthew G. Knepley + dm - The DM
4251c73cfb54SMatthew G. Knepley - dim - The topological dimension
4252c73cfb54SMatthew G. Knepley 
4253c73cfb54SMatthew G. Knepley   Level: beginner
4254c73cfb54SMatthew G. Knepley 
4255c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4256c73cfb54SMatthew G. Knepley @*/
4257c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4258c73cfb54SMatthew G. Knepley {
4259f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4260f17e8794SMatthew G. Knepley 
4261c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4262c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4263c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4264c73cfb54SMatthew G. Knepley   dm->dim = dim;
4265f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4266c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4267c73cfb54SMatthew G. Knepley }
4268c73cfb54SMatthew G. Knepley 
4269793f3fe5SMatthew G. Knepley /*@
4270793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4271793f3fe5SMatthew G. Knepley 
4272793f3fe5SMatthew G. Knepley   Collective on DM
4273793f3fe5SMatthew G. Knepley 
4274793f3fe5SMatthew G. Knepley   Input Parameters:
4275793f3fe5SMatthew G. Knepley + dm - the DM
4276793f3fe5SMatthew G. Knepley - dim - the dimension
4277793f3fe5SMatthew G. Knepley 
4278793f3fe5SMatthew G. Knepley   Output Parameters:
4279793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4280793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4281793f3fe5SMatthew G. Knepley 
4282793f3fe5SMatthew G. Knepley   Note:
4283793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4284793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4285793f3fe5SMatthew G. Knepley   then the interval is empty.
4286793f3fe5SMatthew G. Knepley 
4287793f3fe5SMatthew G. Knepley   Level: intermediate
4288793f3fe5SMatthew G. Knepley 
4289793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4290793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4291793f3fe5SMatthew G. Knepley @*/
4292793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4293793f3fe5SMatthew G. Knepley {
4294793f3fe5SMatthew G. Knepley   PetscInt       d;
4295793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4296793f3fe5SMatthew G. Knepley 
4297793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4298793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4299793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4300793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4301793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4302793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4303793f3fe5SMatthew G. Knepley }
4304793f3fe5SMatthew G. Knepley 
43056636e97aSMatthew G Knepley /*@
43066636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
43076636e97aSMatthew G Knepley 
43086636e97aSMatthew G Knepley   Collective on DM
43096636e97aSMatthew G Knepley 
43106636e97aSMatthew G Knepley   Input Parameters:
43116636e97aSMatthew G Knepley + dm - the DM
43126636e97aSMatthew G Knepley - c - coordinate vector
43136636e97aSMatthew G Knepley 
43142dd40e9bSPatrick Sanan   Notes:
43152dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
43162dd40e9bSPatrick Sanan 
43172dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
43186636e97aSMatthew G Knepley 
43196636e97aSMatthew G Knepley   Level: intermediate
43206636e97aSMatthew G Knepley 
43216636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43222dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
43236636e97aSMatthew G Knepley @*/
43246636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
43256636e97aSMatthew G Knepley {
43266636e97aSMatthew G Knepley   PetscErrorCode ierr;
43276636e97aSMatthew G Knepley 
43286636e97aSMatthew G Knepley   PetscFunctionBegin;
43296636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43306636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
43316636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
43326636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
43336636e97aSMatthew G Knepley   dm->coordinates = c;
43346636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4335b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
433603dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
43376636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43386636e97aSMatthew G Knepley }
43396636e97aSMatthew G Knepley 
43406636e97aSMatthew G Knepley /*@
43416636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
43426636e97aSMatthew G Knepley 
43436636e97aSMatthew G Knepley   Collective on DM
43446636e97aSMatthew G Knepley 
43456636e97aSMatthew G Knepley    Input Parameters:
43466636e97aSMatthew G Knepley +  dm - the DM
43476636e97aSMatthew G Knepley -  c - coordinate vector
43486636e97aSMatthew G Knepley 
43492dd40e9bSPatrick Sanan   Notes:
43506636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
43516636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
43526636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
43536636e97aSMatthew G Knepley 
43542dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
43552dd40e9bSPatrick Sanan 
43566636e97aSMatthew G Knepley   Level: intermediate
43576636e97aSMatthew G Knepley 
43586636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43596636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
43606636e97aSMatthew G Knepley @*/
43616636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
43626636e97aSMatthew G Knepley {
43636636e97aSMatthew G Knepley   PetscErrorCode ierr;
43646636e97aSMatthew G Knepley 
43656636e97aSMatthew G Knepley   PetscFunctionBegin;
43666636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
43686636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
43696636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
43708865f1eaSKarl Rupp 
43716636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
43728865f1eaSKarl Rupp 
43736636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
43746636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43756636e97aSMatthew G Knepley }
43766636e97aSMatthew G Knepley 
43776636e97aSMatthew G Knepley /*@
43786636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
43796636e97aSMatthew G Knepley 
43806636e97aSMatthew G Knepley   Not Collective
43816636e97aSMatthew G Knepley 
43826636e97aSMatthew G Knepley   Input Parameter:
43836636e97aSMatthew G Knepley . dm - the DM
43846636e97aSMatthew G Knepley 
43856636e97aSMatthew G Knepley   Output Parameter:
43866636e97aSMatthew G Knepley . c - global coordinate vector
43876636e97aSMatthew G Knepley 
43886636e97aSMatthew G Knepley   Note:
43896636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
43906636e97aSMatthew G Knepley 
43916636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
43926636e97aSMatthew G Knepley 
43936636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
43946636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
43956636e97aSMatthew G Knepley 
43966636e97aSMatthew G Knepley   Level: intermediate
43976636e97aSMatthew G Knepley 
43986636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43996636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
44006636e97aSMatthew G Knepley @*/
44016636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
44026636e97aSMatthew G Knepley {
44036636e97aSMatthew G Knepley   PetscErrorCode ierr;
44046636e97aSMatthew G Knepley 
44056636e97aSMatthew G Knepley   PetscFunctionBegin;
44066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44076636e97aSMatthew G Knepley   PetscValidPointer(c,2);
44081f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
44090298fd71SBarry Smith     DM cdm = NULL;
44106636e97aSMatthew G Knepley 
44116636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
44126636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
44136636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
44146636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
44156636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
44166636e97aSMatthew G Knepley   }
44176636e97aSMatthew G Knepley   *c = dm->coordinates;
44186636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44196636e97aSMatthew G Knepley }
44206636e97aSMatthew G Knepley 
44216636e97aSMatthew G Knepley /*@
44226636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
44236636e97aSMatthew G Knepley 
44246636e97aSMatthew G Knepley   Collective on DM
44256636e97aSMatthew G Knepley 
44266636e97aSMatthew G Knepley   Input Parameter:
44276636e97aSMatthew G Knepley . dm - the DM
44286636e97aSMatthew G Knepley 
44296636e97aSMatthew G Knepley   Output Parameter:
44306636e97aSMatthew G Knepley . c - coordinate vector
44316636e97aSMatthew G Knepley 
44326636e97aSMatthew G Knepley   Note:
44336636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
44346636e97aSMatthew G Knepley 
44356636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
44366636e97aSMatthew G Knepley 
44376636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
44386636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
44396636e97aSMatthew G Knepley 
44406636e97aSMatthew G Knepley   Level: intermediate
44416636e97aSMatthew G Knepley 
44426636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44436636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
44446636e97aSMatthew G Knepley @*/
44456636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
44466636e97aSMatthew G Knepley {
44476636e97aSMatthew G Knepley   PetscErrorCode ierr;
44486636e97aSMatthew G Knepley 
44496636e97aSMatthew G Knepley   PetscFunctionBegin;
44506636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44516636e97aSMatthew G Knepley   PetscValidPointer(c,2);
44521f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
44530298fd71SBarry Smith     DM cdm = NULL;
44546636e97aSMatthew G Knepley 
44556636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
44566636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
44576636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
44586636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44596636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44606636e97aSMatthew G Knepley   }
44616636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
44626636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44636636e97aSMatthew G Knepley }
44646636e97aSMatthew G Knepley 
4465f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4466f19dbd58SToby Isaac {
4467f19dbd58SToby Isaac   PetscErrorCode ierr;
4468f19dbd58SToby Isaac 
4469f19dbd58SToby Isaac   PetscFunctionBegin;
4470f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4471f19dbd58SToby Isaac   PetscValidPointer(field,2);
4472f19dbd58SToby Isaac   if (!dm->coordinateField) {
4473f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4474f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4475f19dbd58SToby Isaac     }
4476f19dbd58SToby Isaac   }
4477f19dbd58SToby Isaac   *field = dm->coordinateField;
4478f19dbd58SToby Isaac   PetscFunctionReturn(0);
4479f19dbd58SToby Isaac }
4480f19dbd58SToby Isaac 
4481f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4482f19dbd58SToby Isaac {
4483f19dbd58SToby Isaac   PetscErrorCode ierr;
4484f19dbd58SToby Isaac 
4485f19dbd58SToby Isaac   PetscFunctionBegin;
4486f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4487f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4488c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
4489f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4490f19dbd58SToby Isaac   dm->coordinateField = field;
4491f19dbd58SToby Isaac   PetscFunctionReturn(0);
4492f19dbd58SToby Isaac }
4493f19dbd58SToby Isaac 
44946636e97aSMatthew G Knepley /*@
44951cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
44966636e97aSMatthew G Knepley 
44976636e97aSMatthew G Knepley   Collective on DM
44986636e97aSMatthew G Knepley 
44996636e97aSMatthew G Knepley   Input Parameter:
45006636e97aSMatthew G Knepley . dm - the DM
45016636e97aSMatthew G Knepley 
45026636e97aSMatthew G Knepley   Output Parameter:
45036636e97aSMatthew G Knepley . cdm - coordinate DM
45046636e97aSMatthew G Knepley 
45056636e97aSMatthew G Knepley   Level: intermediate
45066636e97aSMatthew G Knepley 
45076636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45081cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
45096636e97aSMatthew G Knepley @*/
45106636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
45116636e97aSMatthew G Knepley {
45126636e97aSMatthew G Knepley   PetscErrorCode ierr;
45136636e97aSMatthew G Knepley 
45146636e97aSMatthew G Knepley   PetscFunctionBegin;
45156636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45166636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
45176636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
451882f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
45196636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
45206636e97aSMatthew G Knepley   }
45216636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
45226636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45236636e97aSMatthew G Knepley }
4524e87bb0d3SMatthew G Knepley 
45251cfe2091SMatthew G. Knepley /*@
45261cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
45271cfe2091SMatthew G. Knepley 
45281cfe2091SMatthew G. Knepley   Logically Collective on DM
45291cfe2091SMatthew G. Knepley 
45301cfe2091SMatthew G. Knepley   Input Parameters:
45311cfe2091SMatthew G. Knepley + dm - the DM
45321cfe2091SMatthew G. Knepley - cdm - coordinate DM
45331cfe2091SMatthew G. Knepley 
45341cfe2091SMatthew G. Knepley   Level: intermediate
45351cfe2091SMatthew G. Knepley 
45361cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45371cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
45381cfe2091SMatthew G. Knepley @*/
45391cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
45401cfe2091SMatthew G. Knepley {
45411cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
45421cfe2091SMatthew G. Knepley 
45431cfe2091SMatthew G. Knepley   PetscFunctionBegin;
45441cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45451cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4546f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
45471cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
45481cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
45491cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
45501cfe2091SMatthew G. Knepley }
45511cfe2091SMatthew G. Knepley 
455246e270d4SMatthew G. Knepley /*@
455346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
455446e270d4SMatthew G. Knepley 
455546e270d4SMatthew G. Knepley   Not Collective
455646e270d4SMatthew G. Knepley 
455746e270d4SMatthew G. Knepley   Input Parameter:
455846e270d4SMatthew G. Knepley . dm - The DM object
455946e270d4SMatthew G. Knepley 
456046e270d4SMatthew G. Knepley   Output Parameter:
456146e270d4SMatthew G. Knepley . dim - The embedding dimension
456246e270d4SMatthew G. Knepley 
456346e270d4SMatthew G. Knepley   Level: intermediate
456446e270d4SMatthew G. Knepley 
456546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4566e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
456746e270d4SMatthew G. Knepley @*/
456846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
456946e270d4SMatthew G. Knepley {
457046e270d4SMatthew G. Knepley   PetscFunctionBegin;
457146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457246e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
45739a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
45749a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
45759a9a41abSToby Isaac   }
457646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
457746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
457846e270d4SMatthew G. Knepley }
457946e270d4SMatthew G. Knepley 
458046e270d4SMatthew G. Knepley /*@
458146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
458246e270d4SMatthew G. Knepley 
458346e270d4SMatthew G. Knepley   Not Collective
458446e270d4SMatthew G. Knepley 
458546e270d4SMatthew G. Knepley   Input Parameters:
458646e270d4SMatthew G. Knepley + dm  - The DM object
458746e270d4SMatthew G. Knepley - dim - The embedding dimension
458846e270d4SMatthew G. Knepley 
458946e270d4SMatthew G. Knepley   Level: intermediate
459046e270d4SMatthew G. Knepley 
459146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4592e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
459346e270d4SMatthew G. Knepley @*/
459446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
459546e270d4SMatthew G. Knepley {
4596f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4597f17e8794SMatthew G. Knepley 
459846e270d4SMatthew G. Knepley   PetscFunctionBegin;
459946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
460046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4601f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
460246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
460346e270d4SMatthew G. Knepley }
460446e270d4SMatthew G. Knepley 
4605e8abe2deSMatthew G. Knepley /*@
4606e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4607e8abe2deSMatthew G. Knepley 
4608e8abe2deSMatthew G. Knepley   Not Collective
4609e8abe2deSMatthew G. Knepley 
4610e8abe2deSMatthew G. Knepley   Input Parameter:
4611e8abe2deSMatthew G. Knepley . dm - The DM object
4612e8abe2deSMatthew G. Knepley 
4613e8abe2deSMatthew G. Knepley   Output Parameter:
4614e8abe2deSMatthew G. Knepley . section - The PetscSection object
4615e8abe2deSMatthew G. Knepley 
4616e8abe2deSMatthew G. Knepley   Level: intermediate
4617e8abe2deSMatthew G. Knepley 
4618e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4619e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
4620e8abe2deSMatthew G. Knepley @*/
4621e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4622e8abe2deSMatthew G. Knepley {
4623e8abe2deSMatthew G. Knepley   DM             cdm;
4624e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4625e8abe2deSMatthew G. Knepley 
4626e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4627e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4628e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4629e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4630e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
4631e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4632e8abe2deSMatthew G. Knepley }
4633e8abe2deSMatthew G. Knepley 
4634e8abe2deSMatthew G. Knepley /*@
4635e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4636e8abe2deSMatthew G. Knepley 
4637e8abe2deSMatthew G. Knepley   Not Collective
4638e8abe2deSMatthew G. Knepley 
4639e8abe2deSMatthew G. Knepley   Input Parameters:
4640e8abe2deSMatthew G. Knepley + dm      - The DM object
464146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4642e8abe2deSMatthew G. Knepley - section - The PetscSection object
4643e8abe2deSMatthew G. Knepley 
4644e8abe2deSMatthew G. Knepley   Level: intermediate
4645e8abe2deSMatthew G. Knepley 
4646e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4647e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
4648e8abe2deSMatthew G. Knepley @*/
464946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4650e8abe2deSMatthew G. Knepley {
4651e8abe2deSMatthew G. Knepley   DM             cdm;
4652e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4653e8abe2deSMatthew G. Knepley 
4654e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4655e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
465646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4657e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4658e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
465946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
46604c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
466146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
466246e270d4SMatthew G. Knepley 
466346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
466446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
466546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
466646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
466746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
466846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
466946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
467046e270d4SMatthew G. Knepley     }
46716be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
467246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
467346e270d4SMatthew G. Knepley   }
4674e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4675e8abe2deSMatthew G. Knepley }
4676e8abe2deSMatthew G. Knepley 
46775dc8c3f7SMatthew G. Knepley /*@C
467890b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
46795dc8c3f7SMatthew G. Knepley 
46805dc8c3f7SMatthew G. Knepley   Input Parameters:
468190b157c4SStefano Zampini . dm      - The DM object
468290b157c4SStefano Zampini 
468390b157c4SStefano Zampini   Output Parameters:
468490b157c4SStefano Zampini + per     - Whether the DM is periodic or not
46855dc8c3f7SMatthew 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
46865dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
46875dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
46885dc8c3f7SMatthew G. Knepley 
46895dc8c3f7SMatthew G. Knepley   Level: developer
46905dc8c3f7SMatthew G. Knepley 
46915dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
46925dc8c3f7SMatthew G. Knepley @*/
469390b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4694c6b900c6SMatthew G. Knepley {
4695c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4696c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
469790b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4698c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4699c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
47005dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4701c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4702c6b900c6SMatthew G. Knepley }
4703c6b900c6SMatthew G. Knepley 
47045dc8c3f7SMatthew G. Knepley /*@C
47055dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
47065dc8c3f7SMatthew G. Knepley 
47075dc8c3f7SMatthew G. Knepley   Input Parameters:
47085dc8c3f7SMatthew G. Knepley + dm      - The DM object
470990b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
47105dc8c3f7SMatthew 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
47115dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
47125dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
47135dc8c3f7SMatthew G. Knepley 
47145dc8c3f7SMatthew G. Knepley   Level: developer
47155dc8c3f7SMatthew G. Knepley 
47165dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
47175dc8c3f7SMatthew G. Knepley @*/
471890b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4719c6b900c6SMatthew G. Knepley {
4720c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4721c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4722c6b900c6SMatthew G. Knepley 
4723c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4724c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
472590b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
472690b157c4SStefano Zampini   if (maxCell) {
472790b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
472890b157c4SStefano Zampini     PetscValidPointer(L,4);
472990b157c4SStefano Zampini     PetscValidPointer(bd,5);
473090b157c4SStefano Zampini   }
47315dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
47325dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
473390b157c4SStefano Zampini   if (maxCell) {
47345dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
47355dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
473690b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
473790b157c4SStefano Zampini   } else {
473890b157c4SStefano Zampini     dm->periodic = per;
473990b157c4SStefano Zampini   }
4740c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4741c6b900c6SMatthew G. Knepley }
4742c6b900c6SMatthew G. Knepley 
47432e17dfb7SMatthew G. Knepley /*@
47442e17dfb7SMatthew 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.
47452e17dfb7SMatthew G. Knepley 
47462e17dfb7SMatthew G. Knepley   Input Parameters:
47472e17dfb7SMatthew G. Knepley + dm     - The DM
474865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
474965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
47502e17dfb7SMatthew G. Knepley 
47512e17dfb7SMatthew G. Knepley   Output Parameter:
47522e17dfb7SMatthew G. Knepley . out - The localized coordinate point
47532e17dfb7SMatthew G. Knepley 
47542e17dfb7SMatthew G. Knepley   Level: developer
47552e17dfb7SMatthew G. Knepley 
47562e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
47572e17dfb7SMatthew G. Knepley @*/
475865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
47592e17dfb7SMatthew G. Knepley {
47602e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
47612e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47622e17dfb7SMatthew G. Knepley 
47632e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47642e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
47652e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47662e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47672e17dfb7SMatthew G. Knepley   } else {
476865da65dcSMatthew G. Knepley     if (endpoint) {
476965da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4770da3333bfSMatthew G. Knepley         if ((PetscAbsReal(PetscRealPart(in[d])/dm->L[d] - PetscFloorReal(PetscRealPart(in[d])/dm->L[d])) < PETSC_SMALL) && (PetscRealPart(in[d])/dm->L[d] > PETSC_SMALL)) {
4771da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
477265da65dcSMatthew G. Knepley         } else {
4773da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
477465da65dcSMatthew G. Knepley         }
477565da65dcSMatthew G. Knepley       }
477665da65dcSMatthew G. Knepley     } else {
47772e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
47781118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
47792e17dfb7SMatthew G. Knepley       }
47802e17dfb7SMatthew G. Knepley     }
478165da65dcSMatthew G. Knepley   }
47822e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47832e17dfb7SMatthew G. Knepley }
47842e17dfb7SMatthew G. Knepley 
47852e17dfb7SMatthew G. Knepley /*
47862e17dfb7SMatthew 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.
47872e17dfb7SMatthew G. Knepley 
47882e17dfb7SMatthew G. Knepley   Input Parameters:
47892e17dfb7SMatthew G. Knepley + dm     - The DM
47902e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
47912e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
47922e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
47932e17dfb7SMatthew G. Knepley 
47942e17dfb7SMatthew G. Knepley   Output Parameter:
47952e17dfb7SMatthew G. Knepley . out - The localized coordinate point
47962e17dfb7SMatthew G. Knepley 
47972e17dfb7SMatthew G. Knepley   Level: developer
47982e17dfb7SMatthew G. Knepley 
47992e17dfb7SMatthew 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
48002e17dfb7SMatthew G. Knepley 
48012e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
48022e17dfb7SMatthew G. Knepley */
48032e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
48042e17dfb7SMatthew G. Knepley {
48052e17dfb7SMatthew G. Knepley   PetscInt d;
48062e17dfb7SMatthew G. Knepley 
48072e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48082e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48092e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
48102e17dfb7SMatthew G. Knepley   } else {
48112e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4812908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
48132e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
48142e17dfb7SMatthew G. Knepley       } else {
48152e17dfb7SMatthew G. Knepley         out[d] = in[d];
48162e17dfb7SMatthew G. Knepley       }
48172e17dfb7SMatthew G. Knepley     }
48182e17dfb7SMatthew G. Knepley   }
48192e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48202e17dfb7SMatthew G. Knepley }
48212e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
48222e17dfb7SMatthew G. Knepley {
48232e17dfb7SMatthew G. Knepley   PetscInt d;
48242e17dfb7SMatthew G. Knepley 
48252e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48262e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48272e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
48282e17dfb7SMatthew G. Knepley   } else {
48292e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4830908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
48312e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
48322e17dfb7SMatthew G. Knepley       } else {
48332e17dfb7SMatthew G. Knepley         out[d] = in[d];
48342e17dfb7SMatthew G. Knepley       }
48352e17dfb7SMatthew G. Knepley     }
48362e17dfb7SMatthew G. Knepley   }
48372e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48382e17dfb7SMatthew G. Knepley }
48392e17dfb7SMatthew G. Knepley 
48402e17dfb7SMatthew G. Knepley /*
48412e17dfb7SMatthew 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.
48422e17dfb7SMatthew G. Knepley 
48432e17dfb7SMatthew G. Knepley   Input Parameters:
48442e17dfb7SMatthew G. Knepley + dm     - The DM
48452e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
48462e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
48472e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
48482e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
48492e17dfb7SMatthew G. Knepley 
48502e17dfb7SMatthew G. Knepley   Output Parameter:
48512e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
48522e17dfb7SMatthew G. Knepley 
48532e17dfb7SMatthew G. Knepley   Level: developer
48542e17dfb7SMatthew G. Knepley 
48552e17dfb7SMatthew 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
48562e17dfb7SMatthew G. Knepley 
48572e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
48582e17dfb7SMatthew G. Knepley */
48592e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
48602e17dfb7SMatthew G. Knepley {
48612e17dfb7SMatthew G. Knepley   PetscInt d;
48622e17dfb7SMatthew G. Knepley 
48632e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48642e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48652e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
48662e17dfb7SMatthew G. Knepley   } else {
48672e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4868908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
48692e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
48702e17dfb7SMatthew G. Knepley       } else {
48712e17dfb7SMatthew G. Knepley         out[d] += in[d];
48722e17dfb7SMatthew G. Knepley       }
48732e17dfb7SMatthew G. Knepley     }
48742e17dfb7SMatthew G. Knepley   }
48752e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48762e17dfb7SMatthew G. Knepley }
48772e17dfb7SMatthew G. Knepley 
487836447a5eSToby Isaac /*@
487936447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
488036447a5eSToby Isaac 
488136447a5eSToby Isaac   Input Parameter:
488236447a5eSToby Isaac . dm - The DM
488336447a5eSToby Isaac 
488436447a5eSToby Isaac   Output Parameter:
488536447a5eSToby Isaac   areLocalized - True if localized
488636447a5eSToby Isaac 
488736447a5eSToby Isaac   Level: developer
488836447a5eSToby Isaac 
488936447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
489036447a5eSToby Isaac @*/
489136447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
489236447a5eSToby Isaac {
489336447a5eSToby Isaac   DM             cdm;
489436447a5eSToby Isaac   PetscSection   coordSection;
489546a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
489646a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
489736447a5eSToby Isaac   PetscErrorCode ierr;
489836447a5eSToby Isaac 
489936447a5eSToby Isaac   PetscFunctionBegin;
490036447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
490146a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
490246a3a80fSLisandro Dalcin 
49038b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
490446a3a80fSLisandro Dalcin   if (!dm->periodic) PetscFunctionReturn(0); /* This is a hideous optimization hack! */
490546a3a80fSLisandro Dalcin 
490636447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
490736447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
490836447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
490946a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
491046a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
491146a3a80fSLisandro Dalcin 
491246a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
491336447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
491436447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
491546a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
491646a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
491736447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
491846a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
491936447a5eSToby Isaac   }
492046a3a80fSLisandro Dalcin   ierr = MPIU_Allreduce(&alreadyLocalized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
492136447a5eSToby Isaac   PetscFunctionReturn(0);
492236447a5eSToby Isaac }
492336447a5eSToby Isaac 
492436447a5eSToby Isaac 
49252e17dfb7SMatthew G. Knepley /*@
4926492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
49272e17dfb7SMatthew G. Knepley 
49282e17dfb7SMatthew G. Knepley   Input Parameter:
49292e17dfb7SMatthew G. Knepley . dm - The DM
49302e17dfb7SMatthew G. Knepley 
49312e17dfb7SMatthew G. Knepley   Level: developer
49322e17dfb7SMatthew G. Knepley 
49332e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
49342e17dfb7SMatthew G. Knepley @*/
49352e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
49362e17dfb7SMatthew G. Knepley {
49372e17dfb7SMatthew G. Knepley   DM             cdm;
49382e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
49392e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
49403e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
49413e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4942e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
49433e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
49443e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
49452e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
49462e17dfb7SMatthew G. Knepley 
49472e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
49482e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
494992c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4950f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4951f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4952f7cbd40bSStefano Zampini 
49532e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
49542e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
49552e17dfb7SMatthew G. Knepley   {
49562e17dfb7SMatthew G. Knepley     PetscBool isplex;
49572e17dfb7SMatthew G. Knepley 
49582e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
49592e17dfb7SMatthew G. Knepley     if (isplex) {
49602e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
49613e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
496269291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
49633e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
49643e922f36SToby Isaac       newStart = vStart;
49653e922f36SToby Isaac       newEnd   = vEnd;
49663e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
49673e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
49683e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
49693e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
49703e922f36SToby Isaac       }
49712e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
49722e17dfb7SMatthew G. Knepley   }
49732e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
497443eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
49752e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
49763e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4977e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
49783e922f36SToby Isaac 
49792e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
49802e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
49812e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
49822e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
49833e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
49843e922f36SToby Isaac 
498569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
49863e922f36SToby Isaac   localized = &anchor[bs];
49873e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
49883e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49893e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49903e922f36SToby Isaac 
49913e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
49923e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
49933e922f36SToby Isaac       PetscInt     b;
49943e922f36SToby Isaac 
49953e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
49963e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49973e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49983e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
49993e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
50003e922f36SToby Isaac         for (b = 0; b < bs; b++) {
50013e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
50023e922f36SToby Isaac         }
50033e922f36SToby Isaac         if (b < bs) break;
50043e922f36SToby Isaac       }
50053e922f36SToby Isaac       if (d < dof/bs) {
50063e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
50073e922f36SToby Isaac           PetscInt cdof;
50083e922f36SToby Isaac 
50093e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
50103e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
50113e922f36SToby Isaac         }
50123e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
50133e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
50143e922f36SToby Isaac       }
50153e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50163e922f36SToby Isaac     }
50173e922f36SToby Isaac   }
50183e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
50193e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
502069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
50213e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
502269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
50233e922f36SToby Isaac     PetscFunctionReturn(0);
50243e922f36SToby Isaac   }
50252e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
50262e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
50272e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
50282e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
50292e17dfb7SMatthew G. Knepley   }
50302e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
50312e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5032c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
50332e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
50342e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
50352e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
50362e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5037c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
50382e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
50392e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
50402e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
50412e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
50422e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
50432e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
50442e17dfb7SMatthew G. Knepley   }
50453e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
50463e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
50473e922f36SToby Isaac 
50482e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
50492e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
50503e922f36SToby Isaac       PetscInt     b, cdof;
50512e17dfb7SMatthew G. Knepley 
50523e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
50533e922f36SToby Isaac       if (!cdof) continue;
50542e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50552e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
50562e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
50572e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
50582e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50592e17dfb7SMatthew G. Knepley     }
50603e922f36SToby Isaac   }
506169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
506269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5063c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
50642e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
50652e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
50662e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
50672e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
50682e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
50692e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50702e17dfb7SMatthew G. Knepley }
50712e17dfb7SMatthew G. Knepley 
5072e87bb0d3SMatthew G Knepley /*@
50733a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5074e87bb0d3SMatthew G Knepley 
50753a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5076e87bb0d3SMatthew G Knepley 
5077e87bb0d3SMatthew G Knepley   Input Parameters:
5078e87bb0d3SMatthew G Knepley + dm - The DM
50793a93e3b7SToby Isaac . v - The Vec of points
508062a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
50813a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5082e87bb0d3SMatthew G Knepley 
508361e3bb9bSMatthew G Knepley   Output Parameter:
508462a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
508562a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
50863a93e3b7SToby Isaac 
5087e87bb0d3SMatthew G Knepley 
5088e87bb0d3SMatthew G Knepley   Level: developer
508961e3bb9bSMatthew G Knepley 
509062a38674SMatthew G. Knepley   Notes:
50913a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
509262a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
50933a93e3b7SToby Isaac 
50943a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
509562a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
50963a93e3b7SToby Isaac 
50973a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
50983a93e3b7SToby Isaac 
509962a38674SMatthew G. Knepley $    const PetscSFNode *cells;
510062a38674SMatthew G. Knepley $    PetscInt           nFound;
5101a6216909SToby Isaac $    const PetscInt    *found;
510262a38674SMatthew G. Knepley $
5103a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
51043a93e3b7SToby Isaac 
51053a93e3b7SToby 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
51063a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
51073a93e3b7SToby Isaac 
510861e3bb9bSMatthew G Knepley .keywords: point location, mesh
510962a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
511061e3bb9bSMatthew G Knepley @*/
511162a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5112e87bb0d3SMatthew G Knepley {
5113735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5114735aa83eSMatthew G Knepley 
5115e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5116e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5117e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5118e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
51193a93e3b7SToby Isaac   if (*cellSF) {
51203a93e3b7SToby Isaac     PetscMPIInt result;
51213a93e3b7SToby Isaac 
5122e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5123a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
51243a93e3b7SToby 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");
5125e0fc9d1bSMatthew G. Knepley   } else {
51263a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
51273a93e3b7SToby Isaac   }
512847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5129735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
513062a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
513182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
513247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5133e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5134e87bb0d3SMatthew G Knepley }
513514f150ffSMatthew G. Knepley 
5136f4d763aaSMatthew G. Knepley /*@
5137f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5138f4d763aaSMatthew G. Knepley 
5139f4d763aaSMatthew G. Knepley   Input Parameter:
5140f4d763aaSMatthew G. Knepley . dm - The original DM
5141f4d763aaSMatthew G. Knepley 
5142f4d763aaSMatthew G. Knepley   Output Parameter:
5143f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5144f4d763aaSMatthew G. Knepley 
5145f4d763aaSMatthew G. Knepley   Level: intermediate
5146f4d763aaSMatthew G. Knepley 
5147e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5148f4d763aaSMatthew G. Knepley @*/
514914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
515014f150ffSMatthew G. Knepley {
5151c26acbdeSMatthew G. Knepley   PetscSection   section;
51522d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
515314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
515414f150ffSMatthew G. Knepley 
515514f150ffSMatthew G. Knepley   PetscFunctionBegin;
515614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
515714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5158e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5159c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5160127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
51612d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5162c26acbdeSMatthew G. Knepley     *odm = dm;
5163c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5164c26acbdeSMatthew G. Knepley   }
516514f150ffSMatthew G. Knepley   if (!dm->dmBC) {
51660305aff6SMatthew G. Knepley     PetscDS      ds;
5167c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
516814f150ffSMatthew G. Knepley     PetscSF      sf;
516914f150ffSMatthew G. Knepley 
517014f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
51710305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
51720305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
517314f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5174e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
517514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
517614f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
517715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5178e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
517914f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
518014f150ffSMatthew G. Knepley   }
518114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
518214f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
518314f150ffSMatthew G. Knepley }
5184f4d763aaSMatthew G. Knepley 
5185f4d763aaSMatthew G. Knepley /*@
5186cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5187f4d763aaSMatthew G. Knepley 
5188f4d763aaSMatthew G. Knepley   Input Parameter:
5189f4d763aaSMatthew G. Knepley . dm - The original DM
5190f4d763aaSMatthew G. Knepley 
5191cdb7a50dSMatthew G. Knepley   Output Parameters:
5192cdb7a50dSMatthew G. Knepley + num - The output sequence number
5193cdb7a50dSMatthew G. Knepley - val - The output sequence value
5194f4d763aaSMatthew G. Knepley 
5195f4d763aaSMatthew G. Knepley   Level: intermediate
5196f4d763aaSMatthew G. Knepley 
5197f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5198f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5199f4d763aaSMatthew G. Knepley 
5200f4d763aaSMatthew G. Knepley .seealso: VecView()
5201f4d763aaSMatthew G. Knepley @*/
5202cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5203f4d763aaSMatthew G. Knepley {
5204f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5205f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5206cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5207cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5208f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5209f4d763aaSMatthew G. Knepley }
5210f4d763aaSMatthew G. Knepley 
5211f4d763aaSMatthew G. Knepley /*@
5212cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5213f4d763aaSMatthew G. Knepley 
5214f4d763aaSMatthew G. Knepley   Input Parameters:
5215f4d763aaSMatthew G. Knepley + dm - The original DM
5216cdb7a50dSMatthew G. Knepley . num - The output sequence number
5217cdb7a50dSMatthew G. Knepley - val - The output sequence value
5218f4d763aaSMatthew G. Knepley 
5219f4d763aaSMatthew G. Knepley   Level: intermediate
5220f4d763aaSMatthew G. Knepley 
5221f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5222f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5223f4d763aaSMatthew G. Knepley 
5224f4d763aaSMatthew G. Knepley .seealso: VecView()
5225f4d763aaSMatthew G. Knepley @*/
5226cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5227f4d763aaSMatthew G. Knepley {
5228f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5229f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5230f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5231cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5232cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5233cdb7a50dSMatthew G. Knepley }
5234cdb7a50dSMatthew G. Knepley 
5235cdb7a50dSMatthew G. Knepley /*@C
5236cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5237cdb7a50dSMatthew G. Knepley 
5238cdb7a50dSMatthew G. Knepley   Input Parameters:
5239cdb7a50dSMatthew G. Knepley + dm   - The original DM
5240cdb7a50dSMatthew G. Knepley . name - The sequence name
5241cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5242cdb7a50dSMatthew G. Knepley 
5243cdb7a50dSMatthew G. Knepley   Output Parameter:
5244cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5245cdb7a50dSMatthew G. Knepley 
5246cdb7a50dSMatthew G. Knepley   Level: intermediate
5247cdb7a50dSMatthew G. Knepley 
5248cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5249cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5250cdb7a50dSMatthew G. Knepley 
5251cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5252cdb7a50dSMatthew G. Knepley @*/
5253cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5254cdb7a50dSMatthew G. Knepley {
5255cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5256cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5257cdb7a50dSMatthew G. Knepley 
5258cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5259cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5260cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5261cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5262cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5263cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5264cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5265cdb7a50dSMatthew G. Knepley     PetscScalar value;
5266cdb7a50dSMatthew G. Knepley 
526739d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
52684aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5269cdb7a50dSMatthew G. Knepley #endif
5270cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5271f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5272f4d763aaSMatthew G. Knepley }
52738e4ac7eaSMatthew G. Knepley 
52748e4ac7eaSMatthew G. Knepley /*@
52758e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
52768e4ac7eaSMatthew G. Knepley 
52778e4ac7eaSMatthew G. Knepley   Not collective
52788e4ac7eaSMatthew G. Knepley 
52798e4ac7eaSMatthew G. Knepley   Input Parameter:
52808e4ac7eaSMatthew G. Knepley . dm - The DM
52818e4ac7eaSMatthew G. Knepley 
52828e4ac7eaSMatthew G. Knepley   Output Parameter:
52838e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
52848e4ac7eaSMatthew G. Knepley 
52858e4ac7eaSMatthew G. Knepley   Level: beginner
52868e4ac7eaSMatthew G. Knepley 
52878e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
52888e4ac7eaSMatthew G. Knepley @*/
52898e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
52908e4ac7eaSMatthew G. Knepley {
52918e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
52928e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52938e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
52948e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
52958e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
52968e4ac7eaSMatthew G. Knepley }
52978e4ac7eaSMatthew G. Knepley 
52988e4ac7eaSMatthew G. Knepley /*@
52998e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
53008e4ac7eaSMatthew G. Knepley 
53018e4ac7eaSMatthew G. Knepley   Collective on dm
53028e4ac7eaSMatthew G. Knepley 
53038e4ac7eaSMatthew G. Knepley   Input Parameters:
53048e4ac7eaSMatthew G. Knepley + dm - The DM
53058e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
53068e4ac7eaSMatthew G. Knepley 
53078e4ac7eaSMatthew G. Knepley   Level: beginner
53088e4ac7eaSMatthew G. Knepley 
53098e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
53108e4ac7eaSMatthew G. Knepley @*/
53118e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
53128e4ac7eaSMatthew G. Knepley {
53138e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
53148e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
53158833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
53168e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
53178e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
53188e4ac7eaSMatthew G. Knepley }
5319c58f1c22SToby Isaac 
5320c58f1c22SToby Isaac 
5321c58f1c22SToby Isaac /*@C
5322c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5323c58f1c22SToby Isaac 
5324c58f1c22SToby Isaac   Not Collective
5325c58f1c22SToby Isaac 
5326c58f1c22SToby Isaac   Input Parameters:
5327c58f1c22SToby Isaac + dm   - The DM object
5328c58f1c22SToby Isaac - name - The label name
5329c58f1c22SToby Isaac 
5330c58f1c22SToby Isaac   Level: intermediate
5331c58f1c22SToby Isaac 
5332c58f1c22SToby Isaac .keywords: mesh
5333c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5334c58f1c22SToby Isaac @*/
5335c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5336c58f1c22SToby Isaac {
5337c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5338c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5339c58f1c22SToby Isaac   PetscErrorCode ierr;
5340c58f1c22SToby Isaac 
5341c58f1c22SToby Isaac   PetscFunctionBegin;
5342c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5343c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5344c58f1c22SToby Isaac   while (next) {
5345c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5346c58f1c22SToby Isaac     if (flg) break;
5347c58f1c22SToby Isaac     next = next->next;
5348c58f1c22SToby Isaac   }
5349c58f1c22SToby Isaac   if (!flg) {
5350c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5351c58f1c22SToby Isaac 
5352c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5353c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5354c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5355c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5356c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5357c58f1c22SToby Isaac   }
5358c58f1c22SToby Isaac   PetscFunctionReturn(0);
5359c58f1c22SToby Isaac }
5360c58f1c22SToby Isaac 
5361c58f1c22SToby Isaac /*@C
5362c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5363c58f1c22SToby Isaac 
5364c58f1c22SToby Isaac   Not Collective
5365c58f1c22SToby Isaac 
5366c58f1c22SToby Isaac   Input Parameters:
5367c58f1c22SToby Isaac + dm   - The DM object
5368c58f1c22SToby Isaac . name - The label name
5369c58f1c22SToby Isaac - point - The mesh point
5370c58f1c22SToby Isaac 
5371c58f1c22SToby Isaac   Output Parameter:
5372c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5373c58f1c22SToby Isaac 
5374c58f1c22SToby Isaac   Level: beginner
5375c58f1c22SToby Isaac 
5376c58f1c22SToby Isaac .keywords: mesh
5377c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5378c58f1c22SToby Isaac @*/
5379c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5380c58f1c22SToby Isaac {
5381c58f1c22SToby Isaac   DMLabel        label;
5382c58f1c22SToby Isaac   PetscErrorCode ierr;
5383c58f1c22SToby Isaac 
5384c58f1c22SToby Isaac   PetscFunctionBegin;
5385c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5386c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5387c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
538813903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5389c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5390c58f1c22SToby Isaac   PetscFunctionReturn(0);
5391c58f1c22SToby Isaac }
5392c58f1c22SToby Isaac 
5393c58f1c22SToby Isaac /*@C
5394c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5395c58f1c22SToby Isaac 
5396c58f1c22SToby Isaac   Not Collective
5397c58f1c22SToby Isaac 
5398c58f1c22SToby Isaac   Input Parameters:
5399c58f1c22SToby Isaac + dm   - The DM object
5400c58f1c22SToby Isaac . name - The label name
5401c58f1c22SToby Isaac . point - The mesh point
5402c58f1c22SToby Isaac - value - The label value for this point
5403c58f1c22SToby Isaac 
5404c58f1c22SToby Isaac   Output Parameter:
5405c58f1c22SToby Isaac 
5406c58f1c22SToby Isaac   Level: beginner
5407c58f1c22SToby Isaac 
5408c58f1c22SToby Isaac .keywords: mesh
5409c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5410c58f1c22SToby Isaac @*/
5411c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5412c58f1c22SToby Isaac {
5413c58f1c22SToby Isaac   DMLabel        label;
5414c58f1c22SToby Isaac   PetscErrorCode ierr;
5415c58f1c22SToby Isaac 
5416c58f1c22SToby Isaac   PetscFunctionBegin;
5417c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5418c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5419c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5420c58f1c22SToby Isaac   if (!label) {
5421c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5422c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5423c58f1c22SToby Isaac   }
5424c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5425c58f1c22SToby Isaac   PetscFunctionReturn(0);
5426c58f1c22SToby Isaac }
5427c58f1c22SToby Isaac 
5428c58f1c22SToby Isaac /*@C
5429c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5430c58f1c22SToby Isaac 
5431c58f1c22SToby Isaac   Not Collective
5432c58f1c22SToby Isaac 
5433c58f1c22SToby Isaac   Input Parameters:
5434c58f1c22SToby Isaac + dm   - The DM object
5435c58f1c22SToby Isaac . name - The label name
5436c58f1c22SToby Isaac . point - The mesh point
5437c58f1c22SToby Isaac - value - The label value for this point
5438c58f1c22SToby Isaac 
5439c58f1c22SToby Isaac   Output Parameter:
5440c58f1c22SToby Isaac 
5441c58f1c22SToby Isaac   Level: beginner
5442c58f1c22SToby Isaac 
5443c58f1c22SToby Isaac .keywords: mesh
5444c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5445c58f1c22SToby Isaac @*/
5446c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5447c58f1c22SToby Isaac {
5448c58f1c22SToby Isaac   DMLabel        label;
5449c58f1c22SToby Isaac   PetscErrorCode ierr;
5450c58f1c22SToby Isaac 
5451c58f1c22SToby Isaac   PetscFunctionBegin;
5452c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5453c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5454c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5455c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5456c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5457c58f1c22SToby Isaac   PetscFunctionReturn(0);
5458c58f1c22SToby Isaac }
5459c58f1c22SToby Isaac 
5460c58f1c22SToby Isaac /*@C
5461c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5462c58f1c22SToby Isaac 
5463c58f1c22SToby Isaac   Not Collective
5464c58f1c22SToby Isaac 
5465c58f1c22SToby Isaac   Input Parameters:
5466c58f1c22SToby Isaac + dm   - The DM object
5467c58f1c22SToby Isaac - name - The label name
5468c58f1c22SToby Isaac 
5469c58f1c22SToby Isaac   Output Parameter:
5470c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5471c58f1c22SToby Isaac 
5472c58f1c22SToby Isaac   Level: beginner
5473c58f1c22SToby Isaac 
5474c58f1c22SToby Isaac .keywords: mesh
5475c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5476c58f1c22SToby Isaac @*/
5477c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5478c58f1c22SToby Isaac {
5479c58f1c22SToby Isaac   DMLabel        label;
5480c58f1c22SToby Isaac   PetscErrorCode ierr;
5481c58f1c22SToby Isaac 
5482c58f1c22SToby Isaac   PetscFunctionBegin;
5483c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5484c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5485c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5486c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5487c58f1c22SToby Isaac   *size = 0;
5488c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5489c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5490c58f1c22SToby Isaac   PetscFunctionReturn(0);
5491c58f1c22SToby Isaac }
5492c58f1c22SToby Isaac 
5493c58f1c22SToby Isaac /*@C
5494c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5495c58f1c22SToby Isaac 
5496c58f1c22SToby Isaac   Not Collective
5497c58f1c22SToby Isaac 
5498c58f1c22SToby Isaac   Input Parameters:
5499c58f1c22SToby Isaac + mesh - The DM object
5500c58f1c22SToby Isaac - name - The label name
5501c58f1c22SToby Isaac 
5502c58f1c22SToby Isaac   Output Parameter:
5503c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5504c58f1c22SToby Isaac 
5505c58f1c22SToby Isaac   Level: beginner
5506c58f1c22SToby Isaac 
5507c58f1c22SToby Isaac .keywords: mesh
5508c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5509c58f1c22SToby Isaac @*/
5510c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5511c58f1c22SToby Isaac {
5512c58f1c22SToby Isaac   DMLabel        label;
5513c58f1c22SToby Isaac   PetscErrorCode ierr;
5514c58f1c22SToby Isaac 
5515c58f1c22SToby Isaac   PetscFunctionBegin;
5516c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5517c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5518c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5519c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5520c58f1c22SToby Isaac   *ids = NULL;
5521dab2e251SBlaise Bourdin  if (label) {
5522c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5523dab2e251SBlaise Bourdin   } else {
5524dab2e251SBlaise Bourdin     /* returning an empty IS */
5525dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5526dab2e251SBlaise Bourdin   }
5527c58f1c22SToby Isaac   PetscFunctionReturn(0);
5528c58f1c22SToby Isaac }
5529c58f1c22SToby Isaac 
5530c58f1c22SToby Isaac /*@C
5531c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5532c58f1c22SToby Isaac 
5533c58f1c22SToby Isaac   Not Collective
5534c58f1c22SToby Isaac 
5535c58f1c22SToby Isaac   Input Parameters:
5536c58f1c22SToby Isaac + dm - The DM object
5537c58f1c22SToby Isaac . name - The label name
5538c58f1c22SToby Isaac - value - The stratum value
5539c58f1c22SToby Isaac 
5540c58f1c22SToby Isaac   Output Parameter:
5541c58f1c22SToby Isaac . size - The stratum size
5542c58f1c22SToby Isaac 
5543c58f1c22SToby Isaac   Level: beginner
5544c58f1c22SToby Isaac 
5545c58f1c22SToby Isaac .keywords: mesh
5546c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5547c58f1c22SToby Isaac @*/
5548c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5549c58f1c22SToby Isaac {
5550c58f1c22SToby Isaac   DMLabel        label;
5551c58f1c22SToby Isaac   PetscErrorCode ierr;
5552c58f1c22SToby Isaac 
5553c58f1c22SToby Isaac   PetscFunctionBegin;
5554c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5555c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5556c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5557c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5558c58f1c22SToby Isaac   *size = 0;
5559c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5560c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5561c58f1c22SToby Isaac   PetscFunctionReturn(0);
5562c58f1c22SToby Isaac }
5563c58f1c22SToby Isaac 
5564c58f1c22SToby Isaac /*@C
5565c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5566c58f1c22SToby Isaac 
5567c58f1c22SToby Isaac   Not Collective
5568c58f1c22SToby Isaac 
5569c58f1c22SToby Isaac   Input Parameters:
5570c58f1c22SToby Isaac + dm - The DM object
5571c58f1c22SToby Isaac . name - The label name
5572c58f1c22SToby Isaac - value - The stratum value
5573c58f1c22SToby Isaac 
5574c58f1c22SToby Isaac   Output Parameter:
5575c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5576c58f1c22SToby Isaac 
5577c58f1c22SToby Isaac   Level: beginner
5578c58f1c22SToby Isaac 
5579c58f1c22SToby Isaac .keywords: mesh
5580c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5581c58f1c22SToby Isaac @*/
5582c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5583c58f1c22SToby Isaac {
5584c58f1c22SToby Isaac   DMLabel        label;
5585c58f1c22SToby Isaac   PetscErrorCode ierr;
5586c58f1c22SToby Isaac 
5587c58f1c22SToby Isaac   PetscFunctionBegin;
5588c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5589c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5590c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5591c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5592c58f1c22SToby Isaac   *points = NULL;
5593c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5594c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5595c58f1c22SToby Isaac   PetscFunctionReturn(0);
5596c58f1c22SToby Isaac }
5597c58f1c22SToby Isaac 
55984de306b1SToby Isaac /*@C
55999044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
56004de306b1SToby Isaac 
56014de306b1SToby Isaac   Not Collective
56024de306b1SToby Isaac 
56034de306b1SToby Isaac   Input Parameters:
56044de306b1SToby Isaac + dm - The DM object
56054de306b1SToby Isaac . name - The label name
56064de306b1SToby Isaac . value - The stratum value
56074de306b1SToby Isaac - points - The stratum points
56084de306b1SToby Isaac 
56094de306b1SToby Isaac   Level: beginner
56104de306b1SToby Isaac 
56114de306b1SToby Isaac .keywords: mesh
56124de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
56134de306b1SToby Isaac @*/
56144de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
56154de306b1SToby Isaac {
56164de306b1SToby Isaac   DMLabel        label;
56174de306b1SToby Isaac   PetscErrorCode ierr;
56184de306b1SToby Isaac 
56194de306b1SToby Isaac   PetscFunctionBegin;
56204de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56214de306b1SToby Isaac   PetscValidCharPointer(name, 2);
56224de306b1SToby Isaac   PetscValidPointer(points, 4);
56234de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
56244de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
56254de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
56264de306b1SToby Isaac   PetscFunctionReturn(0);
56274de306b1SToby Isaac }
56284de306b1SToby Isaac 
5629c58f1c22SToby Isaac /*@C
5630c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5631c58f1c22SToby Isaac 
5632c58f1c22SToby Isaac   Not Collective
5633c58f1c22SToby Isaac 
5634c58f1c22SToby Isaac   Input Parameters:
5635c58f1c22SToby Isaac + dm   - The DM object
5636c58f1c22SToby Isaac . name - The label name
5637c58f1c22SToby Isaac - value - The label value for this point
5638c58f1c22SToby Isaac 
5639c58f1c22SToby Isaac   Output Parameter:
5640c58f1c22SToby Isaac 
5641c58f1c22SToby Isaac   Level: beginner
5642c58f1c22SToby Isaac 
5643c58f1c22SToby Isaac .keywords: mesh
5644c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5645c58f1c22SToby Isaac @*/
5646c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5647c58f1c22SToby Isaac {
5648c58f1c22SToby Isaac   DMLabel        label;
5649c58f1c22SToby Isaac   PetscErrorCode ierr;
5650c58f1c22SToby Isaac 
5651c58f1c22SToby Isaac   PetscFunctionBegin;
5652c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5653c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5654c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5655c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5656c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5657c58f1c22SToby Isaac   PetscFunctionReturn(0);
5658c58f1c22SToby Isaac }
5659c58f1c22SToby Isaac 
5660c58f1c22SToby Isaac /*@
5661c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5662c58f1c22SToby Isaac 
5663c58f1c22SToby Isaac   Not Collective
5664c58f1c22SToby Isaac 
5665c58f1c22SToby Isaac   Input Parameter:
5666c58f1c22SToby Isaac . dm   - The DM object
5667c58f1c22SToby Isaac 
5668c58f1c22SToby Isaac   Output Parameter:
5669c58f1c22SToby Isaac . numLabels - the number of Labels
5670c58f1c22SToby Isaac 
5671c58f1c22SToby Isaac   Level: intermediate
5672c58f1c22SToby Isaac 
5673c58f1c22SToby Isaac .keywords: mesh
5674c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5675c58f1c22SToby Isaac @*/
5676c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5677c58f1c22SToby Isaac {
5678c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5679c58f1c22SToby Isaac   PetscInt  n    = 0;
5680c58f1c22SToby Isaac 
5681c58f1c22SToby Isaac   PetscFunctionBegin;
5682c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5683c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5684c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5685c58f1c22SToby Isaac   *numLabels = n;
5686c58f1c22SToby Isaac   PetscFunctionReturn(0);
5687c58f1c22SToby Isaac }
5688c58f1c22SToby Isaac 
5689c58f1c22SToby Isaac /*@C
5690c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5691c58f1c22SToby Isaac 
5692c58f1c22SToby Isaac   Not Collective
5693c58f1c22SToby Isaac 
5694c58f1c22SToby Isaac   Input Parameters:
5695c58f1c22SToby Isaac + dm - The DM object
5696c58f1c22SToby Isaac - n  - the label number
5697c58f1c22SToby Isaac 
5698c58f1c22SToby Isaac   Output Parameter:
5699c58f1c22SToby Isaac . name - the label name
5700c58f1c22SToby Isaac 
5701c58f1c22SToby Isaac   Level: intermediate
5702c58f1c22SToby Isaac 
5703c58f1c22SToby Isaac .keywords: mesh
5704c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5705c58f1c22SToby Isaac @*/
5706c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5707c58f1c22SToby Isaac {
5708c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5709c58f1c22SToby Isaac   PetscInt  l    = 0;
5710c58f1c22SToby Isaac 
5711c58f1c22SToby Isaac   PetscFunctionBegin;
5712c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5713c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5714c58f1c22SToby Isaac   while (next) {
5715c58f1c22SToby Isaac     if (l == n) {
5716c58f1c22SToby Isaac       *name = next->label->name;
5717c58f1c22SToby Isaac       PetscFunctionReturn(0);
5718c58f1c22SToby Isaac     }
5719c58f1c22SToby Isaac     ++l;
5720c58f1c22SToby Isaac     next = next->next;
5721c58f1c22SToby Isaac   }
5722c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5723c58f1c22SToby Isaac }
5724c58f1c22SToby Isaac 
5725c58f1c22SToby Isaac /*@C
5726c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5727c58f1c22SToby Isaac 
5728c58f1c22SToby Isaac   Not Collective
5729c58f1c22SToby Isaac 
5730c58f1c22SToby Isaac   Input Parameters:
5731c58f1c22SToby Isaac + dm   - The DM object
5732c58f1c22SToby Isaac - name - The label name
5733c58f1c22SToby Isaac 
5734c58f1c22SToby Isaac   Output Parameter:
5735c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac   Level: intermediate
5738c58f1c22SToby Isaac 
5739c58f1c22SToby Isaac .keywords: mesh
5740c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5741c58f1c22SToby Isaac @*/
5742c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5743c58f1c22SToby Isaac {
5744c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5745c58f1c22SToby Isaac   PetscErrorCode ierr;
5746c58f1c22SToby Isaac 
5747c58f1c22SToby Isaac   PetscFunctionBegin;
5748c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5749c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5750c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5751c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5752c58f1c22SToby Isaac   while (next) {
5753c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5754c58f1c22SToby Isaac     if (*hasLabel) break;
5755c58f1c22SToby Isaac     next = next->next;
5756c58f1c22SToby Isaac   }
5757c58f1c22SToby Isaac   PetscFunctionReturn(0);
5758c58f1c22SToby Isaac }
5759c58f1c22SToby Isaac 
5760c58f1c22SToby Isaac /*@C
5761c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5762c58f1c22SToby Isaac 
5763c58f1c22SToby Isaac   Not Collective
5764c58f1c22SToby Isaac 
5765c58f1c22SToby Isaac   Input Parameters:
5766c58f1c22SToby Isaac + dm   - The DM object
5767c58f1c22SToby Isaac - name - The label name
5768c58f1c22SToby Isaac 
5769c58f1c22SToby Isaac   Output Parameter:
5770c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5771c58f1c22SToby Isaac 
5772c58f1c22SToby Isaac   Level: intermediate
5773c58f1c22SToby Isaac 
5774c58f1c22SToby Isaac .keywords: mesh
5775c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5776c58f1c22SToby Isaac @*/
5777c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5778c58f1c22SToby Isaac {
5779c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5780c58f1c22SToby Isaac   PetscBool      hasLabel;
5781c58f1c22SToby Isaac   PetscErrorCode ierr;
5782c58f1c22SToby Isaac 
5783c58f1c22SToby Isaac   PetscFunctionBegin;
5784c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5785c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5786c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5787c58f1c22SToby Isaac   *label = NULL;
5788c58f1c22SToby Isaac   while (next) {
5789c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5790c58f1c22SToby Isaac     if (hasLabel) {
5791c58f1c22SToby Isaac       *label = next->label;
5792c58f1c22SToby Isaac       break;
5793c58f1c22SToby Isaac     }
5794c58f1c22SToby Isaac     next = next->next;
5795c58f1c22SToby Isaac   }
5796c58f1c22SToby Isaac   PetscFunctionReturn(0);
5797c58f1c22SToby Isaac }
5798c58f1c22SToby Isaac 
5799c58f1c22SToby Isaac /*@C
5800c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5801c58f1c22SToby Isaac 
5802c58f1c22SToby Isaac   Not Collective
5803c58f1c22SToby Isaac 
5804c58f1c22SToby Isaac   Input Parameters:
5805c58f1c22SToby Isaac + dm - The DM object
5806c58f1c22SToby Isaac - n  - the label number
5807c58f1c22SToby Isaac 
5808c58f1c22SToby Isaac   Output Parameter:
5809c58f1c22SToby Isaac . label - the label
5810c58f1c22SToby Isaac 
5811c58f1c22SToby Isaac   Level: intermediate
5812c58f1c22SToby Isaac 
5813c58f1c22SToby Isaac .keywords: mesh
5814c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5815c58f1c22SToby Isaac @*/
5816c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5817c58f1c22SToby Isaac {
5818c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5819c58f1c22SToby Isaac   PetscInt    l    = 0;
5820c58f1c22SToby Isaac 
5821c58f1c22SToby Isaac   PetscFunctionBegin;
5822c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5823c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5824c58f1c22SToby Isaac   while (next) {
5825c58f1c22SToby Isaac     if (l == n) {
5826c58f1c22SToby Isaac       *label = next->label;
5827c58f1c22SToby Isaac       PetscFunctionReturn(0);
5828c58f1c22SToby Isaac     }
5829c58f1c22SToby Isaac     ++l;
5830c58f1c22SToby Isaac     next = next->next;
5831c58f1c22SToby Isaac   }
5832c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5833c58f1c22SToby Isaac }
5834c58f1c22SToby Isaac 
5835c58f1c22SToby Isaac /*@C
5836c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5837c58f1c22SToby Isaac 
5838c58f1c22SToby Isaac   Not Collective
5839c58f1c22SToby Isaac 
5840c58f1c22SToby Isaac   Input Parameters:
5841c58f1c22SToby Isaac + dm   - The DM object
5842c58f1c22SToby Isaac - label - The DMLabel
5843c58f1c22SToby Isaac 
5844c58f1c22SToby Isaac   Level: developer
5845c58f1c22SToby Isaac 
5846c58f1c22SToby Isaac .keywords: mesh
5847c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5848c58f1c22SToby Isaac @*/
5849c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5850c58f1c22SToby Isaac {
5851c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5852c58f1c22SToby Isaac   PetscBool      hasLabel;
5853c58f1c22SToby Isaac   PetscErrorCode ierr;
5854c58f1c22SToby Isaac 
5855c58f1c22SToby Isaac   PetscFunctionBegin;
5856c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5857c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5858c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5859c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5860c58f1c22SToby Isaac   tmpLabel->label  = label;
5861c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5862c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5863c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5864c58f1c22SToby Isaac   PetscFunctionReturn(0);
5865c58f1c22SToby Isaac }
5866c58f1c22SToby Isaac 
5867c58f1c22SToby Isaac /*@C
5868c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5869c58f1c22SToby Isaac 
5870c58f1c22SToby Isaac   Not Collective
5871c58f1c22SToby Isaac 
5872c58f1c22SToby Isaac   Input Parameters:
5873c58f1c22SToby Isaac + dm   - The DM object
5874c58f1c22SToby Isaac - name - The label name
5875c58f1c22SToby Isaac 
5876c58f1c22SToby Isaac   Output Parameter:
5877c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5878c58f1c22SToby Isaac 
5879c58f1c22SToby Isaac   Level: developer
5880c58f1c22SToby Isaac 
5881c58f1c22SToby Isaac .keywords: mesh
5882c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5883c58f1c22SToby Isaac @*/
5884c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5885c58f1c22SToby Isaac {
5886c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5887c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5888c58f1c22SToby Isaac   PetscBool      hasLabel;
5889c58f1c22SToby Isaac   PetscErrorCode ierr;
5890c58f1c22SToby Isaac 
5891c58f1c22SToby Isaac   PetscFunctionBegin;
5892c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5893c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5894c58f1c22SToby Isaac   *label = NULL;
5895c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5896c58f1c22SToby Isaac   while (next) {
5897c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5898c58f1c22SToby Isaac     if (hasLabel) {
5899c58f1c22SToby Isaac       if (last) last->next       = next->next;
5900c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5901c58f1c22SToby Isaac       next->next = NULL;
5902c58f1c22SToby Isaac       *label     = next->label;
5903c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5904c58f1c22SToby Isaac       if (hasLabel) {
5905c58f1c22SToby Isaac         dm->depthLabel = NULL;
5906c58f1c22SToby Isaac       }
5907c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5908c58f1c22SToby Isaac       break;
5909c58f1c22SToby Isaac     }
5910c58f1c22SToby Isaac     last = next;
5911c58f1c22SToby Isaac     next = next->next;
5912c58f1c22SToby Isaac   }
5913c58f1c22SToby Isaac   PetscFunctionReturn(0);
5914c58f1c22SToby Isaac }
5915c58f1c22SToby Isaac 
5916c58f1c22SToby Isaac /*@C
5917c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5918c58f1c22SToby Isaac 
5919c58f1c22SToby Isaac   Not Collective
5920c58f1c22SToby Isaac 
5921c58f1c22SToby Isaac   Input Parameters:
5922c58f1c22SToby Isaac + dm   - The DM object
5923c58f1c22SToby Isaac - name - The label name
5924c58f1c22SToby Isaac 
5925c58f1c22SToby Isaac   Output Parameter:
5926c58f1c22SToby Isaac . output - The flag for output
5927c58f1c22SToby Isaac 
5928c58f1c22SToby Isaac   Level: developer
5929c58f1c22SToby Isaac 
5930c58f1c22SToby Isaac .keywords: mesh
5931c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5932c58f1c22SToby Isaac @*/
5933c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5934c58f1c22SToby Isaac {
5935c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5936c58f1c22SToby Isaac   PetscErrorCode ierr;
5937c58f1c22SToby Isaac 
5938c58f1c22SToby Isaac   PetscFunctionBegin;
5939c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5940c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5941c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5942c58f1c22SToby Isaac   while (next) {
5943c58f1c22SToby Isaac     PetscBool flg;
5944c58f1c22SToby Isaac 
5945c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5946c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5947c58f1c22SToby Isaac     next = next->next;
5948c58f1c22SToby Isaac   }
5949c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5950c58f1c22SToby Isaac }
5951c58f1c22SToby Isaac 
5952c58f1c22SToby Isaac /*@C
5953c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5954c58f1c22SToby Isaac 
5955c58f1c22SToby Isaac   Not Collective
5956c58f1c22SToby Isaac 
5957c58f1c22SToby Isaac   Input Parameters:
5958c58f1c22SToby Isaac + dm     - The DM object
5959c58f1c22SToby Isaac . name   - The label name
5960c58f1c22SToby Isaac - output - The flag for output
5961c58f1c22SToby Isaac 
5962c58f1c22SToby Isaac   Level: developer
5963c58f1c22SToby Isaac 
5964c58f1c22SToby Isaac .keywords: mesh
5965c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5966c58f1c22SToby Isaac @*/
5967c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5968c58f1c22SToby Isaac {
5969c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5970c58f1c22SToby Isaac   PetscErrorCode ierr;
5971c58f1c22SToby Isaac 
5972c58f1c22SToby Isaac   PetscFunctionBegin;
5973c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5974c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5975c58f1c22SToby Isaac   while (next) {
5976c58f1c22SToby Isaac     PetscBool flg;
5977c58f1c22SToby Isaac 
5978c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5979c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5980c58f1c22SToby Isaac     next = next->next;
5981c58f1c22SToby Isaac   }
5982c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5983c58f1c22SToby Isaac }
5984c58f1c22SToby Isaac 
5985c58f1c22SToby Isaac 
5986c58f1c22SToby Isaac /*@
5987c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5988c58f1c22SToby Isaac 
5989c58f1c22SToby Isaac   Collective on DM
5990c58f1c22SToby Isaac 
5991c58f1c22SToby Isaac   Input Parameter:
59922e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5993c58f1c22SToby Isaac 
5994c58f1c22SToby Isaac   Output Parameter:
59952e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5996c58f1c22SToby Isaac 
5997c58f1c22SToby Isaac   Level: intermediate
5998c58f1c22SToby Isaac 
5999c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
6000c58f1c22SToby Isaac 
6001c58f1c22SToby Isaac .keywords: mesh
6002c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6003c58f1c22SToby Isaac @*/
6004c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6005c58f1c22SToby Isaac {
6006c58f1c22SToby Isaac   PetscInt       numLabels, l;
6007c58f1c22SToby Isaac   PetscErrorCode ierr;
6008c58f1c22SToby Isaac 
6009c58f1c22SToby Isaac   PetscFunctionBegin;
6010c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
6011c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6012c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
6013c58f1c22SToby Isaac     DMLabel     label, labelNew;
6014c58f1c22SToby Isaac     const char *name;
6015c58f1c22SToby Isaac     PetscBool   flg;
6016c58f1c22SToby Isaac 
6017c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6018c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6019c58f1c22SToby Isaac     if (flg) continue;
60207d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
60217d5acc75SStefano Zampini     if (flg) continue;
6022c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6023c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6024c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6025c58f1c22SToby Isaac   }
6026c58f1c22SToby Isaac   PetscFunctionReturn(0);
6027c58f1c22SToby Isaac }
6028a8fb8f29SToby Isaac 
6029a8fb8f29SToby Isaac /*@
6030a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6031a8fb8f29SToby Isaac 
6032a8fb8f29SToby Isaac   Input Parameter:
6033a8fb8f29SToby Isaac . dm - The DM object
6034a8fb8f29SToby Isaac 
6035a8fb8f29SToby Isaac   Output Parameter:
6036a8fb8f29SToby Isaac . cdm - The coarse DM
6037a8fb8f29SToby Isaac 
6038a8fb8f29SToby Isaac   Level: intermediate
6039a8fb8f29SToby Isaac 
6040a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6041a8fb8f29SToby Isaac @*/
6042a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6043a8fb8f29SToby Isaac {
6044a8fb8f29SToby Isaac   PetscFunctionBegin;
6045a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6046a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6047a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6048a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6049a8fb8f29SToby Isaac }
6050a8fb8f29SToby Isaac 
6051a8fb8f29SToby Isaac /*@
6052a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6053a8fb8f29SToby Isaac 
6054a8fb8f29SToby Isaac   Input Parameters:
6055a8fb8f29SToby Isaac + dm - The DM object
6056a8fb8f29SToby Isaac - cdm - The coarse DM
6057a8fb8f29SToby Isaac 
6058a8fb8f29SToby Isaac   Level: intermediate
6059a8fb8f29SToby Isaac 
6060a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6061a8fb8f29SToby Isaac @*/
6062a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6063a8fb8f29SToby Isaac {
6064a8fb8f29SToby Isaac   PetscErrorCode ierr;
6065a8fb8f29SToby Isaac 
6066a8fb8f29SToby Isaac   PetscFunctionBegin;
6067a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6068a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6069a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6070a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6071a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6072a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6073a8fb8f29SToby Isaac }
6074a8fb8f29SToby Isaac 
607588bdff64SToby Isaac /*@
607688bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
607788bdff64SToby Isaac 
607888bdff64SToby Isaac   Input Parameter:
607988bdff64SToby Isaac . dm - The DM object
608088bdff64SToby Isaac 
608188bdff64SToby Isaac   Output Parameter:
608288bdff64SToby Isaac . fdm - The fine DM
608388bdff64SToby Isaac 
608488bdff64SToby Isaac   Level: intermediate
608588bdff64SToby Isaac 
608688bdff64SToby Isaac .seealso: DMSetFineDM()
608788bdff64SToby Isaac @*/
608888bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
608988bdff64SToby Isaac {
609088bdff64SToby Isaac   PetscFunctionBegin;
609188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
609288bdff64SToby Isaac   PetscValidPointer(fdm, 2);
609388bdff64SToby Isaac   *fdm = dm->fineMesh;
609488bdff64SToby Isaac   PetscFunctionReturn(0);
609588bdff64SToby Isaac }
609688bdff64SToby Isaac 
609788bdff64SToby Isaac /*@
609888bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
609988bdff64SToby Isaac 
610088bdff64SToby Isaac   Input Parameters:
610188bdff64SToby Isaac + dm - The DM object
610288bdff64SToby Isaac - fdm - The fine DM
610388bdff64SToby Isaac 
610488bdff64SToby Isaac   Level: intermediate
610588bdff64SToby Isaac 
610688bdff64SToby Isaac .seealso: DMGetFineDM()
610788bdff64SToby Isaac @*/
610888bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
610988bdff64SToby Isaac {
611088bdff64SToby Isaac   PetscErrorCode ierr;
611188bdff64SToby Isaac 
611288bdff64SToby Isaac   PetscFunctionBegin;
611388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
611488bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
611588bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
611688bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
611788bdff64SToby Isaac   dm->fineMesh = fdm;
611888bdff64SToby Isaac   PetscFunctionReturn(0);
611988bdff64SToby Isaac }
612088bdff64SToby Isaac 
6121a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6122a6ba4734SToby Isaac 
6123a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6124a6ba4734SToby Isaac {
6125a6ba4734SToby Isaac   PetscErrorCode ierr;
6126a6ba4734SToby Isaac 
6127a6ba4734SToby Isaac   PetscFunctionBegin;
6128e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6129a6ba4734SToby Isaac   PetscFunctionReturn(0);
6130a6ba4734SToby Isaac }
6131a6ba4734SToby Isaac 
6132a6ba4734SToby Isaac /*@C
6133a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6134a6ba4734SToby Isaac 
6135a6ba4734SToby Isaac   Input Parameters:
61364c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6137f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6138a6ba4734SToby Isaac . name        - The BC name
6139a6ba4734SToby Isaac . labelname   - The label defining constrained points
6140a6ba4734SToby Isaac . field       - The field to constrain
6141a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6142a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6143a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6144a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6145a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6146a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6147a6ba4734SToby Isaac 
6148a6ba4734SToby Isaac   Options Database Keys:
6149a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6150a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6151a6ba4734SToby Isaac 
6152a6ba4734SToby Isaac   Level: developer
6153a6ba4734SToby Isaac 
6154a6ba4734SToby Isaac .seealso: DMGetBoundary()
6155a6ba4734SToby Isaac @*/
6156a30ec4eaSSatish Balay PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), PetscInt numids, const PetscInt *ids, void *ctx)
6157a6ba4734SToby Isaac {
6158a6ba4734SToby Isaac   PetscErrorCode ierr;
6159a6ba4734SToby Isaac 
6160a6ba4734SToby Isaac   PetscFunctionBegin;
6161a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6162f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6163a6ba4734SToby Isaac   PetscFunctionReturn(0);
6164a6ba4734SToby Isaac }
6165a6ba4734SToby Isaac 
6166a6ba4734SToby Isaac /*@
6167a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6168a6ba4734SToby Isaac 
6169a6ba4734SToby Isaac   Input Parameters:
6170a6ba4734SToby Isaac . dm - The mesh object
6171a6ba4734SToby Isaac 
6172a6ba4734SToby Isaac   Output Parameters:
6173a6ba4734SToby Isaac . numBd - The number of BC
6174a6ba4734SToby Isaac 
6175a6ba4734SToby Isaac   Level: intermediate
6176a6ba4734SToby Isaac 
6177a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6178a6ba4734SToby Isaac @*/
6179a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6180a6ba4734SToby Isaac {
618158ebd649SToby Isaac   PetscErrorCode ierr;
6182a6ba4734SToby Isaac 
6183a6ba4734SToby Isaac   PetscFunctionBegin;
6184a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
618558ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6186a6ba4734SToby Isaac   PetscFunctionReturn(0);
6187a6ba4734SToby Isaac }
6188a6ba4734SToby Isaac 
6189a6ba4734SToby Isaac /*@C
61901c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6191a6ba4734SToby Isaac 
6192a6ba4734SToby Isaac   Input Parameters:
6193a6ba4734SToby Isaac + dm          - The mesh object
6194a6ba4734SToby Isaac - bd          - The BC number
6195a6ba4734SToby Isaac 
6196a6ba4734SToby Isaac   Output Parameters:
6197f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6198a6ba4734SToby Isaac . name        - The BC name
6199a6ba4734SToby Isaac . labelname   - The label defining constrained points
6200a6ba4734SToby Isaac . field       - The field to constrain
6201a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6202a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6203a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6204a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6205a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6206a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6207a6ba4734SToby Isaac 
6208a6ba4734SToby Isaac   Options Database Keys:
6209a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6210a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6211a6ba4734SToby Isaac 
6212a6ba4734SToby Isaac   Level: developer
6213a6ba4734SToby Isaac 
6214a6ba4734SToby Isaac .seealso: DMAddBoundary()
6215a6ba4734SToby Isaac @*/
6216a30ec4eaSSatish Balay PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
6217a6ba4734SToby Isaac {
621858ebd649SToby Isaac   PetscErrorCode ierr;
6219a6ba4734SToby Isaac 
6220a6ba4734SToby Isaac   PetscFunctionBegin;
6221a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6222f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6223a6ba4734SToby Isaac   PetscFunctionReturn(0);
6224a6ba4734SToby Isaac }
6225a6ba4734SToby Isaac 
6226e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6227e6f8dbb6SToby Isaac {
6228dff059c6SToby Isaac   DMBoundary *lastnext;
6229e6f8dbb6SToby Isaac   DSBoundary dsbound;
6230e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6231e6f8dbb6SToby Isaac 
6232e6f8dbb6SToby Isaac   PetscFunctionBegin;
6233e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
623447a1f5adSToby Isaac   if (dm->boundary) {
623547a1f5adSToby Isaac     DMBoundary next = dm->boundary;
623647a1f5adSToby Isaac 
623747a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
623847a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
623947a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
624047a1f5adSToby Isaac     while (next) {
624147a1f5adSToby Isaac       DMBoundary b = next;
624247a1f5adSToby Isaac 
624347a1f5adSToby Isaac       next = b->next;
624447a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6245a6ba4734SToby Isaac     }
624647a1f5adSToby Isaac     dm->boundary = NULL;
6247a6ba4734SToby Isaac   }
624847a1f5adSToby Isaac 
6249dff059c6SToby Isaac   lastnext = &(dm->boundary);
6250e6f8dbb6SToby Isaac   while (dsbound) {
6251e6f8dbb6SToby Isaac     DMBoundary dmbound;
6252e6f8dbb6SToby Isaac 
6253e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6254e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6255e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6256994fe344SLisandro Dalcin     if (!dmbound->label) {ierr = PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);}
625747a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6258dff059c6SToby Isaac     *lastnext = dmbound;
6259dff059c6SToby Isaac     lastnext = &(dmbound->next);
6260dff059c6SToby Isaac     dsbound = dsbound->next;
6261a6ba4734SToby Isaac   }
6262a6ba4734SToby Isaac   PetscFunctionReturn(0);
6263a6ba4734SToby Isaac }
6264a6ba4734SToby Isaac 
6265a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6266a6ba4734SToby Isaac {
6267b95f2879SToby Isaac   DMBoundary     b;
6268a6ba4734SToby Isaac   PetscErrorCode ierr;
6269a6ba4734SToby Isaac 
6270a6ba4734SToby Isaac   PetscFunctionBegin;
6271a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6272a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6273a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6274e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6275b95f2879SToby Isaac   b = dm->boundary;
6276a6ba4734SToby Isaac   while (b && !(*isBd)) {
6277e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6278e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
62793424c85cSToby Isaac 
62803424c85cSToby Isaac     if (label) {
6281a6ba4734SToby Isaac       PetscInt i;
6282a6ba4734SToby Isaac 
6283e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6284e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6285a6ba4734SToby Isaac       }
6286a6ba4734SToby Isaac     }
6287a6ba4734SToby Isaac     b = b->next;
6288a6ba4734SToby Isaac   }
6289a6ba4734SToby Isaac   PetscFunctionReturn(0);
6290a6ba4734SToby Isaac }
62914d6f44ffSToby Isaac 
62924d6f44ffSToby Isaac /*@C
62934d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
62944d6f44ffSToby Isaac 
62954d6f44ffSToby Isaac   Input Parameters:
62964d6f44ffSToby Isaac + dm      - The DM
62970709b2feSToby Isaac . time    - The time
62984d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
62994d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
63004d6f44ffSToby Isaac - mode    - The insertion mode for values
63014d6f44ffSToby Isaac 
63024d6f44ffSToby Isaac   Output Parameter:
63034d6f44ffSToby Isaac . X - vector
63044d6f44ffSToby Isaac 
63054d6f44ffSToby Isaac    Calling sequence of func:
63060709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
63074d6f44ffSToby Isaac 
63084d6f44ffSToby Isaac +  dim - The spatial dimension
63094d6f44ffSToby Isaac .  x   - The coordinates
63104d6f44ffSToby Isaac .  Nf  - The number of fields
63114d6f44ffSToby Isaac .  u   - The output field values
63124d6f44ffSToby Isaac -  ctx - optional user-defined function context
63134d6f44ffSToby Isaac 
63144d6f44ffSToby Isaac   Level: developer
63154d6f44ffSToby Isaac 
63162716604bSToby Isaac .seealso: DMComputeL2Diff()
63174d6f44ffSToby Isaac @*/
63180709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
63194d6f44ffSToby Isaac {
63204d6f44ffSToby Isaac   Vec            localX;
63214d6f44ffSToby Isaac   PetscErrorCode ierr;
63224d6f44ffSToby Isaac 
63234d6f44ffSToby Isaac   PetscFunctionBegin;
63244d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63254d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
63260709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63274d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
63284d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
63294d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
63304d6f44ffSToby Isaac   PetscFunctionReturn(0);
63314d6f44ffSToby Isaac }
63324d6f44ffSToby Isaac 
63330709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
63344d6f44ffSToby Isaac {
63354d6f44ffSToby Isaac   PetscErrorCode ierr;
63364d6f44ffSToby Isaac 
63374d6f44ffSToby Isaac   PetscFunctionBegin;
63384d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63394d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
63400918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
63410709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63424d6f44ffSToby Isaac   PetscFunctionReturn(0);
63434d6f44ffSToby Isaac }
63444d6f44ffSToby Isaac 
63452c53366bSMatthew G. Knepley PetscErrorCode DMProjectFunctionLabel(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
63462c53366bSMatthew G. Knepley {
63472c53366bSMatthew G. Knepley   Vec            localX;
63482c53366bSMatthew G. Knepley   PetscErrorCode ierr;
63492c53366bSMatthew G. Knepley 
63502c53366bSMatthew G. Knepley   PetscFunctionBegin;
63512c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63522c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
63532c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63542c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
63552c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
63562c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
63572c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
63582c53366bSMatthew G. Knepley }
63592c53366bSMatthew G. Knepley 
63601c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFunctionLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
63614d6f44ffSToby Isaac {
63624d6f44ffSToby Isaac   PetscErrorCode ierr;
63634d6f44ffSToby Isaac 
63644d6f44ffSToby Isaac   PetscFunctionBegin;
63654d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63664d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
63670918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
63681c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63694d6f44ffSToby Isaac   PetscFunctionReturn(0);
63704d6f44ffSToby Isaac }
63712716604bSToby Isaac 
63728c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
63738c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
63748c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
63758c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6376191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
63778c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
63788c6c5593SMatthew G. Knepley {
63798c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
63808c6c5593SMatthew G. Knepley 
63818c6c5593SMatthew G. Knepley   PetscFunctionBegin;
63828c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63838c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
63848c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
63850918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
63868c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
63878c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
63888c6c5593SMatthew G. Knepley }
63898c6c5593SMatthew G. Knepley 
63901c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
63918c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
63928c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
63938c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6394191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
63958c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
63968c6c5593SMatthew G. Knepley {
63978c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
63988c6c5593SMatthew G. Knepley 
63998c6c5593SMatthew G. Knepley   PetscFunctionBegin;
64008c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64018c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
64028c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
64030918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
64041c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
64058c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
64068c6c5593SMatthew G. Knepley }
64078c6c5593SMatthew G. Knepley 
64082716604bSToby Isaac /*@C
64092716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
64102716604bSToby Isaac 
64112716604bSToby Isaac   Input Parameters:
64122716604bSToby Isaac + dm    - The DM
64130709b2feSToby Isaac . time  - The time
64142716604bSToby Isaac . funcs - The functions to evaluate for each field component
64152716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6416574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
64172716604bSToby Isaac 
64182716604bSToby Isaac   Output Parameter:
64192716604bSToby Isaac . diff - The diff ||u - u_h||_2
64202716604bSToby Isaac 
64212716604bSToby Isaac   Level: developer
64222716604bSToby Isaac 
64231189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
64242716604bSToby Isaac @*/
64250709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
64262716604bSToby Isaac {
64272716604bSToby Isaac   PetscErrorCode ierr;
64282716604bSToby Isaac 
64292716604bSToby Isaac   PetscFunctionBegin;
64302716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6431b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
64320918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
64330709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
64342716604bSToby Isaac   PetscFunctionReturn(0);
64352716604bSToby Isaac }
6436b698f381SToby Isaac 
6437b698f381SToby Isaac /*@C
6438b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6439b698f381SToby Isaac 
6440b698f381SToby Isaac   Input Parameters:
6441b698f381SToby Isaac + dm    - The DM
6442b698f381SToby Isaac , time  - The time
6443b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6444b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6445574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6446b698f381SToby Isaac - n     - The vector to project along
6447b698f381SToby Isaac 
6448b698f381SToby Isaac   Output Parameter:
6449b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6450b698f381SToby Isaac 
6451b698f381SToby Isaac   Level: developer
6452b698f381SToby Isaac 
6453b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6454b698f381SToby Isaac @*/
6455b698f381SToby 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)
6456b698f381SToby Isaac {
6457b698f381SToby Isaac   PetscErrorCode ierr;
6458b698f381SToby Isaac 
6459b698f381SToby Isaac   PetscFunctionBegin;
6460b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6461b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6462b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6463b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6464b698f381SToby Isaac   PetscFunctionReturn(0);
6465b698f381SToby Isaac }
6466b698f381SToby Isaac 
64672a16baeaSToby Isaac /*@C
64682a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
64692a16baeaSToby Isaac 
64702a16baeaSToby Isaac   Input Parameters:
64712a16baeaSToby Isaac + dm    - The DM
64722a16baeaSToby Isaac . time  - The time
64732a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
64742a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6475574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
64762a16baeaSToby Isaac 
64772a16baeaSToby Isaac   Output Parameter:
64782a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
64792a16baeaSToby Isaac 
64802a16baeaSToby Isaac   Level: developer
64812a16baeaSToby Isaac 
64821189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
64832a16baeaSToby Isaac @*/
64841189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
64852a16baeaSToby Isaac {
64862a16baeaSToby Isaac   PetscErrorCode ierr;
64872a16baeaSToby Isaac 
64882a16baeaSToby Isaac   PetscFunctionBegin;
64892a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64902a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
64910918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
64922a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
64932a16baeaSToby Isaac   PetscFunctionReturn(0);
64942a16baeaSToby Isaac }
64952a16baeaSToby Isaac 
6496df0b854cSToby Isaac /*@C
6497df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6498cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6499df0b854cSToby Isaac 
6500df0b854cSToby Isaac   Collective on dm
6501df0b854cSToby Isaac 
6502df0b854cSToby Isaac   Input parameters:
6503df0b854cSToby Isaac + dm - the pre-adaptation DM object
6504a1b0c543SToby Isaac - label - label with the flags
6505df0b854cSToby Isaac 
6506df0b854cSToby Isaac   Output parameters:
65070d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6508df0b854cSToby Isaac 
6509df0b854cSToby Isaac   Level: intermediate
65100d1cd5e0SMatthew G. Knepley 
65110d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6512df0b854cSToby Isaac @*/
65130d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6514df0b854cSToby Isaac {
6515df0b854cSToby Isaac   PetscErrorCode ierr;
6516df0b854cSToby Isaac 
6517df0b854cSToby Isaac   PetscFunctionBegin;
6518df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6519a1b0c543SToby Isaac   PetscValidPointer(label,2);
65200d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
65210d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
65226f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
65230d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
65240d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
65250d1cd5e0SMatthew G. Knepley }
65260d1cd5e0SMatthew G. Knepley 
65270d1cd5e0SMatthew G. Knepley /*@C
65280d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
65290d1cd5e0SMatthew G. Knepley 
65300d1cd5e0SMatthew G. Knepley   Input Parameters:
65310d1cd5e0SMatthew G. Knepley + dm - The DM object
65320d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
65336f25b0d8SLisandro Dalcin - bdLabel - Label for boundary tags, which will be preserved in the output mesh. bdLabel should be NULL if there is no such label, and should be different from "_boundary_".
65340d1cd5e0SMatthew G. Knepley 
65350d1cd5e0SMatthew G. Knepley   Output Parameter:
65360d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
65370d1cd5e0SMatthew G. Knepley 
65380d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
65390d1cd5e0SMatthew G. Knepley 
65400d1cd5e0SMatthew G. Knepley   Level: advanced
65410d1cd5e0SMatthew G. Knepley 
65420d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
65430d1cd5e0SMatthew G. Knepley @*/
65440d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
65450d1cd5e0SMatthew G. Knepley {
65460d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
65470d1cd5e0SMatthew G. Knepley 
65480d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
65490d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65500d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
65510d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
65520d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
65536f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
65546f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
65550d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6556df0b854cSToby Isaac   PetscFunctionReturn(0);
6557df0b854cSToby Isaac }
6558c4088d22SMatthew G. Knepley 
6559502a2867SDave May /*@C
6560502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6561502a2867SDave May 
6562502a2867SDave May  Not Collective
6563502a2867SDave May 
6564502a2867SDave May  Input Parameter:
6565502a2867SDave May  . dm    - The DM
6566502a2867SDave May 
6567502a2867SDave May  Output Parameter:
6568502a2867SDave May  . nranks - the number of neighbours
6569502a2867SDave May  . ranks - the neighbors ranks
6570502a2867SDave May 
6571502a2867SDave May  Notes:
6572502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6573502a2867SDave May 
6574502a2867SDave May  Level: beginner
6575502a2867SDave May 
6576dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6577502a2867SDave May @*/
6578502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6579502a2867SDave May {
6580502a2867SDave May   PetscErrorCode ierr;
6581502a2867SDave May 
6582502a2867SDave May   PetscFunctionBegin;
6583502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65840918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6585502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6586502a2867SDave May   PetscFunctionReturn(0);
6587502a2867SDave May }
6588502a2867SDave May 
6589531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6590531c7667SBarry Smith 
6591531c7667SBarry Smith /*
6592531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6593531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6594531c7667SBarry Smith */
6595531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6596531c7667SBarry Smith {
6597531c7667SBarry Smith   PetscErrorCode ierr;
6598531c7667SBarry Smith 
6599531c7667SBarry Smith   PetscFunctionBegin;
6600531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6601531c7667SBarry Smith     Vec x1local;
6602531c7667SBarry Smith     DM  dm;
6603531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6604531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6605531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6606531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6607531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6608531c7667SBarry Smith     x1   = x1local;
6609531c7667SBarry Smith   }
6610531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6611531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6612531c7667SBarry Smith     DM  dm;
6613531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6614531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6615531c7667SBarry Smith   }
6616531c7667SBarry Smith   PetscFunctionReturn(0);
6617531c7667SBarry Smith }
6618531c7667SBarry Smith 
6619531c7667SBarry Smith /*@
6620531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6621531c7667SBarry Smith 
6622531c7667SBarry Smith     Input Parameter:
6623531c7667SBarry Smith .    coloring - the MatFDColoring object
6624531c7667SBarry Smith 
662595452b02SPatrick Sanan     Developer Notes:
662695452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
6627531c7667SBarry Smith 
66281b266c99SBarry Smith     Level: advanced
66291b266c99SBarry Smith 
6630531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6631531c7667SBarry Smith @*/
6632531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6633531c7667SBarry Smith {
6634531c7667SBarry Smith   PetscFunctionBegin;
6635531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6636531c7667SBarry Smith   PetscFunctionReturn(0);
6637531c7667SBarry Smith }
66388320bc6fSPatrick Sanan 
66398320bc6fSPatrick Sanan /*@
66408320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
66418320bc6fSPatrick Sanan 
66428320bc6fSPatrick Sanan     Collective
66438320bc6fSPatrick Sanan 
66448320bc6fSPatrick Sanan     Input Parameters:
66458320bc6fSPatrick Sanan +    dm - the first DM
66468320bc6fSPatrick Sanan -    dm2 - the second DM
66478320bc6fSPatrick Sanan 
66488320bc6fSPatrick Sanan     Output Parameters:
66498320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
66508320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
66518320bc6fSPatrick Sanan 
66528320bc6fSPatrick Sanan     Notes:
66538320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
66548320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
66558320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
66568320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
66578320bc6fSPatrick Sanan     decomposition, with different data.
66588320bc6fSPatrick Sanan 
66598320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
66608320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
66618320bc6fSPatrick Sanan 
66628320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
66638320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
66648320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
66658320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
66668320bc6fSPatrick Sanan 
66678320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
66688320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
66698320bc6fSPatrick Sanan .vb
66708320bc6fSPatrick Sanan   ...
66718320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
66728320bc6fSPatrick Sanan   if (set && compatible)  {
66738320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
66748320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
66758320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
66768320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
66778320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
66788320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
66798320bc6fSPatrick Sanan       }
66808320bc6fSPatrick Sanan     }
66818320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
66828320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
66838320bc6fSPatrick Sanan   } else {
66848320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
66858320bc6fSPatrick Sanan   }
66868320bc6fSPatrick Sanan   ...
66878320bc6fSPatrick Sanan .ve
66888320bc6fSPatrick Sanan 
66898320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
66908320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
66918320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
66928320bc6fSPatrick Sanan     always check the "set" output parameter.
66938320bc6fSPatrick Sanan 
66948320bc6fSPatrick Sanan     A DM is always compatible with itself.
66958320bc6fSPatrick Sanan 
66968320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
66978320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
66988320bc6fSPatrick Sanan     incompatible.
66998320bc6fSPatrick Sanan 
67008320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
67018320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
67028320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
67038320bc6fSPatrick Sanan 
67048320bc6fSPatrick Sanan     Developer Notes:
67058320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
67068320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
67078320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
67088320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
67098320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
67108320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
67118320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
67128320bc6fSPatrick Sanan     function will then call that logic.
67138320bc6fSPatrick Sanan 
67148320bc6fSPatrick Sanan     Level: advanced
67158320bc6fSPatrick Sanan 
67168320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
67178320bc6fSPatrick Sanan @*/
67188320bc6fSPatrick Sanan 
67198320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
67208320bc6fSPatrick Sanan {
67218320bc6fSPatrick Sanan   PetscErrorCode ierr;
67228320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
67238320bc6fSPatrick Sanan   DMType         type,type2;
67248320bc6fSPatrick Sanan   PetscBool      sameType;
67258320bc6fSPatrick Sanan 
67268320bc6fSPatrick Sanan   PetscFunctionBegin;
67278320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67288320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
67298320bc6fSPatrick Sanan 
67308320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
67318320bc6fSPatrick Sanan   if (dm == dm2) {
67328320bc6fSPatrick Sanan     *set = PETSC_TRUE;
67338320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
67348320bc6fSPatrick Sanan     PetscFunctionReturn(0);
67358320bc6fSPatrick Sanan   }
67368320bc6fSPatrick Sanan 
67378320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
67388320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
67398320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
67408320bc6fSPatrick Sanan      determined by the implementation-specific logic */
67418320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
67428320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
67438320bc6fSPatrick Sanan     *set = PETSC_TRUE;
67448320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
67458320bc6fSPatrick Sanan     PetscFunctionReturn(0);
67468320bc6fSPatrick Sanan   }
67478320bc6fSPatrick Sanan 
67488320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
67498320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
67508320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
67518320bc6fSPatrick Sanan     if (*set) {
67528320bc6fSPatrick Sanan       PetscFunctionReturn(0);
67538320bc6fSPatrick Sanan     }
67548320bc6fSPatrick Sanan   }
67558320bc6fSPatrick Sanan 
67568320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
67578320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
67588320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
67598320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
67608320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
67618320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
67628320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
67638320bc6fSPatrick Sanan   } else {
67648320bc6fSPatrick Sanan     *set = PETSC_FALSE;
67658320bc6fSPatrick Sanan   }
67668320bc6fSPatrick Sanan   PetscFunctionReturn(0);
67678320bc6fSPatrick Sanan }
6768