xref: /petsc/src/dm/interface/dm.c (revision 49be4549f439c290180abd5e5283c412c6ad3442)
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;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
111ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
14bff4a2f0SMatthew G. Knepley 
154a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
164a7a4c06SLawrence Mitchell {
174a7a4c06SLawrence Mitchell   PetscFunctionBegin;
184a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
194a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
204a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
214a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
224a7a4c06SLawrence Mitchell }
234a7a4c06SLawrence Mitchell 
24a4121054SBarry Smith /*@
25de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
26a4121054SBarry Smith 
27a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
28a4121054SBarry Smith    error when you try to use the vector.
29a4121054SBarry Smith 
30a4121054SBarry Smith   Collective on MPI_Comm
31a4121054SBarry Smith 
32a4121054SBarry Smith   Input Parameter:
33a4121054SBarry Smith . comm - The communicator for the DM object
34a4121054SBarry Smith 
35a4121054SBarry Smith   Output Parameter:
36a4121054SBarry Smith . dm - The DM object
37a4121054SBarry Smith 
38a4121054SBarry Smith   Level: beginner
39a4121054SBarry Smith 
408472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
41a4121054SBarry Smith @*/
427087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
43a4121054SBarry Smith {
44a4121054SBarry Smith   DM             v;
45a4121054SBarry Smith   PetscErrorCode ierr;
46a4121054SBarry Smith 
47a4121054SBarry Smith   PetscFunctionBegin;
481411c6eeSJed Brown   PetscValidPointer(dm,2);
490298fd71SBarry Smith   *dm = NULL;
50607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
51a4121054SBarry Smith 
5273107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
53e7c4fc90SDmitry Karpeev 
54*49be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
55*49be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
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;
76f9d4088aSMatthew 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 
184a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
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 
210a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
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 
338a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), 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 
367a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
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);
835*49be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
836*49be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
837*49be4549SMatthew G. Knepley   if (dm->defaultSF) {ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);}
8383194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8390298fd71SBarry 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);
840a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
841f9ba7244SBarry Smith   if (flg) {
842f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
843f9ba7244SBarry Smith   }
844a264d7a6SBarry 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);
845073dac72SJed Brown   if (flg) {
846521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
847073dac72SJed Brown   }
8488f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
849f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
850e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
851f9ba7244SBarry Smith   }
852f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8530633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85482fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
855d7bf68aeSBarry Smith   PetscFunctionReturn(0);
856d7bf68aeSBarry Smith }
857d7bf68aeSBarry Smith 
858fc9bc008SSatish Balay /*@C
859224748a4SBarry Smith     DMView - Views a DM
86047c6ae99SBarry Smith 
86147c6ae99SBarry Smith     Collective on DM
86247c6ae99SBarry Smith 
86347c6ae99SBarry Smith     Input Parameter:
86447c6ae99SBarry Smith +   dm - the DM object to view
86547c6ae99SBarry Smith -   v - the viewer
86647c6ae99SBarry Smith 
867224748a4SBarry Smith     Level: beginner
86847c6ae99SBarry Smith 
869e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87047c6ae99SBarry Smith 
87147c6ae99SBarry Smith @*/
8727087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
87347c6ae99SBarry Smith {
87447c6ae99SBarry Smith   PetscErrorCode    ierr;
87532c0f0efSBarry Smith   PetscBool         isbinary;
87676a8abe0SBarry Smith   PetscMPIInt       size;
87776a8abe0SBarry Smith   PetscViewerFormat format;
87847c6ae99SBarry Smith 
87947c6ae99SBarry Smith   PetscFunctionBegin;
880171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8813014e516SBarry Smith   if (!v) {
882ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8833014e516SBarry Smith   }
88476a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
88576a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
88676a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
88798c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
88832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
88932c0f0efSBarry Smith   if (isbinary) {
89055849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
89132c0f0efSBarry Smith     char     type[256];
89232c0f0efSBarry Smith 
89332c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
89432c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
89532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
89632c0f0efSBarry Smith   }
8970c010503SBarry Smith   if (dm->ops->view) {
8980c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
89947c6ae99SBarry Smith   }
90047c6ae99SBarry Smith   PetscFunctionReturn(0);
90147c6ae99SBarry Smith }
90247c6ae99SBarry Smith 
90347c6ae99SBarry Smith /*@
9048472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
90547c6ae99SBarry Smith 
90647c6ae99SBarry Smith     Collective on DM
90747c6ae99SBarry Smith 
90847c6ae99SBarry Smith     Input Parameter:
90947c6ae99SBarry Smith .   dm - the DM object
91047c6ae99SBarry Smith 
91147c6ae99SBarry Smith     Output Parameter:
91247c6ae99SBarry Smith .   vec - the global vector
91347c6ae99SBarry Smith 
914073dac72SJed Brown     Level: beginner
91547c6ae99SBarry Smith 
916e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
91747c6ae99SBarry Smith 
91847c6ae99SBarry Smith @*/
9197087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92047c6ae99SBarry Smith {
92147c6ae99SBarry Smith   PetscErrorCode ierr;
92247c6ae99SBarry Smith 
92347c6ae99SBarry Smith   PetscFunctionBegin;
924171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
92547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
92647c6ae99SBarry Smith   PetscFunctionReturn(0);
92747c6ae99SBarry Smith }
92847c6ae99SBarry Smith 
92947c6ae99SBarry Smith /*@
9308472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith     Not Collective
93347c6ae99SBarry Smith 
93447c6ae99SBarry Smith     Input Parameter:
93547c6ae99SBarry Smith .   dm - the DM object
93647c6ae99SBarry Smith 
93747c6ae99SBarry Smith     Output Parameter:
93847c6ae99SBarry Smith .   vec - the local vector
93947c6ae99SBarry Smith 
940073dac72SJed Brown     Level: beginner
94147c6ae99SBarry Smith 
942e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94347c6ae99SBarry Smith 
94447c6ae99SBarry Smith @*/
9457087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
94647c6ae99SBarry Smith {
94747c6ae99SBarry Smith   PetscErrorCode ierr;
94847c6ae99SBarry Smith 
94947c6ae99SBarry Smith   PetscFunctionBegin;
950171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
95147c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
95247c6ae99SBarry Smith   PetscFunctionReturn(0);
95347c6ae99SBarry Smith }
95447c6ae99SBarry Smith 
9551411c6eeSJed Brown /*@
9561411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9571411c6eeSJed Brown 
9581411c6eeSJed Brown    Collective on DM
9591411c6eeSJed Brown 
9601411c6eeSJed Brown    Input Parameter:
9611411c6eeSJed Brown .  dm - the DM that provides the mapping
9621411c6eeSJed Brown 
9631411c6eeSJed Brown    Output Parameter:
9641411c6eeSJed Brown .  ltog - the mapping
9651411c6eeSJed Brown 
9661411c6eeSJed Brown    Level: intermediate
9671411c6eeSJed Brown 
9681411c6eeSJed Brown    Notes:
9691411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9701411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9711411c6eeSJed Brown 
972fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9731411c6eeSJed Brown @*/
9747087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9751411c6eeSJed Brown {
9760be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9771411c6eeSJed Brown   PetscErrorCode ierr;
9781411c6eeSJed Brown 
9791411c6eeSJed Brown   PetscFunctionBegin;
9801411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9811411c6eeSJed Brown   PetscValidPointer(ltog,2);
9821411c6eeSJed Brown   if (!dm->ltogmap) {
98337d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
98437d0c07bSMatthew G Knepley 
985e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
98637d0c07bSMatthew G Knepley     if (section) {
987a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
98837d0c07bSMatthew G Knepley       PetscInt       *ltog;
989ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
99037d0c07bSMatthew G Knepley 
991e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
99237d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
993ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
994ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
99537d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
996a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
99737d0c07bSMatthew G Knepley 
99837d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
99937d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1000a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1001a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
100237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10031a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10041a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10051a7dc684SMatthew G. Knepley         if (dof) {
1006b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1007b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10081a7dc684SMatthew G. Knepley         }
100937d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1010a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1011a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
101237d0c07bSMatthew G Knepley         }
101337d0c07bSMatthew G Knepley       }
1014bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10150be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10160be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10170be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10180be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10197591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10207591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1021ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1022ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1023ccf3bd66SMatthew G. Knepley         n /= bs;
1024ccf3bd66SMatthew G. Knepley       }
1025ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10263bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
102737d0c07bSMatthew G Knepley     } else {
1028184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1029184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10301411c6eeSJed Brown     }
103137d0c07bSMatthew G Knepley   }
10321411c6eeSJed Brown   *ltog = dm->ltogmap;
10331411c6eeSJed Brown   PetscFunctionReturn(0);
10341411c6eeSJed Brown }
10351411c6eeSJed Brown 
10361411c6eeSJed Brown /*@
10371411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10381411c6eeSJed Brown 
10391411c6eeSJed Brown    Not Collective
10401411c6eeSJed Brown 
10411411c6eeSJed Brown    Input Parameter:
10421411c6eeSJed Brown .  dm - the DM with block structure
10431411c6eeSJed Brown 
10441411c6eeSJed Brown    Output Parameter:
10451411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10461411c6eeSJed Brown 
10471411c6eeSJed Brown    Level: intermediate
10481411c6eeSJed Brown 
1049fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10501411c6eeSJed Brown @*/
10517087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10521411c6eeSJed Brown {
10531411c6eeSJed Brown   PetscFunctionBegin;
10541411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10551411c6eeSJed Brown   PetscValidPointer(bs,2);
10561411c6eeSJed 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");
10571411c6eeSJed Brown   *bs = dm->bs;
10581411c6eeSJed Brown   PetscFunctionReturn(0);
10591411c6eeSJed Brown }
10601411c6eeSJed Brown 
106147c6ae99SBarry Smith /*@
10628472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
106347c6ae99SBarry Smith 
106447c6ae99SBarry Smith     Collective on DM
106547c6ae99SBarry Smith 
106647c6ae99SBarry Smith     Input Parameter:
106747c6ae99SBarry Smith +   dm1 - the DM object
106847c6ae99SBarry Smith -   dm2 - the second, finer DM object
106947c6ae99SBarry Smith 
107047c6ae99SBarry Smith     Output Parameter:
107147c6ae99SBarry Smith +  mat - the interpolation
107247c6ae99SBarry Smith -  vec - the scaling (optional)
107347c6ae99SBarry Smith 
107447c6ae99SBarry Smith     Level: developer
107547c6ae99SBarry Smith 
107695452b02SPatrick Sanan     Notes:
107795452b02SPatrick 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
107885afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1079d52bd9f3SBarry Smith 
10801f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1081d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
108285afcc9aSBarry Smith 
108385afcc9aSBarry Smith 
10843ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
108547c6ae99SBarry Smith 
108647c6ae99SBarry Smith @*/
1087e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
108847c6ae99SBarry Smith {
108947c6ae99SBarry Smith   PetscErrorCode ierr;
109047c6ae99SBarry Smith 
109147c6ae99SBarry Smith   PetscFunctionBegin;
1092171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1093171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1094c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1095c7d20fa0SStefano Zampini   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInterpolation not implemented for type %s",((PetscObject)dm1)->type_name);
1096cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
109725296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1098cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
109947c6ae99SBarry Smith   PetscFunctionReturn(0);
110047c6ae99SBarry Smith }
110147c6ae99SBarry Smith 
11023ad4599aSBarry Smith /*@
11033ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11043ad4599aSBarry Smith 
11053ad4599aSBarry Smith     Collective on DM
11063ad4599aSBarry Smith 
11073ad4599aSBarry Smith     Input Parameter:
11083ad4599aSBarry Smith +   dm1 - the DM object
11093ad4599aSBarry Smith -   dm2 - the second, finer DM object
11103ad4599aSBarry Smith 
11113ad4599aSBarry Smith     Output Parameter:
11123ad4599aSBarry Smith .  mat - the restriction
11133ad4599aSBarry Smith 
11143ad4599aSBarry Smith 
11153ad4599aSBarry Smith     Level: developer
11163ad4599aSBarry Smith 
111795452b02SPatrick Sanan     Notes:
111895452b02SPatrick 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
11193ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11203ad4599aSBarry Smith 
11213ad4599aSBarry Smith 
11223ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11233ad4599aSBarry Smith 
11243ad4599aSBarry Smith @*/
11253ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11263ad4599aSBarry Smith {
11273ad4599aSBarry Smith   PetscErrorCode ierr;
11283ad4599aSBarry Smith 
11293ad4599aSBarry Smith   PetscFunctionBegin;
11303ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11313ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11323ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11337294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11343ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11353ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11363ad4599aSBarry Smith   PetscFunctionReturn(0);
11373ad4599aSBarry Smith }
11383ad4599aSBarry Smith 
113947c6ae99SBarry Smith /*@
11408472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114147c6ae99SBarry Smith 
114247c6ae99SBarry Smith     Collective on DM
114347c6ae99SBarry Smith 
114447c6ae99SBarry Smith     Input Parameter:
114547c6ae99SBarry Smith +   dm1 - the DM object
114647c6ae99SBarry Smith -   dm2 - the second, finer DM object
114747c6ae99SBarry Smith 
114847c6ae99SBarry Smith     Output Parameter:
11496dbf9973SLawrence Mitchell .   mat - the injection
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Level: developer
115247c6ae99SBarry Smith 
115395452b02SPatrick Sanan    Notes:
115495452b02SPatrick 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
115585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
115685afcc9aSBarry Smith 
1157e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
115847c6ae99SBarry Smith 
115947c6ae99SBarry Smith @*/
11606dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116147c6ae99SBarry Smith {
116247c6ae99SBarry Smith   PetscErrorCode ierr;
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith   PetscFunctionBegin;
1165171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1166171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11670c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11686dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
116947c6ae99SBarry Smith   PetscFunctionReturn(0);
117047c6ae99SBarry Smith }
117147c6ae99SBarry Smith 
1172b412c318SBarry Smith /*@
1173bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1174bd041c0cSMatthew G. Knepley 
1175bd041c0cSMatthew G. Knepley   Collective on DM
1176bd041c0cSMatthew G. Knepley 
1177bd041c0cSMatthew G. Knepley   Input Parameter:
1178bd041c0cSMatthew G. Knepley + dm1 - the DM object
1179bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1180bd041c0cSMatthew G. Knepley 
1181bd041c0cSMatthew G. Knepley   Output Parameter:
1182bd041c0cSMatthew G. Knepley . mat - the interpolation
1183bd041c0cSMatthew G. Knepley 
1184bd041c0cSMatthew G. Knepley   Level: developer
1185bd041c0cSMatthew G. Knepley 
1186bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1187bd041c0cSMatthew G. Knepley @*/
1188bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1189bd041c0cSMatthew G. Knepley {
1190bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1191bd041c0cSMatthew G. Knepley 
1192bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1193bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1194bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1195bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1196bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1197bd041c0cSMatthew G. Knepley }
1198bd041c0cSMatthew G. Knepley 
1199bd041c0cSMatthew G. Knepley /*@
1200b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
120147c6ae99SBarry Smith 
120247c6ae99SBarry Smith     Collective on DM
120347c6ae99SBarry Smith 
120447c6ae99SBarry Smith     Input Parameter:
120547c6ae99SBarry Smith +   dm - the DM object
12065bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
120747c6ae99SBarry Smith 
120847c6ae99SBarry Smith     Output Parameter:
120947c6ae99SBarry Smith .   coloring - the coloring
121047c6ae99SBarry Smith 
121147c6ae99SBarry Smith     Level: developer
121247c6ae99SBarry Smith 
1213b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
121447c6ae99SBarry Smith 
1215aab9d709SJed Brown @*/
1216b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
121747c6ae99SBarry Smith {
121847c6ae99SBarry Smith   PetscErrorCode ierr;
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith   PetscFunctionBegin;
1221171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1222ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1223b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
122447c6ae99SBarry Smith   PetscFunctionReturn(0);
122547c6ae99SBarry Smith }
122647c6ae99SBarry Smith 
1227b412c318SBarry Smith /*@
12288472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
122947c6ae99SBarry Smith 
123047c6ae99SBarry Smith     Collective on DM
123147c6ae99SBarry Smith 
123247c6ae99SBarry Smith     Input Parameter:
1233b412c318SBarry Smith .   dm - the DM object
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith     Output Parameter:
123647c6ae99SBarry Smith .   mat - the empty Jacobian
123747c6ae99SBarry Smith 
1238073dac72SJed Brown     Level: beginner
123947c6ae99SBarry Smith 
124095452b02SPatrick Sanan     Notes:
124195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
124294013140SBarry Smith        do not need to do it yourself.
124394013140SBarry Smith 
124494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12457889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
124694013140SBarry Smith 
124794013140SBarry 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
124894013140SBarry Smith        internally by PETSc.
124994013140SBarry Smith 
125094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1251aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
125294013140SBarry Smith 
1253b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
125447c6ae99SBarry Smith 
1255aab9d709SJed Brown @*/
1256b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
125747c6ae99SBarry Smith {
125847c6ae99SBarry Smith   PetscErrorCode ierr;
125947c6ae99SBarry Smith 
126047c6ae99SBarry Smith   PetscFunctionBegin;
1261171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1262607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1263c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1264c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1265b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1266e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1267e571a35bSMatthew G. Knepley   if (dm->prob) {
1268e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1269e571a35bSMatthew G. Knepley     PetscInt     Nf;
1270e571a35bSMatthew G. Knepley 
1271e571a35bSMatthew G. Knepley     ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
1272e571a35bSMatthew G. Knepley     if (Nf == 1) {
1273e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1274e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1275e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1276e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1277e571a35bSMatthew G. Knepley       }
1278e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1279e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1280e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1281e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1282e571a35bSMatthew G. Knepley       }
1283e571a35bSMatthew G. Knepley     }
1284e571a35bSMatthew G. Knepley   }
128547c6ae99SBarry Smith   PetscFunctionReturn(0);
128647c6ae99SBarry Smith }
128747c6ae99SBarry Smith 
1288732e2eb9SMatthew G Knepley /*@
1289950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1290732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1291732e2eb9SMatthew G Knepley 
12928472ad0fSDave May   Logically Collective on DM
1293732e2eb9SMatthew G Knepley 
1294732e2eb9SMatthew G Knepley   Input Parameter:
1295732e2eb9SMatthew G Knepley + dm - the DM
1296732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1297732e2eb9SMatthew G Knepley 
1298732e2eb9SMatthew G Knepley   Level: developer
1299b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1300732e2eb9SMatthew G Knepley @*/
1301732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1302732e2eb9SMatthew G Knepley {
1303732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1304732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1305732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1306732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1307732e2eb9SMatthew G Knepley }
1308732e2eb9SMatthew G Knepley 
1309b06ff27eSHong Zhang /*@
1310b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1311b06ff27eSHong Zhang     but the array for values will not be allocated.
1312b06ff27eSHong Zhang 
1313b06ff27eSHong Zhang   Logically Collective on DM
1314b06ff27eSHong Zhang 
1315b06ff27eSHong Zhang   Input Parameter:
1316b06ff27eSHong Zhang + dm - the DM
1317b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1318b06ff27eSHong Zhang 
1319b06ff27eSHong Zhang   Level: developer
1320b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1321b06ff27eSHong Zhang @*/
1322b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1323b06ff27eSHong Zhang {
1324b06ff27eSHong Zhang   PetscFunctionBegin;
1325b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1326b06ff27eSHong Zhang   dm->structure_only = only;
1327b06ff27eSHong Zhang   PetscFunctionReturn(0);
1328b06ff27eSHong Zhang }
1329b06ff27eSHong Zhang 
1330a89ea682SMatthew G Knepley /*@C
1331aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1332a89ea682SMatthew G Knepley 
1333a89ea682SMatthew G Knepley   Not Collective
1334a89ea682SMatthew G Knepley 
1335a89ea682SMatthew G Knepley   Input Parameters:
1336a89ea682SMatthew G Knepley + dm - the DM object
1337aa1993deSMatthew G Knepley . count - The minium size
133869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1339a89ea682SMatthew G Knepley 
1340a89ea682SMatthew G Knepley   Output Parameter:
1341a89ea682SMatthew G Knepley . array - the work array
1342a89ea682SMatthew G Knepley 
1343a89ea682SMatthew G Knepley   Level: developer
1344a89ea682SMatthew G Knepley 
1345a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1346a89ea682SMatthew G Knepley @*/
134769291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1348a89ea682SMatthew G Knepley {
1349a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1350aa1993deSMatthew G Knepley   DMWorkLink     link;
135169291d52SBarry Smith   PetscMPIInt    dsize;
1352a89ea682SMatthew G Knepley 
1353a89ea682SMatthew G Knepley   PetscFunctionBegin;
1354a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1355aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1356aa1993deSMatthew G Knepley   if (dm->workin) {
1357aa1993deSMatthew G Knepley     link       = dm->workin;
1358aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1359aa1993deSMatthew G Knepley   } else {
1360b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1361a89ea682SMatthew G Knepley   }
136269291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13635056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1364aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1365854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1366854ce69bSBarry Smith     link->bytes = dsize*count;
1367aa1993deSMatthew G Knepley   }
1368aa1993deSMatthew G Knepley   link->next   = dm->workout;
1369aa1993deSMatthew G Knepley   dm->workout  = link;
1370aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1371a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1372a89ea682SMatthew G Knepley }
1373a89ea682SMatthew G Knepley 
1374aa1993deSMatthew G Knepley /*@C
1375aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1376aa1993deSMatthew G Knepley 
1377aa1993deSMatthew G Knepley   Not Collective
1378aa1993deSMatthew G Knepley 
1379aa1993deSMatthew G Knepley   Input Parameters:
1380aa1993deSMatthew G Knepley + dm - the DM object
1381aa1993deSMatthew G Knepley . count - The minium size
138269291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1383aa1993deSMatthew G Knepley 
1384aa1993deSMatthew G Knepley   Output Parameter:
1385aa1993deSMatthew G Knepley . array - the work array
1386aa1993deSMatthew G Knepley 
1387aa1993deSMatthew G Knepley   Level: developer
1388aa1993deSMatthew G Knepley 
138995452b02SPatrick Sanan   Developer Notes:
139095452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1391aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1392aa1993deSMatthew G Knepley @*/
139369291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1394aa1993deSMatthew G Knepley {
1395aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1396aa1993deSMatthew G Knepley 
1397aa1993deSMatthew G Knepley   PetscFunctionBegin;
1398aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1399aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1400aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1401aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1402aa1993deSMatthew G Knepley       *p           = link->next;
1403aa1993deSMatthew G Knepley       link->next   = dm->workin;
1404aa1993deSMatthew G Knepley       dm->workin   = link;
14050298fd71SBarry Smith       *(void**)mem = NULL;
1406aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1407aa1993deSMatthew G Knepley     }
1408aa1993deSMatthew G Knepley   }
1409aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1410aa1993deSMatthew G Knepley }
1411e7c4fc90SDmitry Karpeev 
1412435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1413435a35e8SMatthew G Knepley {
1414435a35e8SMatthew G Knepley   PetscFunctionBegin;
1415435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
141682f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1417435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1418435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1419435a35e8SMatthew G Knepley }
1420435a35e8SMatthew G Knepley 
14210a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14220a50eb56SMatthew G. Knepley {
14230a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14240a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1425f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14260a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14270a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14280a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14290a50eb56SMatthew G. Knepley }
14300a50eb56SMatthew G. Knepley 
1431f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1432f9d4088aSMatthew G. Knepley {
1433f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1434f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1435f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1436f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1437f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1438f9d4088aSMatthew G. Knepley }
1439f9d4088aSMatthew G. Knepley 
1440f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1441f9d4088aSMatthew G. Knepley {
1442f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1443f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1444f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1445f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1446f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1447f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1448f9d4088aSMatthew G. Knepley }
1449f9d4088aSMatthew G. Knepley 
14504f3b5142SJed Brown /*@C
14514d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14524d343eeaSMatthew G Knepley 
14534d343eeaSMatthew G Knepley   Not collective
14544d343eeaSMatthew G Knepley 
14554d343eeaSMatthew G Knepley   Input Parameter:
14564d343eeaSMatthew G Knepley . dm - the DM object
14574d343eeaSMatthew G Knepley 
14584d343eeaSMatthew G Knepley   Output Parameters:
14590298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14600298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14610298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14624d343eeaSMatthew G Knepley 
14634d343eeaSMatthew G Knepley   Level: intermediate
14644d343eeaSMatthew G Knepley 
146521c9b008SJed Brown   Notes:
146621c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
146721c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
146821c9b008SJed Brown   PetscFree().
146921c9b008SJed Brown 
14704d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14714d343eeaSMatthew G Knepley @*/
147237d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14734d343eeaSMatthew G Knepley {
147437d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14754d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14764d343eeaSMatthew G Knepley 
14774d343eeaSMatthew G Knepley   PetscFunctionBegin;
14784d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
147969ca1f37SDmitry Karpeev   if (numFields) {
148069ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
148169ca1f37SDmitry Karpeev     *numFields = 0;
148269ca1f37SDmitry Karpeev   }
148337d0c07bSMatthew G Knepley   if (fieldNames) {
148437d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14850298fd71SBarry Smith     *fieldNames = NULL;
148669ca1f37SDmitry Karpeev   }
148769ca1f37SDmitry Karpeev   if (fields) {
148869ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14890298fd71SBarry Smith     *fields = NULL;
149069ca1f37SDmitry Karpeev   }
1491e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
149237d0c07bSMatthew G Knepley   if (section) {
14933a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
149437d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
149537d0c07bSMatthew G Knepley 
1496e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
149737d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
14983a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
149937d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
150037d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
150137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15023a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
150337d0c07bSMatthew G Knepley     }
150437d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
150537d0c07bSMatthew G Knepley       PetscInt gdof;
150637d0c07bSMatthew G Knepley 
150737d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
150837d0c07bSMatthew G Knepley       if (gdof > 0) {
150937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15103a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
151137d0c07bSMatthew G Knepley 
151237d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
151337d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15143a544194SStefano Zampini           fpdof = fdof-fcdof;
15153a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15163a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15173a544194SStefano Zampini             fieldNc[f] = 1;
15183a544194SStefano Zampini           }
15193a544194SStefano Zampini           fieldSizes[f] += fpdof;
152037d0c07bSMatthew G Knepley         }
152137d0c07bSMatthew G Knepley       }
152237d0c07bSMatthew G Knepley     }
152337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1524785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
152537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
152637d0c07bSMatthew G Knepley     }
152737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
152837d0c07bSMatthew G Knepley       PetscInt gdof, goff;
152937d0c07bSMatthew G Knepley 
153037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
153137d0c07bSMatthew G Knepley       if (gdof > 0) {
153237d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
153337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
153437d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
153537d0c07bSMatthew G Knepley 
153637d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
153737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
153837d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
153937d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
154037d0c07bSMatthew G Knepley           }
154137d0c07bSMatthew G Knepley         }
154237d0c07bSMatthew G Knepley       }
154337d0c07bSMatthew G Knepley     }
15448865f1eaSKarl Rupp     if (numFields) *numFields = nF;
154537d0c07bSMatthew G Knepley     if (fieldNames) {
1546785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
154737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
154837d0c07bSMatthew G Knepley         const char *fieldName;
154937d0c07bSMatthew G Knepley 
155037d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
155137d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
155237d0c07bSMatthew G Knepley       }
155337d0c07bSMatthew G Knepley     }
155437d0c07bSMatthew G Knepley     if (fields) {
1555785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
155637d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15573a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15583a544194SStefano Zampini 
155982f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15603a544194SStefano Zampini         in[0] = -fieldNc[f];
15613a544194SStefano Zampini         in[1] = fieldNc[f];
15623a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15633a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15643a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
156537d0c07bSMatthew G Knepley       }
156637d0c07bSMatthew G Knepley     }
15673a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15688865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15698865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
157069ca1f37SDmitry Karpeev   }
15714d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15724d343eeaSMatthew G Knepley }
15734d343eeaSMatthew G Knepley 
157416621825SDmitry Karpeev 
157516621825SDmitry Karpeev /*@C
157616621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
157716621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
157816621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1579e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1580e7c4fc90SDmitry Karpeev 
1581e7c4fc90SDmitry Karpeev   Not collective
1582e7c4fc90SDmitry Karpeev 
1583e7c4fc90SDmitry Karpeev   Input Parameter:
1584e7c4fc90SDmitry Karpeev . dm - the DM object
1585e7c4fc90SDmitry Karpeev 
1586e7c4fc90SDmitry Karpeev   Output Parameters:
15870298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15880298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15890298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15900298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1591e7c4fc90SDmitry Karpeev 
1592e7c4fc90SDmitry Karpeev   Level: intermediate
1593e7c4fc90SDmitry Karpeev 
1594e7c4fc90SDmitry Karpeev   Notes:
1595e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1596e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1597e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1598e7c4fc90SDmitry Karpeev 
1599e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1600e7c4fc90SDmitry Karpeev @*/
160116621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1602e7c4fc90SDmitry Karpeev {
1603e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1604e7c4fc90SDmitry Karpeev 
1605e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1606e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16078865f1eaSKarl Rupp   if (len) {
16088865f1eaSKarl Rupp     PetscValidPointer(len,2);
16098865f1eaSKarl Rupp     *len = 0;
16108865f1eaSKarl Rupp   }
16118865f1eaSKarl Rupp   if (namelist) {
16128865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16138865f1eaSKarl Rupp     *namelist = 0;
16148865f1eaSKarl Rupp   }
16158865f1eaSKarl Rupp   if (islist) {
16168865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16178865f1eaSKarl Rupp     *islist = 0;
16188865f1eaSKarl Rupp   }
16198865f1eaSKarl Rupp   if (dmlist) {
16208865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16218865f1eaSKarl Rupp     *dmlist = 0;
16228865f1eaSKarl Rupp   }
1623f3f0edfdSDmitry Karpeev   /*
1624f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1625f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1626f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1627f3f0edfdSDmitry Karpeev    */
1628ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
162916621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1630435a35e8SMatthew G Knepley     PetscSection section;
1631435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1632435a35e8SMatthew G Knepley 
1633e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1634435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1635435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1636f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
163703dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
163803dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
163903dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1640435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1641435a35e8SMatthew G Knepley         const char *fieldName;
1642435a35e8SMatthew G Knepley 
164303dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
164403dc3394SMatthew G. Knepley         if (namelist) {
1645435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1646435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1647435a35e8SMatthew G Knepley         }
164803dc3394SMatthew G. Knepley       }
1649435a35e8SMatthew G Knepley     } else {
165069ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1651e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16520298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1653e7c4fc90SDmitry Karpeev     }
16548865f1eaSKarl Rupp   } else {
165516621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
165616621825SDmitry Karpeev   }
165716621825SDmitry Karpeev   PetscFunctionReturn(0);
165816621825SDmitry Karpeev }
165916621825SDmitry Karpeev 
1660564cec59SMatthew G. Knepley /*@
1661435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1662435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1663435a35e8SMatthew G Knepley 
1664435a35e8SMatthew G Knepley   Not collective
1665435a35e8SMatthew G Knepley 
1666435a35e8SMatthew G Knepley   Input Parameters:
16672adcc780SMatthew G. Knepley + dm        - The DM object
16682adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16692adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1670435a35e8SMatthew G Knepley 
1671435a35e8SMatthew G Knepley   Output Parameters:
16722adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16732adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1674435a35e8SMatthew G Knepley 
16755d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16765d3b26e6SMatthew G. Knepley 
1677435a35e8SMatthew G Knepley   Level: intermediate
1678435a35e8SMatthew G Knepley 
16795d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1680435a35e8SMatthew G Knepley @*/
168137bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1682435a35e8SMatthew G Knepley {
1683435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1684435a35e8SMatthew G Knepley 
1685435a35e8SMatthew G Knepley   PetscFunctionBegin;
1686435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1687435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16888865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16898865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1690435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1691435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
169282f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1693435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1694435a35e8SMatthew G Knepley }
1695435a35e8SMatthew G Knepley 
16962adcc780SMatthew G. Knepley /*@C
16972adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
16982adcc780SMatthew G. Knepley 
16992adcc780SMatthew G. Knepley   Not collective
17002adcc780SMatthew G. Knepley 
17012adcc780SMatthew G. Knepley   Input Parameter:
17022adcc780SMatthew G. Knepley + dms - The DM objects
17032adcc780SMatthew G. Knepley - len - The number of DMs
17042adcc780SMatthew G. Knepley 
17052adcc780SMatthew G. Knepley   Output Parameters:
1706a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17072adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17082adcc780SMatthew G. Knepley 
17095d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17105d3b26e6SMatthew G. Knepley 
17112adcc780SMatthew G. Knepley   Level: intermediate
17122adcc780SMatthew G. Knepley 
17135d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17142adcc780SMatthew G. Knepley @*/
17152adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17162adcc780SMatthew G. Knepley {
17172adcc780SMatthew G. Knepley   PetscInt       i;
17182adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17192adcc780SMatthew G. Knepley 
17202adcc780SMatthew G. Knepley   PetscFunctionBegin;
17212adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17222adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17232adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1724a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17252adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17262adcc780SMatthew G. Knepley   if (len) {
1727a42bd24dSMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);}
1728a42bd24dSMatthew G. Knepley     else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
17292adcc780SMatthew G. Knepley   }
17302adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17312adcc780SMatthew G. Knepley }
17322adcc780SMatthew G. Knepley 
173316621825SDmitry Karpeev 
173416621825SDmitry Karpeev /*@C
17358d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17368d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17378d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17388d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17398d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
174016621825SDmitry Karpeev 
174116621825SDmitry Karpeev   Not collective
174216621825SDmitry Karpeev 
174316621825SDmitry Karpeev   Input Parameter:
174416621825SDmitry Karpeev . dm - the DM object
174516621825SDmitry Karpeev 
174616621825SDmitry Karpeev   Output Parameters:
17470298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17480298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17490298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17500298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17510298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
175216621825SDmitry Karpeev 
175316621825SDmitry Karpeev   Level: intermediate
175416621825SDmitry Karpeev 
175516621825SDmitry Karpeev   Notes:
175616621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
175716621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
175816621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
175916621825SDmitry Karpeev 
17608d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
176116621825SDmitry Karpeev @*/
17628d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
176316621825SDmitry Karpeev {
176416621825SDmitry Karpeev   PetscErrorCode      ierr;
1765be081cd6SPeter Brune   DMSubDomainHookLink link;
1766be081cd6SPeter Brune   PetscInt            i,l;
176716621825SDmitry Karpeev 
176816621825SDmitry Karpeev   PetscFunctionBegin;
176916621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
177014a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17710298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17720298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17730298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17740298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1775f3f0edfdSDmitry Karpeev   /*
1776f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1777f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1778f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1779f3f0edfdSDmitry Karpeev    */
1780ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
178116621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1782be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
178314a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1784f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1785be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1786be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1787be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1788be081cd6SPeter Brune         }
1789648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1790e7c4fc90SDmitry Karpeev       }
179114a18fd3SPeter Brune     }
179214a18fd3SPeter Brune     if (len) *len = l;
179314a18fd3SPeter Brune   }
1794e30e807fSPeter Brune   PetscFunctionReturn(0);
1795e30e807fSPeter Brune }
1796e30e807fSPeter Brune 
1797e30e807fSPeter Brune 
1798e30e807fSPeter Brune /*@C
1799e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1800e30e807fSPeter Brune 
1801e30e807fSPeter Brune   Not collective
1802e30e807fSPeter Brune 
1803e30e807fSPeter Brune   Input Parameters:
1804e30e807fSPeter Brune + dm - the DM object
1805e30e807fSPeter Brune . n  - the number of subdomain scatters
1806e30e807fSPeter Brune - subdms - the local subdomains
1807e30e807fSPeter Brune 
1808e30e807fSPeter Brune   Output Parameters:
1809e30e807fSPeter Brune + n     - the number of scatters returned
1810e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1811e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1812e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1813e30e807fSPeter Brune 
181495452b02SPatrick Sanan   Notes:
181595452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1816e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1817e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1818e30e807fSPeter Brune   solution and residual data.
1819e30e807fSPeter Brune 
1820e30e807fSPeter Brune   Level: developer
1821e30e807fSPeter Brune 
1822e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1823e30e807fSPeter Brune @*/
1824e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1825e30e807fSPeter Brune {
1826e30e807fSPeter Brune   PetscErrorCode ierr;
1827e30e807fSPeter Brune 
1828e30e807fSPeter Brune   PetscFunctionBegin;
1829e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1830e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1831e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1832e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
18336668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1834e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1835e7c4fc90SDmitry Karpeev }
1836e7c4fc90SDmitry Karpeev 
183747c6ae99SBarry Smith /*@
183847c6ae99SBarry Smith   DMRefine - Refines a DM object
183947c6ae99SBarry Smith 
184047c6ae99SBarry Smith   Collective on DM
184147c6ae99SBarry Smith 
184247c6ae99SBarry Smith   Input Parameter:
184347c6ae99SBarry Smith + dm   - the DM object
184491d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
184547c6ae99SBarry Smith 
184647c6ae99SBarry Smith   Output Parameter:
18470298fd71SBarry Smith . dmf - the refined DM, or NULL
1848ae0a1c52SMatthew G Knepley 
18490298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
185047c6ae99SBarry Smith 
185147c6ae99SBarry Smith   Level: developer
185247c6ae99SBarry Smith 
1853e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
185447c6ae99SBarry Smith @*/
18557087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
185647c6ae99SBarry Smith {
185747c6ae99SBarry Smith   PetscErrorCode   ierr;
1858c833c3b5SJed Brown   DMRefineHookLink link;
185947c6ae99SBarry Smith 
186047c6ae99SBarry Smith   PetscFunctionBegin;
1861732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18621ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1863fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
186447c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18654057135bSMatthew G Knepley   if (*dmf) {
186643842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18678865f1eaSKarl Rupp 
18688cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18698865f1eaSKarl Rupp 
1870644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18710598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1872656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18738865f1eaSKarl Rupp 
1874e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1875c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18768865f1eaSKarl Rupp       if (link->refinehook) {
18778865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18788865f1eaSKarl Rupp       }
1879c833c3b5SJed Brown     }
1880c833c3b5SJed Brown   }
18811ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1882c833c3b5SJed Brown   PetscFunctionReturn(0);
1883c833c3b5SJed Brown }
1884c833c3b5SJed Brown 
1885bb9467b5SJed Brown /*@C
1886c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1887c833c3b5SJed Brown 
1888c833c3b5SJed Brown    Logically Collective
1889c833c3b5SJed Brown 
1890c833c3b5SJed Brown    Input Arguments:
1891c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1892c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1893c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18940298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1895c833c3b5SJed Brown 
1896c833c3b5SJed Brown    Calling sequence of refinehook:
1897c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1898c833c3b5SJed Brown 
1899c833c3b5SJed Brown +  coarse - coarse level DM
1900c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1901c833c3b5SJed Brown -  ctx - optional user-defined function context
1902c833c3b5SJed Brown 
1903c833c3b5SJed Brown    Calling sequence for interphook:
1904c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1905c833c3b5SJed Brown 
1906c833c3b5SJed Brown +  coarse - coarse level DM
1907c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1908c833c3b5SJed Brown .  fine - fine level DM to update
1909c833c3b5SJed Brown -  ctx - optional user-defined function context
1910c833c3b5SJed Brown 
1911c833c3b5SJed Brown    Level: advanced
1912c833c3b5SJed Brown 
1913c833c3b5SJed Brown    Notes:
1914c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1915c833c3b5SJed Brown 
1916c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1917c833c3b5SJed Brown 
1918bb9467b5SJed Brown    This function is currently not available from Fortran.
1919bb9467b5SJed Brown 
1920c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1921c833c3b5SJed Brown @*/
1922c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1923c833c3b5SJed Brown {
1924c833c3b5SJed Brown   PetscErrorCode   ierr;
1925c833c3b5SJed Brown   DMRefineHookLink link,*p;
1926c833c3b5SJed Brown 
1927c833c3b5SJed Brown   PetscFunctionBegin;
1928c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19293d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19303d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19313d8e3701SJed Brown   }
193295dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1933c833c3b5SJed Brown   link->refinehook = refinehook;
1934c833c3b5SJed Brown   link->interphook = interphook;
1935c833c3b5SJed Brown   link->ctx        = ctx;
19360298fd71SBarry Smith   link->next       = NULL;
1937c833c3b5SJed Brown   *p               = link;
1938c833c3b5SJed Brown   PetscFunctionReturn(0);
1939c833c3b5SJed Brown }
1940c833c3b5SJed Brown 
19413d8e3701SJed Brown /*@C
19423d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19433d8e3701SJed Brown 
19443d8e3701SJed Brown    Logically Collective
19453d8e3701SJed Brown 
19463d8e3701SJed Brown    Input Arguments:
19473d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19483d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19493d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19503d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19513d8e3701SJed Brown 
19523d8e3701SJed Brown    Level: advanced
19533d8e3701SJed Brown 
19543d8e3701SJed Brown    Notes:
19553d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19563d8e3701SJed Brown 
19573d8e3701SJed Brown    This function is currently not available from Fortran.
19583d8e3701SJed Brown 
19593d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19603d8e3701SJed Brown @*/
19613d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19623d8e3701SJed Brown {
19633d8e3701SJed Brown   PetscErrorCode   ierr;
19643d8e3701SJed Brown   DMRefineHookLink link,*p;
19653d8e3701SJed Brown 
19663d8e3701SJed Brown   PetscFunctionBegin;
19673d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19683d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19693d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19703d8e3701SJed Brown       link = *p;
19713d8e3701SJed Brown       *p = link->next;
19723d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19733d8e3701SJed Brown       break;
19743d8e3701SJed Brown     }
19753d8e3701SJed Brown   }
19763d8e3701SJed Brown   PetscFunctionReturn(0);
19773d8e3701SJed Brown }
19783d8e3701SJed Brown 
1979c833c3b5SJed Brown /*@
1980c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1981c833c3b5SJed Brown 
1982c833c3b5SJed Brown    Collective if any hooks are
1983c833c3b5SJed Brown 
1984c833c3b5SJed Brown    Input Arguments:
1985c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1986e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1987c833c3b5SJed Brown -  fine - finer DM to update
1988c833c3b5SJed Brown 
1989c833c3b5SJed Brown    Level: developer
1990c833c3b5SJed Brown 
1991c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1992c833c3b5SJed Brown @*/
1993c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1994c833c3b5SJed Brown {
1995c833c3b5SJed Brown   PetscErrorCode   ierr;
1996c833c3b5SJed Brown   DMRefineHookLink link;
1997c833c3b5SJed Brown 
1998c833c3b5SJed Brown   PetscFunctionBegin;
1999c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20008865f1eaSKarl Rupp     if (link->interphook) {
20018865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20028865f1eaSKarl Rupp     }
20034057135bSMatthew G Knepley   }
200447c6ae99SBarry Smith   PetscFunctionReturn(0);
200547c6ae99SBarry Smith }
200647c6ae99SBarry Smith 
2007eb3f98d2SBarry Smith /*@
2008eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2009eb3f98d2SBarry Smith 
2010eb3f98d2SBarry Smith     Not Collective
2011eb3f98d2SBarry Smith 
2012eb3f98d2SBarry Smith     Input Parameter:
2013eb3f98d2SBarry Smith .   dm - the DM object
2014eb3f98d2SBarry Smith 
2015eb3f98d2SBarry Smith     Output Parameter:
2016eb3f98d2SBarry Smith .   level - number of refinements
2017eb3f98d2SBarry Smith 
2018eb3f98d2SBarry Smith     Level: developer
2019eb3f98d2SBarry Smith 
20206a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2021eb3f98d2SBarry Smith 
2022eb3f98d2SBarry Smith @*/
2023eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2024eb3f98d2SBarry Smith {
2025eb3f98d2SBarry Smith   PetscFunctionBegin;
2026eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2027eb3f98d2SBarry Smith   *level = dm->levelup;
2028eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2029eb3f98d2SBarry Smith }
2030eb3f98d2SBarry Smith 
2031fef3a512SBarry Smith /*@
2032fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2033fef3a512SBarry Smith 
2034fef3a512SBarry Smith     Not Collective
2035fef3a512SBarry Smith 
2036fef3a512SBarry Smith     Input Parameter:
2037fef3a512SBarry Smith +   dm - the DM object
2038fef3a512SBarry Smith -   level - number of refinements
2039fef3a512SBarry Smith 
2040fef3a512SBarry Smith     Level: advanced
2041fef3a512SBarry Smith 
204295452b02SPatrick Sanan     Notes:
204395452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2044fef3a512SBarry Smith 
2045fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2046fef3a512SBarry Smith 
2047fef3a512SBarry Smith @*/
2048fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2049fef3a512SBarry Smith {
2050fef3a512SBarry Smith   PetscFunctionBegin;
2051fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2052fef3a512SBarry Smith   dm->levelup = level;
2053fef3a512SBarry Smith   PetscFunctionReturn(0);
2054fef3a512SBarry Smith }
2055fef3a512SBarry Smith 
2056bb9467b5SJed Brown /*@C
2057baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2058baf369e7SPeter Brune 
2059baf369e7SPeter Brune    Logically Collective
2060baf369e7SPeter Brune 
2061baf369e7SPeter Brune    Input Arguments:
2062baf369e7SPeter Brune +  dm - the DM
2063baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2064baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20650298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2066baf369e7SPeter Brune 
2067baf369e7SPeter Brune    Calling sequence for beginhook:
2068baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2069baf369e7SPeter Brune 
2070baf369e7SPeter Brune +  dm - global DM
2071baf369e7SPeter Brune .  g - global vector
2072baf369e7SPeter Brune .  mode - mode
2073baf369e7SPeter Brune .  l - local vector
2074baf369e7SPeter Brune -  ctx - optional user-defined function context
2075baf369e7SPeter Brune 
2076baf369e7SPeter Brune 
2077baf369e7SPeter Brune    Calling sequence for endhook:
2078ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2079baf369e7SPeter Brune 
2080baf369e7SPeter Brune +  global - global DM
2081baf369e7SPeter Brune -  ctx - optional user-defined function context
2082baf369e7SPeter Brune 
2083baf369e7SPeter Brune    Level: advanced
2084baf369e7SPeter Brune 
2085baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2086baf369e7SPeter Brune @*/
2087baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2088baf369e7SPeter Brune {
2089baf369e7SPeter Brune   PetscErrorCode          ierr;
2090baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2091baf369e7SPeter Brune 
2092baf369e7SPeter Brune   PetscFunctionBegin;
2093baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2094baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
209595dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2096baf369e7SPeter Brune   link->beginhook = beginhook;
2097baf369e7SPeter Brune   link->endhook   = endhook;
2098baf369e7SPeter Brune   link->ctx       = ctx;
20990298fd71SBarry Smith   link->next      = NULL;
2100baf369e7SPeter Brune   *p              = link;
2101baf369e7SPeter Brune   PetscFunctionReturn(0);
2102baf369e7SPeter Brune }
2103baf369e7SPeter Brune 
21044c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
21054c274da1SToby Isaac {
21064c274da1SToby Isaac   Mat cMat;
21074c274da1SToby Isaac   Vec cVec;
21084c274da1SToby Isaac   PetscSection section, cSec;
21094c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
21104c274da1SToby Isaac   PetscErrorCode ierr;
21114c274da1SToby Isaac 
21124c274da1SToby Isaac   PetscFunctionBegin;
21134c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
21144c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
21154c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
21165db9a05bSToby Isaac     PetscInt nRows;
21175db9a05bSToby Isaac 
21185db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
21195db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2120e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
21217711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
21224c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
21234c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
21244c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
21254c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
21264c274da1SToby Isaac       if (dof) {
21274c274da1SToby Isaac         PetscScalar *vals;
21284c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
21294c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
21304c274da1SToby Isaac       }
21314c274da1SToby Isaac     }
21324c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
21334c274da1SToby Isaac   }
21344c274da1SToby Isaac   PetscFunctionReturn(0);
21354c274da1SToby Isaac }
21364c274da1SToby Isaac 
213747c6ae99SBarry Smith /*@
213801729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
213901729b5cSPatrick Sanan 
214001729b5cSPatrick Sanan     Neighbor-wise Collective on DM
214101729b5cSPatrick Sanan 
214201729b5cSPatrick Sanan     Input Parameters:
214301729b5cSPatrick Sanan +   dm - the DM object
214401729b5cSPatrick Sanan .   g - the global vector
214501729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
214601729b5cSPatrick Sanan -   l - the local vector
214701729b5cSPatrick Sanan 
214801729b5cSPatrick Sanan     Notes:
214901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
215001729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
215101729b5cSPatrick Sanan 
215201729b5cSPatrick Sanan     Level: beginner
215301729b5cSPatrick Sanan 
215401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
215501729b5cSPatrick Sanan 
215601729b5cSPatrick Sanan @*/
215701729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
215801729b5cSPatrick Sanan {
215901729b5cSPatrick Sanan   PetscErrorCode ierr;
216001729b5cSPatrick Sanan 
216101729b5cSPatrick Sanan   PetscFunctionBegin;
216201729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
216301729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
216401729b5cSPatrick Sanan   PetscFunctionReturn(0);
216501729b5cSPatrick Sanan }
216601729b5cSPatrick Sanan 
216701729b5cSPatrick Sanan /*@
216847c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
216947c6ae99SBarry Smith 
217047c6ae99SBarry Smith     Neighbor-wise Collective on DM
217147c6ae99SBarry Smith 
217247c6ae99SBarry Smith     Input Parameters:
217347c6ae99SBarry Smith +   dm - the DM object
217447c6ae99SBarry Smith .   g - the global vector
217547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
217647c6ae99SBarry Smith -   l - the local vector
217747c6ae99SBarry Smith 
217801729b5cSPatrick Sanan     Level: intermediate
217947c6ae99SBarry Smith 
218001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
218147c6ae99SBarry Smith 
218247c6ae99SBarry Smith @*/
21837087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
218447c6ae99SBarry Smith {
21857128ae9fSMatthew G Knepley   PetscSF                 sf;
218647c6ae99SBarry Smith   PetscErrorCode          ierr;
2187baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
218847c6ae99SBarry Smith 
218947c6ae99SBarry Smith   PetscFunctionBegin;
2190171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2191baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
21928865f1eaSKarl Rupp     if (link->beginhook) {
21938865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
21948865f1eaSKarl Rupp     }
2195baf369e7SPeter Brune   }
21967128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21977128ae9fSMatthew G Knepley   if (sf) {
2198ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2199ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
22007128ae9fSMatthew G Knepley 
220182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22027128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2203ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22047128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22057128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2206ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22077128ae9fSMatthew G Knepley   } else {
220833907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2209843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22107128ae9fSMatthew G Knepley   }
221147c6ae99SBarry Smith   PetscFunctionReturn(0);
221247c6ae99SBarry Smith }
221347c6ae99SBarry Smith 
221447c6ae99SBarry Smith /*@
221547c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
221647c6ae99SBarry Smith 
221747c6ae99SBarry Smith     Neighbor-wise Collective on DM
221847c6ae99SBarry Smith 
221947c6ae99SBarry Smith     Input Parameters:
222047c6ae99SBarry Smith +   dm - the DM object
222147c6ae99SBarry Smith .   g - the global vector
222247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
222347c6ae99SBarry Smith -   l - the local vector
222447c6ae99SBarry Smith 
222501729b5cSPatrick Sanan     Level: intermediate
222647c6ae99SBarry Smith 
222701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
222847c6ae99SBarry Smith 
222947c6ae99SBarry Smith @*/
22307087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
223147c6ae99SBarry Smith {
22327128ae9fSMatthew G Knepley   PetscSF                 sf;
223347c6ae99SBarry Smith   PetscErrorCode          ierr;
2234ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2235ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2236baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
223747c6ae99SBarry Smith 
223847c6ae99SBarry Smith   PetscFunctionBegin;
2239171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
22407128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
22417128ae9fSMatthew G Knepley   if (sf) {
224282f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
22437128ae9fSMatthew G Knepley 
22447128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2245ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
22467128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
22477128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2248ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
22497128ae9fSMatthew G Knepley   } else {
225033907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2251843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
22527128ae9fSMatthew G Knepley   }
22534c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2254baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2255baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2256baf369e7SPeter Brune   }
225747c6ae99SBarry Smith   PetscFunctionReturn(0);
225847c6ae99SBarry Smith }
225947c6ae99SBarry Smith 
2260d4d07f1eSToby Isaac /*@C
2261d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2262d4d07f1eSToby Isaac 
2263d4d07f1eSToby Isaac    Logically Collective
2264d4d07f1eSToby Isaac 
2265d4d07f1eSToby Isaac    Input Arguments:
2266d4d07f1eSToby Isaac +  dm - the DM
2267d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2268d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2269d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2270d4d07f1eSToby Isaac 
2271d4d07f1eSToby Isaac    Calling sequence for beginhook:
2272d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2273d4d07f1eSToby Isaac 
2274d4d07f1eSToby Isaac +  dm - global DM
2275d4d07f1eSToby Isaac .  l - local vector
2276d4d07f1eSToby Isaac .  mode - mode
2277d4d07f1eSToby Isaac .  g - global vector
2278d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2279d4d07f1eSToby Isaac 
2280d4d07f1eSToby Isaac 
2281d4d07f1eSToby Isaac    Calling sequence for endhook:
2282d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2283d4d07f1eSToby Isaac 
2284d4d07f1eSToby Isaac +  global - global DM
2285d4d07f1eSToby Isaac .  l - local vector
2286d4d07f1eSToby Isaac .  mode - mode
2287d4d07f1eSToby Isaac .  g - global vector
2288d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2289d4d07f1eSToby Isaac 
2290d4d07f1eSToby Isaac    Level: advanced
2291d4d07f1eSToby Isaac 
2292d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2293d4d07f1eSToby Isaac @*/
2294d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2295d4d07f1eSToby Isaac {
2296d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2297d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2298d4d07f1eSToby Isaac 
2299d4d07f1eSToby Isaac   PetscFunctionBegin;
2300d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2301d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
230295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2303d4d07f1eSToby Isaac   link->beginhook = beginhook;
2304d4d07f1eSToby Isaac   link->endhook   = endhook;
2305d4d07f1eSToby Isaac   link->ctx       = ctx;
2306d4d07f1eSToby Isaac   link->next      = NULL;
2307d4d07f1eSToby Isaac   *p              = link;
2308d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2309d4d07f1eSToby Isaac }
2310d4d07f1eSToby Isaac 
23114c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
23124c274da1SToby Isaac {
23134c274da1SToby Isaac   Mat cMat;
23144c274da1SToby Isaac   Vec cVec;
23154c274da1SToby Isaac   PetscSection section, cSec;
23164c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
23174c274da1SToby Isaac   PetscErrorCode ierr;
23184c274da1SToby Isaac 
23194c274da1SToby Isaac   PetscFunctionBegin;
23204c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
23214c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
23224c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
23235db9a05bSToby Isaac     PetscInt nRows;
23245db9a05bSToby Isaac 
23255db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
23265db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2327e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
23287711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
23294c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
23304c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
23314c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
23324c274da1SToby Isaac       if (dof) {
23334c274da1SToby Isaac         PetscInt d;
23344c274da1SToby Isaac         PetscScalar *vals;
23354c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
23364c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
23374c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
23384c274da1SToby Isaac          * we just extracted */
23394c274da1SToby Isaac         for (d = 0; d < dof; d++) {
23404c274da1SToby Isaac           vals[d] = 0.;
23414c274da1SToby Isaac         }
23424c274da1SToby Isaac       }
23434c274da1SToby Isaac     }
23444c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
23454c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
23464c274da1SToby Isaac   }
23474c274da1SToby Isaac   PetscFunctionReturn(0);
23484c274da1SToby Isaac }
234901729b5cSPatrick Sanan /*@
235001729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
235101729b5cSPatrick Sanan 
235201729b5cSPatrick Sanan     Neighbor-wise Collective on DM
235301729b5cSPatrick Sanan 
235401729b5cSPatrick Sanan     Input Parameters:
235501729b5cSPatrick Sanan +   dm - the DM object
235601729b5cSPatrick Sanan .   l - the local vector
235701729b5cSPatrick Sanan .   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.
235801729b5cSPatrick Sanan -   g - the global vector
235901729b5cSPatrick Sanan 
236001729b5cSPatrick Sanan     Notes:
236101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
236201729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
236301729b5cSPatrick Sanan 
236401729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
236501729b5cSPatrick Sanan            INSERT_VALUES is not supported for DMDA; in that case simply compute the values directly into a global vector instead of a local one.
236601729b5cSPatrick Sanan 
236701729b5cSPatrick Sanan     Level: beginner
236801729b5cSPatrick Sanan 
236901729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
237001729b5cSPatrick Sanan 
237101729b5cSPatrick Sanan @*/
237201729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
237301729b5cSPatrick Sanan {
237401729b5cSPatrick Sanan   PetscErrorCode ierr;
237501729b5cSPatrick Sanan 
237601729b5cSPatrick Sanan   PetscFunctionBegin;
237701729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
237801729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
237901729b5cSPatrick Sanan   PetscFunctionReturn(0);
238001729b5cSPatrick Sanan }
23814c274da1SToby Isaac 
238247c6ae99SBarry Smith /*@
238301729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
23849a42bb27SBarry Smith 
23859a42bb27SBarry Smith     Neighbor-wise Collective on DM
23869a42bb27SBarry Smith 
23879a42bb27SBarry Smith     Input Parameters:
23889a42bb27SBarry Smith +   dm - the DM object
2389f6813fd5SJed Brown .   l - the local vector
23901eb28f2eSBarry 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.
23911eb28f2eSBarry Smith -   g - the global vector
23929a42bb27SBarry Smith 
239395452b02SPatrick Sanan     Notes:
239495452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
239584330215SMatthew 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.
23969a42bb27SBarry Smith 
239701729b5cSPatrick Sanan     Level: intermediate
23989a42bb27SBarry Smith 
239901729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
24009a42bb27SBarry Smith 
24019a42bb27SBarry Smith @*/
24027087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
24039a42bb27SBarry Smith {
24047128ae9fSMatthew G Knepley   PetscSF                 sf;
240584330215SMatthew G. Knepley   PetscSection            s, gs;
2406d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2407ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2408ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
240984330215SMatthew G. Knepley   PetscBool               isInsert;
241084330215SMatthew G. Knepley   PetscErrorCode          ierr;
24119a42bb27SBarry Smith 
24129a42bb27SBarry Smith   PetscFunctionBegin;
2413171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2414d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2415d4d07f1eSToby Isaac     if (link->beginhook) {
2416d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2417d4d07f1eSToby Isaac     }
2418d4d07f1eSToby Isaac   }
24194c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
24207128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2421e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
24227128ae9fSMatthew G Knepley   switch (mode) {
24237128ae9fSMatthew G Knepley   case INSERT_VALUES:
24247128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2425304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
242684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
24277128ae9fSMatthew G Knepley   case ADD_VALUES:
24287128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2429304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
243084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
24317128ae9fSMatthew G Knepley   default:
243282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24337128ae9fSMatthew G Knepley   }
243484330215SMatthew G. Knepley   if (sf && !isInsert) {
2435ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24367128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2437a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2438ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
243984330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
244084330215SMatthew G. Knepley   } else if (s && isInsert) {
244184330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
244284330215SMatthew G. Knepley 
2443e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
244484330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
244584330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2446ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
244784330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
244884330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2449b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
245084330215SMatthew G. Knepley 
245184330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
245203442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
245384330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2454b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
245584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
245684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2457b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
245803442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2459b3b16f48SMatthew 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);
2460b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2461b3b16f48SMatthew G. Knepley       if (!gcdof) {
246284330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2463b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2464b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
246584330215SMatthew G. Knepley         const PetscInt *cdofs;
246684330215SMatthew G. Knepley         PetscInt        cind = 0;
246784330215SMatthew G. Knepley 
246884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2469b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
247084330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2471b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
247284330215SMatthew G. Knepley         }
2473b3b16f48SMatthew 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);
247484330215SMatthew G. Knepley     }
2475ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24767128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
24777128ae9fSMatthew G Knepley   } else {
2478843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24797128ae9fSMatthew G Knepley   }
24809a42bb27SBarry Smith   PetscFunctionReturn(0);
24819a42bb27SBarry Smith }
24829a42bb27SBarry Smith 
24839a42bb27SBarry Smith /*@
24849a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
248547c6ae99SBarry Smith 
248647c6ae99SBarry Smith     Neighbor-wise Collective on DM
248747c6ae99SBarry Smith 
248847c6ae99SBarry Smith     Input Parameters:
248947c6ae99SBarry Smith +   dm - the DM object
2490f6813fd5SJed Brown .   l - the local vector
249147c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2492f6813fd5SJed Brown -   g - the global vector
249347c6ae99SBarry Smith 
249401729b5cSPatrick Sanan     Level: intermediate
249547c6ae99SBarry Smith 
2496e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
249747c6ae99SBarry Smith 
249847c6ae99SBarry Smith @*/
24997087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
250047c6ae99SBarry Smith {
25017128ae9fSMatthew G Knepley   PetscSF                 sf;
250284330215SMatthew G. Knepley   PetscSection            s;
2503d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
250484330215SMatthew G. Knepley   PetscBool               isInsert;
250584330215SMatthew G. Knepley   PetscErrorCode          ierr;
250647c6ae99SBarry Smith 
250747c6ae99SBarry Smith   PetscFunctionBegin;
2508171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25097128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2510e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
25117128ae9fSMatthew G Knepley   switch (mode) {
25127128ae9fSMatthew G Knepley   case INSERT_VALUES:
25137128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
251484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25157128ae9fSMatthew G Knepley   case ADD_VALUES:
25167128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
251784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25187128ae9fSMatthew G Knepley   default:
251982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25207128ae9fSMatthew G Knepley   }
252184330215SMatthew G. Knepley   if (sf && !isInsert) {
2522ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2523ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
252484330215SMatthew G. Knepley 
2525ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
25267128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2527a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2528ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
25297128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
253084330215SMatthew G. Knepley   } else if (s && isInsert) {
25317128ae9fSMatthew G Knepley   } else {
2532843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
25337128ae9fSMatthew G Knepley   }
2534d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2535d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2536d4d07f1eSToby Isaac   }
253747c6ae99SBarry Smith   PetscFunctionReturn(0);
253847c6ae99SBarry Smith }
253947c6ae99SBarry Smith 
2540f089877aSRichard Tran Mills /*@
2541bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2542bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2543d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2544f089877aSRichard Tran Mills 
2545bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2546f089877aSRichard Tran Mills 
2547f089877aSRichard Tran Mills    Input Parameters:
2548f089877aSRichard Tran Mills +  dm - the DM object
2549bc0a1609SRichard Tran Mills .  g - the original local vector
2550bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2551f089877aSRichard Tran Mills 
2552bc0a1609SRichard Tran Mills    Output Parameter:
2553bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2554f089877aSRichard Tran Mills 
2555f089877aSRichard Tran Mills    Level: intermediate
2556f089877aSRichard Tran Mills 
2557bc0a1609SRichard Tran Mills    Notes:
2558bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2559bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2560bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2561bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2562bc0a1609SRichard Tran Mills 
2563bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2564bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2565f089877aSRichard Tran Mills 
2566f089877aSRichard Tran Mills @*/
2567f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2568f089877aSRichard Tran Mills {
2569f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2570f089877aSRichard Tran Mills 
2571f089877aSRichard Tran Mills   PetscFunctionBegin;
2572f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2573bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2574f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2575f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2576f089877aSRichard Tran Mills }
2577f089877aSRichard Tran Mills 
2578f089877aSRichard Tran Mills /*@
2579bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2580bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2581d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2582f089877aSRichard Tran Mills 
2583bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2584f089877aSRichard Tran Mills 
2585f089877aSRichard Tran Mills    Input Parameters:
2586bc0a1609SRichard Tran Mills +  da - the DM object
2587bc0a1609SRichard Tran Mills .  g - the original local vector
2588bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2589f089877aSRichard Tran Mills 
2590bc0a1609SRichard Tran Mills    Output Parameter:
2591bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2592f089877aSRichard Tran Mills 
2593f089877aSRichard Tran Mills    Level: intermediate
2594f089877aSRichard Tran Mills 
2595bc0a1609SRichard Tran Mills    Notes:
2596bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2597bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2598bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2599bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2600bc0a1609SRichard Tran Mills 
2601bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2602bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2603f089877aSRichard Tran Mills 
2604f089877aSRichard Tran Mills @*/
2605f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2606f089877aSRichard Tran Mills {
2607f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2608f089877aSRichard Tran Mills 
2609f089877aSRichard Tran Mills   PetscFunctionBegin;
2610f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2611bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2612f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2613f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2614f089877aSRichard Tran Mills }
2615f089877aSRichard Tran Mills 
2616f089877aSRichard Tran Mills 
261747c6ae99SBarry Smith /*@
261847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
261947c6ae99SBarry Smith 
262047c6ae99SBarry Smith     Collective on DM
262147c6ae99SBarry Smith 
262247c6ae99SBarry Smith     Input Parameter:
262347c6ae99SBarry Smith +   dm - the DM object
262491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
262547c6ae99SBarry Smith 
262647c6ae99SBarry Smith     Output Parameter:
262747c6ae99SBarry Smith .   dmc - the coarsened DM
262847c6ae99SBarry Smith 
262947c6ae99SBarry Smith     Level: developer
263047c6ae99SBarry Smith 
2631e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
263247c6ae99SBarry Smith 
263347c6ae99SBarry Smith @*/
26347087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
263547c6ae99SBarry Smith {
263647c6ae99SBarry Smith   PetscErrorCode    ierr;
2637b17ce1afSJed Brown   DMCoarsenHookLink link;
263847c6ae99SBarry Smith 
263947c6ae99SBarry Smith   PetscFunctionBegin;
2640171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2641fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
264247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
264347c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
26448892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2645a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
264643842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
26478cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2648644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
26490598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2650656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2651e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2652b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2653b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2654b17ce1afSJed Brown   }
265547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2656b17ce1afSJed Brown   PetscFunctionReturn(0);
2657b17ce1afSJed Brown }
2658b17ce1afSJed Brown 
2659bb9467b5SJed Brown /*@C
2660b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2661b17ce1afSJed Brown 
2662b17ce1afSJed Brown    Logically Collective
2663b17ce1afSJed Brown 
2664b17ce1afSJed Brown    Input Arguments:
2665b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2666b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2667b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
26680298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2669b17ce1afSJed Brown 
2670b17ce1afSJed Brown    Calling sequence of coarsenhook:
2671b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2672b17ce1afSJed Brown 
2673b17ce1afSJed Brown +  fine - fine level DM
2674b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2675b17ce1afSJed Brown -  ctx - optional user-defined function context
2676b17ce1afSJed Brown 
2677b17ce1afSJed Brown    Calling sequence for restricthook:
2678c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2679b17ce1afSJed Brown 
2680b17ce1afSJed Brown +  fine - fine level DM
2681b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2682c833c3b5SJed Brown .  rscale - scaling vector for restriction
2683c833c3b5SJed Brown .  inject - matrix restricting by injection
2684b17ce1afSJed Brown .  coarse - coarse level DM to update
2685b17ce1afSJed Brown -  ctx - optional user-defined function context
2686b17ce1afSJed Brown 
2687b17ce1afSJed Brown    Level: advanced
2688b17ce1afSJed Brown 
2689b17ce1afSJed Brown    Notes:
2690b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2691b17ce1afSJed Brown 
2692b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2693b17ce1afSJed Brown 
2694b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2695b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2696b17ce1afSJed Brown 
2697bb9467b5SJed Brown    This function is currently not available from Fortran.
2698bb9467b5SJed Brown 
2699dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2700b17ce1afSJed Brown @*/
2701b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2702b17ce1afSJed Brown {
2703b17ce1afSJed Brown   PetscErrorCode    ierr;
2704b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2705b17ce1afSJed Brown 
2706b17ce1afSJed Brown   PetscFunctionBegin;
2707b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
27081e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
27091e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
27101e3d8eccSJed Brown   }
271195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2712b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2713b17ce1afSJed Brown   link->restricthook = restricthook;
2714b17ce1afSJed Brown   link->ctx          = ctx;
27150298fd71SBarry Smith   link->next         = NULL;
2716b17ce1afSJed Brown   *p                 = link;
2717b17ce1afSJed Brown   PetscFunctionReturn(0);
2718b17ce1afSJed Brown }
2719b17ce1afSJed Brown 
2720dc822a44SJed Brown /*@C
2721dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2722dc822a44SJed Brown 
2723dc822a44SJed Brown    Logically Collective
2724dc822a44SJed Brown 
2725dc822a44SJed Brown    Input Arguments:
2726dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2727dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2728dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2729dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2730dc822a44SJed Brown 
2731dc822a44SJed Brown    Level: advanced
2732dc822a44SJed Brown 
2733dc822a44SJed Brown    Notes:
2734dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2735dc822a44SJed Brown 
2736dc822a44SJed Brown    This function is currently not available from Fortran.
2737dc822a44SJed Brown 
2738dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2739dc822a44SJed Brown @*/
2740dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2741dc822a44SJed Brown {
2742dc822a44SJed Brown   PetscErrorCode    ierr;
2743dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2744dc822a44SJed Brown 
2745dc822a44SJed Brown   PetscFunctionBegin;
2746dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2747dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2748dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2749dc822a44SJed Brown       link = *p;
2750dc822a44SJed Brown       *p = link->next;
2751dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2752dc822a44SJed Brown       break;
2753dc822a44SJed Brown     }
2754dc822a44SJed Brown   }
2755dc822a44SJed Brown   PetscFunctionReturn(0);
2756dc822a44SJed Brown }
2757dc822a44SJed Brown 
2758dc822a44SJed Brown 
2759b17ce1afSJed Brown /*@
2760b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2761b17ce1afSJed Brown 
2762b17ce1afSJed Brown    Collective if any hooks are
2763b17ce1afSJed Brown 
2764b17ce1afSJed Brown    Input Arguments:
2765b17ce1afSJed Brown +  fine - finer DM to use as a base
2766b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2767e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2768b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2769e91eccc2SStefano Zampini -  coarse - coarser DM to update
2770b17ce1afSJed Brown 
2771b17ce1afSJed Brown    Level: developer
2772b17ce1afSJed Brown 
2773b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2774b17ce1afSJed Brown @*/
2775b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2776b17ce1afSJed Brown {
2777b17ce1afSJed Brown   PetscErrorCode    ierr;
2778b17ce1afSJed Brown   DMCoarsenHookLink link;
2779b17ce1afSJed Brown 
2780b17ce1afSJed Brown   PetscFunctionBegin;
2781b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
27828865f1eaSKarl Rupp     if (link->restricthook) {
27838865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
27848865f1eaSKarl Rupp     }
2785b17ce1afSJed Brown   }
278647c6ae99SBarry Smith   PetscFunctionReturn(0);
278747c6ae99SBarry Smith }
278847c6ae99SBarry Smith 
2789bb9467b5SJed Brown /*@C
2790be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
27915dbd56e3SPeter Brune 
27925dbd56e3SPeter Brune    Logically Collective
27935dbd56e3SPeter Brune 
27945dbd56e3SPeter Brune    Input Arguments:
27955dbd56e3SPeter Brune +  global - global DM
2796ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
27975dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
27980298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
27995dbd56e3SPeter Brune 
2800ec4806b8SPeter Brune 
2801ec4806b8SPeter Brune    Calling sequence for ddhook:
2802ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2803ec4806b8SPeter Brune 
2804ec4806b8SPeter Brune +  global - global DM
2805ec4806b8SPeter Brune .  block  - block DM
2806ec4806b8SPeter Brune -  ctx - optional user-defined function context
2807ec4806b8SPeter Brune 
28085dbd56e3SPeter Brune    Calling sequence for restricthook:
2809ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
28105dbd56e3SPeter Brune 
28115dbd56e3SPeter Brune +  global - global DM
28125dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
28135dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2814ec4806b8SPeter Brune .  block  - block DM
28155dbd56e3SPeter Brune -  ctx - optional user-defined function context
28165dbd56e3SPeter Brune 
28175dbd56e3SPeter Brune    Level: advanced
28185dbd56e3SPeter Brune 
28195dbd56e3SPeter Brune    Notes:
2820ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
28215dbd56e3SPeter Brune 
28225dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
28235dbd56e3SPeter Brune 
28245dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2825ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
28265dbd56e3SPeter Brune 
2827bb9467b5SJed Brown    This function is currently not available from Fortran.
2828bb9467b5SJed Brown 
28295dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
28305dbd56e3SPeter Brune @*/
2831be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
28325dbd56e3SPeter Brune {
28335dbd56e3SPeter Brune   PetscErrorCode      ierr;
2834be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
28355dbd56e3SPeter Brune 
28365dbd56e3SPeter Brune   PetscFunctionBegin;
28375dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2838b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2839b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2840b3a6b972SJed Brown   }
284195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
28425dbd56e3SPeter Brune   link->restricthook = restricthook;
2843be081cd6SPeter Brune   link->ddhook       = ddhook;
28445dbd56e3SPeter Brune   link->ctx          = ctx;
28450298fd71SBarry Smith   link->next         = NULL;
28465dbd56e3SPeter Brune   *p                 = link;
28475dbd56e3SPeter Brune   PetscFunctionReturn(0);
28485dbd56e3SPeter Brune }
28495dbd56e3SPeter Brune 
2850b3a6b972SJed Brown /*@C
2851b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2852b3a6b972SJed Brown 
2853b3a6b972SJed Brown    Logically Collective
2854b3a6b972SJed Brown 
2855b3a6b972SJed Brown    Input Arguments:
2856b3a6b972SJed Brown +  global - global DM
2857b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2858b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2859b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2860b3a6b972SJed Brown 
2861b3a6b972SJed Brown    Level: advanced
2862b3a6b972SJed Brown 
2863b3a6b972SJed Brown    Notes:
2864b3a6b972SJed Brown 
2865b3a6b972SJed Brown    This function is currently not available from Fortran.
2866b3a6b972SJed Brown 
2867b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2868b3a6b972SJed Brown @*/
2869b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2870b3a6b972SJed Brown {
2871b3a6b972SJed Brown   PetscErrorCode      ierr;
2872b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2873b3a6b972SJed Brown 
2874b3a6b972SJed Brown   PetscFunctionBegin;
2875b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2876b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2877b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2878b3a6b972SJed Brown       link = *p;
2879b3a6b972SJed Brown       *p = link->next;
2880b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2881b3a6b972SJed Brown       break;
2882b3a6b972SJed Brown     }
2883b3a6b972SJed Brown   }
2884b3a6b972SJed Brown   PetscFunctionReturn(0);
2885b3a6b972SJed Brown }
2886b3a6b972SJed Brown 
28875dbd56e3SPeter Brune /*@
2888be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
28895dbd56e3SPeter Brune 
28905dbd56e3SPeter Brune    Collective if any hooks are
28915dbd56e3SPeter Brune 
28925dbd56e3SPeter Brune    Input Arguments:
28935dbd56e3SPeter Brune +  fine - finer DM to use as a base
2894be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2895be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
28965dbd56e3SPeter Brune -  coarse - coarer DM to update
28975dbd56e3SPeter Brune 
28985dbd56e3SPeter Brune    Level: developer
28995dbd56e3SPeter Brune 
29005dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
29015dbd56e3SPeter Brune @*/
2902be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
29035dbd56e3SPeter Brune {
29045dbd56e3SPeter Brune   PetscErrorCode      ierr;
2905be081cd6SPeter Brune   DMSubDomainHookLink link;
29065dbd56e3SPeter Brune 
29075dbd56e3SPeter Brune   PetscFunctionBegin;
2908be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
29098865f1eaSKarl Rupp     if (link->restricthook) {
29108865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
29118865f1eaSKarl Rupp     }
29125dbd56e3SPeter Brune   }
29135dbd56e3SPeter Brune   PetscFunctionReturn(0);
29145dbd56e3SPeter Brune }
29155dbd56e3SPeter Brune 
29165fe1f584SPeter Brune /*@
29176a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
29185fe1f584SPeter Brune 
29195fe1f584SPeter Brune     Not Collective
29205fe1f584SPeter Brune 
29215fe1f584SPeter Brune     Input Parameter:
29225fe1f584SPeter Brune .   dm - the DM object
29235fe1f584SPeter Brune 
29245fe1f584SPeter Brune     Output Parameter:
29256a7d9d85SPeter Brune .   level - number of coarsenings
29265fe1f584SPeter Brune 
29275fe1f584SPeter Brune     Level: developer
29285fe1f584SPeter Brune 
29296a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
29305fe1f584SPeter Brune 
29315fe1f584SPeter Brune @*/
29325fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
29335fe1f584SPeter Brune {
29345fe1f584SPeter Brune   PetscFunctionBegin;
29355fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29365fe1f584SPeter Brune   *level = dm->leveldown;
29375fe1f584SPeter Brune   PetscFunctionReturn(0);
29385fe1f584SPeter Brune }
29395fe1f584SPeter Brune 
29405fe1f584SPeter Brune 
29415fe1f584SPeter Brune 
294247c6ae99SBarry Smith /*@C
294347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
294447c6ae99SBarry Smith 
294547c6ae99SBarry Smith     Collective on DM
294647c6ae99SBarry Smith 
294747c6ae99SBarry Smith     Input Parameter:
294847c6ae99SBarry Smith +   dm - the DM object
294947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
295047c6ae99SBarry Smith 
295147c6ae99SBarry Smith     Output Parameter:
295247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
295347c6ae99SBarry Smith 
295447c6ae99SBarry Smith     Level: developer
295547c6ae99SBarry Smith 
2956e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
295747c6ae99SBarry Smith 
295847c6ae99SBarry Smith @*/
29597087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
296047c6ae99SBarry Smith {
296147c6ae99SBarry Smith   PetscErrorCode ierr;
296247c6ae99SBarry Smith 
296347c6ae99SBarry Smith   PetscFunctionBegin;
2964171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2965ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
296647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
296747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
296847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
296947c6ae99SBarry Smith   } else if (dm->ops->refine) {
297047c6ae99SBarry Smith     PetscInt i;
297147c6ae99SBarry Smith 
2972ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
297347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2974ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
297547c6ae99SBarry Smith     }
2976ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
297747c6ae99SBarry Smith   PetscFunctionReturn(0);
297847c6ae99SBarry Smith }
297947c6ae99SBarry Smith 
298047c6ae99SBarry Smith /*@C
298147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
298247c6ae99SBarry Smith 
298347c6ae99SBarry Smith     Collective on DM
298447c6ae99SBarry Smith 
298547c6ae99SBarry Smith     Input Parameter:
298647c6ae99SBarry Smith +   dm - the DM object
298747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
298847c6ae99SBarry Smith 
298947c6ae99SBarry Smith     Output Parameter:
299047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
299147c6ae99SBarry Smith 
299247c6ae99SBarry Smith     Level: developer
299347c6ae99SBarry Smith 
2994e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
299547c6ae99SBarry Smith 
299647c6ae99SBarry Smith @*/
29977087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
299847c6ae99SBarry Smith {
299947c6ae99SBarry Smith   PetscErrorCode ierr;
300047c6ae99SBarry Smith 
300147c6ae99SBarry Smith   PetscFunctionBegin;
3002171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3003ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
300447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
300547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
300647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
300747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
300847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
300947c6ae99SBarry Smith     PetscInt i;
301047c6ae99SBarry Smith 
3011ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
301247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3013ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
301447c6ae99SBarry Smith     }
3015ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
301647c6ae99SBarry Smith   PetscFunctionReturn(0);
301747c6ae99SBarry Smith }
301847c6ae99SBarry Smith 
301947c6ae99SBarry Smith /*@
3020e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
302147c6ae99SBarry Smith    grids associated with two DMs.
302247c6ae99SBarry Smith 
302347c6ae99SBarry Smith    Collective on DM
302447c6ae99SBarry Smith 
302547c6ae99SBarry Smith    Input Parameters:
302647c6ae99SBarry Smith +  dmc - the coarse grid DM
302747c6ae99SBarry Smith -  dmf - the fine grid DM
302847c6ae99SBarry Smith 
302947c6ae99SBarry Smith    Output Parameters:
303047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
303147c6ae99SBarry Smith 
303247c6ae99SBarry Smith    Level: intermediate
303347c6ae99SBarry Smith 
303447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
303547c6ae99SBarry Smith 
3036e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
303747c6ae99SBarry Smith @*/
3038e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
303947c6ae99SBarry Smith {
304047c6ae99SBarry Smith   PetscErrorCode ierr;
304147c6ae99SBarry Smith 
304247c6ae99SBarry Smith   PetscFunctionBegin;
3043171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
3044171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
304547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
304647c6ae99SBarry Smith   PetscFunctionReturn(0);
304747c6ae99SBarry Smith }
304847c6ae99SBarry Smith 
30491a266240SBarry Smith /*@C
30501a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
30511a266240SBarry Smith 
30521a266240SBarry Smith     Not Collective
30531a266240SBarry Smith 
30541a266240SBarry Smith     Input Parameters:
30551a266240SBarry Smith +   dm - the DM object
30561a266240SBarry Smith -   destroy - the destroy function
30571a266240SBarry Smith 
30581a266240SBarry Smith     Level: intermediate
30591a266240SBarry Smith 
3060e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
30611a266240SBarry Smith 
3062f07f9ceaSJed Brown @*/
30631a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
30641a266240SBarry Smith {
30651a266240SBarry Smith   PetscFunctionBegin;
3066171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
30671a266240SBarry Smith   dm->ctxdestroy = destroy;
30681a266240SBarry Smith   PetscFunctionReturn(0);
30691a266240SBarry Smith }
30701a266240SBarry Smith 
3071b07ff414SBarry Smith /*@
30721b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
307347c6ae99SBarry Smith 
307447c6ae99SBarry Smith     Not Collective
307547c6ae99SBarry Smith 
307647c6ae99SBarry Smith     Input Parameters:
307747c6ae99SBarry Smith +   dm - the DM object
307847c6ae99SBarry Smith -   ctx - the user context
307947c6ae99SBarry Smith 
308047c6ae99SBarry Smith     Level: intermediate
308147c6ae99SBarry Smith 
3082e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
308347c6ae99SBarry Smith 
308447c6ae99SBarry Smith @*/
30851b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
308647c6ae99SBarry Smith {
308747c6ae99SBarry Smith   PetscFunctionBegin;
3088171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
308947c6ae99SBarry Smith   dm->ctx = ctx;
309047c6ae99SBarry Smith   PetscFunctionReturn(0);
309147c6ae99SBarry Smith }
309247c6ae99SBarry Smith 
309347c6ae99SBarry Smith /*@
30941b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
309547c6ae99SBarry Smith 
309647c6ae99SBarry Smith     Not Collective
309747c6ae99SBarry Smith 
309847c6ae99SBarry Smith     Input Parameter:
309947c6ae99SBarry Smith .   dm - the DM object
310047c6ae99SBarry Smith 
310147c6ae99SBarry Smith     Output Parameter:
310247c6ae99SBarry Smith .   ctx - the user context
310347c6ae99SBarry Smith 
310447c6ae99SBarry Smith     Level: intermediate
310547c6ae99SBarry Smith 
3106e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
310747c6ae99SBarry Smith 
310847c6ae99SBarry Smith @*/
31091b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
311047c6ae99SBarry Smith {
311147c6ae99SBarry Smith   PetscFunctionBegin;
3112171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31131b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
311447c6ae99SBarry Smith   PetscFunctionReturn(0);
311547c6ae99SBarry Smith }
311647c6ae99SBarry Smith 
311708da532bSDmitry Karpeev /*@C
3118df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
311908da532bSDmitry Karpeev 
312008da532bSDmitry Karpeev     Logically Collective on DM
312108da532bSDmitry Karpeev 
312208da532bSDmitry Karpeev     Input Parameter:
312308da532bSDmitry Karpeev +   dm - the DM object
31240298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
312508da532bSDmitry Karpeev 
312608da532bSDmitry Karpeev     Level: intermediate
312708da532bSDmitry Karpeev 
3128835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
312908da532bSDmitry Karpeev          DMSetJacobian()
313008da532bSDmitry Karpeev 
313108da532bSDmitry Karpeev @*/
313208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
313308da532bSDmitry Karpeev {
313408da532bSDmitry Karpeev   PetscFunctionBegin;
313508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
313608da532bSDmitry Karpeev   PetscFunctionReturn(0);
313708da532bSDmitry Karpeev }
313808da532bSDmitry Karpeev 
313908da532bSDmitry Karpeev /*@
314008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
314108da532bSDmitry Karpeev 
314208da532bSDmitry Karpeev     Not Collective
314308da532bSDmitry Karpeev 
314408da532bSDmitry Karpeev     Input Parameter:
314508da532bSDmitry Karpeev .   dm - the DM object to destroy
314608da532bSDmitry Karpeev 
314708da532bSDmitry Karpeev     Output Parameter:
314808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
314908da532bSDmitry Karpeev 
315008da532bSDmitry Karpeev     Level: developer
315108da532bSDmitry Karpeev 
315274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
315308da532bSDmitry Karpeev 
315408da532bSDmitry Karpeev @*/
315508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
315608da532bSDmitry Karpeev {
315708da532bSDmitry Karpeev   PetscFunctionBegin;
315808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
315908da532bSDmitry Karpeev   PetscFunctionReturn(0);
316008da532bSDmitry Karpeev }
316108da532bSDmitry Karpeev 
316208da532bSDmitry Karpeev /*@C
316308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
316408da532bSDmitry Karpeev 
316508da532bSDmitry Karpeev     Logically Collective on DM
316608da532bSDmitry Karpeev 
316708da532bSDmitry Karpeev     Input Parameters:
3168907376e6SBarry Smith .   dm - the DM object
316908da532bSDmitry Karpeev 
317008da532bSDmitry Karpeev     Output parameters:
317108da532bSDmitry Karpeev +   xl - lower bound
317208da532bSDmitry Karpeev -   xu - upper bound
317308da532bSDmitry Karpeev 
3174907376e6SBarry Smith     Level: advanced
3175907376e6SBarry Smith 
317695452b02SPatrick Sanan     Notes:
317795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
317808da532bSDmitry Karpeev 
317974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
318008da532bSDmitry Karpeev 
318108da532bSDmitry Karpeev @*/
318208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
318308da532bSDmitry Karpeev {
318408da532bSDmitry Karpeev   PetscErrorCode ierr;
31855fd66863SKarl Rupp 
318608da532bSDmitry Karpeev   PetscFunctionBegin;
318708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
318808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
318908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
319008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
31918865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
319208da532bSDmitry Karpeev   PetscFunctionReturn(0);
319308da532bSDmitry Karpeev }
319408da532bSDmitry Karpeev 
3195b0ae01b7SPeter Brune /*@
3196b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3197b0ae01b7SPeter Brune 
3198b0ae01b7SPeter Brune     Not Collective
3199b0ae01b7SPeter Brune 
3200b0ae01b7SPeter Brune     Input Parameter:
3201b0ae01b7SPeter Brune .   dm - the DM object
3202b0ae01b7SPeter Brune 
3203b0ae01b7SPeter Brune     Output Parameter:
3204b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3205b0ae01b7SPeter Brune 
3206b0ae01b7SPeter Brune     Level: developer
3207b0ae01b7SPeter Brune 
3208b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3209b0ae01b7SPeter Brune 
3210b0ae01b7SPeter Brune @*/
3211b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3212b0ae01b7SPeter Brune {
3213b0ae01b7SPeter Brune   PetscFunctionBegin;
3214b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3215b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3216b0ae01b7SPeter Brune }
3217b0ae01b7SPeter Brune 
32183ad4599aSBarry Smith /*@
32193ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
32203ad4599aSBarry Smith 
32213ad4599aSBarry Smith     Not Collective
32223ad4599aSBarry Smith 
32233ad4599aSBarry Smith     Input Parameter:
32243ad4599aSBarry Smith .   dm - the DM object
32253ad4599aSBarry Smith 
32263ad4599aSBarry Smith     Output Parameter:
32273ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
32283ad4599aSBarry Smith 
32293ad4599aSBarry Smith     Level: developer
32303ad4599aSBarry Smith 
32313ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
32323ad4599aSBarry Smith 
32333ad4599aSBarry Smith @*/
32343ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
32353ad4599aSBarry Smith {
32363ad4599aSBarry Smith   PetscFunctionBegin;
32373ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
32383ad4599aSBarry Smith   PetscFunctionReturn(0);
32393ad4599aSBarry Smith }
32403ad4599aSBarry Smith 
3241a7058e45SLawrence Mitchell 
3242a7058e45SLawrence Mitchell /*@
3243a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3244a7058e45SLawrence Mitchell 
3245a7058e45SLawrence Mitchell     Not Collective
3246a7058e45SLawrence Mitchell 
3247a7058e45SLawrence Mitchell     Input Parameter:
3248a7058e45SLawrence Mitchell .   dm - the DM object
3249a7058e45SLawrence Mitchell 
3250a7058e45SLawrence Mitchell     Output Parameter:
3251a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3252a7058e45SLawrence Mitchell 
3253a7058e45SLawrence Mitchell     Level: developer
3254a7058e45SLawrence Mitchell 
3255a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3256a7058e45SLawrence Mitchell 
3257a7058e45SLawrence Mitchell @*/
3258a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3259a7058e45SLawrence Mitchell {
32604a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3261a7058e45SLawrence Mitchell   PetscFunctionBegin;
32624a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
32634a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3264a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3265a7058e45SLawrence Mitchell }
3266a7058e45SLawrence Mitchell 
3267748fac09SDmitry Karpeev /*@C
326808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
326908da532bSDmitry Karpeev 
327008da532bSDmitry Karpeev     Collective on DM
327108da532bSDmitry Karpeev 
327208da532bSDmitry Karpeev     Input Parameter:
327308da532bSDmitry Karpeev +   dm - the DM object
32740298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
327508da532bSDmitry Karpeev 
327608da532bSDmitry Karpeev     Level: developer
327708da532bSDmitry Karpeev 
327874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
327908da532bSDmitry Karpeev 
328008da532bSDmitry Karpeev @*/
328108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
328208da532bSDmitry Karpeev {
328308da532bSDmitry Karpeev   PetscErrorCode ierr;
32845fd66863SKarl Rupp 
328508da532bSDmitry Karpeev   PetscFunctionBegin;
328608da532bSDmitry Karpeev   if (x) {
328708da532bSDmitry Karpeev     if (!dm->x) {
328808da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
328908da532bSDmitry Karpeev     }
329008da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
32918865f1eaSKarl Rupp   } else if (dm->x) {
329208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
329308da532bSDmitry Karpeev   }
329408da532bSDmitry Karpeev   PetscFunctionReturn(0);
329508da532bSDmitry Karpeev }
329608da532bSDmitry Karpeev 
32970298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3298264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3299264ace61SBarry Smith 
3300264ace61SBarry Smith /*@C
3301264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3302264ace61SBarry Smith 
3303264ace61SBarry Smith   Collective on DM
3304264ace61SBarry Smith 
3305264ace61SBarry Smith   Input Parameters:
3306264ace61SBarry Smith + dm     - The DM object
3307264ace61SBarry Smith - method - The name of the DM type
3308264ace61SBarry Smith 
3309264ace61SBarry Smith   Options Database Key:
3310264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3311264ace61SBarry Smith 
3312264ace61SBarry Smith   Notes:
3313e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3314264ace61SBarry Smith 
3315264ace61SBarry Smith   Level: intermediate
3316264ace61SBarry Smith 
3317264ace61SBarry Smith .keywords: DM, set, type
3318264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3319264ace61SBarry Smith @*/
332019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3321264ace61SBarry Smith {
3322264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3323264ace61SBarry Smith   PetscBool      match;
3324264ace61SBarry Smith   PetscErrorCode ierr;
3325264ace61SBarry Smith 
3326264ace61SBarry Smith   PetscFunctionBegin;
3327264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3328251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3329264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3330264ace61SBarry Smith 
33310f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
33321c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3333ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3334264ace61SBarry Smith 
3335264ace61SBarry Smith   if (dm->ops->destroy) {
3336264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
33370298fd71SBarry Smith     dm->ops->destroy = NULL;
3338264ace61SBarry Smith   }
3339264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3340264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3341264ace61SBarry Smith   PetscFunctionReturn(0);
3342264ace61SBarry Smith }
3343264ace61SBarry Smith 
3344264ace61SBarry Smith /*@C
3345264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3346264ace61SBarry Smith 
3347264ace61SBarry Smith   Not Collective
3348264ace61SBarry Smith 
3349264ace61SBarry Smith   Input Parameter:
3350264ace61SBarry Smith . dm  - The DM
3351264ace61SBarry Smith 
3352264ace61SBarry Smith   Output Parameter:
3353264ace61SBarry Smith . type - The DM type name
3354264ace61SBarry Smith 
3355264ace61SBarry Smith   Level: intermediate
3356264ace61SBarry Smith 
3357264ace61SBarry Smith .keywords: DM, get, type, name
3358264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3359264ace61SBarry Smith @*/
336019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3361264ace61SBarry Smith {
3362264ace61SBarry Smith   PetscErrorCode ierr;
3363264ace61SBarry Smith 
3364264ace61SBarry Smith   PetscFunctionBegin;
3365264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3366c959eef4SJed Brown   PetscValidPointer(type,2);
3367607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3368264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3369264ace61SBarry Smith   PetscFunctionReturn(0);
3370264ace61SBarry Smith }
3371264ace61SBarry Smith 
337267a56275SMatthew G Knepley /*@C
337367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
337467a56275SMatthew G Knepley 
337567a56275SMatthew G Knepley   Collective on DM
337667a56275SMatthew G Knepley 
337767a56275SMatthew G Knepley   Input Parameters:
337867a56275SMatthew G Knepley + dm - the DM
337967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
338067a56275SMatthew G Knepley 
338167a56275SMatthew G Knepley   Output Parameter:
338267a56275SMatthew G Knepley . M - pointer to new DM
338367a56275SMatthew G Knepley 
338467a56275SMatthew G Knepley   Notes:
338567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
338667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
338767a56275SMatthew G Knepley   of the input DM.
338867a56275SMatthew G Knepley 
338967a56275SMatthew G Knepley   Level: intermediate
339067a56275SMatthew G Knepley 
339167a56275SMatthew G Knepley .seealso: DMCreate()
339267a56275SMatthew G Knepley @*/
339319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
339467a56275SMatthew G Knepley {
339567a56275SMatthew G Knepley   DM             B;
339667a56275SMatthew G Knepley   char           convname[256];
3397c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
339867a56275SMatthew G Knepley   PetscErrorCode ierr;
339967a56275SMatthew G Knepley 
340067a56275SMatthew G Knepley   PetscFunctionBegin;
340167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
340267a56275SMatthew G Knepley   PetscValidType(dm,1);
340367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3404251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3405c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3406c067b6caSMatthew G. Knepley   if (sametype) {
3407c067b6caSMatthew G. Knepley     *M   = dm;
3408c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3409c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3410c067b6caSMatthew G. Knepley   } else {
34110298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
341267a56275SMatthew G Knepley 
341367a56275SMatthew G Knepley     /*
341467a56275SMatthew G Knepley        Order of precedence:
341567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
341667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
341767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
341867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
341967a56275SMatthew G Knepley        5) Use a really basic converter.
342067a56275SMatthew G Knepley     */
342167a56275SMatthew G Knepley 
342267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3423a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3424a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3425a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3426a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3427a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34280005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
342967a56275SMatthew G Knepley     if (conv) goto foundconv;
343067a56275SMatthew G Knepley 
343167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
343282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
343367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3434a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3435a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3436a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3437a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3438a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
34390005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
344067a56275SMatthew G Knepley     if (conv) {
3441fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
344267a56275SMatthew G Knepley       goto foundconv;
344367a56275SMatthew G Knepley     }
344467a56275SMatthew G Knepley 
344567a56275SMatthew G Knepley #if 0
344667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
344767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3448fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
344967a56275SMatthew G Knepley     if (conv) goto foundconv;
345067a56275SMatthew G Knepley 
345167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
345267a56275SMatthew G Knepley     if (dm->ops->convert) {
345367a56275SMatthew G Knepley       conv = dm->ops->convert;
345467a56275SMatthew G Knepley     }
345567a56275SMatthew G Knepley     if (conv) goto foundconv;
345667a56275SMatthew G Knepley #endif
345767a56275SMatthew G Knepley 
345867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
345982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
346067a56275SMatthew G Knepley 
346167a56275SMatthew G Knepley foundconv:
346267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
346367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
346412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
346590b157c4SStefano Zampini     {
346690b157c4SStefano Zampini       PetscBool             isper;
346712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
346812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
346990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
347090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
347112fa691eSMatthew G. Knepley     }
347267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
347367a56275SMatthew G Knepley   }
347467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
347567a56275SMatthew G Knepley   PetscFunctionReturn(0);
347667a56275SMatthew G Knepley }
3477264ace61SBarry Smith 
3478264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3479264ace61SBarry Smith 
3480264ace61SBarry Smith /*@C
34811c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
34821c84c290SBarry Smith 
34831c84c290SBarry Smith   Not Collective
34841c84c290SBarry Smith 
34851c84c290SBarry Smith   Input Parameters:
34861c84c290SBarry Smith + name        - The name of a new user-defined creation routine
34871c84c290SBarry Smith - create_func - The creation routine itself
34881c84c290SBarry Smith 
34891c84c290SBarry Smith   Notes:
34901c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
34911c84c290SBarry Smith 
34921c84c290SBarry Smith 
34931c84c290SBarry Smith   Sample usage:
34941c84c290SBarry Smith .vb
3495bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
34961c84c290SBarry Smith .ve
34971c84c290SBarry Smith 
34981c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
34991c84c290SBarry Smith .vb
35001c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
35011c84c290SBarry Smith     DMSetType(DM,"my_da");
35021c84c290SBarry Smith .ve
35031c84c290SBarry Smith    or at runtime via the option
35041c84c290SBarry Smith .vb
35051c84c290SBarry Smith     -da_type my_da
35061c84c290SBarry Smith .ve
3507264ace61SBarry Smith 
3508264ace61SBarry Smith   Level: advanced
35091c84c290SBarry Smith 
35101c84c290SBarry Smith .keywords: DM, register
3511bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
35121c84c290SBarry Smith 
3513264ace61SBarry Smith @*/
3514bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3515264ace61SBarry Smith {
3516264ace61SBarry Smith   PetscErrorCode ierr;
3517264ace61SBarry Smith 
3518264ace61SBarry Smith   PetscFunctionBegin;
35191d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3520a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3521264ace61SBarry Smith   PetscFunctionReturn(0);
3522264ace61SBarry Smith }
3523264ace61SBarry Smith 
3524b859378eSBarry Smith /*@C
352555849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3526b859378eSBarry Smith 
3527b859378eSBarry Smith   Collective on PetscViewer
3528b859378eSBarry Smith 
3529b859378eSBarry Smith   Input Parameters:
3530b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3531b859378eSBarry Smith            some related function before a call to DMLoad().
3532b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3533b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3534b859378eSBarry Smith 
3535b859378eSBarry Smith    Level: intermediate
3536b859378eSBarry Smith 
3537b859378eSBarry Smith   Notes:
353855849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3539b859378eSBarry Smith 
3540b859378eSBarry Smith   Notes for advanced users:
3541b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3542b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3543b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3544b859378eSBarry Smith   format is
3545b859378eSBarry Smith .vb
3546b859378eSBarry Smith      has not yet been determined
3547b859378eSBarry Smith .ve
3548b859378eSBarry Smith 
3549b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3550b859378eSBarry Smith @*/
3551b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3552b859378eSBarry Smith {
35539331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3554b859378eSBarry Smith   PetscErrorCode ierr;
3555b859378eSBarry Smith 
3556b859378eSBarry Smith   PetscFunctionBegin;
3557b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3558b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
355932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
35609331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
35619331c7a4SMatthew G. Knepley   if (isbinary) {
35629331c7a4SMatthew G. Knepley     PetscInt classid;
35639331c7a4SMatthew G. Knepley     char     type[256];
3564b859378eSBarry Smith 
3565060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
35669200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3567060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
356832c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
35699331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35709331c7a4SMatthew G. Knepley   } else if (ishdf5) {
35719331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
35729331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3573b859378eSBarry Smith   PetscFunctionReturn(0);
3574b859378eSBarry Smith }
3575b859378eSBarry Smith 
35767da65231SMatthew G Knepley /******************************** FEM Support **********************************/
35777da65231SMatthew G Knepley 
3578a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3579a6dfd86eSKarl Rupp {
35801d47ebbbSSatish Balay   PetscInt       f;
35811b30c384SMatthew G Knepley   PetscErrorCode ierr;
35821b30c384SMatthew G Knepley 
35837da65231SMatthew G Knepley   PetscFunctionBegin;
358474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35851d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
358657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
35877da65231SMatthew G Knepley   }
35887da65231SMatthew G Knepley   PetscFunctionReturn(0);
35897da65231SMatthew G Knepley }
35907da65231SMatthew G Knepley 
3591a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3592a6dfd86eSKarl Rupp {
35931b30c384SMatthew G Knepley   PetscInt       f, g;
35947da65231SMatthew G Knepley   PetscErrorCode ierr;
35957da65231SMatthew G Knepley 
35967da65231SMatthew G Knepley   PetscFunctionBegin;
359774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
35981d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
359974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
36001d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3601e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
36027da65231SMatthew G Knepley     }
360374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
36047da65231SMatthew G Knepley   }
36057da65231SMatthew G Knepley   PetscFunctionReturn(0);
36067da65231SMatthew G Knepley }
3607e7c4fc90SDmitry Karpeev 
36086113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3609e759306cSMatthew G. Knepley {
36100c5b8624SToby Isaac   PetscInt          localSize, bs;
36110c5b8624SToby Isaac   PetscMPIInt       size;
36120c5b8624SToby Isaac   Vec               x, xglob;
36130c5b8624SToby Isaac   const PetscScalar *xarray;
3614e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3615e759306cSMatthew G. Knepley 
3616e759306cSMatthew G. Knepley   PetscFunctionBegin;
36179852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3618e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3619e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
36206113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
36210c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
36220c5b8624SToby Isaac   if (size > 1) {
36230c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
36240c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
36250c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
36260c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
36270c5b8624SToby Isaac   } else {
36280c5b8624SToby Isaac     xglob = x;
36290c5b8624SToby Isaac   }
36300c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
36310c5b8624SToby Isaac   if (size > 1) {
36320c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
36330c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
36340c5b8624SToby Isaac   }
3635e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3636e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3637e759306cSMatthew G. Knepley }
3638e759306cSMatthew G. Knepley 
363988ed4aceSMatthew G Knepley /*@
3640e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
364188ed4aceSMatthew G Knepley 
364288ed4aceSMatthew G Knepley   Input Parameter:
364388ed4aceSMatthew G Knepley . dm - The DM
364488ed4aceSMatthew G Knepley 
364588ed4aceSMatthew G Knepley   Output Parameter:
364688ed4aceSMatthew G Knepley . section - The PetscSection
364788ed4aceSMatthew G Knepley 
3648e5893cccSMatthew G. Knepley   Options Database Keys:
3649e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3650e5893cccSMatthew G. Knepley 
365188ed4aceSMatthew G Knepley   Level: intermediate
365288ed4aceSMatthew G Knepley 
365388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
365488ed4aceSMatthew G Knepley 
3655e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
365688ed4aceSMatthew G Knepley @*/
3657e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
36580adebc6cSBarry Smith {
3659fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3660fd59a867SMatthew G. Knepley 
366188ed4aceSMatthew G Knepley   PetscFunctionBegin;
366288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
366388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
36642f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
36652f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3666ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
36672f0f8703SMatthew G. Knepley   }
366888ed4aceSMatthew G Knepley   *section = dm->defaultSection;
366988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
367088ed4aceSMatthew G Knepley }
367188ed4aceSMatthew G Knepley 
367288ed4aceSMatthew G Knepley /*@
3673e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
367488ed4aceSMatthew G Knepley 
367588ed4aceSMatthew G Knepley   Input Parameters:
367688ed4aceSMatthew G Knepley + dm - The DM
367788ed4aceSMatthew G Knepley - section - The PetscSection
367888ed4aceSMatthew G Knepley 
367988ed4aceSMatthew G Knepley   Level: intermediate
368088ed4aceSMatthew G Knepley 
368188ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
368288ed4aceSMatthew G Knepley 
3683e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
368488ed4aceSMatthew G Knepley @*/
3685e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
36860adebc6cSBarry Smith {
3687c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3688af122d2aSMatthew G Knepley   PetscInt       f;
368988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
369088ed4aceSMatthew G Knepley 
369188ed4aceSMatthew G Knepley   PetscFunctionBegin;
369288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3693c473ab19SMatthew G. Knepley   if (section) {
36941d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
36951d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3696c473ab19SMatthew G. Knepley   }
369788ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
369888ed4aceSMatthew G Knepley   dm->defaultSection = section;
3699c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3700af122d2aSMatthew G Knepley   if (numFields) {
3701af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3702af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
37030f21e855SMatthew G. Knepley       PetscObject disc;
3704af122d2aSMatthew G Knepley       const char *name;
3705af122d2aSMatthew G Knepley 
3706af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
370744a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
37080f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3709af122d2aSMatthew G Knepley     }
3710af122d2aSMatthew G Knepley   }
3711e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
37121d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
371388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
371488ed4aceSMatthew G Knepley }
371588ed4aceSMatthew G Knepley 
37169435951eSToby Isaac /*@
37179435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
37189435951eSToby Isaac 
3719e228b242SToby Isaac   not collective
3720e228b242SToby Isaac 
37219435951eSToby Isaac   Input Parameter:
37229435951eSToby Isaac . dm - The DM
37239435951eSToby Isaac 
37249435951eSToby Isaac   Output Parameter:
37259435951eSToby 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.
37269435951eSToby 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.
37279435951eSToby Isaac 
37289435951eSToby Isaac   Level: advanced
37299435951eSToby Isaac 
37309435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
37319435951eSToby Isaac 
37329435951eSToby Isaac .seealso: DMSetDefaultConstraints()
37339435951eSToby Isaac @*/
37349435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
37359435951eSToby Isaac {
37369435951eSToby Isaac   PetscErrorCode ierr;
37379435951eSToby Isaac 
37389435951eSToby Isaac   PetscFunctionBegin;
37399435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37409435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
374145a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
374245a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
37439435951eSToby Isaac   PetscFunctionReturn(0);
37449435951eSToby Isaac }
37459435951eSToby Isaac 
37469435951eSToby Isaac /*@
37479435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
37489435951eSToby Isaac 
37499435951eSToby 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().
37509435951eSToby Isaac 
37519435951eSToby 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.
37529435951eSToby Isaac 
3753e228b242SToby Isaac   collective on dm
3754e228b242SToby Isaac 
37559435951eSToby Isaac   Input Parameters:
37569435951eSToby Isaac + dm - The DM
3757e228b242SToby 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).
3758e228b242SToby 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).
37599435951eSToby Isaac 
37609435951eSToby Isaac   Level: advanced
37619435951eSToby Isaac 
37629435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
37639435951eSToby Isaac 
37649435951eSToby Isaac .seealso: DMGetDefaultConstraints()
37659435951eSToby Isaac @*/
37669435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
37679435951eSToby Isaac {
3768e228b242SToby Isaac   PetscMPIInt result;
37699435951eSToby Isaac   PetscErrorCode ierr;
37709435951eSToby Isaac 
37719435951eSToby Isaac   PetscFunctionBegin;
37729435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3773e228b242SToby Isaac   if (section) {
3774e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3775e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3776f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3777e228b242SToby Isaac   }
3778e228b242SToby Isaac   if (mat) {
3779e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3780e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3781f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3782e228b242SToby Isaac   }
37839435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
37849435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
37859435951eSToby Isaac   dm->defaultConstraintSection = section;
37869435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
37879435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
37889435951eSToby Isaac   dm->defaultConstraintMat = mat;
37899435951eSToby Isaac   PetscFunctionReturn(0);
37909435951eSToby Isaac }
37919435951eSToby Isaac 
3792497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3793507e4973SMatthew G. Knepley /*
3794507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3795507e4973SMatthew G. Knepley 
3796507e4973SMatthew G. Knepley   Input Parameters:
3797507e4973SMatthew G. Knepley + dm - The DM
3798507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3799507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3800507e4973SMatthew G. Knepley 
3801507e4973SMatthew G. Knepley   Level: intermediate
3802507e4973SMatthew G. Knepley 
3803507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3804507e4973SMatthew G. Knepley */
3805f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3806507e4973SMatthew G. Knepley {
3807507e4973SMatthew G. Knepley   MPI_Comm        comm;
3808507e4973SMatthew G. Knepley   PetscLayout     layout;
3809507e4973SMatthew G. Knepley   const PetscInt *ranges;
3810507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3811507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3812507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3813507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3814507e4973SMatthew G. Knepley 
3815507e4973SMatthew G. Knepley   PetscFunctionBegin;
3816507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3817507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3818507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3819507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3820507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3821507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3822507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3823507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3824507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3825507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3826507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3827507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3828f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3829507e4973SMatthew G. Knepley 
3830507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3831507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3832507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3833507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3834507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3835507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3836507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3837507e4973SMatthew 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;}
3838507e4973SMatthew 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;}
3839507e4973SMatthew G. Knepley     if (gdof < 0) {
3840507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3841507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3842507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3843507e4973SMatthew G. Knepley 
3844507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3845507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3846507e4973SMatthew 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;}
3847507e4973SMatthew G. Knepley       }
3848507e4973SMatthew G. Knepley     }
3849507e4973SMatthew G. Knepley   }
3850507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3851507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3852b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3853507e4973SMatthew G. Knepley   if (!gvalid) {
3854507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3855507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3856507e4973SMatthew G. Knepley   }
3857507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3858507e4973SMatthew G. Knepley }
3859f741bcd2SMatthew G. Knepley #endif
3860507e4973SMatthew G. Knepley 
386188ed4aceSMatthew G Knepley /*@
3862e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
386388ed4aceSMatthew G Knepley 
38648b1ab98fSJed Brown   Collective on DM
38658b1ab98fSJed Brown 
386688ed4aceSMatthew G Knepley   Input Parameter:
386788ed4aceSMatthew G Knepley . dm - The DM
386888ed4aceSMatthew G Knepley 
386988ed4aceSMatthew G Knepley   Output Parameter:
387088ed4aceSMatthew G Knepley . section - The PetscSection
387188ed4aceSMatthew G Knepley 
387288ed4aceSMatthew G Knepley   Level: intermediate
387388ed4aceSMatthew G Knepley 
387488ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
387588ed4aceSMatthew G Knepley 
3876e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
387788ed4aceSMatthew G Knepley @*/
3878e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
38790adebc6cSBarry Smith {
388088ed4aceSMatthew G Knepley   PetscErrorCode ierr;
388188ed4aceSMatthew G Knepley 
388288ed4aceSMatthew G Knepley   PetscFunctionBegin;
388388ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
388488ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
388588ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3886fd59a867SMatthew G. Knepley     PetscSection s;
3887fd59a867SMatthew G. Knepley 
3888e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3889fd59a867SMatthew 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");
389033907cc2SStefano Zampini     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
389115b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3892cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3893ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3894685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
389588ed4aceSMatthew G Knepley   }
389688ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
389788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
389888ed4aceSMatthew G Knepley }
389988ed4aceSMatthew G Knepley 
3900b21d0597SMatthew G Knepley /*@
3901e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3902b21d0597SMatthew G Knepley 
3903b21d0597SMatthew G Knepley   Input Parameters:
3904b21d0597SMatthew G Knepley + dm - The DM
39055080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3906b21d0597SMatthew G Knepley 
3907b21d0597SMatthew G Knepley   Level: intermediate
3908b21d0597SMatthew G Knepley 
3909b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3910b21d0597SMatthew G Knepley 
3911e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3912b21d0597SMatthew G Knepley @*/
3913e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
39140adebc6cSBarry Smith {
3915b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3916b21d0597SMatthew G Knepley 
3917b21d0597SMatthew G Knepley   PetscFunctionBegin;
3918b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
39195080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39201d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3921b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3922b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3923497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3924f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3925507e4973SMatthew G. Knepley #endif
3926b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3927b21d0597SMatthew G Knepley }
3928b21d0597SMatthew G Knepley 
392988ed4aceSMatthew G Knepley /*@
393088ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
393188ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
393288ed4aceSMatthew G Knepley 
393388ed4aceSMatthew G Knepley   Input Parameter:
393488ed4aceSMatthew G Knepley . dm - The DM
393588ed4aceSMatthew G Knepley 
393688ed4aceSMatthew G Knepley   Output Parameter:
393788ed4aceSMatthew G Knepley . sf - The PetscSF
393888ed4aceSMatthew G Knepley 
393988ed4aceSMatthew G Knepley   Level: intermediate
394088ed4aceSMatthew G Knepley 
394188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
394288ed4aceSMatthew G Knepley 
394388ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
394488ed4aceSMatthew G Knepley @*/
39450adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
39460adebc6cSBarry Smith {
394788ed4aceSMatthew G Knepley   PetscInt       nroots;
394888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
394988ed4aceSMatthew G Knepley 
395088ed4aceSMatthew G Knepley   PetscFunctionBegin;
395188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
395288ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
395333907cc2SStefano Zampini   if (!dm->defaultSF) {
395433907cc2SStefano Zampini     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->defaultSF);CHKERRQ(ierr);
395533907cc2SStefano Zampini   }
39560298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
395788ed4aceSMatthew G Knepley   if (nroots < 0) {
395888ed4aceSMatthew G Knepley     PetscSection section, gSection;
395988ed4aceSMatthew G Knepley 
3960e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
396131ea6d37SMatthew G Knepley     if (section) {
3962e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
396388ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
396431ea6d37SMatthew G Knepley     } else {
39650298fd71SBarry Smith       *sf = NULL;
396631ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
396731ea6d37SMatthew G Knepley     }
396888ed4aceSMatthew G Knepley   }
396988ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
397088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
397188ed4aceSMatthew G Knepley }
397288ed4aceSMatthew G Knepley 
397388ed4aceSMatthew G Knepley /*@
397488ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
397588ed4aceSMatthew G Knepley 
397688ed4aceSMatthew G Knepley   Input Parameters:
397788ed4aceSMatthew G Knepley + dm - The DM
397888ed4aceSMatthew G Knepley - sf - The PetscSF
397988ed4aceSMatthew G Knepley 
398088ed4aceSMatthew G Knepley   Level: intermediate
398188ed4aceSMatthew G Knepley 
398288ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
398388ed4aceSMatthew G Knepley 
398488ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
398588ed4aceSMatthew G Knepley @*/
39860adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
39870adebc6cSBarry Smith {
398888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
398988ed4aceSMatthew G Knepley 
399088ed4aceSMatthew G Knepley   PetscFunctionBegin;
399188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
399233907cc2SStefano Zampini   if (sf) {
399388ed4aceSMatthew G Knepley     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
399433907cc2SStefano Zampini     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
399533907cc2SStefano Zampini   }
399688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
399788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
399888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
399988ed4aceSMatthew G Knepley }
400088ed4aceSMatthew G Knepley 
400188ed4aceSMatthew G Knepley /*@C
400288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
400388ed4aceSMatthew G Knepley   describing the data layout.
400488ed4aceSMatthew G Knepley 
400588ed4aceSMatthew G Knepley   Input Parameters:
400688ed4aceSMatthew G Knepley + dm - The DM
400788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
400888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
400988ed4aceSMatthew G Knepley 
401088ed4aceSMatthew G Knepley   Level: intermediate
401188ed4aceSMatthew G Knepley 
401288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
401388ed4aceSMatthew G Knepley @*/
401488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
401588ed4aceSMatthew G Knepley {
401682f516ccSBarry Smith   MPI_Comm       comm;
401788ed4aceSMatthew G Knepley   PetscLayout    layout;
401888ed4aceSMatthew G Knepley   const PetscInt *ranges;
401988ed4aceSMatthew G Knepley   PetscInt       *local;
402088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4021ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
402288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
402388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
402488ed4aceSMatthew G Knepley 
402588ed4aceSMatthew G Knepley   PetscFunctionBegin;
402688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4027367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
402888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
402988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
403088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
403188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
403288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
403388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
403488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
403588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
403688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4037ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
40386636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
403988ed4aceSMatthew G Knepley 
40406636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40416636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4042235fbf56SMatthew 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));
40436636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
404488ed4aceSMatthew G Knepley   }
4045785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4046785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
404788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
40481f588964SMatthew G Knepley     const PetscInt *cind;
40496636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
405088ed4aceSMatthew G Knepley 
405188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
405288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
405388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
405488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
405588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
40566636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
405788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
40586636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
40596636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
40606636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4061057b4bcdSMatthew 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);
40626636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
40636636e97aSMatthew G Knepley     }
406488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
406588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
406688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
406788ed4aceSMatthew G Knepley     }
406888ed4aceSMatthew G Knepley     if (gdof < 0) {
40696636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
407088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
407188ed4aceSMatthew G Knepley 
407205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
407331d3f06eSJed Brown         if (r < 0) r = -(r+2);
407405376888SMatthew 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);
407588ed4aceSMatthew G Knepley         remote[l].rank  = r;
407688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
407788ed4aceSMatthew G Knepley       }
407888ed4aceSMatthew G Knepley     } else {
40796636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
408088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
408188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
408288ed4aceSMatthew G Knepley       }
408388ed4aceSMatthew G Knepley     }
408488ed4aceSMatthew G Knepley   }
40856636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
408688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
408788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
408888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
408988ed4aceSMatthew G Knepley }
4090af122d2aSMatthew G Knepley 
4091b21d0597SMatthew G Knepley /*@
4092b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4093b21d0597SMatthew G Knepley 
4094b21d0597SMatthew G Knepley   Input Parameter:
4095b21d0597SMatthew G Knepley . dm - The DM
4096b21d0597SMatthew G Knepley 
4097b21d0597SMatthew G Knepley   Output Parameter:
4098b21d0597SMatthew G Knepley . sf - The PetscSF
4099b21d0597SMatthew G Knepley 
4100b21d0597SMatthew G Knepley   Level: intermediate
4101b21d0597SMatthew G Knepley 
4102b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4103b21d0597SMatthew G Knepley 
4104057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4105b21d0597SMatthew G Knepley @*/
41060adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
41070adebc6cSBarry Smith {
4108b21d0597SMatthew G Knepley   PetscFunctionBegin;
4109b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4110b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4111b21d0597SMatthew G Knepley   *sf = dm->sf;
4112b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4113b21d0597SMatthew G Knepley }
4114b21d0597SMatthew G Knepley 
4115057b4bcdSMatthew G Knepley /*@
4116057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4117057b4bcdSMatthew G Knepley 
4118057b4bcdSMatthew G Knepley   Input Parameters:
4119057b4bcdSMatthew G Knepley + dm - The DM
4120057b4bcdSMatthew G Knepley - sf - The PetscSF
4121057b4bcdSMatthew G Knepley 
4122057b4bcdSMatthew G Knepley   Level: intermediate
4123057b4bcdSMatthew G Knepley 
4124057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
4125057b4bcdSMatthew G Knepley @*/
41260adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
41270adebc6cSBarry Smith {
4128057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4129057b4bcdSMatthew G Knepley 
4130057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4131057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
413233907cc2SStefano Zampini   if (sf) {
413333907cc2SStefano Zampini     PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4134057b4bcdSMatthew G Knepley     ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
413533907cc2SStefano Zampini   }
413633907cc2SStefano Zampini   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4137057b4bcdSMatthew G Knepley   dm->sf = sf;
4138057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4139057b4bcdSMatthew G Knepley }
4140057b4bcdSMatthew G Knepley 
41412764a2aaSMatthew G. Knepley /*@
41422764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
41432764a2aaSMatthew G. Knepley 
41442764a2aaSMatthew G. Knepley   Input Parameter:
41452764a2aaSMatthew G. Knepley . dm - The DM
41462764a2aaSMatthew G. Knepley 
41472764a2aaSMatthew G. Knepley   Output Parameter:
41482764a2aaSMatthew G. Knepley . prob - The PetscDS
41492764a2aaSMatthew G. Knepley 
41502764a2aaSMatthew G. Knepley   Level: developer
41512764a2aaSMatthew G. Knepley 
41522764a2aaSMatthew G. Knepley .seealso: DMSetDS()
41532764a2aaSMatthew G. Knepley @*/
41542764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4155af122d2aSMatthew G Knepley {
4156af122d2aSMatthew G Knepley   PetscFunctionBegin;
4157af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41580f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
41590f21e855SMatthew G. Knepley   *prob = dm->prob;
41600f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41610f21e855SMatthew G. Knepley }
41620f21e855SMatthew G. Knepley 
41632764a2aaSMatthew G. Knepley /*@
41642764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
41652764a2aaSMatthew G. Knepley 
41662764a2aaSMatthew G. Knepley   Input Parameters:
41672764a2aaSMatthew G. Knepley + dm - The DM
41682764a2aaSMatthew G. Knepley - prob - The PetscDS
41692764a2aaSMatthew G. Knepley 
41702764a2aaSMatthew G. Knepley   Level: developer
41712764a2aaSMatthew G. Knepley 
41722764a2aaSMatthew G. Knepley .seealso: DMGetDS()
41732764a2aaSMatthew G. Knepley @*/
41742764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
41750f21e855SMatthew G. Knepley {
4176f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
41770f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41780f21e855SMatthew G. Knepley 
41790f21e855SMatthew G. Knepley   PetscFunctionBegin;
41800f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41812764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
418237316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
41832764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
41840f21e855SMatthew G. Knepley   dm->prob = prob;
4185f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4186f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
41870f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
41880f21e855SMatthew G. Knepley }
41890f21e855SMatthew G. Knepley 
419044a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
419144a7f3ddSMatthew G. Knepley {
419244a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
419344a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
419444a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
419544a7f3ddSMatthew G. Knepley 
419644a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
419744a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
419844a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
419944a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
420044a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
420144a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
420244a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
420344a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
420444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
420544a7f3ddSMatthew G. Knepley }
420644a7f3ddSMatthew G. Knepley 
420744a7f3ddSMatthew G. Knepley /*@
420844a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
420944a7f3ddSMatthew G. Knepley 
421044a7f3ddSMatthew G. Knepley   Logically collective on DM
421144a7f3ddSMatthew G. Knepley 
421244a7f3ddSMatthew G. Knepley   Input Parameter:
421344a7f3ddSMatthew G. Knepley . dm - The DM
421444a7f3ddSMatthew G. Knepley 
421544a7f3ddSMatthew G. Knepley   Level: intermediate
421644a7f3ddSMatthew G. Knepley 
421744a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
421844a7f3ddSMatthew G. Knepley @*/
421944a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
422044a7f3ddSMatthew G. Knepley {
422144a7f3ddSMatthew G. Knepley   PetscInt       f;
422244a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
422344a7f3ddSMatthew G. Knepley 
422444a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
422544a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
422644a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
422744a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
422844a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
422944a7f3ddSMatthew G. Knepley   }
423044a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
423144a7f3ddSMatthew G. Knepley   dm->fields = NULL;
423244a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
423344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
423444a7f3ddSMatthew G. Knepley }
423544a7f3ddSMatthew G. Knepley 
4236689b5837SMatthew G. Knepley /*@
4237689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4238689b5837SMatthew G. Knepley 
4239689b5837SMatthew G. Knepley   Not collective
4240689b5837SMatthew G. Knepley 
4241689b5837SMatthew G. Knepley   Input Parameter:
4242689b5837SMatthew G. Knepley . dm - The DM
4243689b5837SMatthew G. Knepley 
4244689b5837SMatthew G. Knepley   Output Parameter:
4245689b5837SMatthew G. Knepley . Nf - The number of fields
4246689b5837SMatthew G. Knepley 
4247689b5837SMatthew G. Knepley   Level: intermediate
4248689b5837SMatthew G. Knepley 
4249689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4250689b5837SMatthew G. Knepley @*/
42510f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
42520f21e855SMatthew G. Knepley {
42530f21e855SMatthew G. Knepley   PetscFunctionBegin;
42540f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425544a7f3ddSMatthew G. Knepley   PetscValidPointer(numFields, 2);
425644a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4257af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4258af122d2aSMatthew G Knepley }
4259af122d2aSMatthew G Knepley 
4260689b5837SMatthew G. Knepley /*@
4261689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4262689b5837SMatthew G. Knepley 
4263689b5837SMatthew G. Knepley   Logically collective on DM
4264689b5837SMatthew G. Knepley 
4265689b5837SMatthew G. Knepley   Input Parameters:
4266689b5837SMatthew G. Knepley + dm - The DM
4267689b5837SMatthew G. Knepley - Nf - The number of fields
4268689b5837SMatthew G. Knepley 
4269689b5837SMatthew G. Knepley   Level: intermediate
4270689b5837SMatthew G. Knepley 
4271689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4272689b5837SMatthew G. Knepley @*/
4273af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4274af122d2aSMatthew G Knepley {
42750f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4276af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4277af122d2aSMatthew G Knepley 
4278af122d2aSMatthew G Knepley   PetscFunctionBegin;
4279af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
428044a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
42810f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
42820f21e855SMatthew G. Knepley     PetscContainer obj;
42830f21e855SMatthew G. Knepley 
42840f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
428544a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
42860f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4287af122d2aSMatthew G Knepley   }
4288af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4289af122d2aSMatthew G Knepley }
4290af122d2aSMatthew G Knepley 
4291c1929be8SMatthew G. Knepley /*@
4292c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4293c1929be8SMatthew G. Knepley 
4294c1929be8SMatthew G. Knepley   Not collective
4295c1929be8SMatthew G. Knepley 
4296c1929be8SMatthew G. Knepley   Input Parameters:
4297c1929be8SMatthew G. Knepley + dm - The DM
4298c1929be8SMatthew G. Knepley - f  - The field number
4299c1929be8SMatthew G. Knepley 
430044a7f3ddSMatthew G. Knepley   Output Parameters:
430144a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
430244a7f3ddSMatthew G. Knepley - field - The discretization object
4303c1929be8SMatthew G. Knepley 
430444a7f3ddSMatthew G. Knepley   Level: intermediate
4305c1929be8SMatthew G. Knepley 
430644a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4307c1929be8SMatthew G. Knepley @*/
430844a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4309af122d2aSMatthew G Knepley {
4310af122d2aSMatthew G Knepley   PetscFunctionBegin;
4311af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
431244a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
431344a7f3ddSMatthew G. Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
431444a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
431544a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4316decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4317decb47aaSMatthew G. Knepley }
4318decb47aaSMatthew G. Knepley 
4319c1929be8SMatthew G. Knepley /*@
4320c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4321c1929be8SMatthew G. Knepley 
4322c1929be8SMatthew G. Knepley   Logically collective on DM
4323c1929be8SMatthew G. Knepley 
4324c1929be8SMatthew G. Knepley   Input Parameters:
4325c1929be8SMatthew G. Knepley + dm    - The DM
4326c1929be8SMatthew G. Knepley . f     - The field number
432744a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4328c1929be8SMatthew G. Knepley - field - The discretization object
4329c1929be8SMatthew G. Knepley 
433044a7f3ddSMatthew G. Knepley   Level: intermediate
4331c1929be8SMatthew G. Knepley 
433244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4333c1929be8SMatthew G. Knepley @*/
433444a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4335decb47aaSMatthew G. Knepley {
4336decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4337decb47aaSMatthew G. Knepley 
4338decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4339decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
434044a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
434144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
434244a7f3ddSMatthew G. Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
434344a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
434444a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
434544a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
434644a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
434744a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
434844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
434944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
435044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
435144a7f3ddSMatthew G. Knepley }
435244a7f3ddSMatthew G. Knepley 
435344a7f3ddSMatthew G. Knepley /*@
435444a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
435544a7f3ddSMatthew G. Knepley 
435644a7f3ddSMatthew G. Knepley   Logically collective on DM
435744a7f3ddSMatthew G. Knepley 
435844a7f3ddSMatthew G. Knepley   Input Parameters:
435944a7f3ddSMatthew G. Knepley + dm    - The DM
436044a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
436144a7f3ddSMatthew G. Knepley - field - The discretization object
436244a7f3ddSMatthew G. Knepley 
436344a7f3ddSMatthew G. Knepley   Level: intermediate
436444a7f3ddSMatthew G. Knepley 
436544a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
436644a7f3ddSMatthew G. Knepley @*/
436744a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
436844a7f3ddSMatthew G. Knepley {
436944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
437044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
437144a7f3ddSMatthew G. Knepley 
437244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
437344a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
437444a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
437544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
437644a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
437744a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
437844a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
437944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
438044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4381af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4382af122d2aSMatthew G Knepley }
43836636e97aSMatthew G Knepley 
4384b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4385b64e0483SPeter Brune {
4386b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4387b64e0483SPeter Brune   PetscErrorCode ierr;
4388b64e0483SPeter Brune   Vec coords,ccoords;
43896dbf9973SLawrence Mitchell   Mat inject;
4390b64e0483SPeter Brune   PetscFunctionBegin;
4391b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4392b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4393b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4394b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4395b64e0483SPeter Brune   if (coords && !ccoords) {
4396b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
43976668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
43986dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
43992adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
44006dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4401b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4402b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4403b64e0483SPeter Brune   }
4404b64e0483SPeter Brune   PetscFunctionReturn(0);
4405b64e0483SPeter Brune }
4406b64e0483SPeter Brune 
440703dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
440803dadc2fSPeter Brune {
440903dadc2fSPeter Brune   DM dm_coord,subdm_coord;
441003dadc2fSPeter Brune   PetscErrorCode ierr;
441103dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
441203dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
441303dadc2fSPeter Brune   PetscFunctionBegin;
441403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
441503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
441603dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
441703dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
441803dadc2fSPeter Brune   if (coords && !ccoords) {
441903dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
44206668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
442103dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
442224640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
442303dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
442403dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
442503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
442603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
442703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
442803dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
442903dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
443003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
443103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
443203dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
443303dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
443403dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
443503dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
443603dadc2fSPeter Brune   }
443703dadc2fSPeter Brune   PetscFunctionReturn(0);
443803dadc2fSPeter Brune }
443903dadc2fSPeter Brune 
4440c73cfb54SMatthew G. Knepley /*@
4441c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4442c73cfb54SMatthew G. Knepley 
4443c73cfb54SMatthew G. Knepley   Not collective
4444c73cfb54SMatthew G. Knepley 
4445c73cfb54SMatthew G. Knepley   Input Parameter:
4446c73cfb54SMatthew G. Knepley . dm - The DM
4447c73cfb54SMatthew G. Knepley 
4448c73cfb54SMatthew G. Knepley   Output Parameter:
4449c73cfb54SMatthew G. Knepley . dim - The topological dimension
4450c73cfb54SMatthew G. Knepley 
4451c73cfb54SMatthew G. Knepley   Level: beginner
4452c73cfb54SMatthew G. Knepley 
4453c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4454c73cfb54SMatthew G. Knepley @*/
4455c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4456c73cfb54SMatthew G. Knepley {
4457c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4458c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4459c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4460c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4461c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4462c73cfb54SMatthew G. Knepley }
4463c73cfb54SMatthew G. Knepley 
4464c73cfb54SMatthew G. Knepley /*@
4465c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4466c73cfb54SMatthew G. Knepley 
4467c73cfb54SMatthew G. Knepley   Collective on dm
4468c73cfb54SMatthew G. Knepley 
4469c73cfb54SMatthew G. Knepley   Input Parameters:
4470c73cfb54SMatthew G. Knepley + dm - The DM
4471c73cfb54SMatthew G. Knepley - dim - The topological dimension
4472c73cfb54SMatthew G. Knepley 
4473c73cfb54SMatthew G. Knepley   Level: beginner
4474c73cfb54SMatthew G. Knepley 
4475c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4476c73cfb54SMatthew G. Knepley @*/
4477c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4478c73cfb54SMatthew G. Knepley {
4479f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4480f17e8794SMatthew G. Knepley 
4481c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4482c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4483c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4484c73cfb54SMatthew G. Knepley   dm->dim = dim;
4485f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4486c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4487c73cfb54SMatthew G. Knepley }
4488c73cfb54SMatthew G. Knepley 
4489793f3fe5SMatthew G. Knepley /*@
4490793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4491793f3fe5SMatthew G. Knepley 
4492793f3fe5SMatthew G. Knepley   Collective on DM
4493793f3fe5SMatthew G. Knepley 
4494793f3fe5SMatthew G. Knepley   Input Parameters:
4495793f3fe5SMatthew G. Knepley + dm - the DM
4496793f3fe5SMatthew G. Knepley - dim - the dimension
4497793f3fe5SMatthew G. Knepley 
4498793f3fe5SMatthew G. Knepley   Output Parameters:
4499793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4500793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4501793f3fe5SMatthew G. Knepley 
4502793f3fe5SMatthew G. Knepley   Note:
4503793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4504793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4505793f3fe5SMatthew G. Knepley   then the interval is empty.
4506793f3fe5SMatthew G. Knepley 
4507793f3fe5SMatthew G. Knepley   Level: intermediate
4508793f3fe5SMatthew G. Knepley 
4509793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4510793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4511793f3fe5SMatthew G. Knepley @*/
4512793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4513793f3fe5SMatthew G. Knepley {
4514793f3fe5SMatthew G. Knepley   PetscInt       d;
4515793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4516793f3fe5SMatthew G. Knepley 
4517793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4518793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4519793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4520793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4521793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4522793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4523793f3fe5SMatthew G. Knepley }
4524793f3fe5SMatthew G. Knepley 
45256636e97aSMatthew G Knepley /*@
45266636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
45276636e97aSMatthew G Knepley 
45286636e97aSMatthew G Knepley   Collective on DM
45296636e97aSMatthew G Knepley 
45306636e97aSMatthew G Knepley   Input Parameters:
45316636e97aSMatthew G Knepley + dm - the DM
45326636e97aSMatthew G Knepley - c - coordinate vector
45336636e97aSMatthew G Knepley 
45342dd40e9bSPatrick Sanan   Notes:
45352dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
45362dd40e9bSPatrick Sanan 
45372dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
45386636e97aSMatthew G Knepley 
45396636e97aSMatthew G Knepley   Level: intermediate
45406636e97aSMatthew G Knepley 
45416636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45422dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
45436636e97aSMatthew G Knepley @*/
45446636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
45456636e97aSMatthew G Knepley {
45466636e97aSMatthew G Knepley   PetscErrorCode ierr;
45476636e97aSMatthew G Knepley 
45486636e97aSMatthew G Knepley   PetscFunctionBegin;
45496636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45506636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
45516636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
45526636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
45536636e97aSMatthew G Knepley   dm->coordinates = c;
45546636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4555b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
455603dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
45576636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45586636e97aSMatthew G Knepley }
45596636e97aSMatthew G Knepley 
45606636e97aSMatthew G Knepley /*@
45616636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
45626636e97aSMatthew G Knepley 
45637058e716SVaclav Hapla   Not collective
45646636e97aSMatthew G Knepley 
45656636e97aSMatthew G Knepley    Input Parameters:
45666636e97aSMatthew G Knepley +  dm - the DM
45676636e97aSMatthew G Knepley -  c - coordinate vector
45686636e97aSMatthew G Knepley 
45692dd40e9bSPatrick Sanan   Notes:
45706636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
45716636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
45726636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
45736636e97aSMatthew G Knepley 
45742dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
45752dd40e9bSPatrick Sanan 
45766636e97aSMatthew G Knepley   Level: intermediate
45776636e97aSMatthew G Knepley 
45786636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45796636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
45806636e97aSMatthew G Knepley @*/
45816636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
45826636e97aSMatthew G Knepley {
45836636e97aSMatthew G Knepley   PetscErrorCode ierr;
45846636e97aSMatthew G Knepley 
45856636e97aSMatthew G Knepley   PetscFunctionBegin;
45866636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45876636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
45886636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
45896636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
45908865f1eaSKarl Rupp 
45916636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
45928865f1eaSKarl Rupp 
45936636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
45946636e97aSMatthew G Knepley   PetscFunctionReturn(0);
45956636e97aSMatthew G Knepley }
45966636e97aSMatthew G Knepley 
45976636e97aSMatthew G Knepley /*@
45986636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
45996636e97aSMatthew G Knepley 
4600c4a54dccSVaclav Hapla   Collective on DM
46016636e97aSMatthew G Knepley 
46026636e97aSMatthew G Knepley   Input Parameter:
46036636e97aSMatthew G Knepley . dm - the DM
46046636e97aSMatthew G Knepley 
46056636e97aSMatthew G Knepley   Output Parameter:
46066636e97aSMatthew G Knepley . c - global coordinate vector
46076636e97aSMatthew G Knepley 
46086636e97aSMatthew G Knepley   Note:
46096636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
46106636e97aSMatthew G Knepley 
46116636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
46126636e97aSMatthew G Knepley 
46136636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
46146636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
46156636e97aSMatthew G Knepley 
46166636e97aSMatthew G Knepley   Level: intermediate
46176636e97aSMatthew G Knepley 
46186636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
46196636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
46206636e97aSMatthew G Knepley @*/
46216636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
46226636e97aSMatthew G Knepley {
46236636e97aSMatthew G Knepley   PetscErrorCode ierr;
46246636e97aSMatthew G Knepley 
46256636e97aSMatthew G Knepley   PetscFunctionBegin;
46266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
46276636e97aSMatthew G Knepley   PetscValidPointer(c,2);
46281f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
46290298fd71SBarry Smith     DM cdm = NULL;
46306636e97aSMatthew G Knepley 
46316636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
46326636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
46336636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
46346636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
46356636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
46366636e97aSMatthew G Knepley   }
46376636e97aSMatthew G Knepley   *c = dm->coordinates;
46386636e97aSMatthew G Knepley   PetscFunctionReturn(0);
46396636e97aSMatthew G Knepley }
46406636e97aSMatthew G Knepley 
46416636e97aSMatthew G Knepley /*@
464281e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
464381e9a530SVaclav Hapla 
464481e9a530SVaclav Hapla   Collective on DM
464581e9a530SVaclav Hapla 
464681e9a530SVaclav Hapla   Input Parameter:
464781e9a530SVaclav Hapla . dm - the DM
464881e9a530SVaclav Hapla 
464981e9a530SVaclav Hapla   Level: advanced
465081e9a530SVaclav Hapla 
465181e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
465281e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
465381e9a530SVaclav Hapla @*/
465481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
465581e9a530SVaclav Hapla {
465681e9a530SVaclav Hapla   DM cdm = NULL;
465781e9a530SVaclav Hapla   PetscErrorCode ierr;
465881e9a530SVaclav Hapla 
465981e9a530SVaclav Hapla   PetscFunctionBegin;
466081e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
466181e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
466281e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
466381e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
466481e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
466581e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
466681e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
466781e9a530SVaclav Hapla   }
466881e9a530SVaclav Hapla   PetscFunctionReturn(0);
466981e9a530SVaclav Hapla }
467081e9a530SVaclav Hapla 
467181e9a530SVaclav Hapla /*@
46726636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
46736636e97aSMatthew G Knepley 
46746636e97aSMatthew G Knepley   Collective on DM
46756636e97aSMatthew G Knepley 
46766636e97aSMatthew G Knepley   Input Parameter:
46776636e97aSMatthew G Knepley . dm - the DM
46786636e97aSMatthew G Knepley 
46796636e97aSMatthew G Knepley   Output Parameter:
46806636e97aSMatthew G Knepley . c - coordinate vector
46816636e97aSMatthew G Knepley 
46826636e97aSMatthew G Knepley   Note:
46836636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
46846636e97aSMatthew G Knepley 
46856636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
46866636e97aSMatthew G Knepley 
46876636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
46886636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
46896636e97aSMatthew G Knepley 
46906636e97aSMatthew G Knepley   Level: intermediate
46916636e97aSMatthew G Knepley 
46926636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
469381e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
46946636e97aSMatthew G Knepley @*/
46956636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
46966636e97aSMatthew G Knepley {
46976636e97aSMatthew G Knepley   PetscErrorCode ierr;
46986636e97aSMatthew G Knepley 
46996636e97aSMatthew G Knepley   PetscFunctionBegin;
47006636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
47016636e97aSMatthew G Knepley   PetscValidPointer(c,2);
470281e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
47036636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
47046636e97aSMatthew G Knepley   PetscFunctionReturn(0);
47056636e97aSMatthew G Knepley }
47066636e97aSMatthew G Knepley 
470781e9a530SVaclav Hapla /*@
470881e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
470981e9a530SVaclav Hapla 
471081e9a530SVaclav Hapla   Not collective
471181e9a530SVaclav Hapla 
471281e9a530SVaclav Hapla   Input Parameter:
471381e9a530SVaclav Hapla . dm - the DM
471481e9a530SVaclav Hapla 
471581e9a530SVaclav Hapla   Output Parameter:
471681e9a530SVaclav Hapla . c - coordinate vector
471781e9a530SVaclav Hapla 
471881e9a530SVaclav Hapla   Level: advanced
471981e9a530SVaclav Hapla 
472081e9a530SVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
472181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
472281e9a530SVaclav Hapla @*/
472381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
472481e9a530SVaclav Hapla {
472581e9a530SVaclav Hapla   PetscFunctionBegin;
472681e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
472781e9a530SVaclav Hapla   PetscValidPointer(c,2);
472881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
47296636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
47306636e97aSMatthew G Knepley   PetscFunctionReturn(0);
47316636e97aSMatthew G Knepley }
47326636e97aSMatthew G Knepley 
47332db98f8dSVaclav Hapla /*@
47342db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
47352db98f8dSVaclav Hapla 
47362db98f8dSVaclav Hapla   Not collective
47372db98f8dSVaclav Hapla 
47382db98f8dSVaclav Hapla   Input Parameter:
47392db98f8dSVaclav Hapla + dm - the DM
47402db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
47412db98f8dSVaclav Hapla 
47422db98f8dSVaclav Hapla   Output Parameter:
47432db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
47442db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
47452db98f8dSVaclav Hapla 
47462db98f8dSVaclav Hapla   Note:
47472db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
47482db98f8dSVaclav Hapla 
47492db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
47502db98f8dSVaclav Hapla 
47512db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
47522db98f8dSVaclav Hapla 
47532db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
47542db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
47552db98f8dSVaclav Hapla 
47562db98f8dSVaclav Hapla   Level: advanced
47572db98f8dSVaclav Hapla 
47582db98f8dSVaclav Hapla .keywords: distributed array, get, corners, nodes, local indices, coordinates
47592db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
47602db98f8dSVaclav Hapla @*/
47612db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
47622db98f8dSVaclav Hapla {
47632db98f8dSVaclav Hapla   PetscSection        cs, newcs;
47642db98f8dSVaclav Hapla   Vec                 coords;
47652db98f8dSVaclav Hapla   const PetscScalar   *arr;
47662db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
47672db98f8dSVaclav Hapla   PetscInt            n;
47682db98f8dSVaclav Hapla   PetscErrorCode      ierr;
47692db98f8dSVaclav Hapla 
47702db98f8dSVaclav Hapla   PetscFunctionBegin;
47712db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
47722db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
47732db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
47742db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
47752db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
47762db98f8dSVaclav Hapla   if (!dm->coordinateDM || !dm->coordinateDM->defaultSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
47772db98f8dSVaclav Hapla   cs = dm->coordinateDM->defaultSection;
47782db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
47792db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
47802db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
47812db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
47822db98f8dSVaclav Hapla   if (pCoord) {
47832db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
47842db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
47852db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
47862db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
4787ad9ac99dSVaclav Hapla   } else {
4788ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
47892db98f8dSVaclav Hapla   }
4790ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
4791ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
47922db98f8dSVaclav Hapla   PetscFunctionReturn(0);
47932db98f8dSVaclav Hapla }
47942db98f8dSVaclav Hapla 
4795f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4796f19dbd58SToby Isaac {
4797f19dbd58SToby Isaac   PetscErrorCode ierr;
4798f19dbd58SToby Isaac 
4799f19dbd58SToby Isaac   PetscFunctionBegin;
4800f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4801f19dbd58SToby Isaac   PetscValidPointer(field,2);
4802f19dbd58SToby Isaac   if (!dm->coordinateField) {
4803f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4804f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4805f19dbd58SToby Isaac     }
4806f19dbd58SToby Isaac   }
4807f19dbd58SToby Isaac   *field = dm->coordinateField;
4808f19dbd58SToby Isaac   PetscFunctionReturn(0);
4809f19dbd58SToby Isaac }
4810f19dbd58SToby Isaac 
4811f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4812f19dbd58SToby Isaac {
4813f19dbd58SToby Isaac   PetscErrorCode ierr;
4814f19dbd58SToby Isaac 
4815f19dbd58SToby Isaac   PetscFunctionBegin;
4816f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4817f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4818c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
4819f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4820f19dbd58SToby Isaac   dm->coordinateField = field;
4821f19dbd58SToby Isaac   PetscFunctionReturn(0);
4822f19dbd58SToby Isaac }
4823f19dbd58SToby Isaac 
48246636e97aSMatthew G Knepley /*@
48251cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
48266636e97aSMatthew G Knepley 
48276636e97aSMatthew G Knepley   Collective on DM
48286636e97aSMatthew G Knepley 
48296636e97aSMatthew G Knepley   Input Parameter:
48306636e97aSMatthew G Knepley . dm - the DM
48316636e97aSMatthew G Knepley 
48326636e97aSMatthew G Knepley   Output Parameter:
48336636e97aSMatthew G Knepley . cdm - coordinate DM
48346636e97aSMatthew G Knepley 
48356636e97aSMatthew G Knepley   Level: intermediate
48366636e97aSMatthew G Knepley 
48376636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
48381cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
48396636e97aSMatthew G Knepley @*/
48406636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
48416636e97aSMatthew G Knepley {
48426636e97aSMatthew G Knepley   PetscErrorCode ierr;
48436636e97aSMatthew G Knepley 
48446636e97aSMatthew G Knepley   PetscFunctionBegin;
48456636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
48466636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
48476636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
4848308f8a94SToby Isaac     DM cdm;
4849308f8a94SToby Isaac 
485082f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
4851308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
4852308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
4853308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
4854308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
4855308f8a94SToby Isaac     dm->coordinateDM = cdm;
48566636e97aSMatthew G Knepley   }
48576636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
48586636e97aSMatthew G Knepley   PetscFunctionReturn(0);
48596636e97aSMatthew G Knepley }
4860e87bb0d3SMatthew G Knepley 
48611cfe2091SMatthew G. Knepley /*@
48621cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
48631cfe2091SMatthew G. Knepley 
48641cfe2091SMatthew G. Knepley   Logically Collective on DM
48651cfe2091SMatthew G. Knepley 
48661cfe2091SMatthew G. Knepley   Input Parameters:
48671cfe2091SMatthew G. Knepley + dm - the DM
48681cfe2091SMatthew G. Knepley - cdm - coordinate DM
48691cfe2091SMatthew G. Knepley 
48701cfe2091SMatthew G. Knepley   Level: intermediate
48711cfe2091SMatthew G. Knepley 
48721cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
48731cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
48741cfe2091SMatthew G. Knepley @*/
48751cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
48761cfe2091SMatthew G. Knepley {
48771cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
48781cfe2091SMatthew G. Knepley 
48791cfe2091SMatthew G. Knepley   PetscFunctionBegin;
48801cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
48811cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4882f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
48831cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
48841cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
48851cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
48861cfe2091SMatthew G. Knepley }
48871cfe2091SMatthew G. Knepley 
488846e270d4SMatthew G. Knepley /*@
488946e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
489046e270d4SMatthew G. Knepley 
489146e270d4SMatthew G. Knepley   Not Collective
489246e270d4SMatthew G. Knepley 
489346e270d4SMatthew G. Knepley   Input Parameter:
489446e270d4SMatthew G. Knepley . dm - The DM object
489546e270d4SMatthew G. Knepley 
489646e270d4SMatthew G. Knepley   Output Parameter:
489746e270d4SMatthew G. Knepley . dim - The embedding dimension
489846e270d4SMatthew G. Knepley 
489946e270d4SMatthew G. Knepley   Level: intermediate
490046e270d4SMatthew G. Knepley 
490146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4902e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
490346e270d4SMatthew G. Knepley @*/
490446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
490546e270d4SMatthew G. Knepley {
490646e270d4SMatthew G. Knepley   PetscFunctionBegin;
490746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
490846e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
49099a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
49109a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
49119a9a41abSToby Isaac   }
491246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
491346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
491446e270d4SMatthew G. Knepley }
491546e270d4SMatthew G. Knepley 
491646e270d4SMatthew G. Knepley /*@
491746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
491846e270d4SMatthew G. Knepley 
491946e270d4SMatthew G. Knepley   Not Collective
492046e270d4SMatthew G. Knepley 
492146e270d4SMatthew G. Knepley   Input Parameters:
492246e270d4SMatthew G. Knepley + dm  - The DM object
492346e270d4SMatthew G. Knepley - dim - The embedding dimension
492446e270d4SMatthew G. Knepley 
492546e270d4SMatthew G. Knepley   Level: intermediate
492646e270d4SMatthew G. Knepley 
492746e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4928e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
492946e270d4SMatthew G. Knepley @*/
493046e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
493146e270d4SMatthew G. Knepley {
4932f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4933f17e8794SMatthew G. Knepley 
493446e270d4SMatthew G. Knepley   PetscFunctionBegin;
493546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
493646e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4937f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
493846e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
493946e270d4SMatthew G. Knepley }
494046e270d4SMatthew G. Knepley 
4941e8abe2deSMatthew G. Knepley /*@
4942e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4943e8abe2deSMatthew G. Knepley 
4944116501dfSVaclav Hapla   Collective on DM
4945e8abe2deSMatthew G. Knepley 
4946e8abe2deSMatthew G. Knepley   Input Parameter:
4947e8abe2deSMatthew G. Knepley . dm - The DM object
4948e8abe2deSMatthew G. Knepley 
4949e8abe2deSMatthew G. Knepley   Output Parameter:
4950e8abe2deSMatthew G. Knepley . section - The PetscSection object
4951e8abe2deSMatthew G. Knepley 
4952e8abe2deSMatthew G. Knepley   Level: intermediate
4953e8abe2deSMatthew G. Knepley 
4954e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4955e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
4956e8abe2deSMatthew G. Knepley @*/
4957e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4958e8abe2deSMatthew G. Knepley {
4959e8abe2deSMatthew G. Knepley   DM             cdm;
4960e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4961e8abe2deSMatthew G. Knepley 
4962e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4963e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4964e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4965e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4966e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
4967e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4968e8abe2deSMatthew G. Knepley }
4969e8abe2deSMatthew G. Knepley 
4970e8abe2deSMatthew G. Knepley /*@
4971e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4972e8abe2deSMatthew G. Knepley 
4973e8abe2deSMatthew G. Knepley   Not Collective
4974e8abe2deSMatthew G. Knepley 
4975e8abe2deSMatthew G. Knepley   Input Parameters:
4976e8abe2deSMatthew G. Knepley + dm      - The DM object
497746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4978e8abe2deSMatthew G. Knepley - section - The PetscSection object
4979e8abe2deSMatthew G. Knepley 
4980e8abe2deSMatthew G. Knepley   Level: intermediate
4981e8abe2deSMatthew G. Knepley 
4982e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4983e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
4984e8abe2deSMatthew G. Knepley @*/
498546e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4986e8abe2deSMatthew G. Knepley {
4987e8abe2deSMatthew G. Knepley   DM             cdm;
4988e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4989e8abe2deSMatthew G. Knepley 
4990e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4991e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
499246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4993e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4994e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
499546e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
49964c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
499746e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
499846e270d4SMatthew G. Knepley 
499946e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
500046e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
500146e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
500246e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
500346e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
500446e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
500546e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
500646e270d4SMatthew G. Knepley     }
50076be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
500846e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
500946e270d4SMatthew G. Knepley   }
5010e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5011e8abe2deSMatthew G. Knepley }
5012e8abe2deSMatthew G. Knepley 
50135dc8c3f7SMatthew G. Knepley /*@C
501490b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
50155dc8c3f7SMatthew G. Knepley 
50165dc8c3f7SMatthew G. Knepley   Input Parameters:
501790b157c4SStefano Zampini . dm      - The DM object
501890b157c4SStefano Zampini 
501990b157c4SStefano Zampini   Output Parameters:
502090b157c4SStefano Zampini + per     - Whether the DM is periodic or not
50215dc8c3f7SMatthew 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
50225dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
50235dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
50245dc8c3f7SMatthew G. Knepley 
50255dc8c3f7SMatthew G. Knepley   Level: developer
50265dc8c3f7SMatthew G. Knepley 
50275dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
50285dc8c3f7SMatthew G. Knepley @*/
502990b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5030c6b900c6SMatthew G. Knepley {
5031c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5032c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
503390b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5034c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5035c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
50365dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5037c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5038c6b900c6SMatthew G. Knepley }
5039c6b900c6SMatthew G. Knepley 
50405dc8c3f7SMatthew G. Knepley /*@C
50415dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
50425dc8c3f7SMatthew G. Knepley 
50435dc8c3f7SMatthew G. Knepley   Input Parameters:
50445dc8c3f7SMatthew G. Knepley + dm      - The DM object
504590b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
50465dc8c3f7SMatthew 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
50475dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
50485dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
50495dc8c3f7SMatthew G. Knepley 
50505dc8c3f7SMatthew G. Knepley   Level: developer
50515dc8c3f7SMatthew G. Knepley 
50525dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
50535dc8c3f7SMatthew G. Knepley @*/
505490b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5055c6b900c6SMatthew G. Knepley {
5056c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5057c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5058c6b900c6SMatthew G. Knepley 
5059c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5060c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
506190b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
506290b157c4SStefano Zampini   if (maxCell) {
506390b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
506490b157c4SStefano Zampini     PetscValidPointer(L,4);
506590b157c4SStefano Zampini     PetscValidPointer(bd,5);
506690b157c4SStefano Zampini   }
50675dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
50685dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
506990b157c4SStefano Zampini   if (maxCell) {
50705dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
50715dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
507290b157c4SStefano Zampini   }
5073072d7d67SStefano Zampini   dm->periodic = per;
5074c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5075c6b900c6SMatthew G. Knepley }
5076c6b900c6SMatthew G. Knepley 
50772e17dfb7SMatthew G. Knepley /*@
50782e17dfb7SMatthew 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.
50792e17dfb7SMatthew G. Knepley 
50802e17dfb7SMatthew G. Knepley   Input Parameters:
50812e17dfb7SMatthew G. Knepley + dm     - The DM
508265da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
508365da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
50842e17dfb7SMatthew G. Knepley 
50852e17dfb7SMatthew G. Knepley   Output Parameter:
50862e17dfb7SMatthew G. Knepley . out - The localized coordinate point
50872e17dfb7SMatthew G. Knepley 
50882e17dfb7SMatthew G. Knepley   Level: developer
50892e17dfb7SMatthew G. Knepley 
50902e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
50912e17dfb7SMatthew G. Knepley @*/
509265da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
50932e17dfb7SMatthew G. Knepley {
50942e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
50952e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
50962e17dfb7SMatthew G. Knepley 
50972e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
50982e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
50992e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
51002e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
51012e17dfb7SMatthew G. Knepley   } else {
510265da65dcSMatthew G. Knepley     if (endpoint) {
510365da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
5104da3333bfSMatthew 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)) {
5105da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
510665da65dcSMatthew G. Knepley         } else {
5107da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
510865da65dcSMatthew G. Knepley         }
510965da65dcSMatthew G. Knepley       }
511065da65dcSMatthew G. Knepley     } else {
51112e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
51121118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
51132e17dfb7SMatthew G. Knepley       }
51142e17dfb7SMatthew G. Knepley     }
511565da65dcSMatthew G. Knepley   }
51162e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
51172e17dfb7SMatthew G. Knepley }
51182e17dfb7SMatthew G. Knepley 
51192e17dfb7SMatthew G. Knepley /*
51202e17dfb7SMatthew 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.
51212e17dfb7SMatthew G. Knepley 
51222e17dfb7SMatthew G. Knepley   Input Parameters:
51232e17dfb7SMatthew G. Knepley + dm     - The DM
51242e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
51252e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
51262e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
51272e17dfb7SMatthew G. Knepley 
51282e17dfb7SMatthew G. Knepley   Output Parameter:
51292e17dfb7SMatthew G. Knepley . out - The localized coordinate point
51302e17dfb7SMatthew G. Knepley 
51312e17dfb7SMatthew G. Knepley   Level: developer
51322e17dfb7SMatthew G. Knepley 
51332e17dfb7SMatthew 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
51342e17dfb7SMatthew G. Knepley 
51352e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
51362e17dfb7SMatthew G. Knepley */
51372e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
51382e17dfb7SMatthew G. Knepley {
51392e17dfb7SMatthew G. Knepley   PetscInt d;
51402e17dfb7SMatthew G. Knepley 
51412e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
51422e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
51432e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
51442e17dfb7SMatthew G. Knepley   } else {
51452e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5146908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
51472e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
51482e17dfb7SMatthew G. Knepley       } else {
51492e17dfb7SMatthew G. Knepley         out[d] = in[d];
51502e17dfb7SMatthew G. Knepley       }
51512e17dfb7SMatthew G. Knepley     }
51522e17dfb7SMatthew G. Knepley   }
51532e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
51542e17dfb7SMatthew G. Knepley }
51552e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
51562e17dfb7SMatthew G. Knepley {
51572e17dfb7SMatthew G. Knepley   PetscInt d;
51582e17dfb7SMatthew G. Knepley 
51592e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
51602e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
51612e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
51622e17dfb7SMatthew G. Knepley   } else {
51632e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5164908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
51652e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
51662e17dfb7SMatthew G. Knepley       } else {
51672e17dfb7SMatthew G. Knepley         out[d] = in[d];
51682e17dfb7SMatthew G. Knepley       }
51692e17dfb7SMatthew G. Knepley     }
51702e17dfb7SMatthew G. Knepley   }
51712e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
51722e17dfb7SMatthew G. Knepley }
51732e17dfb7SMatthew G. Knepley 
51742e17dfb7SMatthew G. Knepley /*
51752e17dfb7SMatthew 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.
51762e17dfb7SMatthew G. Knepley 
51772e17dfb7SMatthew G. Knepley   Input Parameters:
51782e17dfb7SMatthew G. Knepley + dm     - The DM
51792e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
51802e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
51812e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
51822e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
51832e17dfb7SMatthew G. Knepley 
51842e17dfb7SMatthew G. Knepley   Output Parameter:
51852e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
51862e17dfb7SMatthew G. Knepley 
51872e17dfb7SMatthew G. Knepley   Level: developer
51882e17dfb7SMatthew G. Knepley 
51892e17dfb7SMatthew 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
51902e17dfb7SMatthew G. Knepley 
51912e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
51922e17dfb7SMatthew G. Knepley */
51932e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
51942e17dfb7SMatthew G. Knepley {
51952e17dfb7SMatthew G. Knepley   PetscInt d;
51962e17dfb7SMatthew G. Knepley 
51972e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
51982e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
51992e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
52002e17dfb7SMatthew G. Knepley   } else {
52012e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
5202908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
52032e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
52042e17dfb7SMatthew G. Knepley       } else {
52052e17dfb7SMatthew G. Knepley         out[d] += in[d];
52062e17dfb7SMatthew G. Knepley       }
52072e17dfb7SMatthew G. Knepley     }
52082e17dfb7SMatthew G. Knepley   }
52092e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
52102e17dfb7SMatthew G. Knepley }
52112e17dfb7SMatthew G. Knepley 
521236447a5eSToby Isaac /*@
52138f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
52148f700142SStefano Zampini 
52158f700142SStefano Zampini   Not collective
521636447a5eSToby Isaac 
521736447a5eSToby Isaac   Input Parameter:
521836447a5eSToby Isaac . dm - The DM
521936447a5eSToby Isaac 
522036447a5eSToby Isaac   Output Parameter:
522136447a5eSToby Isaac   areLocalized - True if localized
522236447a5eSToby Isaac 
522336447a5eSToby Isaac   Level: developer
522436447a5eSToby Isaac 
52258f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
522636447a5eSToby Isaac @*/
52278f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
522836447a5eSToby Isaac {
522936447a5eSToby Isaac   DM             cdm;
523036447a5eSToby Isaac   PetscSection   coordSection;
523146a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
523246a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
523336447a5eSToby Isaac   PetscErrorCode ierr;
523436447a5eSToby Isaac 
523536447a5eSToby Isaac   PetscFunctionBegin;
523636447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
523746a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
52388b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
523946a3a80fSLisandro Dalcin 
524036447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
524136447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
524236447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
524346a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
524446a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
524546a3a80fSLisandro Dalcin 
524646a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
524736447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
524836447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
524946a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
525046a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
525136447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
525246a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
525336447a5eSToby Isaac   }
52548f700142SStefano Zampini   *areLocalized = alreadyLocalized;
525536447a5eSToby Isaac   PetscFunctionReturn(0);
525636447a5eSToby Isaac }
525736447a5eSToby Isaac 
52588f700142SStefano Zampini /*@
52598f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
52608f700142SStefano Zampini 
52618f700142SStefano Zampini   Collective on dm
52628f700142SStefano Zampini 
52638f700142SStefano Zampini   Input Parameter:
52648f700142SStefano Zampini . dm - The DM
52658f700142SStefano Zampini 
52668f700142SStefano Zampini   Output Parameter:
52678f700142SStefano Zampini   areLocalized - True if localized
52688f700142SStefano Zampini 
52698f700142SStefano Zampini   Level: developer
52708f700142SStefano Zampini 
52718f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
52728f700142SStefano Zampini @*/
52738f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
52748f700142SStefano Zampini {
52758f700142SStefano Zampini   PetscBool      localized;
52768f700142SStefano Zampini   PetscErrorCode ierr;
52778f700142SStefano Zampini 
52788f700142SStefano Zampini   PetscFunctionBegin;
52798f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52808f700142SStefano Zampini   PetscValidPointer(areLocalized, 2);
52818f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
52828f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
52838f700142SStefano Zampini   PetscFunctionReturn(0);
52848f700142SStefano Zampini }
528536447a5eSToby Isaac 
52862e17dfb7SMatthew G. Knepley /*@
5287492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
52882e17dfb7SMatthew G. Knepley 
52898f700142SStefano Zampini   Collective on dm
52908f700142SStefano Zampini 
52912e17dfb7SMatthew G. Knepley   Input Parameter:
52922e17dfb7SMatthew G. Knepley . dm - The DM
52932e17dfb7SMatthew G. Knepley 
52942e17dfb7SMatthew G. Knepley   Level: developer
52952e17dfb7SMatthew G. Knepley 
52968f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
52972e17dfb7SMatthew G. Knepley @*/
52982e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
52992e17dfb7SMatthew G. Knepley {
53002e17dfb7SMatthew G. Knepley   DM             cdm;
53012e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
53022e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
53033e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
53043e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
5305e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
53063e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
53073e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
53082e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
53092e17dfb7SMatthew G. Knepley 
53102e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
53112e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
531292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
5313f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
5314f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
5315f7cbd40bSStefano Zampini 
53162e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
53172e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
53182e17dfb7SMatthew G. Knepley   {
53192e17dfb7SMatthew G. Knepley     PetscBool isplex;
53202e17dfb7SMatthew G. Knepley 
53212e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
53222e17dfb7SMatthew G. Knepley     if (isplex) {
53232e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
53243e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
532569291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
53263e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
53273e922f36SToby Isaac       newStart = vStart;
53283e922f36SToby Isaac       newEnd   = vEnd;
53293e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
53303e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
53313e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
53323e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
53333e922f36SToby Isaac       }
53342e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
53352e17dfb7SMatthew G. Knepley   }
53362e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
533743eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
53382e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
53393e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
5340e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
53413e922f36SToby Isaac 
53422e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
53432e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
53442e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
53452e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
53463e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
53473e922f36SToby Isaac 
534869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
53493e922f36SToby Isaac   localized = &anchor[bs];
53503e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
53513e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
53523e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
53533e922f36SToby Isaac 
53543e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
53553e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
53563e922f36SToby Isaac       PetscInt     b;
53573e922f36SToby Isaac 
53583e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
53593e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
53603e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
53613e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
53623e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
53633e922f36SToby Isaac         for (b = 0; b < bs; b++) {
53643e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
53653e922f36SToby Isaac         }
53663e922f36SToby Isaac         if (b < bs) break;
53673e922f36SToby Isaac       }
53683e922f36SToby Isaac       if (d < dof/bs) {
53693e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
53703e922f36SToby Isaac           PetscInt cdof;
53713e922f36SToby Isaac 
53723e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
53733e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
53743e922f36SToby Isaac         }
53753e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
53763e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
53773e922f36SToby Isaac       }
53783e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
53793e922f36SToby Isaac     }
53803e922f36SToby Isaac   }
53813e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
53823e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
538369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
53843e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
538569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
53863e922f36SToby Isaac     PetscFunctionReturn(0);
53873e922f36SToby Isaac   }
53882e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
53892e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
53902e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
53912e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
53922e17dfb7SMatthew G. Knepley   }
53932e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
53942e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5395c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
53962e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
53972e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
53982e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
53992e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5400c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
54012e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
54022e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
54032e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
54042e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
54052e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
54062e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
54072e17dfb7SMatthew G. Knepley   }
54083e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
54093e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
54103e922f36SToby Isaac 
54112e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
54122e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
54133e922f36SToby Isaac       PetscInt     b, cdof;
54142e17dfb7SMatthew G. Knepley 
54153e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
54163e922f36SToby Isaac       if (!cdof) continue;
54172e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
54182e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
54192e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
54202e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
54212e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
54222e17dfb7SMatthew G. Knepley     }
54233e922f36SToby Isaac   }
542469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
542569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5426c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
54272e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
54282e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
54292e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
54302e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
54312e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
54322e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
54332e17dfb7SMatthew G. Knepley }
54342e17dfb7SMatthew G. Knepley 
5435e87bb0d3SMatthew G Knepley /*@
54363a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5437e87bb0d3SMatthew G Knepley 
54383a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5439e87bb0d3SMatthew G Knepley 
5440e87bb0d3SMatthew G Knepley   Input Parameters:
5441e87bb0d3SMatthew G Knepley + dm - The DM
54423a93e3b7SToby Isaac . v - The Vec of points
544362a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
54443a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5445e87bb0d3SMatthew G Knepley 
544661e3bb9bSMatthew G Knepley   Output Parameter:
544762a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
544862a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
54493a93e3b7SToby Isaac 
5450e87bb0d3SMatthew G Knepley 
5451e87bb0d3SMatthew G Knepley   Level: developer
545261e3bb9bSMatthew G Knepley 
545362a38674SMatthew G. Knepley   Notes:
54543a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
545562a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
54563a93e3b7SToby Isaac 
54573a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
545862a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
54593a93e3b7SToby Isaac 
54603a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
54613a93e3b7SToby Isaac 
546262a38674SMatthew G. Knepley $    const PetscSFNode *cells;
546362a38674SMatthew G. Knepley $    PetscInt           nFound;
5464a6216909SToby Isaac $    const PetscInt    *found;
546562a38674SMatthew G. Knepley $
5466a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
54673a93e3b7SToby Isaac 
54683a93e3b7SToby 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
54693a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
54703a93e3b7SToby Isaac 
547161e3bb9bSMatthew G Knepley .keywords: point location, mesh
547262a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
547361e3bb9bSMatthew G Knepley @*/
547462a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5475e87bb0d3SMatthew G Knepley {
5476735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5477735aa83eSMatthew G Knepley 
5478e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5479e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5480e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5481e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
54823a93e3b7SToby Isaac   if (*cellSF) {
54833a93e3b7SToby Isaac     PetscMPIInt result;
54843a93e3b7SToby Isaac 
5485e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5486a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
54873a93e3b7SToby 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");
5488e0fc9d1bSMatthew G. Knepley   } else {
54893a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
54903a93e3b7SToby Isaac   }
549147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5492735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
549362a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
549482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
549547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5496e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5497e87bb0d3SMatthew G Knepley }
549814f150ffSMatthew G. Knepley 
5499f4d763aaSMatthew G. Knepley /*@
5500f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5501f4d763aaSMatthew G. Knepley 
55028f700142SStefano Zampini   Collective on dm
55038f700142SStefano Zampini 
5504f4d763aaSMatthew G. Knepley   Input Parameter:
5505f4d763aaSMatthew G. Knepley . dm - The original DM
5506f4d763aaSMatthew G. Knepley 
5507f4d763aaSMatthew G. Knepley   Output Parameter:
5508f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5509f4d763aaSMatthew G. Knepley 
5510f4d763aaSMatthew G. Knepley   Level: intermediate
5511f4d763aaSMatthew G. Knepley 
5512e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5513f4d763aaSMatthew G. Knepley @*/
551414f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
551514f150ffSMatthew G. Knepley {
5516c26acbdeSMatthew G. Knepley   PetscSection   section;
55172d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
551814f150ffSMatthew G. Knepley   PetscErrorCode ierr;
551914f150ffSMatthew G. Knepley 
552014f150ffSMatthew G. Knepley   PetscFunctionBegin;
552114f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
552214f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5523e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5524c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5525127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
55262d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5527c26acbdeSMatthew G. Knepley     *odm = dm;
5528c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5529c26acbdeSMatthew G. Knepley   }
553014f150ffSMatthew G. Knepley   if (!dm->dmBC) {
55310305aff6SMatthew G. Knepley     PetscDS      ds;
5532c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
553314f150ffSMatthew G. Knepley     PetscSF      sf;
553414f150ffSMatthew G. Knepley 
553514f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
55360305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
55370305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
553814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5539e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
554014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
554114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
554215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5543e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
554414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
554514f150ffSMatthew G. Knepley   }
554614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
554714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
554814f150ffSMatthew G. Knepley }
5549f4d763aaSMatthew G. Knepley 
5550f4d763aaSMatthew G. Knepley /*@
5551cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5552f4d763aaSMatthew G. Knepley 
5553f4d763aaSMatthew G. Knepley   Input Parameter:
5554f4d763aaSMatthew G. Knepley . dm - The original DM
5555f4d763aaSMatthew G. Knepley 
5556cdb7a50dSMatthew G. Knepley   Output Parameters:
5557cdb7a50dSMatthew G. Knepley + num - The output sequence number
5558cdb7a50dSMatthew G. Knepley - val - The output sequence value
5559f4d763aaSMatthew G. Knepley 
5560f4d763aaSMatthew G. Knepley   Level: intermediate
5561f4d763aaSMatthew G. Knepley 
5562f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5563f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5564f4d763aaSMatthew G. Knepley 
5565f4d763aaSMatthew G. Knepley .seealso: VecView()
5566f4d763aaSMatthew G. Knepley @*/
5567cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5568f4d763aaSMatthew G. Knepley {
5569f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5570f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5571cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5572cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5573f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5574f4d763aaSMatthew G. Knepley }
5575f4d763aaSMatthew G. Knepley 
5576f4d763aaSMatthew G. Knepley /*@
5577cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5578f4d763aaSMatthew G. Knepley 
5579f4d763aaSMatthew G. Knepley   Input Parameters:
5580f4d763aaSMatthew G. Knepley + dm - The original DM
5581cdb7a50dSMatthew G. Knepley . num - The output sequence number
5582cdb7a50dSMatthew G. Knepley - val - The output sequence value
5583f4d763aaSMatthew G. Knepley 
5584f4d763aaSMatthew G. Knepley   Level: intermediate
5585f4d763aaSMatthew G. Knepley 
5586f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5587f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5588f4d763aaSMatthew G. Knepley 
5589f4d763aaSMatthew G. Knepley .seealso: VecView()
5590f4d763aaSMatthew G. Knepley @*/
5591cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5592f4d763aaSMatthew G. Knepley {
5593f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5594f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5595f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5596cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5597cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5598cdb7a50dSMatthew G. Knepley }
5599cdb7a50dSMatthew G. Knepley 
5600cdb7a50dSMatthew G. Knepley /*@C
5601cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5602cdb7a50dSMatthew G. Knepley 
5603cdb7a50dSMatthew G. Knepley   Input Parameters:
5604cdb7a50dSMatthew G. Knepley + dm   - The original DM
5605cdb7a50dSMatthew G. Knepley . name - The sequence name
5606cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5607cdb7a50dSMatthew G. Knepley 
5608cdb7a50dSMatthew G. Knepley   Output Parameter:
5609cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5610cdb7a50dSMatthew G. Knepley 
5611cdb7a50dSMatthew G. Knepley   Level: intermediate
5612cdb7a50dSMatthew G. Knepley 
5613cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5614cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5615cdb7a50dSMatthew G. Knepley 
5616cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5617cdb7a50dSMatthew G. Knepley @*/
5618cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5619cdb7a50dSMatthew G. Knepley {
5620cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5621cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5622cdb7a50dSMatthew G. Knepley 
5623cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5624cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5625cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5626cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5627cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5628cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5629cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5630cdb7a50dSMatthew G. Knepley     PetscScalar value;
5631cdb7a50dSMatthew G. Knepley 
563239d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
56334aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5634cdb7a50dSMatthew G. Knepley #endif
5635cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5636f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5637f4d763aaSMatthew G. Knepley }
56388e4ac7eaSMatthew G. Knepley 
56398e4ac7eaSMatthew G. Knepley /*@
56408e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
56418e4ac7eaSMatthew G. Knepley 
56428e4ac7eaSMatthew G. Knepley   Not collective
56438e4ac7eaSMatthew G. Knepley 
56448e4ac7eaSMatthew G. Knepley   Input Parameter:
56458e4ac7eaSMatthew G. Knepley . dm - The DM
56468e4ac7eaSMatthew G. Knepley 
56478e4ac7eaSMatthew G. Knepley   Output Parameter:
56488e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
56498e4ac7eaSMatthew G. Knepley 
56508e4ac7eaSMatthew G. Knepley   Level: beginner
56518e4ac7eaSMatthew G. Knepley 
56528e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
56538e4ac7eaSMatthew G. Knepley @*/
56548e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
56558e4ac7eaSMatthew G. Knepley {
56568e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
56578e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56588e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
56598e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
56608e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
56618e4ac7eaSMatthew G. Knepley }
56628e4ac7eaSMatthew G. Knepley 
56638e4ac7eaSMatthew G. Knepley /*@
56645d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
56658e4ac7eaSMatthew G. Knepley 
56668e4ac7eaSMatthew G. Knepley   Collective on dm
56678e4ac7eaSMatthew G. Knepley 
56688e4ac7eaSMatthew G. Knepley   Input Parameters:
56698e4ac7eaSMatthew G. Knepley + dm - The DM
56708e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
56718e4ac7eaSMatthew G. Knepley 
56725d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
56735d3b26e6SMatthew G. Knepley 
56748e4ac7eaSMatthew G. Knepley   Level: beginner
56758e4ac7eaSMatthew G. Knepley 
56765d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
56778e4ac7eaSMatthew G. Knepley @*/
56788e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
56798e4ac7eaSMatthew G. Knepley {
56808e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
56818e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
56828833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
56838e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
56848e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
56858e4ac7eaSMatthew G. Knepley }
5686c58f1c22SToby Isaac 
5687c58f1c22SToby Isaac 
5688c58f1c22SToby Isaac /*@C
5689c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5690c58f1c22SToby Isaac 
5691c58f1c22SToby Isaac   Not Collective
5692c58f1c22SToby Isaac 
5693c58f1c22SToby Isaac   Input Parameters:
5694c58f1c22SToby Isaac + dm   - The DM object
5695c58f1c22SToby Isaac - name - The label name
5696c58f1c22SToby Isaac 
5697c58f1c22SToby Isaac   Level: intermediate
5698c58f1c22SToby Isaac 
5699c58f1c22SToby Isaac .keywords: mesh
5700c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5701c58f1c22SToby Isaac @*/
5702c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5703c58f1c22SToby Isaac {
5704c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5705c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5706d67d17b1SMatthew G. Knepley   const char    *lname;
5707c58f1c22SToby Isaac   PetscErrorCode ierr;
5708c58f1c22SToby Isaac 
5709c58f1c22SToby Isaac   PetscFunctionBegin;
5710c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5711c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5712c58f1c22SToby Isaac   while (next) {
5713d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
5714d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
5715c58f1c22SToby Isaac     if (flg) break;
5716c58f1c22SToby Isaac     next = next->next;
5717c58f1c22SToby Isaac   }
5718c58f1c22SToby Isaac   if (!flg) {
5719c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5720c58f1c22SToby Isaac 
5721c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5722d67d17b1SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &tmpLabel->label);CHKERRQ(ierr);
5723c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5724c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5725c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5726c58f1c22SToby Isaac   }
5727c58f1c22SToby Isaac   PetscFunctionReturn(0);
5728c58f1c22SToby Isaac }
5729c58f1c22SToby Isaac 
5730c58f1c22SToby Isaac /*@C
5731c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5732c58f1c22SToby Isaac 
5733c58f1c22SToby Isaac   Not Collective
5734c58f1c22SToby Isaac 
5735c58f1c22SToby Isaac   Input Parameters:
5736c58f1c22SToby Isaac + dm   - The DM object
5737c58f1c22SToby Isaac . name - The label name
5738c58f1c22SToby Isaac - point - The mesh point
5739c58f1c22SToby Isaac 
5740c58f1c22SToby Isaac   Output Parameter:
5741c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5742c58f1c22SToby Isaac 
5743c58f1c22SToby Isaac   Level: beginner
5744c58f1c22SToby Isaac 
5745c58f1c22SToby Isaac .keywords: mesh
5746c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5747c58f1c22SToby Isaac @*/
5748c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5749c58f1c22SToby Isaac {
5750c58f1c22SToby Isaac   DMLabel        label;
5751c58f1c22SToby Isaac   PetscErrorCode ierr;
5752c58f1c22SToby Isaac 
5753c58f1c22SToby Isaac   PetscFunctionBegin;
5754c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5755c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5756c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
575713903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5758c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5759c58f1c22SToby Isaac   PetscFunctionReturn(0);
5760c58f1c22SToby Isaac }
5761c58f1c22SToby Isaac 
5762c58f1c22SToby Isaac /*@C
5763c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5764c58f1c22SToby Isaac 
5765c58f1c22SToby Isaac   Not Collective
5766c58f1c22SToby Isaac 
5767c58f1c22SToby Isaac   Input Parameters:
5768c58f1c22SToby Isaac + dm   - The DM object
5769c58f1c22SToby Isaac . name - The label name
5770c58f1c22SToby Isaac . point - The mesh point
5771c58f1c22SToby Isaac - value - The label value for this point
5772c58f1c22SToby Isaac 
5773c58f1c22SToby Isaac   Output Parameter:
5774c58f1c22SToby Isaac 
5775c58f1c22SToby Isaac   Level: beginner
5776c58f1c22SToby Isaac 
5777c58f1c22SToby Isaac .keywords: mesh
5778c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5779c58f1c22SToby Isaac @*/
5780c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5781c58f1c22SToby Isaac {
5782c58f1c22SToby Isaac   DMLabel        label;
5783c58f1c22SToby Isaac   PetscErrorCode ierr;
5784c58f1c22SToby Isaac 
5785c58f1c22SToby Isaac   PetscFunctionBegin;
5786c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5787c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5788c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5789c58f1c22SToby Isaac   if (!label) {
5790c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5791c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5792c58f1c22SToby Isaac   }
5793c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5794c58f1c22SToby Isaac   PetscFunctionReturn(0);
5795c58f1c22SToby Isaac }
5796c58f1c22SToby Isaac 
5797c58f1c22SToby Isaac /*@C
5798c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5799c58f1c22SToby Isaac 
5800c58f1c22SToby Isaac   Not Collective
5801c58f1c22SToby Isaac 
5802c58f1c22SToby Isaac   Input Parameters:
5803c58f1c22SToby Isaac + dm   - The DM object
5804c58f1c22SToby Isaac . name - The label name
5805c58f1c22SToby Isaac . point - The mesh point
5806c58f1c22SToby Isaac - value - The label value for this point
5807c58f1c22SToby Isaac 
5808c58f1c22SToby Isaac   Output Parameter:
5809c58f1c22SToby Isaac 
5810c58f1c22SToby Isaac   Level: beginner
5811c58f1c22SToby Isaac 
5812c58f1c22SToby Isaac .keywords: mesh
5813c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5814c58f1c22SToby Isaac @*/
5815c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5816c58f1c22SToby Isaac {
5817c58f1c22SToby Isaac   DMLabel        label;
5818c58f1c22SToby Isaac   PetscErrorCode ierr;
5819c58f1c22SToby Isaac 
5820c58f1c22SToby Isaac   PetscFunctionBegin;
5821c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5822c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5823c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5824c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5825c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5826c58f1c22SToby Isaac   PetscFunctionReturn(0);
5827c58f1c22SToby Isaac }
5828c58f1c22SToby Isaac 
5829c58f1c22SToby Isaac /*@C
5830c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5831c58f1c22SToby Isaac 
5832c58f1c22SToby Isaac   Not Collective
5833c58f1c22SToby Isaac 
5834c58f1c22SToby Isaac   Input Parameters:
5835c58f1c22SToby Isaac + dm   - The DM object
5836c58f1c22SToby Isaac - name - The label name
5837c58f1c22SToby Isaac 
5838c58f1c22SToby Isaac   Output Parameter:
5839c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5840c58f1c22SToby Isaac 
5841c58f1c22SToby Isaac   Level: beginner
5842c58f1c22SToby Isaac 
5843c58f1c22SToby Isaac .keywords: mesh
5844df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
5845c58f1c22SToby Isaac @*/
5846c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5847c58f1c22SToby Isaac {
5848c58f1c22SToby Isaac   DMLabel        label;
5849c58f1c22SToby Isaac   PetscErrorCode ierr;
5850c58f1c22SToby Isaac 
5851c58f1c22SToby Isaac   PetscFunctionBegin;
5852c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5853c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5854c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5855c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5856c58f1c22SToby Isaac   *size = 0;
5857c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5858c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5859c58f1c22SToby Isaac   PetscFunctionReturn(0);
5860c58f1c22SToby Isaac }
5861c58f1c22SToby Isaac 
5862c58f1c22SToby Isaac /*@C
5863c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5864c58f1c22SToby Isaac 
5865c58f1c22SToby Isaac   Not Collective
5866c58f1c22SToby Isaac 
5867c58f1c22SToby Isaac   Input Parameters:
5868c58f1c22SToby Isaac + mesh - The DM object
5869c58f1c22SToby Isaac - name - The label name
5870c58f1c22SToby Isaac 
5871c58f1c22SToby Isaac   Output Parameter:
5872c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5873c58f1c22SToby Isaac 
5874c58f1c22SToby Isaac   Level: beginner
5875c58f1c22SToby Isaac 
5876c58f1c22SToby Isaac .keywords: mesh
5877c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5878c58f1c22SToby Isaac @*/
5879c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5880c58f1c22SToby Isaac {
5881c58f1c22SToby Isaac   DMLabel        label;
5882c58f1c22SToby Isaac   PetscErrorCode ierr;
5883c58f1c22SToby Isaac 
5884c58f1c22SToby Isaac   PetscFunctionBegin;
5885c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5886c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5887c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5888c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5889c58f1c22SToby Isaac   *ids = NULL;
5890dab2e251SBlaise Bourdin  if (label) {
5891c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5892dab2e251SBlaise Bourdin   } else {
5893dab2e251SBlaise Bourdin     /* returning an empty IS */
5894dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5895dab2e251SBlaise Bourdin   }
5896c58f1c22SToby Isaac   PetscFunctionReturn(0);
5897c58f1c22SToby Isaac }
5898c58f1c22SToby Isaac 
5899c58f1c22SToby Isaac /*@C
5900c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5901c58f1c22SToby Isaac 
5902c58f1c22SToby Isaac   Not Collective
5903c58f1c22SToby Isaac 
5904c58f1c22SToby Isaac   Input Parameters:
5905c58f1c22SToby Isaac + dm - The DM object
5906c58f1c22SToby Isaac . name - The label name
5907c58f1c22SToby Isaac - value - The stratum value
5908c58f1c22SToby Isaac 
5909c58f1c22SToby Isaac   Output Parameter:
5910c58f1c22SToby Isaac . size - The stratum size
5911c58f1c22SToby Isaac 
5912c58f1c22SToby Isaac   Level: beginner
5913c58f1c22SToby Isaac 
5914c58f1c22SToby Isaac .keywords: mesh
5915c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5916c58f1c22SToby Isaac @*/
5917c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5918c58f1c22SToby Isaac {
5919c58f1c22SToby Isaac   DMLabel        label;
5920c58f1c22SToby Isaac   PetscErrorCode ierr;
5921c58f1c22SToby Isaac 
5922c58f1c22SToby Isaac   PetscFunctionBegin;
5923c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5924c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5925c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5926c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5927c58f1c22SToby Isaac   *size = 0;
5928c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5929c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5930c58f1c22SToby Isaac   PetscFunctionReturn(0);
5931c58f1c22SToby Isaac }
5932c58f1c22SToby Isaac 
5933c58f1c22SToby Isaac /*@C
5934c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5935c58f1c22SToby Isaac 
5936c58f1c22SToby Isaac   Not Collective
5937c58f1c22SToby Isaac 
5938c58f1c22SToby Isaac   Input Parameters:
5939c58f1c22SToby Isaac + dm - The DM object
5940c58f1c22SToby Isaac . name - The label name
5941c58f1c22SToby Isaac - value - The stratum value
5942c58f1c22SToby Isaac 
5943c58f1c22SToby Isaac   Output Parameter:
5944c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5945c58f1c22SToby Isaac 
5946c58f1c22SToby Isaac   Level: beginner
5947c58f1c22SToby Isaac 
5948c58f1c22SToby Isaac .keywords: mesh
5949c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5950c58f1c22SToby Isaac @*/
5951c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5952c58f1c22SToby Isaac {
5953c58f1c22SToby Isaac   DMLabel        label;
5954c58f1c22SToby Isaac   PetscErrorCode ierr;
5955c58f1c22SToby Isaac 
5956c58f1c22SToby Isaac   PetscFunctionBegin;
5957c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5958c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5959c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5960c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5961c58f1c22SToby Isaac   *points = NULL;
5962c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5963c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5964c58f1c22SToby Isaac   PetscFunctionReturn(0);
5965c58f1c22SToby Isaac }
5966c58f1c22SToby Isaac 
59674de306b1SToby Isaac /*@C
59689044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
59694de306b1SToby Isaac 
59704de306b1SToby Isaac   Not Collective
59714de306b1SToby Isaac 
59724de306b1SToby Isaac   Input Parameters:
59734de306b1SToby Isaac + dm - The DM object
59744de306b1SToby Isaac . name - The label name
59754de306b1SToby Isaac . value - The stratum value
59764de306b1SToby Isaac - points - The stratum points
59774de306b1SToby Isaac 
59784de306b1SToby Isaac   Level: beginner
59794de306b1SToby Isaac 
59804de306b1SToby Isaac .keywords: mesh
59814de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
59824de306b1SToby Isaac @*/
59834de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
59844de306b1SToby Isaac {
59854de306b1SToby Isaac   DMLabel        label;
59864de306b1SToby Isaac   PetscErrorCode ierr;
59874de306b1SToby Isaac 
59884de306b1SToby Isaac   PetscFunctionBegin;
59894de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
59904de306b1SToby Isaac   PetscValidCharPointer(name, 2);
59914de306b1SToby Isaac   PetscValidPointer(points, 4);
59924de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
59934de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
59944de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
59954de306b1SToby Isaac   PetscFunctionReturn(0);
59964de306b1SToby Isaac }
59974de306b1SToby Isaac 
5998c58f1c22SToby Isaac /*@C
5999c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6000c58f1c22SToby Isaac 
6001c58f1c22SToby Isaac   Not Collective
6002c58f1c22SToby Isaac 
6003c58f1c22SToby Isaac   Input Parameters:
6004c58f1c22SToby Isaac + dm   - The DM object
6005c58f1c22SToby Isaac . name - The label name
6006c58f1c22SToby Isaac - value - The label value for this point
6007c58f1c22SToby Isaac 
6008c58f1c22SToby Isaac   Output Parameter:
6009c58f1c22SToby Isaac 
6010c58f1c22SToby Isaac   Level: beginner
6011c58f1c22SToby Isaac 
6012c58f1c22SToby Isaac .keywords: mesh
6013c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6014c58f1c22SToby Isaac @*/
6015c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6016c58f1c22SToby Isaac {
6017c58f1c22SToby Isaac   DMLabel        label;
6018c58f1c22SToby Isaac   PetscErrorCode ierr;
6019c58f1c22SToby Isaac 
6020c58f1c22SToby Isaac   PetscFunctionBegin;
6021c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6022c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6023c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6024c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6025c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6026c58f1c22SToby Isaac   PetscFunctionReturn(0);
6027c58f1c22SToby Isaac }
6028c58f1c22SToby Isaac 
6029c58f1c22SToby Isaac /*@
6030c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6031c58f1c22SToby Isaac 
6032c58f1c22SToby Isaac   Not Collective
6033c58f1c22SToby Isaac 
6034c58f1c22SToby Isaac   Input Parameter:
6035c58f1c22SToby Isaac . dm   - The DM object
6036c58f1c22SToby Isaac 
6037c58f1c22SToby Isaac   Output Parameter:
6038c58f1c22SToby Isaac . numLabels - the number of Labels
6039c58f1c22SToby Isaac 
6040c58f1c22SToby Isaac   Level: intermediate
6041c58f1c22SToby Isaac 
6042c58f1c22SToby Isaac .keywords: mesh
6043c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6044c58f1c22SToby Isaac @*/
6045c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6046c58f1c22SToby Isaac {
6047c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6048c58f1c22SToby Isaac   PetscInt  n    = 0;
6049c58f1c22SToby Isaac 
6050c58f1c22SToby Isaac   PetscFunctionBegin;
6051c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6052c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
6053c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6054c58f1c22SToby Isaac   *numLabels = n;
6055c58f1c22SToby Isaac   PetscFunctionReturn(0);
6056c58f1c22SToby Isaac }
6057c58f1c22SToby Isaac 
6058c58f1c22SToby Isaac /*@C
6059c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6060c58f1c22SToby Isaac 
6061c58f1c22SToby Isaac   Not Collective
6062c58f1c22SToby Isaac 
6063c58f1c22SToby Isaac   Input Parameters:
6064c58f1c22SToby Isaac + dm - The DM object
6065c58f1c22SToby Isaac - n  - the label number
6066c58f1c22SToby Isaac 
6067c58f1c22SToby Isaac   Output Parameter:
6068c58f1c22SToby Isaac . name - the label name
6069c58f1c22SToby Isaac 
6070c58f1c22SToby Isaac   Level: intermediate
6071c58f1c22SToby Isaac 
6072c58f1c22SToby Isaac .keywords: mesh
6073c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6074c58f1c22SToby Isaac @*/
6075c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6076c58f1c22SToby Isaac {
6077c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6078c58f1c22SToby Isaac   PetscInt       l    = 0;
6079d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
6080c58f1c22SToby Isaac 
6081c58f1c22SToby Isaac   PetscFunctionBegin;
6082c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6083c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6084c58f1c22SToby Isaac   while (next) {
6085c58f1c22SToby Isaac     if (l == n) {
6086d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6087c58f1c22SToby Isaac       PetscFunctionReturn(0);
6088c58f1c22SToby Isaac     }
6089c58f1c22SToby Isaac     ++l;
6090c58f1c22SToby Isaac     next = next->next;
6091c58f1c22SToby Isaac   }
6092c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6093c58f1c22SToby Isaac }
6094c58f1c22SToby Isaac 
6095c58f1c22SToby Isaac /*@C
6096c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6097c58f1c22SToby Isaac 
6098c58f1c22SToby Isaac   Not Collective
6099c58f1c22SToby Isaac 
6100c58f1c22SToby Isaac   Input Parameters:
6101c58f1c22SToby Isaac + dm   - The DM object
6102c58f1c22SToby Isaac - name - The label name
6103c58f1c22SToby Isaac 
6104c58f1c22SToby Isaac   Output Parameter:
6105c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
6106c58f1c22SToby Isaac 
6107c58f1c22SToby Isaac   Level: intermediate
6108c58f1c22SToby Isaac 
6109c58f1c22SToby Isaac .keywords: mesh
6110c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6111c58f1c22SToby Isaac @*/
6112c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
6113c58f1c22SToby Isaac {
6114c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6115d67d17b1SMatthew G. Knepley   const char    *lname;
6116c58f1c22SToby Isaac   PetscErrorCode ierr;
6117c58f1c22SToby Isaac 
6118c58f1c22SToby Isaac   PetscFunctionBegin;
6119c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6120c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6121c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
6122c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
6123c58f1c22SToby Isaac   while (next) {
6124d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6125d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
6126c58f1c22SToby Isaac     if (*hasLabel) break;
6127c58f1c22SToby Isaac     next = next->next;
6128c58f1c22SToby Isaac   }
6129c58f1c22SToby Isaac   PetscFunctionReturn(0);
6130c58f1c22SToby Isaac }
6131c58f1c22SToby Isaac 
6132c58f1c22SToby Isaac /*@C
6133c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
6134c58f1c22SToby Isaac 
6135c58f1c22SToby Isaac   Not Collective
6136c58f1c22SToby Isaac 
6137c58f1c22SToby Isaac   Input Parameters:
6138c58f1c22SToby Isaac + dm   - The DM object
6139c58f1c22SToby Isaac - name - The label name
6140c58f1c22SToby Isaac 
6141c58f1c22SToby Isaac   Output Parameter:
6142c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6143c58f1c22SToby Isaac 
6144c58f1c22SToby Isaac   Level: intermediate
6145c58f1c22SToby Isaac 
6146c58f1c22SToby Isaac .keywords: mesh
6147c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6148c58f1c22SToby Isaac @*/
6149c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
6150c58f1c22SToby Isaac {
6151c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6152c58f1c22SToby Isaac   PetscBool      hasLabel;
6153d67d17b1SMatthew G. Knepley   const char    *lname;
6154c58f1c22SToby Isaac   PetscErrorCode ierr;
6155c58f1c22SToby Isaac 
6156c58f1c22SToby Isaac   PetscFunctionBegin;
6157c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6158c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6159c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6160c58f1c22SToby Isaac   *label = NULL;
6161c58f1c22SToby Isaac   while (next) {
6162d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6163d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6164c58f1c22SToby Isaac     if (hasLabel) {
6165c58f1c22SToby Isaac       *label = next->label;
6166c58f1c22SToby Isaac       break;
6167c58f1c22SToby Isaac     }
6168c58f1c22SToby Isaac     next = next->next;
6169c58f1c22SToby Isaac   }
6170c58f1c22SToby Isaac   PetscFunctionReturn(0);
6171c58f1c22SToby Isaac }
6172c58f1c22SToby Isaac 
6173c58f1c22SToby Isaac /*@C
6174c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
6175c58f1c22SToby Isaac 
6176c58f1c22SToby Isaac   Not Collective
6177c58f1c22SToby Isaac 
6178c58f1c22SToby Isaac   Input Parameters:
6179c58f1c22SToby Isaac + dm - The DM object
6180c58f1c22SToby Isaac - n  - the label number
6181c58f1c22SToby Isaac 
6182c58f1c22SToby Isaac   Output Parameter:
6183c58f1c22SToby Isaac . label - the label
6184c58f1c22SToby Isaac 
6185c58f1c22SToby Isaac   Level: intermediate
6186c58f1c22SToby Isaac 
6187c58f1c22SToby Isaac .keywords: mesh
6188c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6189c58f1c22SToby Isaac @*/
6190c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
6191c58f1c22SToby Isaac {
6192c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
6193c58f1c22SToby Isaac   PetscInt    l    = 0;
6194c58f1c22SToby Isaac 
6195c58f1c22SToby Isaac   PetscFunctionBegin;
6196c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6197c58f1c22SToby Isaac   PetscValidPointer(label, 3);
6198c58f1c22SToby Isaac   while (next) {
6199c58f1c22SToby Isaac     if (l == n) {
6200c58f1c22SToby Isaac       *label = next->label;
6201c58f1c22SToby Isaac       PetscFunctionReturn(0);
6202c58f1c22SToby Isaac     }
6203c58f1c22SToby Isaac     ++l;
6204c58f1c22SToby Isaac     next = next->next;
6205c58f1c22SToby Isaac   }
6206c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6207c58f1c22SToby Isaac }
6208c58f1c22SToby Isaac 
6209c58f1c22SToby Isaac /*@C
6210c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
6211c58f1c22SToby Isaac 
6212c58f1c22SToby Isaac   Not Collective
6213c58f1c22SToby Isaac 
6214c58f1c22SToby Isaac   Input Parameters:
6215c58f1c22SToby Isaac + dm   - The DM object
6216c58f1c22SToby Isaac - label - The DMLabel
6217c58f1c22SToby Isaac 
6218c58f1c22SToby Isaac   Level: developer
6219c58f1c22SToby Isaac 
6220c58f1c22SToby Isaac .keywords: mesh
6221c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6222c58f1c22SToby Isaac @*/
6223c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
6224c58f1c22SToby Isaac {
6225c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
6226c58f1c22SToby Isaac   PetscBool      hasLabel;
6227d67d17b1SMatthew G. Knepley   const char    *lname;
6228c58f1c22SToby Isaac   PetscErrorCode ierr;
6229c58f1c22SToby Isaac 
6230c58f1c22SToby Isaac   PetscFunctionBegin;
6231c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6232d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
6233d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
6234d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
6235c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
6236c58f1c22SToby Isaac   tmpLabel->label  = label;
6237c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
6238c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
6239c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
6240c58f1c22SToby Isaac   PetscFunctionReturn(0);
6241c58f1c22SToby Isaac }
6242c58f1c22SToby Isaac 
6243c58f1c22SToby Isaac /*@C
6244c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
6245c58f1c22SToby Isaac 
6246c58f1c22SToby Isaac   Not Collective
6247c58f1c22SToby Isaac 
6248c58f1c22SToby Isaac   Input Parameters:
6249c58f1c22SToby Isaac + dm   - The DM object
6250c58f1c22SToby Isaac - name - The label name
6251c58f1c22SToby Isaac 
6252c58f1c22SToby Isaac   Output Parameter:
6253c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
6254c58f1c22SToby Isaac 
6255c58f1c22SToby Isaac   Level: developer
6256c58f1c22SToby Isaac 
6257c58f1c22SToby Isaac .keywords: mesh
6258c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6259c58f1c22SToby Isaac @*/
6260c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
6261c58f1c22SToby Isaac {
6262c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6263c58f1c22SToby Isaac   DMLabelLink    last = NULL;
6264c58f1c22SToby Isaac   PetscBool      hasLabel;
6265d67d17b1SMatthew G. Knepley   const char    *lname;
6266c58f1c22SToby Isaac   PetscErrorCode ierr;
6267c58f1c22SToby Isaac 
6268c58f1c22SToby Isaac   PetscFunctionBegin;
6269c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6270c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
6271c58f1c22SToby Isaac   *label = NULL;
6272c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
6273c58f1c22SToby Isaac   while (next) {
6274d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6275d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
6276c58f1c22SToby Isaac     if (hasLabel) {
6277c58f1c22SToby Isaac       if (last) last->next       = next->next;
6278c58f1c22SToby Isaac       else      dm->labels->next = next->next;
6279c58f1c22SToby Isaac       next->next = NULL;
6280c58f1c22SToby Isaac       *label     = next->label;
6281c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
6282c58f1c22SToby Isaac       if (hasLabel) {
6283c58f1c22SToby Isaac         dm->depthLabel = NULL;
6284c58f1c22SToby Isaac       }
6285c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
6286c58f1c22SToby Isaac       break;
6287c58f1c22SToby Isaac     }
6288c58f1c22SToby Isaac     last = next;
6289c58f1c22SToby Isaac     next = next->next;
6290c58f1c22SToby Isaac   }
6291c58f1c22SToby Isaac   PetscFunctionReturn(0);
6292c58f1c22SToby Isaac }
6293c58f1c22SToby Isaac 
6294c58f1c22SToby Isaac /*@C
6295c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
6296c58f1c22SToby Isaac 
6297c58f1c22SToby Isaac   Not Collective
6298c58f1c22SToby Isaac 
6299c58f1c22SToby Isaac   Input Parameters:
6300c58f1c22SToby Isaac + dm   - The DM object
6301c58f1c22SToby Isaac - name - The label name
6302c58f1c22SToby Isaac 
6303c58f1c22SToby Isaac   Output Parameter:
6304c58f1c22SToby Isaac . output - The flag for output
6305c58f1c22SToby Isaac 
6306c58f1c22SToby Isaac   Level: developer
6307c58f1c22SToby Isaac 
6308c58f1c22SToby Isaac .keywords: mesh
6309c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6310c58f1c22SToby Isaac @*/
6311c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
6312c58f1c22SToby Isaac {
6313c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6314d67d17b1SMatthew G. Knepley   const char    *lname;
6315c58f1c22SToby Isaac   PetscErrorCode ierr;
6316c58f1c22SToby Isaac 
6317c58f1c22SToby Isaac   PetscFunctionBegin;
6318c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6319c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6320c58f1c22SToby Isaac   PetscValidPointer(output, 3);
6321c58f1c22SToby Isaac   while (next) {
6322c58f1c22SToby Isaac     PetscBool flg;
6323c58f1c22SToby Isaac 
6324d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6325d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6326c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
6327c58f1c22SToby Isaac     next = next->next;
6328c58f1c22SToby Isaac   }
6329c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6330c58f1c22SToby Isaac }
6331c58f1c22SToby Isaac 
6332c58f1c22SToby Isaac /*@C
6333c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
6334c58f1c22SToby Isaac 
6335c58f1c22SToby Isaac   Not Collective
6336c58f1c22SToby Isaac 
6337c58f1c22SToby Isaac   Input Parameters:
6338c58f1c22SToby Isaac + dm     - The DM object
6339c58f1c22SToby Isaac . name   - The label name
6340c58f1c22SToby Isaac - output - The flag for output
6341c58f1c22SToby Isaac 
6342c58f1c22SToby Isaac   Level: developer
6343c58f1c22SToby Isaac 
6344c58f1c22SToby Isaac .keywords: mesh
6345c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6346c58f1c22SToby Isaac @*/
6347c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
6348c58f1c22SToby Isaac {
6349c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
6350d67d17b1SMatthew G. Knepley   const char    *lname;
6351c58f1c22SToby Isaac   PetscErrorCode ierr;
6352c58f1c22SToby Isaac 
6353c58f1c22SToby Isaac   PetscFunctionBegin;
6354c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6355c58f1c22SToby Isaac   PetscValidPointer(name, 2);
6356c58f1c22SToby Isaac   while (next) {
6357c58f1c22SToby Isaac     PetscBool flg;
6358c58f1c22SToby Isaac 
6359d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
6360d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
6361c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
6362c58f1c22SToby Isaac     next = next->next;
6363c58f1c22SToby Isaac   }
6364c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
6365c58f1c22SToby Isaac }
6366c58f1c22SToby Isaac 
6367c58f1c22SToby Isaac 
6368c58f1c22SToby Isaac /*@
6369c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
6370c58f1c22SToby Isaac 
6371c58f1c22SToby Isaac   Collective on DM
6372c58f1c22SToby Isaac 
6373c58f1c22SToby Isaac   Input Parameter:
63742e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
6375c58f1c22SToby Isaac 
6376c58f1c22SToby Isaac   Output Parameter:
63772e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
6378c58f1c22SToby Isaac 
6379c58f1c22SToby Isaac   Level: intermediate
6380c58f1c22SToby Isaac 
6381c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
6382c58f1c22SToby Isaac 
6383c58f1c22SToby Isaac .keywords: mesh
6384367003a6SStefano Zampini .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
6385c58f1c22SToby Isaac @*/
6386c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
6387c58f1c22SToby Isaac {
6388c58f1c22SToby Isaac   PetscInt       numLabels, l;
6389c58f1c22SToby Isaac   PetscErrorCode ierr;
6390c58f1c22SToby Isaac 
6391c58f1c22SToby Isaac   PetscFunctionBegin;
6392c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
6393c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
6394c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
6395c58f1c22SToby Isaac     DMLabel     label, labelNew;
6396c58f1c22SToby Isaac     const char *name;
6397c58f1c22SToby Isaac     PetscBool   flg;
6398c58f1c22SToby Isaac 
6399c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
6400c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
6401c58f1c22SToby Isaac     if (flg) continue;
64027d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
64037d5acc75SStefano Zampini     if (flg) continue;
6404c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
6405c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
6406c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
6407c58f1c22SToby Isaac   }
6408c58f1c22SToby Isaac   PetscFunctionReturn(0);
6409c58f1c22SToby Isaac }
6410a8fb8f29SToby Isaac 
6411a8fb8f29SToby Isaac /*@
6412a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
6413a8fb8f29SToby Isaac 
6414a8fb8f29SToby Isaac   Input Parameter:
6415a8fb8f29SToby Isaac . dm - The DM object
6416a8fb8f29SToby Isaac 
6417a8fb8f29SToby Isaac   Output Parameter:
6418a8fb8f29SToby Isaac . cdm - The coarse DM
6419a8fb8f29SToby Isaac 
6420a8fb8f29SToby Isaac   Level: intermediate
6421a8fb8f29SToby Isaac 
6422a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6423a8fb8f29SToby Isaac @*/
6424a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6425a8fb8f29SToby Isaac {
6426a8fb8f29SToby Isaac   PetscFunctionBegin;
6427a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6428a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6429a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6430a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6431a8fb8f29SToby Isaac }
6432a8fb8f29SToby Isaac 
6433a8fb8f29SToby Isaac /*@
6434a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6435a8fb8f29SToby Isaac 
6436a8fb8f29SToby Isaac   Input Parameters:
6437a8fb8f29SToby Isaac + dm - The DM object
6438a8fb8f29SToby Isaac - cdm - The coarse DM
6439a8fb8f29SToby Isaac 
6440a8fb8f29SToby Isaac   Level: intermediate
6441a8fb8f29SToby Isaac 
6442a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6443a8fb8f29SToby Isaac @*/
6444a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6445a8fb8f29SToby Isaac {
6446a8fb8f29SToby Isaac   PetscErrorCode ierr;
6447a8fb8f29SToby Isaac 
6448a8fb8f29SToby Isaac   PetscFunctionBegin;
6449a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6450a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6451a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6452a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6453a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6454a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6455a8fb8f29SToby Isaac }
6456a8fb8f29SToby Isaac 
645788bdff64SToby Isaac /*@
645888bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
645988bdff64SToby Isaac 
646088bdff64SToby Isaac   Input Parameter:
646188bdff64SToby Isaac . dm - The DM object
646288bdff64SToby Isaac 
646388bdff64SToby Isaac   Output Parameter:
646488bdff64SToby Isaac . fdm - The fine DM
646588bdff64SToby Isaac 
646688bdff64SToby Isaac   Level: intermediate
646788bdff64SToby Isaac 
646888bdff64SToby Isaac .seealso: DMSetFineDM()
646988bdff64SToby Isaac @*/
647088bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
647188bdff64SToby Isaac {
647288bdff64SToby Isaac   PetscFunctionBegin;
647388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
647488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
647588bdff64SToby Isaac   *fdm = dm->fineMesh;
647688bdff64SToby Isaac   PetscFunctionReturn(0);
647788bdff64SToby Isaac }
647888bdff64SToby Isaac 
647988bdff64SToby Isaac /*@
648088bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
648188bdff64SToby Isaac 
648288bdff64SToby Isaac   Input Parameters:
648388bdff64SToby Isaac + dm - The DM object
648488bdff64SToby Isaac - fdm - The fine DM
648588bdff64SToby Isaac 
648688bdff64SToby Isaac   Level: intermediate
648788bdff64SToby Isaac 
648888bdff64SToby Isaac .seealso: DMGetFineDM()
648988bdff64SToby Isaac @*/
649088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
649188bdff64SToby Isaac {
649288bdff64SToby Isaac   PetscErrorCode ierr;
649388bdff64SToby Isaac 
649488bdff64SToby Isaac   PetscFunctionBegin;
649588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
649688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
649788bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
649888bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
649988bdff64SToby Isaac   dm->fineMesh = fdm;
650088bdff64SToby Isaac   PetscFunctionReturn(0);
650188bdff64SToby Isaac }
650288bdff64SToby Isaac 
6503a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6504a6ba4734SToby Isaac 
6505a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6506a6ba4734SToby Isaac {
6507a6ba4734SToby Isaac   PetscErrorCode ierr;
6508a6ba4734SToby Isaac 
6509a6ba4734SToby Isaac   PetscFunctionBegin;
6510e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6511a6ba4734SToby Isaac   PetscFunctionReturn(0);
6512a6ba4734SToby Isaac }
6513a6ba4734SToby Isaac 
6514a6ba4734SToby Isaac /*@C
6515a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6516a6ba4734SToby Isaac 
6517a6ba4734SToby Isaac   Input Parameters:
65184c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6519f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6520a6ba4734SToby Isaac . name        - The BC name
6521a6ba4734SToby Isaac . labelname   - The label defining constrained points
6522a6ba4734SToby Isaac . field       - The field to constrain
6523e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
6524a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6525a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6526a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6527a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6528a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6529a6ba4734SToby Isaac 
6530a6ba4734SToby Isaac   Options Database Keys:
6531a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6532a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6533a6ba4734SToby Isaac 
6534a6ba4734SToby Isaac   Level: developer
6535a6ba4734SToby Isaac 
6536a6ba4734SToby Isaac .seealso: DMGetBoundary()
6537a6ba4734SToby Isaac @*/
6538a30ec4eaSSatish 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)
6539a6ba4734SToby Isaac {
6540a6ba4734SToby Isaac   PetscErrorCode ierr;
6541a6ba4734SToby Isaac 
6542a6ba4734SToby Isaac   PetscFunctionBegin;
6543a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6544f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6545a6ba4734SToby Isaac   PetscFunctionReturn(0);
6546a6ba4734SToby Isaac }
6547a6ba4734SToby Isaac 
6548a6ba4734SToby Isaac /*@
6549a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6550a6ba4734SToby Isaac 
6551a6ba4734SToby Isaac   Input Parameters:
6552a6ba4734SToby Isaac . dm - The mesh object
6553a6ba4734SToby Isaac 
6554a6ba4734SToby Isaac   Output Parameters:
6555a6ba4734SToby Isaac . numBd - The number of BC
6556a6ba4734SToby Isaac 
6557a6ba4734SToby Isaac   Level: intermediate
6558a6ba4734SToby Isaac 
6559a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6560a6ba4734SToby Isaac @*/
6561a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6562a6ba4734SToby Isaac {
656358ebd649SToby Isaac   PetscErrorCode ierr;
6564a6ba4734SToby Isaac 
6565a6ba4734SToby Isaac   PetscFunctionBegin;
6566a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
656758ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6568a6ba4734SToby Isaac   PetscFunctionReturn(0);
6569a6ba4734SToby Isaac }
6570a6ba4734SToby Isaac 
6571a6ba4734SToby Isaac /*@C
65721c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6573a6ba4734SToby Isaac 
6574a6ba4734SToby Isaac   Input Parameters:
6575a6ba4734SToby Isaac + dm          - The mesh object
6576a6ba4734SToby Isaac - bd          - The BC number
6577a6ba4734SToby Isaac 
6578a6ba4734SToby Isaac   Output Parameters:
6579f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6580a6ba4734SToby Isaac . name        - The BC name
6581a6ba4734SToby Isaac . labelname   - The label defining constrained points
6582a6ba4734SToby Isaac . field       - The field to constrain
6583a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6584a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6585a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6586a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6587a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6588a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6589a6ba4734SToby Isaac 
6590a6ba4734SToby Isaac   Options Database Keys:
6591a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6592a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6593a6ba4734SToby Isaac 
6594a6ba4734SToby Isaac   Level: developer
6595a6ba4734SToby Isaac 
6596a6ba4734SToby Isaac .seealso: DMAddBoundary()
6597a6ba4734SToby Isaac @*/
6598a30ec4eaSSatish 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)
6599a6ba4734SToby Isaac {
660058ebd649SToby Isaac   PetscErrorCode ierr;
6601a6ba4734SToby Isaac 
6602a6ba4734SToby Isaac   PetscFunctionBegin;
6603a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6604f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6605a6ba4734SToby Isaac   PetscFunctionReturn(0);
6606a6ba4734SToby Isaac }
6607a6ba4734SToby Isaac 
6608e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6609e6f8dbb6SToby Isaac {
6610dff059c6SToby Isaac   DMBoundary *lastnext;
6611e6f8dbb6SToby Isaac   DSBoundary dsbound;
6612e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6613e6f8dbb6SToby Isaac 
6614e6f8dbb6SToby Isaac   PetscFunctionBegin;
6615e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
661647a1f5adSToby Isaac   if (dm->boundary) {
661747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
661847a1f5adSToby Isaac 
661947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
662047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
662147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
662247a1f5adSToby Isaac     while (next) {
662347a1f5adSToby Isaac       DMBoundary b = next;
662447a1f5adSToby Isaac 
662547a1f5adSToby Isaac       next = b->next;
662647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6627a6ba4734SToby Isaac     }
662847a1f5adSToby Isaac     dm->boundary = NULL;
6629a6ba4734SToby Isaac   }
663047a1f5adSToby Isaac 
6631dff059c6SToby Isaac   lastnext = &(dm->boundary);
6632e6f8dbb6SToby Isaac   while (dsbound) {
6633e6f8dbb6SToby Isaac     DMBoundary dmbound;
6634e6f8dbb6SToby Isaac 
6635e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6636e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6637e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6638994fe344SLisandro 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);}
663947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6640dff059c6SToby Isaac     *lastnext = dmbound;
6641dff059c6SToby Isaac     lastnext = &(dmbound->next);
6642dff059c6SToby Isaac     dsbound = dsbound->next;
6643a6ba4734SToby Isaac   }
6644a6ba4734SToby Isaac   PetscFunctionReturn(0);
6645a6ba4734SToby Isaac }
6646a6ba4734SToby Isaac 
6647a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6648a6ba4734SToby Isaac {
6649b95f2879SToby Isaac   DMBoundary     b;
6650a6ba4734SToby Isaac   PetscErrorCode ierr;
6651a6ba4734SToby Isaac 
6652a6ba4734SToby Isaac   PetscFunctionBegin;
6653a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6654a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6655a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6656e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6657b95f2879SToby Isaac   b = dm->boundary;
6658a6ba4734SToby Isaac   while (b && !(*isBd)) {
6659e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6660e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
66613424c85cSToby Isaac 
66623424c85cSToby Isaac     if (label) {
6663a6ba4734SToby Isaac       PetscInt i;
6664a6ba4734SToby Isaac 
6665e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6666e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6667a6ba4734SToby Isaac       }
6668a6ba4734SToby Isaac     }
6669a6ba4734SToby Isaac     b = b->next;
6670a6ba4734SToby Isaac   }
6671a6ba4734SToby Isaac   PetscFunctionReturn(0);
6672a6ba4734SToby Isaac }
66734d6f44ffSToby Isaac 
66744d6f44ffSToby Isaac /*@C
66754d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
66764d6f44ffSToby Isaac 
66774d6f44ffSToby Isaac   Input Parameters:
66784d6f44ffSToby Isaac + dm      - The DM
66790709b2feSToby Isaac . time    - The time
66804d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
66814d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
66824d6f44ffSToby Isaac - mode    - The insertion mode for values
66834d6f44ffSToby Isaac 
66844d6f44ffSToby Isaac   Output Parameter:
66854d6f44ffSToby Isaac . X - vector
66864d6f44ffSToby Isaac 
66874d6f44ffSToby Isaac    Calling sequence of func:
66880709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
66894d6f44ffSToby Isaac 
66904d6f44ffSToby Isaac +  dim - The spatial dimension
66914d6f44ffSToby Isaac .  x   - The coordinates
66924d6f44ffSToby Isaac .  Nf  - The number of fields
66934d6f44ffSToby Isaac .  u   - The output field values
66944d6f44ffSToby Isaac -  ctx - optional user-defined function context
66954d6f44ffSToby Isaac 
66964d6f44ffSToby Isaac   Level: developer
66974d6f44ffSToby Isaac 
66982716604bSToby Isaac .seealso: DMComputeL2Diff()
66994d6f44ffSToby Isaac @*/
67000709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
67014d6f44ffSToby Isaac {
67024d6f44ffSToby Isaac   Vec            localX;
67034d6f44ffSToby Isaac   PetscErrorCode ierr;
67044d6f44ffSToby Isaac 
67054d6f44ffSToby Isaac   PetscFunctionBegin;
67064d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67074d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
67080709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
67094d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
67104d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
67114d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
67124d6f44ffSToby Isaac   PetscFunctionReturn(0);
67134d6f44ffSToby Isaac }
67144d6f44ffSToby Isaac 
67150709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
67164d6f44ffSToby Isaac {
67174d6f44ffSToby Isaac   PetscErrorCode ierr;
67184d6f44ffSToby Isaac 
67194d6f44ffSToby Isaac   PetscFunctionBegin;
67204d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67214d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
67220918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
67230709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
67244d6f44ffSToby Isaac   PetscFunctionReturn(0);
67254d6f44ffSToby Isaac }
67264d6f44ffSToby Isaac 
67272c53366bSMatthew 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)
67282c53366bSMatthew G. Knepley {
67292c53366bSMatthew G. Knepley   Vec            localX;
67302c53366bSMatthew G. Knepley   PetscErrorCode ierr;
67312c53366bSMatthew G. Knepley 
67322c53366bSMatthew G. Knepley   PetscFunctionBegin;
67332c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
67342c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
67352c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
67362c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
67372c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
67382c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
67392c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
67402c53366bSMatthew G. Knepley }
67412c53366bSMatthew G. Knepley 
67421c531cf8SMatthew 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)
67434d6f44ffSToby Isaac {
67444d6f44ffSToby Isaac   PetscErrorCode ierr;
67454d6f44ffSToby Isaac 
67464d6f44ffSToby Isaac   PetscFunctionBegin;
67474d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67484d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
67490918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
67501c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
67514d6f44ffSToby Isaac   PetscFunctionReturn(0);
67524d6f44ffSToby Isaac }
67532716604bSToby Isaac 
67548c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
67558c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
67568c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
67578c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6758191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
67598c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
67608c6c5593SMatthew G. Knepley {
67618c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
67628c6c5593SMatthew G. Knepley 
67638c6c5593SMatthew G. Knepley   PetscFunctionBegin;
67648c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67658c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
67668c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
67670918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
67688c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
67698c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
67708c6c5593SMatthew G. Knepley }
67718c6c5593SMatthew G. Knepley 
67721c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
67738c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
67748c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
67758c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6776191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
67778c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
67788c6c5593SMatthew G. Knepley {
67798c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
67808c6c5593SMatthew G. Knepley 
67818c6c5593SMatthew G. Knepley   PetscFunctionBegin;
67828c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
67838c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
67848c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
67850918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
67861c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
67878c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
67888c6c5593SMatthew G. Knepley }
67898c6c5593SMatthew G. Knepley 
67902716604bSToby Isaac /*@C
67912716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
67922716604bSToby Isaac 
67932716604bSToby Isaac   Input Parameters:
67942716604bSToby Isaac + dm    - The DM
67950709b2feSToby Isaac . time  - The time
67962716604bSToby Isaac . funcs - The functions to evaluate for each field component
67972716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6798574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
67992716604bSToby Isaac 
68002716604bSToby Isaac   Output Parameter:
68012716604bSToby Isaac . diff - The diff ||u - u_h||_2
68022716604bSToby Isaac 
68032716604bSToby Isaac   Level: developer
68042716604bSToby Isaac 
68051189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
68062716604bSToby Isaac @*/
68070709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
68082716604bSToby Isaac {
68092716604bSToby Isaac   PetscErrorCode ierr;
68102716604bSToby Isaac 
68112716604bSToby Isaac   PetscFunctionBegin;
68122716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6813b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
68140918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
68150709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
68162716604bSToby Isaac   PetscFunctionReturn(0);
68172716604bSToby Isaac }
6818b698f381SToby Isaac 
6819b698f381SToby Isaac /*@C
6820b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6821b698f381SToby Isaac 
6822b698f381SToby Isaac   Input Parameters:
6823b698f381SToby Isaac + dm    - The DM
6824b698f381SToby Isaac , time  - The time
6825b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6826b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6827574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6828b698f381SToby Isaac - n     - The vector to project along
6829b698f381SToby Isaac 
6830b698f381SToby Isaac   Output Parameter:
6831b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6832b698f381SToby Isaac 
6833b698f381SToby Isaac   Level: developer
6834b698f381SToby Isaac 
6835b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6836b698f381SToby Isaac @*/
6837b698f381SToby 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)
6838b698f381SToby Isaac {
6839b698f381SToby Isaac   PetscErrorCode ierr;
6840b698f381SToby Isaac 
6841b698f381SToby Isaac   PetscFunctionBegin;
6842b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6843b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6844b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6845b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6846b698f381SToby Isaac   PetscFunctionReturn(0);
6847b698f381SToby Isaac }
6848b698f381SToby Isaac 
68492a16baeaSToby Isaac /*@C
68502a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
68512a16baeaSToby Isaac 
68522a16baeaSToby Isaac   Input Parameters:
68532a16baeaSToby Isaac + dm    - The DM
68542a16baeaSToby Isaac . time  - The time
68552a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
68562a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6857574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
68582a16baeaSToby Isaac 
68592a16baeaSToby Isaac   Output Parameter:
68602a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
68612a16baeaSToby Isaac 
68622a16baeaSToby Isaac   Level: developer
68632a16baeaSToby Isaac 
68641189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
68652a16baeaSToby Isaac @*/
68661189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
68672a16baeaSToby Isaac {
68682a16baeaSToby Isaac   PetscErrorCode ierr;
68692a16baeaSToby Isaac 
68702a16baeaSToby Isaac   PetscFunctionBegin;
68712a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
68722a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
68730918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
68742a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
68752a16baeaSToby Isaac   PetscFunctionReturn(0);
68762a16baeaSToby Isaac }
68772a16baeaSToby Isaac 
6878df0b854cSToby Isaac /*@C
6879df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6880cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6881df0b854cSToby Isaac 
6882df0b854cSToby Isaac   Collective on dm
6883df0b854cSToby Isaac 
6884df0b854cSToby Isaac   Input parameters:
6885df0b854cSToby Isaac + dm - the pre-adaptation DM object
6886a1b0c543SToby Isaac - label - label with the flags
6887df0b854cSToby Isaac 
6888df0b854cSToby Isaac   Output parameters:
68890d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6890df0b854cSToby Isaac 
6891df0b854cSToby Isaac   Level: intermediate
68920d1cd5e0SMatthew G. Knepley 
68930d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6894df0b854cSToby Isaac @*/
68950d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6896df0b854cSToby Isaac {
6897df0b854cSToby Isaac   PetscErrorCode ierr;
6898df0b854cSToby Isaac 
6899df0b854cSToby Isaac   PetscFunctionBegin;
6900df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6901a1b0c543SToby Isaac   PetscValidPointer(label,2);
69020d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
69030d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
69046f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
69050d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
69060d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
69070d1cd5e0SMatthew G. Knepley }
69080d1cd5e0SMatthew G. Knepley 
69090d1cd5e0SMatthew G. Knepley /*@C
69100d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
69110d1cd5e0SMatthew G. Knepley 
69120d1cd5e0SMatthew G. Knepley   Input Parameters:
69130d1cd5e0SMatthew G. Knepley + dm - The DM object
69140d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
69156f25b0d8SLisandro 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_".
69160d1cd5e0SMatthew G. Knepley 
69170d1cd5e0SMatthew G. Knepley   Output Parameter:
69180d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
69190d1cd5e0SMatthew G. Knepley 
69200d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
69210d1cd5e0SMatthew G. Knepley 
69220d1cd5e0SMatthew G. Knepley   Level: advanced
69230d1cd5e0SMatthew G. Knepley 
69240d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
69250d1cd5e0SMatthew G. Knepley @*/
69260d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
69270d1cd5e0SMatthew G. Knepley {
69280d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
69290d1cd5e0SMatthew G. Knepley 
69300d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
69310d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69320d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
69330d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
69340d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
69356f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
69366f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
69370d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6938df0b854cSToby Isaac   PetscFunctionReturn(0);
6939df0b854cSToby Isaac }
6940c4088d22SMatthew G. Knepley 
6941502a2867SDave May /*@C
6942502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6943502a2867SDave May 
6944502a2867SDave May  Not Collective
6945502a2867SDave May 
6946502a2867SDave May  Input Parameter:
6947502a2867SDave May  . dm    - The DM
6948502a2867SDave May 
6949502a2867SDave May  Output Parameter:
6950502a2867SDave May  . nranks - the number of neighbours
6951502a2867SDave May  . ranks - the neighbors ranks
6952502a2867SDave May 
6953502a2867SDave May  Notes:
6954502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6955502a2867SDave May 
6956502a2867SDave May  Level: beginner
6957502a2867SDave May 
6958dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6959502a2867SDave May @*/
6960502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6961502a2867SDave May {
6962502a2867SDave May   PetscErrorCode ierr;
6963502a2867SDave May 
6964502a2867SDave May   PetscFunctionBegin;
6965502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
69660918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6967502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6968502a2867SDave May   PetscFunctionReturn(0);
6969502a2867SDave May }
6970502a2867SDave May 
6971531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6972531c7667SBarry Smith 
6973531c7667SBarry Smith /*
6974531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6975531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6976531c7667SBarry Smith */
6977531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6978531c7667SBarry Smith {
6979531c7667SBarry Smith   PetscErrorCode ierr;
6980531c7667SBarry Smith 
6981531c7667SBarry Smith   PetscFunctionBegin;
6982531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6983531c7667SBarry Smith     Vec x1local;
6984531c7667SBarry Smith     DM  dm;
6985531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6986531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6987531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6988531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6989531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6990531c7667SBarry Smith     x1   = x1local;
6991531c7667SBarry Smith   }
6992531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6993531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6994531c7667SBarry Smith     DM  dm;
6995531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6996531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6997531c7667SBarry Smith   }
6998531c7667SBarry Smith   PetscFunctionReturn(0);
6999531c7667SBarry Smith }
7000531c7667SBarry Smith 
7001531c7667SBarry Smith /*@
7002531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7003531c7667SBarry Smith 
7004531c7667SBarry Smith     Input Parameter:
7005531c7667SBarry Smith .    coloring - the MatFDColoring object
7006531c7667SBarry Smith 
700795452b02SPatrick Sanan     Developer Notes:
700895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7009531c7667SBarry Smith 
70101b266c99SBarry Smith     Level: advanced
70111b266c99SBarry Smith 
7012531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7013531c7667SBarry Smith @*/
7014531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7015531c7667SBarry Smith {
7016531c7667SBarry Smith   PetscFunctionBegin;
7017531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7018531c7667SBarry Smith   PetscFunctionReturn(0);
7019531c7667SBarry Smith }
70208320bc6fSPatrick Sanan 
70218320bc6fSPatrick Sanan /*@
70228320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
70238320bc6fSPatrick Sanan 
70248320bc6fSPatrick Sanan     Collective
70258320bc6fSPatrick Sanan 
70268320bc6fSPatrick Sanan     Input Parameters:
70278320bc6fSPatrick Sanan +    dm - the first DM
70288320bc6fSPatrick Sanan -    dm2 - the second DM
70298320bc6fSPatrick Sanan 
70308320bc6fSPatrick Sanan     Output Parameters:
70318320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
70328320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
70338320bc6fSPatrick Sanan 
70348320bc6fSPatrick Sanan     Notes:
70358320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
70368320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
70378320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
70388320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
70398320bc6fSPatrick Sanan     decomposition, with different data.
70408320bc6fSPatrick Sanan 
70418320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
70428320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
70438320bc6fSPatrick Sanan 
70448320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
70458320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
70468320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
70478320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
70488320bc6fSPatrick Sanan 
70498320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
70508320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
70518320bc6fSPatrick Sanan .vb
70528320bc6fSPatrick Sanan   ...
70538320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
70548320bc6fSPatrick Sanan   if (set && compatible)  {
70558320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
70568320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
70578320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
70588320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
70598320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
70608320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
70618320bc6fSPatrick Sanan       }
70628320bc6fSPatrick Sanan     }
70638320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
70648320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
70658320bc6fSPatrick Sanan   } else {
70668320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
70678320bc6fSPatrick Sanan   }
70688320bc6fSPatrick Sanan   ...
70698320bc6fSPatrick Sanan .ve
70708320bc6fSPatrick Sanan 
70718320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
70728320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
70738320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
70748320bc6fSPatrick Sanan     always check the "set" output parameter.
70758320bc6fSPatrick Sanan 
70768320bc6fSPatrick Sanan     A DM is always compatible with itself.
70778320bc6fSPatrick Sanan 
70788320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
70798320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
70808320bc6fSPatrick Sanan     incompatible.
70818320bc6fSPatrick Sanan 
70828320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
70838320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
70848320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
70858320bc6fSPatrick Sanan 
70868320bc6fSPatrick Sanan     Developer Notes:
70878320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
70888320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
70898320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
70908320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
70918320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
70928320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
70938320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
70948320bc6fSPatrick Sanan     function will then call that logic.
70958320bc6fSPatrick Sanan 
70968320bc6fSPatrick Sanan     Level: advanced
70978320bc6fSPatrick Sanan 
70988320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
70998320bc6fSPatrick Sanan @*/
71008320bc6fSPatrick Sanan 
71018320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
71028320bc6fSPatrick Sanan {
71038320bc6fSPatrick Sanan   PetscErrorCode ierr;
71048320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
71058320bc6fSPatrick Sanan   DMType         type,type2;
71068320bc6fSPatrick Sanan   PetscBool      sameType;
71078320bc6fSPatrick Sanan 
71088320bc6fSPatrick Sanan   PetscFunctionBegin;
71098320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
71108320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
71118320bc6fSPatrick Sanan 
71128320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
71138320bc6fSPatrick Sanan   if (dm == dm2) {
71148320bc6fSPatrick Sanan     *set = PETSC_TRUE;
71158320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
71168320bc6fSPatrick Sanan     PetscFunctionReturn(0);
71178320bc6fSPatrick Sanan   }
71188320bc6fSPatrick Sanan 
71198320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
71208320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
71218320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
71228320bc6fSPatrick Sanan      determined by the implementation-specific logic */
71238320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
71248320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
71258320bc6fSPatrick Sanan     *set = PETSC_TRUE;
71268320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
71278320bc6fSPatrick Sanan     PetscFunctionReturn(0);
71288320bc6fSPatrick Sanan   }
71298320bc6fSPatrick Sanan 
71308320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
71318320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
71328320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
71338320bc6fSPatrick Sanan     if (*set) {
71348320bc6fSPatrick Sanan       PetscFunctionReturn(0);
71358320bc6fSPatrick Sanan     }
71368320bc6fSPatrick Sanan   }
71378320bc6fSPatrick Sanan 
71388320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
71398320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
71408320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
71418320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
71428320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
71438320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
71448320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
71458320bc6fSPatrick Sanan   } else {
71468320bc6fSPatrick Sanan     *set = PETSC_FALSE;
71478320bc6fSPatrick Sanan   }
71488320bc6fSPatrick Sanan   PetscFunctionReturn(0);
71498320bc6fSPatrick Sanan }
7150