xref: /petsc/src/dm/interface/dm.c (revision 8320bc6f256ccc90b9c7345e8d83bca74aa092be)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
101ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1167a56275SMatthew G Knepley 
12e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
13bff4a2f0SMatthew G. Knepley 
144a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
154a7a4c06SLawrence Mitchell {
164a7a4c06SLawrence Mitchell   PetscFunctionBegin;
174a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
184a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
194a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
204a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
214a7a4c06SLawrence Mitchell }
224a7a4c06SLawrence Mitchell 
23a4121054SBarry Smith /*@
24de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
25a4121054SBarry Smith 
26a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
27a4121054SBarry Smith    error when you try to use the vector.
28a4121054SBarry Smith 
29a4121054SBarry Smith   Collective on MPI_Comm
30a4121054SBarry Smith 
31a4121054SBarry Smith   Input Parameter:
32a4121054SBarry Smith . comm - The communicator for the DM object
33a4121054SBarry Smith 
34a4121054SBarry Smith   Output Parameter:
35a4121054SBarry Smith . dm - The DM object
36a4121054SBarry Smith 
37a4121054SBarry Smith   Level: beginner
38a4121054SBarry Smith 
398472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
40a4121054SBarry Smith @*/
417087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
42a4121054SBarry Smith {
43a4121054SBarry Smith   DM             v;
44a4121054SBarry Smith   PetscErrorCode ierr;
45a4121054SBarry Smith 
46a4121054SBarry Smith   PetscFunctionBegin;
471411c6eeSJed Brown   PetscValidPointer(dm,2);
480298fd71SBarry Smith   *dm = NULL;
498b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
50607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
51607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
52607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
53a4121054SBarry Smith 
5473107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
55e7c4fc90SDmitry Karpeev 
560298fd71SBarry Smith   v->ltogmap                  = NULL;
571411c6eeSJed Brown   v->bs                       = 1;
58171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
6088ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
61c58f1c22SToby Isaac   v->labels                   = NULL;
62c58f1c22SToby Isaac   v->depthLabel               = NULL;
630298fd71SBarry Smith   v->defaultSection           = NULL;
640298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
65fba222abSToby Isaac   v->defaultConstraintSection = NULL;
66fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
67c6b900c6SMatthew G. Knepley   v->L                        = NULL;
68c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
695dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
709a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7196173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
72435a35e8SMatthew G Knepley   {
73435a35e8SMatthew G Knepley     PetscInt i;
74435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
750298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
76435a35e8SMatthew G Knepley     }
77435a35e8SMatthew G Knepley   }
782764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
7914f150ffSMatthew G. Knepley   v->dmBC = NULL;
80a8fb8f29SToby Isaac   v->coarseMesh = NULL;
81f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
82cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
83c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
84b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
85bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
86c58f1c22SToby Isaac   v->labels->refct = 1;
874a7a4c06SLawrence Mitchell 
884a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
894a7a4c06SLawrence Mitchell 
901411c6eeSJed Brown   *dm = v;
91a4121054SBarry Smith   PetscFunctionReturn(0);
92a4121054SBarry Smith }
93a4121054SBarry Smith 
9438221697SMatthew G. Knepley /*@
9538221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   Collective on MPI_Comm
9838221697SMatthew G. Knepley 
9938221697SMatthew G. Knepley   Input Parameter:
10038221697SMatthew G. Knepley . dm - The original DM object
10138221697SMatthew G. Knepley 
10238221697SMatthew G. Knepley   Output Parameter:
10338221697SMatthew G. Knepley . newdm  - The new DM object
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley   Level: beginner
10638221697SMatthew G. Knepley 
10738221697SMatthew G. Knepley .keywords: DM, topology, create
10838221697SMatthew G. Knepley @*/
10938221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11038221697SMatthew G. Knepley {
11138221697SMatthew G. Knepley   PetscSF        sf;
11238221697SMatthew G. Knepley   Vec            coords;
11338221697SMatthew G. Knepley   void          *ctx;
114a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11538221697SMatthew G. Knepley   PetscErrorCode ierr;
11638221697SMatthew G. Knepley 
11738221697SMatthew G. Knepley   PetscFunctionBegin;
11838221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11938221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12038221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
121c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
122c58f1c22SToby Isaac   dm->labels->refct++;
123c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
124c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1251de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1261de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12738221697SMatthew G. Knepley   if (dm->ops->clone) {
12838221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
12938221697SMatthew G. Knepley   }
1303f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13138221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
135be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
136be4c1c3eSMatthew G. Knepley     DM           ncdm;
137be4c1c3eSMatthew G. Knepley     PetscSection cs;
1385a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
139be4c1c3eSMatthew G. Knepley 
140be4c1c3eSMatthew G. Knepley     ierr = DMGetDefaultSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
141be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1425a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1435a0206caSToby Isaac     if (pEndMax >= 0) {
144be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
14513bffc4cSMatthew G. Knepley       ierr = DMSetDefaultSection(ncdm, cs);CHKERRQ(ierr);
146a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
148be4c1c3eSMatthew G. Knepley     }
149be4c1c3eSMatthew G. Knepley   }
150a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
151a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   if (coords) {
15438221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15538221697SMatthew G. Knepley   } else {
15638221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15738221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15838221697SMatthew G. Knepley   }
15990b157c4SStefano Zampini   {
16090b157c4SStefano Zampini     PetscBool             isper;
161c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1625dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16390b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16490b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
165c6b900c6SMatthew G. Knepley   }
16638221697SMatthew G. Knepley   PetscFunctionReturn(0);
16738221697SMatthew G. Knepley }
16838221697SMatthew G. Knepley 
1699a42bb27SBarry Smith /*@C
170564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1719a42bb27SBarry Smith 
1728472ad0fSDave May    Logically Collective on DM
1739a42bb27SBarry Smith 
1749a42bb27SBarry Smith    Input Parameter:
1759a42bb27SBarry Smith +  da - initial distributed array
176e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1779a42bb27SBarry Smith 
1789a42bb27SBarry Smith    Options Database:
179dd85299cSBarry Smith .   -dm_vec_type ctype
1809a42bb27SBarry Smith 
1819a42bb27SBarry Smith    Level: intermediate
1829a42bb27SBarry Smith 
1838472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1849a42bb27SBarry Smith @*/
18519fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1869a42bb27SBarry Smith {
1879a42bb27SBarry Smith   PetscErrorCode ierr;
1889a42bb27SBarry Smith 
1899a42bb27SBarry Smith   PetscFunctionBegin;
1909a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1919a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19219fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1939a42bb27SBarry Smith   PetscFunctionReturn(0);
1949a42bb27SBarry Smith }
1959a42bb27SBarry Smith 
196c0dedaeaSBarry Smith /*@C
197c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
198c0dedaeaSBarry Smith 
1998472ad0fSDave May    Logically Collective on DM
200c0dedaeaSBarry Smith 
201c0dedaeaSBarry Smith    Input Parameter:
202c0dedaeaSBarry Smith .  da - initial distributed array
203c0dedaeaSBarry Smith 
204c0dedaeaSBarry Smith    Output Parameter:
205c0dedaeaSBarry Smith .  ctype - the vector type
206c0dedaeaSBarry Smith 
207c0dedaeaSBarry Smith    Level: intermediate
208c0dedaeaSBarry Smith 
2098472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
210c0dedaeaSBarry Smith @*/
211c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
212c0dedaeaSBarry Smith {
213c0dedaeaSBarry Smith   PetscFunctionBegin;
214c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
215c0dedaeaSBarry Smith   *ctype = da->vectype;
216c0dedaeaSBarry Smith   PetscFunctionReturn(0);
217c0dedaeaSBarry Smith }
218c0dedaeaSBarry Smith 
2195f1ad066SMatthew G Knepley /*@
22034f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2215f1ad066SMatthew G Knepley 
2225f1ad066SMatthew G Knepley   Not collective
2235f1ad066SMatthew G Knepley 
2245f1ad066SMatthew G Knepley   Input Parameter:
2255f1ad066SMatthew G Knepley . v - The Vec
2265f1ad066SMatthew G Knepley 
2275f1ad066SMatthew G Knepley   Output Parameter:
2285f1ad066SMatthew G Knepley . dm - The DM
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley   Level: intermediate
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2335f1ad066SMatthew G Knepley @*/
2345f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2355f1ad066SMatthew G Knepley {
2365f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley   PetscFunctionBegin;
2395f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2405f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2415f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2425f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2435f1ad066SMatthew G Knepley }
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley /*@
246d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Not collective
2495f1ad066SMatthew G Knepley 
2505f1ad066SMatthew G Knepley   Input Parameters:
2515f1ad066SMatthew G Knepley + v - The Vec
2525f1ad066SMatthew G Knepley - dm - The DM
2535f1ad066SMatthew G Knepley 
254d9805387SMatthew 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.
255d9805387SMatthew G. Knepley 
2565f1ad066SMatthew G Knepley   Level: intermediate
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2595f1ad066SMatthew G Knepley @*/
2605f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2615f1ad066SMatthew G Knepley {
2625f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   PetscFunctionBegin;
2655f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
266d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2675f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2685f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2695f1ad066SMatthew G Knepley }
2705f1ad066SMatthew G Knepley 
271521d9a4cSLisandro Dalcin /*@C
2728f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2738f1509bcSBarry Smith 
2748f1509bcSBarry Smith    Logically Collective on DM
2758f1509bcSBarry Smith 
2768f1509bcSBarry Smith    Input Parameters:
2778f1509bcSBarry Smith +  dm - the DM context
2788f1509bcSBarry Smith -  ctype - the matrix type
2798f1509bcSBarry Smith 
2808f1509bcSBarry Smith    Options Database:
2818f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2828f1509bcSBarry Smith 
2838f1509bcSBarry Smith    Level: intermediate
2848f1509bcSBarry Smith 
2858f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2868f1509bcSBarry Smith           DMGetISColoringType()
2878f1509bcSBarry Smith @*/
2888f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2898f1509bcSBarry Smith {
2908f1509bcSBarry Smith   PetscFunctionBegin;
2918f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2928f1509bcSBarry Smith   dm->coloringtype = ctype;
2938f1509bcSBarry Smith   PetscFunctionReturn(0);
2948f1509bcSBarry Smith }
2958f1509bcSBarry Smith 
2968f1509bcSBarry Smith /*@C
2978f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
298521d9a4cSLisandro Dalcin 
299521d9a4cSLisandro Dalcin    Logically Collective on DM
300521d9a4cSLisandro Dalcin 
301521d9a4cSLisandro Dalcin    Input Parameter:
3028f1509bcSBarry Smith .  dm - the DM context
3038f1509bcSBarry Smith 
3048f1509bcSBarry Smith    Output Parameter:
3058f1509bcSBarry Smith .  ctype - the matrix type
3068f1509bcSBarry Smith 
3078f1509bcSBarry Smith    Options Database:
3088f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith    Level: intermediate
3118f1509bcSBarry Smith 
3128f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3138f1509bcSBarry Smith           DMGetISColoringType()
3148f1509bcSBarry Smith @*/
3158f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3168f1509bcSBarry Smith {
3178f1509bcSBarry Smith   PetscFunctionBegin;
3188f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3198f1509bcSBarry Smith   *ctype = dm->coloringtype;
3208f1509bcSBarry Smith   PetscFunctionReturn(0);
3218f1509bcSBarry Smith }
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith /*@C
3248f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3258f1509bcSBarry Smith 
3268f1509bcSBarry Smith    Logically Collective on DM
3278f1509bcSBarry Smith 
3288f1509bcSBarry Smith    Input Parameters:
329521d9a4cSLisandro Dalcin +  dm - the DM context
330a2b5a043SBarry Smith -  ctype - the matrix type
331521d9a4cSLisandro Dalcin 
332521d9a4cSLisandro Dalcin    Options Database:
333521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
334521d9a4cSLisandro Dalcin 
335521d9a4cSLisandro Dalcin    Level: intermediate
336521d9a4cSLisandro Dalcin 
337c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
338521d9a4cSLisandro Dalcin @*/
33919fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
340521d9a4cSLisandro Dalcin {
341521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34288f0584fSBarry Smith 
343521d9a4cSLisandro Dalcin   PetscFunctionBegin;
344521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
345521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
34619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
347521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
348521d9a4cSLisandro Dalcin }
349521d9a4cSLisandro Dalcin 
350c0dedaeaSBarry Smith /*@C
351c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
352c0dedaeaSBarry Smith 
353c0dedaeaSBarry Smith    Logically Collective on DM
354c0dedaeaSBarry Smith 
355c0dedaeaSBarry Smith    Input Parameter:
356c0dedaeaSBarry Smith .  dm - the DM context
357c0dedaeaSBarry Smith 
358c0dedaeaSBarry Smith    Output Parameter:
359c0dedaeaSBarry Smith .  ctype - the matrix type
360c0dedaeaSBarry Smith 
361c0dedaeaSBarry Smith    Options Database:
362c0dedaeaSBarry Smith .   -dm_mat_type ctype
363c0dedaeaSBarry Smith 
364c0dedaeaSBarry Smith    Level: intermediate
365c0dedaeaSBarry Smith 
366c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
367c0dedaeaSBarry Smith @*/
368c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
369c0dedaeaSBarry Smith {
370c0dedaeaSBarry Smith   PetscFunctionBegin;
371c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
372c0dedaeaSBarry Smith   *ctype = dm->mattype;
373c0dedaeaSBarry Smith   PetscFunctionReturn(0);
374c0dedaeaSBarry Smith }
375c0dedaeaSBarry Smith 
376c688c046SMatthew G Knepley /*@
37734f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
378c688c046SMatthew G Knepley 
379c688c046SMatthew G Knepley   Not collective
380c688c046SMatthew G Knepley 
381c688c046SMatthew G Knepley   Input Parameter:
382c688c046SMatthew G Knepley . A - The Mat
383c688c046SMatthew G Knepley 
384c688c046SMatthew G Knepley   Output Parameter:
385c688c046SMatthew G Knepley . dm - The DM
386c688c046SMatthew G Knepley 
387c688c046SMatthew G Knepley   Level: intermediate
388c688c046SMatthew G Knepley 
3898f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3908f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3918f1509bcSBarry Smith 
392c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
393c688c046SMatthew G Knepley @*/
394c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
395c688c046SMatthew G Knepley {
396c688c046SMatthew G Knepley   PetscErrorCode ierr;
397c688c046SMatthew G Knepley 
398c688c046SMatthew G Knepley   PetscFunctionBegin;
399c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
400c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
401c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
402c688c046SMatthew G Knepley   PetscFunctionReturn(0);
403c688c046SMatthew G Knepley }
404c688c046SMatthew G Knepley 
405c688c046SMatthew G Knepley /*@
406c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
407c688c046SMatthew G Knepley 
408c688c046SMatthew G Knepley   Not collective
409c688c046SMatthew G Knepley 
410c688c046SMatthew G Knepley   Input Parameters:
411c688c046SMatthew G Knepley + A - The Mat
412c688c046SMatthew G Knepley - dm - The DM
413c688c046SMatthew G Knepley 
414c688c046SMatthew G Knepley   Level: intermediate
415c688c046SMatthew G Knepley 
4168f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4178f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4188f1509bcSBarry Smith 
4198f1509bcSBarry Smith 
420c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
421c688c046SMatthew G Knepley @*/
422c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
423c688c046SMatthew G Knepley {
424c688c046SMatthew G Knepley   PetscErrorCode ierr;
425c688c046SMatthew G Knepley 
426c688c046SMatthew G Knepley   PetscFunctionBegin;
427c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4288865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
429c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
430c688c046SMatthew G Knepley   PetscFunctionReturn(0);
431c688c046SMatthew G Knepley }
432c688c046SMatthew G Knepley 
4339a42bb27SBarry Smith /*@C
4349a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4356757b960SDave May    DM options in the database.
4369a42bb27SBarry Smith 
4378353ddbbSDave May    Logically Collective on DM
4389a42bb27SBarry Smith 
4399a42bb27SBarry Smith    Input Parameter:
4408353ddbbSDave May +  da - the DM context
4419a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4429a42bb27SBarry Smith 
4439a42bb27SBarry Smith    Notes:
4449a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4459a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4469a42bb27SBarry Smith 
4479a42bb27SBarry Smith    Level: advanced
4489a42bb27SBarry Smith 
4498353ddbbSDave May .keywords: DM, set, options, prefix, database
4509a42bb27SBarry Smith 
4519a42bb27SBarry Smith .seealso: DMSetFromOptions()
4529a42bb27SBarry Smith @*/
4537087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4549a42bb27SBarry Smith {
4559a42bb27SBarry Smith   PetscErrorCode ierr;
4569a42bb27SBarry Smith 
4579a42bb27SBarry Smith   PetscFunctionBegin;
4589a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4599a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
460691be533SLawrence Mitchell   if (dm->sf) {
461691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
462691be533SLawrence Mitchell   }
463691be533SLawrence Mitchell   if (dm->defaultSF) {
464691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
465691be533SLawrence Mitchell   }
4669a42bb27SBarry Smith   PetscFunctionReturn(0);
4679a42bb27SBarry Smith }
4689a42bb27SBarry Smith 
46931697293SDave May /*@C
47031697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47131697293SDave May    DM options in the database.
47231697293SDave May 
47331697293SDave May    Logically Collective on DM
47431697293SDave May 
47531697293SDave May    Input Parameters:
47631697293SDave May +  dm - the DM context
47731697293SDave May -  prefix - the prefix string to prepend to all DM option requests
47831697293SDave May 
47931697293SDave May    Notes:
48031697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48131697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48231697293SDave May 
48331697293SDave May    Level: advanced
48431697293SDave May 
48531697293SDave May .keywords: DM, append, options, prefix, database
48631697293SDave May 
48731697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
48831697293SDave May @*/
48931697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49031697293SDave May {
49131697293SDave May   PetscErrorCode ierr;
49231697293SDave May 
49331697293SDave May   PetscFunctionBegin;
49431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49531697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49631697293SDave May   PetscFunctionReturn(0);
49731697293SDave May }
49831697293SDave May 
49931697293SDave May /*@C
50031697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50131697293SDave May    DM options in the database.
50231697293SDave May 
50331697293SDave May    Not Collective
50431697293SDave May 
50531697293SDave May    Input Parameters:
50631697293SDave May .  dm - the DM context
50731697293SDave May 
50831697293SDave May    Output Parameters:
50931697293SDave May .  prefix - pointer to the prefix string used is returned
51031697293SDave May 
51195452b02SPatrick Sanan    Notes:
51295452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51331697293SDave May    sufficient length to hold the prefix.
51431697293SDave May 
51531697293SDave May    Level: advanced
51631697293SDave May 
51731697293SDave May .keywords: DM, set, options, prefix, database
51831697293SDave May 
51931697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52031697293SDave May @*/
52131697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52231697293SDave May {
52331697293SDave May   PetscErrorCode ierr;
52431697293SDave May 
52531697293SDave May   PetscFunctionBegin;
52631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52731697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52831697293SDave May   PetscFunctionReturn(0);
52931697293SDave May }
53031697293SDave May 
53188bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53288bdff64SToby Isaac {
53388bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53488bdff64SToby Isaac   DMNamedVecLink nlink;
53588bdff64SToby Isaac   PetscErrorCode ierr;
53688bdff64SToby Isaac 
53788bdff64SToby Isaac   PetscFunctionBegin;
538aab5bcd8SJed Brown   *ncrefct = 0;
53988bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54088bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54188bdff64SToby Isaac     if (dm->localin[i])  refct--;
54288bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54388bdff64SToby Isaac   }
54488bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54588bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54688bdff64SToby Isaac   if (dm->x) {
54788bdff64SToby Isaac     DM obj;
54888bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
54988bdff64SToby Isaac     if (obj == dm) refct--;
55088bdff64SToby Isaac   }
55188bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55288bdff64SToby Isaac     refct--;
55388bdff64SToby Isaac     if (recurseCoarse) {
55488bdff64SToby Isaac       PetscInt coarseCount;
55588bdff64SToby Isaac 
55688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55788bdff64SToby Isaac       refct += coarseCount;
55888bdff64SToby Isaac     }
55988bdff64SToby Isaac   }
56088bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56188bdff64SToby Isaac     refct--;
56288bdff64SToby Isaac     if (recurseFine) {
56388bdff64SToby Isaac       PetscInt fineCount;
56488bdff64SToby Isaac 
56588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56688bdff64SToby Isaac       refct += fineCount;
56788bdff64SToby Isaac     }
56888bdff64SToby Isaac   }
56988bdff64SToby Isaac   *ncrefct = refct;
57088bdff64SToby Isaac   PetscFunctionReturn(0);
57188bdff64SToby Isaac }
57288bdff64SToby Isaac 
573354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
574354557abSToby Isaac {
575354557abSToby Isaac   PetscErrorCode ierr;
576354557abSToby Isaac 
577354557abSToby Isaac   PetscFunctionBegin;
578354557abSToby Isaac   if (!--(dm->labels->refct)) {
579354557abSToby Isaac     DMLabelLink next = dm->labels->next;
580354557abSToby Isaac 
581354557abSToby Isaac     /* destroy the labels */
582354557abSToby Isaac     while (next) {
583354557abSToby Isaac       DMLabelLink tmp = next->next;
584354557abSToby Isaac 
585354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
586354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
587354557abSToby Isaac       next = tmp;
588354557abSToby Isaac     }
589354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
590354557abSToby Isaac   }
591354557abSToby Isaac   PetscFunctionReturn(0);
592354557abSToby Isaac }
593354557abSToby Isaac 
59447c6ae99SBarry Smith /*@
5958472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59647c6ae99SBarry Smith 
59747c6ae99SBarry Smith     Collective on DM
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith     Input Parameter:
60047c6ae99SBarry Smith .   dm - the DM object to destroy
60147c6ae99SBarry Smith 
60247c6ae99SBarry Smith     Level: developer
60347c6ae99SBarry Smith 
604e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith @*/
607fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60847c6ae99SBarry Smith {
60988bdff64SToby Isaac   PetscInt       i, cnt;
610dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61147c6ae99SBarry Smith   PetscErrorCode ierr;
61247c6ae99SBarry Smith 
61347c6ae99SBarry Smith   PetscFunctionBegin;
6146bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6156bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61687e657c6SBarry Smith 
61788bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61888bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61988bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
62088bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
621732e2eb9SMatthew G Knepley   /*
622732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
623732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
624732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
625732e2eb9SMatthew G Knepley   */
6266bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6276bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
628732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6296bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6306bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
631732e2eb9SMatthew G Knepley   }
632f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6330298fd71SBarry Smith   (*dm)->namedglobal = NULL;
634f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6352348bcf4SPeter Brune     nnext = nlink->next;
6362348bcf4SPeter 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);
6372348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6382348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6392348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6402348bcf4SPeter Brune   }
641f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6420298fd71SBarry Smith   (*dm)->namedlocal = NULL;
643f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
644f490541aSPeter Brune     nnext = nlink->next;
645f490541aSPeter 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);
646f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
647f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
648f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
649f490541aSPeter Brune   }
6502348bcf4SPeter Brune 
651b17ce1afSJed Brown   /* Destroy the list of hooks */
652c833c3b5SJed Brown   {
653c833c3b5SJed Brown     DMCoarsenHookLink link,next;
654b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
655b17ce1afSJed Brown       next = link->next;
656b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
657b17ce1afSJed Brown     }
6580298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
659c833c3b5SJed Brown   }
660c833c3b5SJed Brown   {
661c833c3b5SJed Brown     DMRefineHookLink link,next;
662c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
663c833c3b5SJed Brown       next = link->next;
664c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
665c833c3b5SJed Brown     }
6660298fd71SBarry Smith     (*dm)->refinehook = NULL;
667c833c3b5SJed Brown   }
668be081cd6SPeter Brune   {
669be081cd6SPeter Brune     DMSubDomainHookLink link,next;
670be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
671be081cd6SPeter Brune       next = link->next;
672be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
673be081cd6SPeter Brune     }
6740298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
675be081cd6SPeter Brune   }
676baf369e7SPeter Brune   {
677baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
678baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
679baf369e7SPeter Brune       next = link->next;
680baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
681baf369e7SPeter Brune     }
6820298fd71SBarry Smith     (*dm)->gtolhook = NULL;
683baf369e7SPeter Brune   }
684d4d07f1eSToby Isaac   {
685d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
686d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
687d4d07f1eSToby Isaac       next = link->next;
688d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
689d4d07f1eSToby Isaac     }
690d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
691d4d07f1eSToby Isaac   }
692aa1993deSMatthew G Knepley   /* Destroy the work arrays */
693aa1993deSMatthew G Knepley   {
694aa1993deSMatthew G Knepley     DMWorkLink link,next;
695aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
696aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
697aa1993deSMatthew G Knepley       next = link->next;
698aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
699aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
700aa1993deSMatthew G Knepley     }
7010298fd71SBarry Smith     (*dm)->workin = NULL;
702aa1993deSMatthew G Knepley   }
703c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
704c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
705c58f1c22SToby Isaac 
706c58f1c22SToby Isaac     /* destroy the labels */
707c58f1c22SToby Isaac     while (next) {
708c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
709c58f1c22SToby Isaac 
710c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
711c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
712c58f1c22SToby Isaac       next = tmp;
713c58f1c22SToby Isaac     }
714c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
715c58f1c22SToby Isaac   }
716e6f8dbb6SToby Isaac   {
717e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
718e6f8dbb6SToby Isaac     while (next) {
719e6f8dbb6SToby Isaac       DMBoundary b = next;
720e6f8dbb6SToby Isaac 
721e6f8dbb6SToby Isaac       next = b->next;
722e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
723e6f8dbb6SToby Isaac     }
724e6f8dbb6SToby Isaac   }
725b17ce1afSJed Brown 
72652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
72952536dc3SBarry Smith 
7301a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7311a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7321a266240SBarry Smith   }
73387e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
73471cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7354dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7366bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7376bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
738073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
73988ed4aceSMatthew G Knepley 
74088ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
74188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7428b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
743fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
744fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
74588ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
74688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
747736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
748736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7498e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
750736995cdSBlaise Bourdin     }
751736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
752736995cdSBlaise Bourdin   }
75388bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75488bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75588bdff64SToby Isaac   }
756a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75788bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75888bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
75988bdff64SToby Isaac   }
76088bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
761f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7626636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7636636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7646636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7655dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
7666636e97aSMatthew G Knepley 
7672764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&(*dm)->prob);CHKERRQ(ierr);
76814f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
769e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
770e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
771732e2eb9SMatthew G Knepley 
7726bf464f9SBarry Smith   ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
773435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
774732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
77547c6ae99SBarry Smith   PetscFunctionReturn(0);
77647c6ae99SBarry Smith }
77747c6ae99SBarry Smith 
778d7bf68aeSBarry Smith /*@
779d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
780d7bf68aeSBarry Smith 
781d7bf68aeSBarry Smith     Collective on DM
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith     Input Parameter:
784d7bf68aeSBarry Smith .   dm - the DM object to setup
785d7bf68aeSBarry Smith 
786d7bf68aeSBarry Smith     Level: developer
787d7bf68aeSBarry Smith 
788e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
789d7bf68aeSBarry Smith 
790d7bf68aeSBarry Smith @*/
7917087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
792d7bf68aeSBarry Smith {
793d7bf68aeSBarry Smith   PetscErrorCode ierr;
794d7bf68aeSBarry Smith 
795d7bf68aeSBarry Smith   PetscFunctionBegin;
796171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7978387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
798d7bf68aeSBarry Smith   if (dm->ops->setup) {
799d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
800d7bf68aeSBarry Smith   }
8018387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
802d7bf68aeSBarry Smith   PetscFunctionReturn(0);
803d7bf68aeSBarry Smith }
804d7bf68aeSBarry Smith 
805d7bf68aeSBarry Smith /*@
806d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
807d7bf68aeSBarry Smith 
808d7bf68aeSBarry Smith     Collective on DM
809d7bf68aeSBarry Smith 
810d7bf68aeSBarry Smith     Input Parameter:
811d7bf68aeSBarry Smith .   dm - the DM object to set options for
812d7bf68aeSBarry Smith 
813732e2eb9SMatthew G Knepley     Options Database:
8144164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8154164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8164164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8178f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
818732e2eb9SMatthew G Knepley 
819d7bf68aeSBarry Smith     Level: developer
820d7bf68aeSBarry Smith 
821e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
822d7bf68aeSBarry Smith 
823d7bf68aeSBarry Smith @*/
8247087cfbeSBarry Smith PetscErrorCode  DMSetFromOptions(DM dm)
825d7bf68aeSBarry Smith {
8267781c08eSBarry Smith   char           typeName[256];
827ca266f36SBarry Smith   PetscBool      flg;
828d7bf68aeSBarry Smith   PetscErrorCode ierr;
829d7bf68aeSBarry Smith 
830d7bf68aeSBarry Smith   PetscFunctionBegin;
831171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
832f1fd5e65SToby Isaac   if (dm->prob) {
833f1fd5e65SToby Isaac     ierr = PetscDSSetFromOptions(dm->prob);CHKERRQ(ierr);
834f1fd5e65SToby Isaac   }
835691be533SLawrence Mitchell   if (dm->sf) {
836691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);
837691be533SLawrence Mitchell   }
838691be533SLawrence Mitchell   if (dm->defaultSF) {
839691be533SLawrence Mitchell     ierr = PetscSFSetFromOptions(dm->defaultSF);CHKERRQ(ierr);
840691be533SLawrence Mitchell   }
8413194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8420298fd71SBarry 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);
843a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
844f9ba7244SBarry Smith   if (flg) {
845f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
846f9ba7244SBarry Smith   }
847a264d7a6SBarry 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);
848073dac72SJed Brown   if (flg) {
849521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
850073dac72SJed Brown   }
8518f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
852f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
853e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
854f9ba7244SBarry Smith   }
855f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8560633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
85782fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
858d7bf68aeSBarry Smith   PetscFunctionReturn(0);
859d7bf68aeSBarry Smith }
860d7bf68aeSBarry Smith 
861fc9bc008SSatish Balay /*@C
862224748a4SBarry Smith     DMView - Views a DM
86347c6ae99SBarry Smith 
86447c6ae99SBarry Smith     Collective on DM
86547c6ae99SBarry Smith 
86647c6ae99SBarry Smith     Input Parameter:
86747c6ae99SBarry Smith +   dm - the DM object to view
86847c6ae99SBarry Smith -   v - the viewer
86947c6ae99SBarry Smith 
870224748a4SBarry Smith     Level: beginner
87147c6ae99SBarry Smith 
872e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
87347c6ae99SBarry Smith 
87447c6ae99SBarry Smith @*/
8757087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
87647c6ae99SBarry Smith {
87747c6ae99SBarry Smith   PetscErrorCode    ierr;
87832c0f0efSBarry Smith   PetscBool         isbinary;
87976a8abe0SBarry Smith   PetscMPIInt       size;
88076a8abe0SBarry Smith   PetscViewerFormat format;
88163b15415SAlp Dener   PetscInt          tabs;
88247c6ae99SBarry Smith 
88347c6ae99SBarry Smith   PetscFunctionBegin;
884171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8853014e516SBarry Smith   if (!v) {
886ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8873014e516SBarry Smith   }
88876a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
88976a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
89076a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
89163b15415SAlp Dener   ierr = PetscViewerASCIIGetTab(v, &tabs);CHKERRQ(ierr);
89255784310SAlp Dener   ierr = PetscViewerASCIISetTab(v, ((PetscObject)dm)->tablevel);CHKERRQ(ierr);
89398c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
89532c0f0efSBarry Smith   if (isbinary) {
89655849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
89732c0f0efSBarry Smith     char     type[256];
89832c0f0efSBarry Smith 
89932c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
90032c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
90132c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
90232c0f0efSBarry Smith   }
9030c010503SBarry Smith   if (dm->ops->view) {
9040c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
90547c6ae99SBarry Smith   }
90663b15415SAlp Dener   ierr = PetscViewerASCIISetTab(v, tabs);CHKERRQ(ierr);
90747c6ae99SBarry Smith   PetscFunctionReturn(0);
90847c6ae99SBarry Smith }
90947c6ae99SBarry Smith 
91047c6ae99SBarry Smith /*@
9118472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith     Collective on DM
91447c6ae99SBarry Smith 
91547c6ae99SBarry Smith     Input Parameter:
91647c6ae99SBarry Smith .   dm - the DM object
91747c6ae99SBarry Smith 
91847c6ae99SBarry Smith     Output Parameter:
91947c6ae99SBarry Smith .   vec - the global vector
92047c6ae99SBarry Smith 
921073dac72SJed Brown     Level: beginner
92247c6ae99SBarry Smith 
923e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith @*/
9267087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92747c6ae99SBarry Smith {
92847c6ae99SBarry Smith   PetscErrorCode ierr;
92947c6ae99SBarry Smith 
93047c6ae99SBarry Smith   PetscFunctionBegin;
931171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
93247c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93347c6ae99SBarry Smith   PetscFunctionReturn(0);
93447c6ae99SBarry Smith }
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith /*@
9378472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith     Not Collective
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith     Input Parameter:
94247c6ae99SBarry Smith .   dm - the DM object
94347c6ae99SBarry Smith 
94447c6ae99SBarry Smith     Output Parameter:
94547c6ae99SBarry Smith .   vec - the local vector
94647c6ae99SBarry Smith 
947073dac72SJed Brown     Level: beginner
94847c6ae99SBarry Smith 
949e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
95047c6ae99SBarry Smith 
95147c6ae99SBarry Smith @*/
9527087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95347c6ae99SBarry Smith {
95447c6ae99SBarry Smith   PetscErrorCode ierr;
95547c6ae99SBarry Smith 
95647c6ae99SBarry Smith   PetscFunctionBegin;
957171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
95847c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
95947c6ae99SBarry Smith   PetscFunctionReturn(0);
96047c6ae99SBarry Smith }
96147c6ae99SBarry Smith 
9621411c6eeSJed Brown /*@
9631411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9641411c6eeSJed Brown 
9651411c6eeSJed Brown    Collective on DM
9661411c6eeSJed Brown 
9671411c6eeSJed Brown    Input Parameter:
9681411c6eeSJed Brown .  dm - the DM that provides the mapping
9691411c6eeSJed Brown 
9701411c6eeSJed Brown    Output Parameter:
9711411c6eeSJed Brown .  ltog - the mapping
9721411c6eeSJed Brown 
9731411c6eeSJed Brown    Level: intermediate
9741411c6eeSJed Brown 
9751411c6eeSJed Brown    Notes:
9761411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9771411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9781411c6eeSJed Brown 
979fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9801411c6eeSJed Brown @*/
9817087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9821411c6eeSJed Brown {
9830be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9841411c6eeSJed Brown   PetscErrorCode ierr;
9851411c6eeSJed Brown 
9861411c6eeSJed Brown   PetscFunctionBegin;
9871411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9881411c6eeSJed Brown   PetscValidPointer(ltog,2);
9891411c6eeSJed Brown   if (!dm->ltogmap) {
99037d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
99137d0c07bSMatthew G Knepley 
99237d0c07bSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
99337d0c07bSMatthew G Knepley     if (section) {
994a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
99537d0c07bSMatthew G Knepley       PetscInt       *ltog;
996ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
99737d0c07bSMatthew G Knepley 
99837d0c07bSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
99937d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1000ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1001ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
100237d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1003a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
100437d0c07bSMatthew G Knepley 
100537d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
100637d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1007a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1008a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
100937d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10101a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10111a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10121a7dc684SMatthew G. Knepley         if (dof) {
1013b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1014b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10151a7dc684SMatthew G. Knepley         }
101637d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1017a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1018a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
101937d0c07bSMatthew G Knepley         }
102037d0c07bSMatthew G Knepley       }
1021bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10220be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10230be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10240be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10250be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10267591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10277591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1028ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1029ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1030ccf3bd66SMatthew G. Knepley         n /= bs;
1031ccf3bd66SMatthew G. Knepley       }
1032ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10333bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
103437d0c07bSMatthew G Knepley     } else {
1035184d77edSJed Brown       if (!dm->ops->getlocaltoglobalmapping) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM can not create LocalToGlobalMapping");
1036184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10371411c6eeSJed Brown     }
103837d0c07bSMatthew G Knepley   }
10391411c6eeSJed Brown   *ltog = dm->ltogmap;
10401411c6eeSJed Brown   PetscFunctionReturn(0);
10411411c6eeSJed Brown }
10421411c6eeSJed Brown 
10431411c6eeSJed Brown /*@
10441411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10451411c6eeSJed Brown 
10461411c6eeSJed Brown    Not Collective
10471411c6eeSJed Brown 
10481411c6eeSJed Brown    Input Parameter:
10491411c6eeSJed Brown .  dm - the DM with block structure
10501411c6eeSJed Brown 
10511411c6eeSJed Brown    Output Parameter:
10521411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10531411c6eeSJed Brown 
10541411c6eeSJed Brown    Level: intermediate
10551411c6eeSJed Brown 
1056fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10571411c6eeSJed Brown @*/
10587087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10591411c6eeSJed Brown {
10601411c6eeSJed Brown   PetscFunctionBegin;
10611411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10621411c6eeSJed Brown   PetscValidPointer(bs,2);
10631411c6eeSJed 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");
10641411c6eeSJed Brown   *bs = dm->bs;
10651411c6eeSJed Brown   PetscFunctionReturn(0);
10661411c6eeSJed Brown }
10671411c6eeSJed Brown 
106847c6ae99SBarry Smith /*@
10698472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
107047c6ae99SBarry Smith 
107147c6ae99SBarry Smith     Collective on DM
107247c6ae99SBarry Smith 
107347c6ae99SBarry Smith     Input Parameter:
107447c6ae99SBarry Smith +   dm1 - the DM object
107547c6ae99SBarry Smith -   dm2 - the second, finer DM object
107647c6ae99SBarry Smith 
107747c6ae99SBarry Smith     Output Parameter:
107847c6ae99SBarry Smith +  mat - the interpolation
107947c6ae99SBarry Smith -  vec - the scaling (optional)
108047c6ae99SBarry Smith 
108147c6ae99SBarry Smith     Level: developer
108247c6ae99SBarry Smith 
108395452b02SPatrick Sanan     Notes:
108495452b02SPatrick 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
108585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1086d52bd9f3SBarry Smith 
10871f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1088d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
108985afcc9aSBarry Smith 
109085afcc9aSBarry Smith 
10913ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
109247c6ae99SBarry Smith 
109347c6ae99SBarry Smith @*/
1094e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
109547c6ae99SBarry Smith {
109647c6ae99SBarry Smith   PetscErrorCode ierr;
109747c6ae99SBarry Smith 
109847c6ae99SBarry Smith   PetscFunctionBegin;
1099171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1100171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1101cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110225296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1103cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110447c6ae99SBarry Smith   PetscFunctionReturn(0);
110547c6ae99SBarry Smith }
110647c6ae99SBarry Smith 
11073ad4599aSBarry Smith /*@
11083ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11093ad4599aSBarry Smith 
11103ad4599aSBarry Smith     Collective on DM
11113ad4599aSBarry Smith 
11123ad4599aSBarry Smith     Input Parameter:
11133ad4599aSBarry Smith +   dm1 - the DM object
11143ad4599aSBarry Smith -   dm2 - the second, finer DM object
11153ad4599aSBarry Smith 
11163ad4599aSBarry Smith     Output Parameter:
11173ad4599aSBarry Smith .  mat - the restriction
11183ad4599aSBarry Smith 
11193ad4599aSBarry Smith 
11203ad4599aSBarry Smith     Level: developer
11213ad4599aSBarry Smith 
112295452b02SPatrick Sanan     Notes:
112395452b02SPatrick 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
11243ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11253ad4599aSBarry Smith 
11263ad4599aSBarry Smith 
11273ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11283ad4599aSBarry Smith 
11293ad4599aSBarry Smith @*/
11303ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11313ad4599aSBarry Smith {
11323ad4599aSBarry Smith   PetscErrorCode ierr;
11333ad4599aSBarry Smith 
11343ad4599aSBarry Smith   PetscFunctionBegin;
11353ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11363ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11373ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11387294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11393ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11403ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11413ad4599aSBarry Smith   PetscFunctionReturn(0);
11423ad4599aSBarry Smith }
11433ad4599aSBarry Smith 
114447c6ae99SBarry Smith /*@
11458472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith     Collective on DM
114847c6ae99SBarry Smith 
114947c6ae99SBarry Smith     Input Parameter:
115047c6ae99SBarry Smith +   dm1 - the DM object
115147c6ae99SBarry Smith -   dm2 - the second, finer DM object
115247c6ae99SBarry Smith 
115347c6ae99SBarry Smith     Output Parameter:
11546dbf9973SLawrence Mitchell .   mat - the injection
115547c6ae99SBarry Smith 
115647c6ae99SBarry Smith     Level: developer
115747c6ae99SBarry Smith 
115895452b02SPatrick Sanan    Notes:
115995452b02SPatrick 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
116085afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
116185afcc9aSBarry Smith 
1162e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
116347c6ae99SBarry Smith 
116447c6ae99SBarry Smith @*/
11656dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116647c6ae99SBarry Smith {
116747c6ae99SBarry Smith   PetscErrorCode ierr;
116847c6ae99SBarry Smith 
116947c6ae99SBarry Smith   PetscFunctionBegin;
1170171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1171171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11720c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11736dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
117447c6ae99SBarry Smith   PetscFunctionReturn(0);
117547c6ae99SBarry Smith }
117647c6ae99SBarry Smith 
1177b412c318SBarry Smith /*@
1178bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1179bd041c0cSMatthew G. Knepley 
1180bd041c0cSMatthew G. Knepley   Collective on DM
1181bd041c0cSMatthew G. Knepley 
1182bd041c0cSMatthew G. Knepley   Input Parameter:
1183bd041c0cSMatthew G. Knepley + dm1 - the DM object
1184bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1185bd041c0cSMatthew G. Knepley 
1186bd041c0cSMatthew G. Knepley   Output Parameter:
1187bd041c0cSMatthew G. Knepley . mat - the interpolation
1188bd041c0cSMatthew G. Knepley 
1189bd041c0cSMatthew G. Knepley   Level: developer
1190bd041c0cSMatthew G. Knepley 
1191bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1192bd041c0cSMatthew G. Knepley @*/
1193bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1194bd041c0cSMatthew G. Knepley {
1195bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1196bd041c0cSMatthew G. Knepley 
1197bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1198bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1199bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1200bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1201bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1202bd041c0cSMatthew G. Knepley }
1203bd041c0cSMatthew G. Knepley 
1204bd041c0cSMatthew G. Knepley /*@
1205b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
120647c6ae99SBarry Smith 
120747c6ae99SBarry Smith     Collective on DM
120847c6ae99SBarry Smith 
120947c6ae99SBarry Smith     Input Parameter:
121047c6ae99SBarry Smith +   dm - the DM object
12115bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
121247c6ae99SBarry Smith 
121347c6ae99SBarry Smith     Output Parameter:
121447c6ae99SBarry Smith .   coloring - the coloring
121547c6ae99SBarry Smith 
121647c6ae99SBarry Smith     Level: developer
121747c6ae99SBarry Smith 
1218b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
121947c6ae99SBarry Smith 
1220aab9d709SJed Brown @*/
1221b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
122247c6ae99SBarry Smith {
122347c6ae99SBarry Smith   PetscErrorCode ierr;
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith   PetscFunctionBegin;
1226171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1227ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1228b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
122947c6ae99SBarry Smith   PetscFunctionReturn(0);
123047c6ae99SBarry Smith }
123147c6ae99SBarry Smith 
1232b412c318SBarry Smith /*@
12338472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
123447c6ae99SBarry Smith 
123547c6ae99SBarry Smith     Collective on DM
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith     Input Parameter:
1238b412c318SBarry Smith .   dm - the DM object
123947c6ae99SBarry Smith 
124047c6ae99SBarry Smith     Output Parameter:
124147c6ae99SBarry Smith .   mat - the empty Jacobian
124247c6ae99SBarry Smith 
1243073dac72SJed Brown     Level: beginner
124447c6ae99SBarry Smith 
124595452b02SPatrick Sanan     Notes:
124695452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
124794013140SBarry Smith        do not need to do it yourself.
124894013140SBarry Smith 
124994013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12507889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
125194013140SBarry Smith 
125294013140SBarry 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
125394013140SBarry Smith        internally by PETSc.
125494013140SBarry Smith 
125594013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1256aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
125794013140SBarry Smith 
1258b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
125947c6ae99SBarry Smith 
1260aab9d709SJed Brown @*/
1261b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
126247c6ae99SBarry Smith {
126347c6ae99SBarry Smith   PetscErrorCode ierr;
126447c6ae99SBarry Smith 
126547c6ae99SBarry Smith   PetscFunctionBegin;
1266171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1267607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1268c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1269c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1270b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
127147c6ae99SBarry Smith   PetscFunctionReturn(0);
127247c6ae99SBarry Smith }
127347c6ae99SBarry Smith 
1274732e2eb9SMatthew G Knepley /*@
1275950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1276732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1277732e2eb9SMatthew G Knepley 
12788472ad0fSDave May   Logically Collective on DM
1279732e2eb9SMatthew G Knepley 
1280732e2eb9SMatthew G Knepley   Input Parameter:
1281732e2eb9SMatthew G Knepley + dm - the DM
1282732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1283732e2eb9SMatthew G Knepley 
1284732e2eb9SMatthew G Knepley   Level: developer
1285b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1286732e2eb9SMatthew G Knepley @*/
1287732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1288732e2eb9SMatthew G Knepley {
1289732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1290732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1291732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1292732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1293732e2eb9SMatthew G Knepley }
1294732e2eb9SMatthew G Knepley 
1295b06ff27eSHong Zhang /*@
1296b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1297b06ff27eSHong Zhang     but the array for values will not be allocated.
1298b06ff27eSHong Zhang 
1299b06ff27eSHong Zhang   Logically Collective on DM
1300b06ff27eSHong Zhang 
1301b06ff27eSHong Zhang   Input Parameter:
1302b06ff27eSHong Zhang + dm - the DM
1303b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1304b06ff27eSHong Zhang 
1305b06ff27eSHong Zhang   Level: developer
1306b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1307b06ff27eSHong Zhang @*/
1308b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1309b06ff27eSHong Zhang {
1310b06ff27eSHong Zhang   PetscFunctionBegin;
1311b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1312b06ff27eSHong Zhang   dm->structure_only = only;
1313b06ff27eSHong Zhang   PetscFunctionReturn(0);
1314b06ff27eSHong Zhang }
1315b06ff27eSHong Zhang 
1316a89ea682SMatthew G Knepley /*@C
1317aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1318a89ea682SMatthew G Knepley 
1319a89ea682SMatthew G Knepley   Not Collective
1320a89ea682SMatthew G Knepley 
1321a89ea682SMatthew G Knepley   Input Parameters:
1322a89ea682SMatthew G Knepley + dm - the DM object
1323aa1993deSMatthew G Knepley . count - The minium size
132469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1325a89ea682SMatthew G Knepley 
1326a89ea682SMatthew G Knepley   Output Parameter:
1327a89ea682SMatthew G Knepley . array - the work array
1328a89ea682SMatthew G Knepley 
1329a89ea682SMatthew G Knepley   Level: developer
1330a89ea682SMatthew G Knepley 
1331a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1332a89ea682SMatthew G Knepley @*/
133369291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1334a89ea682SMatthew G Knepley {
1335a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1336aa1993deSMatthew G Knepley   DMWorkLink     link;
133769291d52SBarry Smith   PetscMPIInt    dsize;
1338a89ea682SMatthew G Knepley 
1339a89ea682SMatthew G Knepley   PetscFunctionBegin;
1340a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1341aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1342aa1993deSMatthew G Knepley   if (dm->workin) {
1343aa1993deSMatthew G Knepley     link       = dm->workin;
1344aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1345aa1993deSMatthew G Knepley   } else {
1346b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1347a89ea682SMatthew G Knepley   }
134869291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13495056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1350aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1351854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1352854ce69bSBarry Smith     link->bytes = dsize*count;
1353aa1993deSMatthew G Knepley   }
1354aa1993deSMatthew G Knepley   link->next   = dm->workout;
1355aa1993deSMatthew G Knepley   dm->workout  = link;
1356aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1357a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1358a89ea682SMatthew G Knepley }
1359a89ea682SMatthew G Knepley 
1360aa1993deSMatthew G Knepley /*@C
1361aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1362aa1993deSMatthew G Knepley 
1363aa1993deSMatthew G Knepley   Not Collective
1364aa1993deSMatthew G Knepley 
1365aa1993deSMatthew G Knepley   Input Parameters:
1366aa1993deSMatthew G Knepley + dm - the DM object
1367aa1993deSMatthew G Knepley . count - The minium size
136869291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1369aa1993deSMatthew G Knepley 
1370aa1993deSMatthew G Knepley   Output Parameter:
1371aa1993deSMatthew G Knepley . array - the work array
1372aa1993deSMatthew G Knepley 
1373aa1993deSMatthew G Knepley   Level: developer
1374aa1993deSMatthew G Knepley 
137595452b02SPatrick Sanan   Developer Notes:
137695452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1377aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1378aa1993deSMatthew G Knepley @*/
137969291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1380aa1993deSMatthew G Knepley {
1381aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1382aa1993deSMatthew G Knepley 
1383aa1993deSMatthew G Knepley   PetscFunctionBegin;
1384aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1385aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1386aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1387aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1388aa1993deSMatthew G Knepley       *p           = link->next;
1389aa1993deSMatthew G Knepley       link->next   = dm->workin;
1390aa1993deSMatthew G Knepley       dm->workin   = link;
13910298fd71SBarry Smith       *(void**)mem = NULL;
1392aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1393aa1993deSMatthew G Knepley     }
1394aa1993deSMatthew G Knepley   }
1395aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1396aa1993deSMatthew G Knepley }
1397e7c4fc90SDmitry Karpeev 
1398435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1399435a35e8SMatthew G Knepley {
1400435a35e8SMatthew G Knepley   PetscFunctionBegin;
1401435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
140282f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1403435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1404435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1405435a35e8SMatthew G Knepley }
1406435a35e8SMatthew G Knepley 
14074f3b5142SJed Brown /*@C
14084d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14094d343eeaSMatthew G Knepley 
14104d343eeaSMatthew G Knepley   Not collective
14114d343eeaSMatthew G Knepley 
14124d343eeaSMatthew G Knepley   Input Parameter:
14134d343eeaSMatthew G Knepley . dm - the DM object
14144d343eeaSMatthew G Knepley 
14154d343eeaSMatthew G Knepley   Output Parameters:
14160298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14170298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14180298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14194d343eeaSMatthew G Knepley 
14204d343eeaSMatthew G Knepley   Level: intermediate
14214d343eeaSMatthew G Knepley 
142221c9b008SJed Brown   Notes:
142321c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
142421c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
142521c9b008SJed Brown   PetscFree().
142621c9b008SJed Brown 
14274d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14284d343eeaSMatthew G Knepley @*/
142937d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14304d343eeaSMatthew G Knepley {
143137d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14324d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14334d343eeaSMatthew G Knepley 
14344d343eeaSMatthew G Knepley   PetscFunctionBegin;
14354d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143669ca1f37SDmitry Karpeev   if (numFields) {
143769ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
143869ca1f37SDmitry Karpeev     *numFields = 0;
143969ca1f37SDmitry Karpeev   }
144037d0c07bSMatthew G Knepley   if (fieldNames) {
144137d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14420298fd71SBarry Smith     *fieldNames = NULL;
144369ca1f37SDmitry Karpeev   }
144469ca1f37SDmitry Karpeev   if (fields) {
144569ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14460298fd71SBarry Smith     *fields = NULL;
144769ca1f37SDmitry Karpeev   }
144837d0c07bSMatthew G Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
144937d0c07bSMatthew G Knepley   if (section) {
145037d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
145137d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
145237d0c07bSMatthew G Knepley 
145337d0c07bSMatthew G Knepley     ierr = DMGetDefaultGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
145437d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1455dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
145637d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
145737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
145837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
145937d0c07bSMatthew G Knepley     }
146037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
146137d0c07bSMatthew G Knepley       PetscInt gdof;
146237d0c07bSMatthew G Knepley 
146337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
146437d0c07bSMatthew G Knepley       if (gdof > 0) {
146537d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
146637d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
146737d0c07bSMatthew G Knepley 
146837d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
146937d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
147037d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
147137d0c07bSMatthew G Knepley         }
147237d0c07bSMatthew G Knepley       }
147337d0c07bSMatthew G Knepley     }
147437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1475785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
147637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
147737d0c07bSMatthew G Knepley     }
147837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
147937d0c07bSMatthew G Knepley       PetscInt gdof, goff;
148037d0c07bSMatthew G Knepley 
148137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
148237d0c07bSMatthew G Knepley       if (gdof > 0) {
148337d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
148437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
148537d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
148637d0c07bSMatthew G Knepley 
148737d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
148837d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
148937d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
149037d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
149137d0c07bSMatthew G Knepley           }
149237d0c07bSMatthew G Knepley         }
149337d0c07bSMatthew G Knepley       }
149437d0c07bSMatthew G Knepley     }
14958865f1eaSKarl Rupp     if (numFields) *numFields = nF;
149637d0c07bSMatthew G Knepley     if (fieldNames) {
1497785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
149837d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
149937d0c07bSMatthew G Knepley         const char *fieldName;
150037d0c07bSMatthew G Knepley 
150137d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
150237d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
150337d0c07bSMatthew G Knepley       }
150437d0c07bSMatthew G Knepley     }
150537d0c07bSMatthew G Knepley     if (fields) {
1506785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
150737d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
150882f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
150937d0c07bSMatthew G Knepley       }
151037d0c07bSMatthew G Knepley     }
151137d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
15128865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15138865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
151469ca1f37SDmitry Karpeev   }
15154d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15164d343eeaSMatthew G Knepley }
15174d343eeaSMatthew G Knepley 
151816621825SDmitry Karpeev 
151916621825SDmitry Karpeev /*@C
152016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
152116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
152216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1523e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1524e7c4fc90SDmitry Karpeev 
1525e7c4fc90SDmitry Karpeev   Not collective
1526e7c4fc90SDmitry Karpeev 
1527e7c4fc90SDmitry Karpeev   Input Parameter:
1528e7c4fc90SDmitry Karpeev . dm - the DM object
1529e7c4fc90SDmitry Karpeev 
1530e7c4fc90SDmitry Karpeev   Output Parameters:
15310298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15320298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15330298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15340298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1535e7c4fc90SDmitry Karpeev 
1536e7c4fc90SDmitry Karpeev   Level: intermediate
1537e7c4fc90SDmitry Karpeev 
1538e7c4fc90SDmitry Karpeev   Notes:
1539e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1540e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1541e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1542e7c4fc90SDmitry Karpeev 
1543e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1544e7c4fc90SDmitry Karpeev @*/
154516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1546e7c4fc90SDmitry Karpeev {
1547e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1548e7c4fc90SDmitry Karpeev 
1549e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1550e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15518865f1eaSKarl Rupp   if (len) {
15528865f1eaSKarl Rupp     PetscValidPointer(len,2);
15538865f1eaSKarl Rupp     *len = 0;
15548865f1eaSKarl Rupp   }
15558865f1eaSKarl Rupp   if (namelist) {
15568865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
15578865f1eaSKarl Rupp     *namelist = 0;
15588865f1eaSKarl Rupp   }
15598865f1eaSKarl Rupp   if (islist) {
15608865f1eaSKarl Rupp     PetscValidPointer(islist,4);
15618865f1eaSKarl Rupp     *islist = 0;
15628865f1eaSKarl Rupp   }
15638865f1eaSKarl Rupp   if (dmlist) {
15648865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
15658865f1eaSKarl Rupp     *dmlist = 0;
15668865f1eaSKarl Rupp   }
1567f3f0edfdSDmitry Karpeev   /*
1568f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1569f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1570f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1571f3f0edfdSDmitry Karpeev    */
1572ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
157316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1574435a35e8SMatthew G Knepley     PetscSection section;
1575435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1576435a35e8SMatthew G Knepley 
1577435a35e8SMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
1578435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1579435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1580f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
158103dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
158203dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
158303dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1584435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1585435a35e8SMatthew G Knepley         const char *fieldName;
1586435a35e8SMatthew G Knepley 
158703dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
158803dc3394SMatthew G. Knepley         if (namelist) {
1589435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1590435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1591435a35e8SMatthew G Knepley         }
159203dc3394SMatthew G. Knepley       }
1593435a35e8SMatthew G Knepley     } else {
159469ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1595e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15960298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1597e7c4fc90SDmitry Karpeev     }
15988865f1eaSKarl Rupp   } else {
159916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
160016621825SDmitry Karpeev   }
160116621825SDmitry Karpeev   PetscFunctionReturn(0);
160216621825SDmitry Karpeev }
160316621825SDmitry Karpeev 
1604564cec59SMatthew G. Knepley /*@
1605435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1606435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1607435a35e8SMatthew G Knepley 
1608435a35e8SMatthew G Knepley   Not collective
1609435a35e8SMatthew G Knepley 
1610435a35e8SMatthew G Knepley   Input Parameters:
16112adcc780SMatthew G. Knepley + dm        - The DM object
16122adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16132adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1614435a35e8SMatthew G Knepley 
1615435a35e8SMatthew G Knepley   Output Parameters:
16162adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16172adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1618435a35e8SMatthew G Knepley 
1619435a35e8SMatthew G Knepley   Level: intermediate
1620435a35e8SMatthew G Knepley 
1621435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1622435a35e8SMatthew G Knepley @*/
162337bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1624435a35e8SMatthew G Knepley {
1625435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1626435a35e8SMatthew G Knepley 
1627435a35e8SMatthew G Knepley   PetscFunctionBegin;
1628435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1629435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16308865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16318865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1632435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1633435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
163482f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1635435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1636435a35e8SMatthew G Knepley }
1637435a35e8SMatthew G Knepley 
16382adcc780SMatthew G. Knepley /*@C
16392adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
16402adcc780SMatthew G. Knepley 
16412adcc780SMatthew G. Knepley   Not collective
16422adcc780SMatthew G. Knepley 
16432adcc780SMatthew G. Knepley   Input Parameter:
16442adcc780SMatthew G. Knepley + dms - The DM objects
16452adcc780SMatthew G. Knepley - len - The number of DMs
16462adcc780SMatthew G. Knepley 
16472adcc780SMatthew G. Knepley   Output Parameters:
16482adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16492adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
16502adcc780SMatthew G. Knepley 
16512adcc780SMatthew G. Knepley   Level: intermediate
16522adcc780SMatthew G. Knepley 
16532adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
16542adcc780SMatthew G. Knepley @*/
16552adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
16562adcc780SMatthew G. Knepley {
16572adcc780SMatthew G. Knepley   PetscInt       i;
16582adcc780SMatthew G. Knepley   PetscErrorCode ierr;
16592adcc780SMatthew G. Knepley 
16602adcc780SMatthew G. Knepley   PetscFunctionBegin;
16612adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
16622adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
16632adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
16642adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
16652adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
16662adcc780SMatthew G. Knepley   if (len) {
16672adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
16682adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
16692adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
16702adcc780SMatthew G. Knepley   }
16712adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
16722adcc780SMatthew G. Knepley }
16732adcc780SMatthew G. Knepley 
167416621825SDmitry Karpeev 
167516621825SDmitry Karpeev /*@C
16768d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
16778d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
16788d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
16798d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
16808d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
168116621825SDmitry Karpeev 
168216621825SDmitry Karpeev   Not collective
168316621825SDmitry Karpeev 
168416621825SDmitry Karpeev   Input Parameter:
168516621825SDmitry Karpeev . dm - the DM object
168616621825SDmitry Karpeev 
168716621825SDmitry Karpeev   Output Parameters:
16880298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
16890298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
16900298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
16910298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
16920298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
169316621825SDmitry Karpeev 
169416621825SDmitry Karpeev   Level: intermediate
169516621825SDmitry Karpeev 
169616621825SDmitry Karpeev   Notes:
169716621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
169816621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
169916621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
170016621825SDmitry Karpeev 
17018d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
170216621825SDmitry Karpeev @*/
17038d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
170416621825SDmitry Karpeev {
170516621825SDmitry Karpeev   PetscErrorCode      ierr;
1706be081cd6SPeter Brune   DMSubDomainHookLink link;
1707be081cd6SPeter Brune   PetscInt            i,l;
170816621825SDmitry Karpeev 
170916621825SDmitry Karpeev   PetscFunctionBegin;
171016621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
171114a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17120298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17130298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17140298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17150298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1716f3f0edfdSDmitry Karpeev   /*
1717f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1718f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1719f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1720f3f0edfdSDmitry Karpeev    */
1721ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
172216621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1723be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
172414a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1725f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1726be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1727be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1728be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1729be081cd6SPeter Brune         }
1730648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1731e7c4fc90SDmitry Karpeev       }
173214a18fd3SPeter Brune     }
173314a18fd3SPeter Brune     if (len) *len = l;
173414a18fd3SPeter Brune   }
1735e30e807fSPeter Brune   PetscFunctionReturn(0);
1736e30e807fSPeter Brune }
1737e30e807fSPeter Brune 
1738e30e807fSPeter Brune 
1739e30e807fSPeter Brune /*@C
1740e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1741e30e807fSPeter Brune 
1742e30e807fSPeter Brune   Not collective
1743e30e807fSPeter Brune 
1744e30e807fSPeter Brune   Input Parameters:
1745e30e807fSPeter Brune + dm - the DM object
1746e30e807fSPeter Brune . n  - the number of subdomain scatters
1747e30e807fSPeter Brune - subdms - the local subdomains
1748e30e807fSPeter Brune 
1749e30e807fSPeter Brune   Output Parameters:
1750e30e807fSPeter Brune + n     - the number of scatters returned
1751e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1752e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1753e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1754e30e807fSPeter Brune 
175595452b02SPatrick Sanan   Notes:
175695452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1757e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1758e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1759e30e807fSPeter Brune   solution and residual data.
1760e30e807fSPeter Brune 
1761e30e807fSPeter Brune   Level: developer
1762e30e807fSPeter Brune 
1763e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1764e30e807fSPeter Brune @*/
1765e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1766e30e807fSPeter Brune {
1767e30e807fSPeter Brune   PetscErrorCode ierr;
1768e30e807fSPeter Brune 
1769e30e807fSPeter Brune   PetscFunctionBegin;
1770e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1771e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1772e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1773e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
17746668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1775e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1776e7c4fc90SDmitry Karpeev }
1777e7c4fc90SDmitry Karpeev 
177847c6ae99SBarry Smith /*@
177947c6ae99SBarry Smith   DMRefine - Refines a DM object
178047c6ae99SBarry Smith 
178147c6ae99SBarry Smith   Collective on DM
178247c6ae99SBarry Smith 
178347c6ae99SBarry Smith   Input Parameter:
178447c6ae99SBarry Smith + dm   - the DM object
178591d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
178647c6ae99SBarry Smith 
178747c6ae99SBarry Smith   Output Parameter:
17880298fd71SBarry Smith . dmf - the refined DM, or NULL
1789ae0a1c52SMatthew G Knepley 
17900298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
179147c6ae99SBarry Smith 
179247c6ae99SBarry Smith   Level: developer
179347c6ae99SBarry Smith 
1794e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
179547c6ae99SBarry Smith @*/
17967087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
179747c6ae99SBarry Smith {
179847c6ae99SBarry Smith   PetscErrorCode   ierr;
1799c833c3b5SJed Brown   DMRefineHookLink link;
180047c6ae99SBarry Smith 
180147c6ae99SBarry Smith   PetscFunctionBegin;
1802732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18031ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1804fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
180547c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18064057135bSMatthew G Knepley   if (*dmf) {
180743842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18088865f1eaSKarl Rupp 
18098cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18108865f1eaSKarl Rupp 
1811644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18120598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1813656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18148865f1eaSKarl Rupp 
1815e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1816c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18178865f1eaSKarl Rupp       if (link->refinehook) {
18188865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18198865f1eaSKarl Rupp       }
1820c833c3b5SJed Brown     }
1821c833c3b5SJed Brown   }
18221ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1823c833c3b5SJed Brown   PetscFunctionReturn(0);
1824c833c3b5SJed Brown }
1825c833c3b5SJed Brown 
1826bb9467b5SJed Brown /*@C
1827c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1828c833c3b5SJed Brown 
1829c833c3b5SJed Brown    Logically Collective
1830c833c3b5SJed Brown 
1831c833c3b5SJed Brown    Input Arguments:
1832c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1833c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1834c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18350298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1836c833c3b5SJed Brown 
1837c833c3b5SJed Brown    Calling sequence of refinehook:
1838c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1839c833c3b5SJed Brown 
1840c833c3b5SJed Brown +  coarse - coarse level DM
1841c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1842c833c3b5SJed Brown -  ctx - optional user-defined function context
1843c833c3b5SJed Brown 
1844c833c3b5SJed Brown    Calling sequence for interphook:
1845c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1846c833c3b5SJed Brown 
1847c833c3b5SJed Brown +  coarse - coarse level DM
1848c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1849c833c3b5SJed Brown .  fine - fine level DM to update
1850c833c3b5SJed Brown -  ctx - optional user-defined function context
1851c833c3b5SJed Brown 
1852c833c3b5SJed Brown    Level: advanced
1853c833c3b5SJed Brown 
1854c833c3b5SJed Brown    Notes:
1855c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1856c833c3b5SJed Brown 
1857c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1858c833c3b5SJed Brown 
1859bb9467b5SJed Brown    This function is currently not available from Fortran.
1860bb9467b5SJed Brown 
1861c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1862c833c3b5SJed Brown @*/
1863c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1864c833c3b5SJed Brown {
1865c833c3b5SJed Brown   PetscErrorCode   ierr;
1866c833c3b5SJed Brown   DMRefineHookLink link,*p;
1867c833c3b5SJed Brown 
1868c833c3b5SJed Brown   PetscFunctionBegin;
1869c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18703d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
18713d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
18723d8e3701SJed Brown   }
187395dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1874c833c3b5SJed Brown   link->refinehook = refinehook;
1875c833c3b5SJed Brown   link->interphook = interphook;
1876c833c3b5SJed Brown   link->ctx        = ctx;
18770298fd71SBarry Smith   link->next       = NULL;
1878c833c3b5SJed Brown   *p               = link;
1879c833c3b5SJed Brown   PetscFunctionReturn(0);
1880c833c3b5SJed Brown }
1881c833c3b5SJed Brown 
18823d8e3701SJed Brown /*@C
18833d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
18843d8e3701SJed Brown 
18853d8e3701SJed Brown    Logically Collective
18863d8e3701SJed Brown 
18873d8e3701SJed Brown    Input Arguments:
18883d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
18893d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
18903d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18913d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
18923d8e3701SJed Brown 
18933d8e3701SJed Brown    Level: advanced
18943d8e3701SJed Brown 
18953d8e3701SJed Brown    Notes:
18963d8e3701SJed Brown    This function does nothing if the hook is not in the list.
18973d8e3701SJed Brown 
18983d8e3701SJed Brown    This function is currently not available from Fortran.
18993d8e3701SJed Brown 
19003d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19013d8e3701SJed Brown @*/
19023d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19033d8e3701SJed Brown {
19043d8e3701SJed Brown   PetscErrorCode   ierr;
19053d8e3701SJed Brown   DMRefineHookLink link,*p;
19063d8e3701SJed Brown 
19073d8e3701SJed Brown   PetscFunctionBegin;
19083d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19093d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19103d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19113d8e3701SJed Brown       link = *p;
19123d8e3701SJed Brown       *p = link->next;
19133d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19143d8e3701SJed Brown       break;
19153d8e3701SJed Brown     }
19163d8e3701SJed Brown   }
19173d8e3701SJed Brown   PetscFunctionReturn(0);
19183d8e3701SJed Brown }
19193d8e3701SJed Brown 
1920c833c3b5SJed Brown /*@
1921c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1922c833c3b5SJed Brown 
1923c833c3b5SJed Brown    Collective if any hooks are
1924c833c3b5SJed Brown 
1925c833c3b5SJed Brown    Input Arguments:
1926c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1927e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1928c833c3b5SJed Brown -  fine - finer DM to update
1929c833c3b5SJed Brown 
1930c833c3b5SJed Brown    Level: developer
1931c833c3b5SJed Brown 
1932c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1933c833c3b5SJed Brown @*/
1934c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1935c833c3b5SJed Brown {
1936c833c3b5SJed Brown   PetscErrorCode   ierr;
1937c833c3b5SJed Brown   DMRefineHookLink link;
1938c833c3b5SJed Brown 
1939c833c3b5SJed Brown   PetscFunctionBegin;
1940c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
19418865f1eaSKarl Rupp     if (link->interphook) {
19428865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
19438865f1eaSKarl Rupp     }
19444057135bSMatthew G Knepley   }
194547c6ae99SBarry Smith   PetscFunctionReturn(0);
194647c6ae99SBarry Smith }
194747c6ae99SBarry Smith 
1948eb3f98d2SBarry Smith /*@
1949eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1950eb3f98d2SBarry Smith 
1951eb3f98d2SBarry Smith     Not Collective
1952eb3f98d2SBarry Smith 
1953eb3f98d2SBarry Smith     Input Parameter:
1954eb3f98d2SBarry Smith .   dm - the DM object
1955eb3f98d2SBarry Smith 
1956eb3f98d2SBarry Smith     Output Parameter:
1957eb3f98d2SBarry Smith .   level - number of refinements
1958eb3f98d2SBarry Smith 
1959eb3f98d2SBarry Smith     Level: developer
1960eb3f98d2SBarry Smith 
19616a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1962eb3f98d2SBarry Smith 
1963eb3f98d2SBarry Smith @*/
1964eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1965eb3f98d2SBarry Smith {
1966eb3f98d2SBarry Smith   PetscFunctionBegin;
1967eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1968eb3f98d2SBarry Smith   *level = dm->levelup;
1969eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1970eb3f98d2SBarry Smith }
1971eb3f98d2SBarry Smith 
1972fef3a512SBarry Smith /*@
1973fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1974fef3a512SBarry Smith 
1975fef3a512SBarry Smith     Not Collective
1976fef3a512SBarry Smith 
1977fef3a512SBarry Smith     Input Parameter:
1978fef3a512SBarry Smith +   dm - the DM object
1979fef3a512SBarry Smith -   level - number of refinements
1980fef3a512SBarry Smith 
1981fef3a512SBarry Smith     Level: advanced
1982fef3a512SBarry Smith 
198395452b02SPatrick Sanan     Notes:
198495452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
1985fef3a512SBarry Smith 
1986fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1987fef3a512SBarry Smith 
1988fef3a512SBarry Smith @*/
1989fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1990fef3a512SBarry Smith {
1991fef3a512SBarry Smith   PetscFunctionBegin;
1992fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1993fef3a512SBarry Smith   dm->levelup = level;
1994fef3a512SBarry Smith   PetscFunctionReturn(0);
1995fef3a512SBarry Smith }
1996fef3a512SBarry Smith 
1997bb9467b5SJed Brown /*@C
1998baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1999baf369e7SPeter Brune 
2000baf369e7SPeter Brune    Logically Collective
2001baf369e7SPeter Brune 
2002baf369e7SPeter Brune    Input Arguments:
2003baf369e7SPeter Brune +  dm - the DM
2004baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2005baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
20060298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2007baf369e7SPeter Brune 
2008baf369e7SPeter Brune    Calling sequence for beginhook:
2009baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2010baf369e7SPeter Brune 
2011baf369e7SPeter Brune +  dm - global DM
2012baf369e7SPeter Brune .  g - global vector
2013baf369e7SPeter Brune .  mode - mode
2014baf369e7SPeter Brune .  l - local vector
2015baf369e7SPeter Brune -  ctx - optional user-defined function context
2016baf369e7SPeter Brune 
2017baf369e7SPeter Brune 
2018baf369e7SPeter Brune    Calling sequence for endhook:
2019ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2020baf369e7SPeter Brune 
2021baf369e7SPeter Brune +  global - global DM
2022baf369e7SPeter Brune -  ctx - optional user-defined function context
2023baf369e7SPeter Brune 
2024baf369e7SPeter Brune    Level: advanced
2025baf369e7SPeter Brune 
2026baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2027baf369e7SPeter Brune @*/
2028baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2029baf369e7SPeter Brune {
2030baf369e7SPeter Brune   PetscErrorCode          ierr;
2031baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2032baf369e7SPeter Brune 
2033baf369e7SPeter Brune   PetscFunctionBegin;
2034baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2035baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
203695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2037baf369e7SPeter Brune   link->beginhook = beginhook;
2038baf369e7SPeter Brune   link->endhook   = endhook;
2039baf369e7SPeter Brune   link->ctx       = ctx;
20400298fd71SBarry Smith   link->next      = NULL;
2041baf369e7SPeter Brune   *p              = link;
2042baf369e7SPeter Brune   PetscFunctionReturn(0);
2043baf369e7SPeter Brune }
2044baf369e7SPeter Brune 
20454c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
20464c274da1SToby Isaac {
20474c274da1SToby Isaac   Mat cMat;
20484c274da1SToby Isaac   Vec cVec;
20494c274da1SToby Isaac   PetscSection section, cSec;
20504c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20514c274da1SToby Isaac   PetscErrorCode ierr;
20524c274da1SToby Isaac 
20534c274da1SToby Isaac   PetscFunctionBegin;
20544c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20554c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20564c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
20575db9a05bSToby Isaac     PetscInt nRows;
20585db9a05bSToby Isaac 
20595db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
20605db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
20614c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
20627711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20634c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
20644c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20654c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20664c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20674c274da1SToby Isaac       if (dof) {
20684c274da1SToby Isaac         PetscScalar *vals;
20694c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
20704c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
20714c274da1SToby Isaac       }
20724c274da1SToby Isaac     }
20734c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20744c274da1SToby Isaac   }
20754c274da1SToby Isaac   PetscFunctionReturn(0);
20764c274da1SToby Isaac }
20774c274da1SToby Isaac 
207847c6ae99SBarry Smith /*@
207947c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
208047c6ae99SBarry Smith 
208147c6ae99SBarry Smith     Neighbor-wise Collective on DM
208247c6ae99SBarry Smith 
208347c6ae99SBarry Smith     Input Parameters:
208447c6ae99SBarry Smith +   dm - the DM object
208547c6ae99SBarry Smith .   g - the global vector
208647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
208747c6ae99SBarry Smith -   l - the local vector
208847c6ae99SBarry Smith 
208947c6ae99SBarry Smith 
209047c6ae99SBarry Smith     Level: beginner
209147c6ae99SBarry Smith 
2092e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith @*/
20957087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
209647c6ae99SBarry Smith {
20977128ae9fSMatthew G Knepley   PetscSF                 sf;
209847c6ae99SBarry Smith   PetscErrorCode          ierr;
2099baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
210047c6ae99SBarry Smith 
210147c6ae99SBarry Smith   PetscFunctionBegin;
2102171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2103baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
21048865f1eaSKarl Rupp     if (link->beginhook) {
21058865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
21068865f1eaSKarl Rupp     }
2107baf369e7SPeter Brune   }
21087128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21097128ae9fSMatthew G Knepley   if (sf) {
2110ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2111ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
21127128ae9fSMatthew G Knepley 
211382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21147128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2115ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21167128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21177128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2118ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21197128ae9fSMatthew G Knepley   } else {
2120843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21217128ae9fSMatthew G Knepley   }
212247c6ae99SBarry Smith   PetscFunctionReturn(0);
212347c6ae99SBarry Smith }
212447c6ae99SBarry Smith 
212547c6ae99SBarry Smith /*@
212647c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
212747c6ae99SBarry Smith 
212847c6ae99SBarry Smith     Neighbor-wise Collective on DM
212947c6ae99SBarry Smith 
213047c6ae99SBarry Smith     Input Parameters:
213147c6ae99SBarry Smith +   dm - the DM object
213247c6ae99SBarry Smith .   g - the global vector
213347c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
213447c6ae99SBarry Smith -   l - the local vector
213547c6ae99SBarry Smith 
213647c6ae99SBarry Smith 
213747c6ae99SBarry Smith     Level: beginner
213847c6ae99SBarry Smith 
2139e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
214047c6ae99SBarry Smith 
214147c6ae99SBarry Smith @*/
21427087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
214347c6ae99SBarry Smith {
21447128ae9fSMatthew G Knepley   PetscSF                 sf;
214547c6ae99SBarry Smith   PetscErrorCode          ierr;
2146ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2147ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2148baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
214947c6ae99SBarry Smith 
215047c6ae99SBarry Smith   PetscFunctionBegin;
2151171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21527128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21537128ae9fSMatthew G Knepley   if (sf) {
215482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21557128ae9fSMatthew G Knepley 
21567128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2157ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21587128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21597128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2160ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21617128ae9fSMatthew G Knepley   } else {
2162843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21637128ae9fSMatthew G Knepley   }
21644c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2165baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2166baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2167baf369e7SPeter Brune   }
216847c6ae99SBarry Smith   PetscFunctionReturn(0);
216947c6ae99SBarry Smith }
217047c6ae99SBarry Smith 
2171d4d07f1eSToby Isaac /*@C
2172d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2173d4d07f1eSToby Isaac 
2174d4d07f1eSToby Isaac    Logically Collective
2175d4d07f1eSToby Isaac 
2176d4d07f1eSToby Isaac    Input Arguments:
2177d4d07f1eSToby Isaac +  dm - the DM
2178d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2179d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2180d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2181d4d07f1eSToby Isaac 
2182d4d07f1eSToby Isaac    Calling sequence for beginhook:
2183d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2184d4d07f1eSToby Isaac 
2185d4d07f1eSToby Isaac +  dm - global DM
2186d4d07f1eSToby Isaac .  l - local vector
2187d4d07f1eSToby Isaac .  mode - mode
2188d4d07f1eSToby Isaac .  g - global vector
2189d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2190d4d07f1eSToby Isaac 
2191d4d07f1eSToby Isaac 
2192d4d07f1eSToby Isaac    Calling sequence for endhook:
2193d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2194d4d07f1eSToby Isaac 
2195d4d07f1eSToby Isaac +  global - global DM
2196d4d07f1eSToby Isaac .  l - local vector
2197d4d07f1eSToby Isaac .  mode - mode
2198d4d07f1eSToby Isaac .  g - global vector
2199d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2200d4d07f1eSToby Isaac 
2201d4d07f1eSToby Isaac    Level: advanced
2202d4d07f1eSToby Isaac 
2203d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2204d4d07f1eSToby Isaac @*/
2205d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2206d4d07f1eSToby Isaac {
2207d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2208d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2209d4d07f1eSToby Isaac 
2210d4d07f1eSToby Isaac   PetscFunctionBegin;
2211d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2212d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
221395dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2214d4d07f1eSToby Isaac   link->beginhook = beginhook;
2215d4d07f1eSToby Isaac   link->endhook   = endhook;
2216d4d07f1eSToby Isaac   link->ctx       = ctx;
2217d4d07f1eSToby Isaac   link->next      = NULL;
2218d4d07f1eSToby Isaac   *p              = link;
2219d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2220d4d07f1eSToby Isaac }
2221d4d07f1eSToby Isaac 
22224c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
22234c274da1SToby Isaac {
22244c274da1SToby Isaac   Mat cMat;
22254c274da1SToby Isaac   Vec cVec;
22264c274da1SToby Isaac   PetscSection section, cSec;
22274c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22284c274da1SToby Isaac   PetscErrorCode ierr;
22294c274da1SToby Isaac 
22304c274da1SToby Isaac   PetscFunctionBegin;
22314c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22324c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22334c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
22345db9a05bSToby Isaac     PetscInt nRows;
22355db9a05bSToby Isaac 
22365db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22375db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
22384c274da1SToby Isaac     ierr = DMGetDefaultSection(dm,&section);CHKERRQ(ierr);
22397711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22404c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22414c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22424c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22434c274da1SToby Isaac       if (dof) {
22444c274da1SToby Isaac         PetscInt d;
22454c274da1SToby Isaac         PetscScalar *vals;
22464c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
22474c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
22484c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
22494c274da1SToby Isaac          * we just extracted */
22504c274da1SToby Isaac         for (d = 0; d < dof; d++) {
22514c274da1SToby Isaac           vals[d] = 0.;
22524c274da1SToby Isaac         }
22534c274da1SToby Isaac       }
22544c274da1SToby Isaac     }
22554c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
22564c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22574c274da1SToby Isaac   }
22584c274da1SToby Isaac   PetscFunctionReturn(0);
22594c274da1SToby Isaac }
22604c274da1SToby Isaac 
226147c6ae99SBarry Smith /*@
22629a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
22639a42bb27SBarry Smith 
22649a42bb27SBarry Smith     Neighbor-wise Collective on DM
22659a42bb27SBarry Smith 
22669a42bb27SBarry Smith     Input Parameters:
22679a42bb27SBarry Smith +   dm - the DM object
2268f6813fd5SJed Brown .   l - the local vector
22691eb28f2eSBarry 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.
22701eb28f2eSBarry Smith -   g - the global vector
22719a42bb27SBarry Smith 
227295452b02SPatrick Sanan     Notes:
227395452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
227484330215SMatthew 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.
22759a42bb27SBarry Smith 
22769a42bb27SBarry Smith     Level: beginner
22779a42bb27SBarry Smith 
2278e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
22799a42bb27SBarry Smith 
22809a42bb27SBarry Smith @*/
22817087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
22829a42bb27SBarry Smith {
22837128ae9fSMatthew G Knepley   PetscSF                 sf;
228484330215SMatthew G. Knepley   PetscSection            s, gs;
2285d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2286ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2287ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
228884330215SMatthew G. Knepley   PetscBool               isInsert;
228984330215SMatthew G. Knepley   PetscErrorCode          ierr;
22909a42bb27SBarry Smith 
22919a42bb27SBarry Smith   PetscFunctionBegin;
2292171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2293d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2294d4d07f1eSToby Isaac     if (link->beginhook) {
2295d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2296d4d07f1eSToby Isaac     }
2297d4d07f1eSToby Isaac   }
22984c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
22997128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
230084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
23017128ae9fSMatthew G Knepley   switch (mode) {
23027128ae9fSMatthew G Knepley   case INSERT_VALUES:
23037128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2304304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
230584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23067128ae9fSMatthew G Knepley   case ADD_VALUES:
23077128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2308304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
230984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23107128ae9fSMatthew G Knepley   default:
231182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23127128ae9fSMatthew G Knepley   }
231384330215SMatthew G. Knepley   if (sf && !isInsert) {
2314ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23157128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2316a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2317ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
231884330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
231984330215SMatthew G. Knepley   } else if (s && isInsert) {
232084330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
232184330215SMatthew G. Knepley 
232284330215SMatthew G. Knepley     ierr = DMGetDefaultGlobalSection(dm, &gs);CHKERRQ(ierr);
232384330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
232484330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2325ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
232684330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
232784330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2328b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
232984330215SMatthew G. Knepley 
233084330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
233103442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
233284330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2333b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
233484330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
233584330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2336b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
233703442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2338b3b16f48SMatthew 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);
2339b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2340b3b16f48SMatthew G. Knepley       if (!gcdof) {
234184330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2342b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2343b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
234484330215SMatthew G. Knepley         const PetscInt *cdofs;
234584330215SMatthew G. Knepley         PetscInt        cind = 0;
234684330215SMatthew G. Knepley 
234784330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2348b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
234984330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2350b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
235184330215SMatthew G. Knepley         }
2352b3b16f48SMatthew 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);
235384330215SMatthew G. Knepley     }
2354ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23557128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
23567128ae9fSMatthew G Knepley   } else {
2357843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23587128ae9fSMatthew G Knepley   }
23599a42bb27SBarry Smith   PetscFunctionReturn(0);
23609a42bb27SBarry Smith }
23619a42bb27SBarry Smith 
23629a42bb27SBarry Smith /*@
23639a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
236447c6ae99SBarry Smith 
236547c6ae99SBarry Smith     Neighbor-wise Collective on DM
236647c6ae99SBarry Smith 
236747c6ae99SBarry Smith     Input Parameters:
236847c6ae99SBarry Smith +   dm - the DM object
2369f6813fd5SJed Brown .   l - the local vector
237047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2371f6813fd5SJed Brown -   g - the global vector
237247c6ae99SBarry Smith 
237347c6ae99SBarry Smith 
237447c6ae99SBarry Smith     Level: beginner
237547c6ae99SBarry Smith 
2376e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
237747c6ae99SBarry Smith 
237847c6ae99SBarry Smith @*/
23797087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
238047c6ae99SBarry Smith {
23817128ae9fSMatthew G Knepley   PetscSF                 sf;
238284330215SMatthew G. Knepley   PetscSection            s;
2383d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
238484330215SMatthew G. Knepley   PetscBool               isInsert;
238584330215SMatthew G. Knepley   PetscErrorCode          ierr;
238647c6ae99SBarry Smith 
238747c6ae99SBarry Smith   PetscFunctionBegin;
2388171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23897128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
239084330215SMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
23917128ae9fSMatthew G Knepley   switch (mode) {
23927128ae9fSMatthew G Knepley   case INSERT_VALUES:
23937128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
239484330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23957128ae9fSMatthew G Knepley   case ADD_VALUES:
23967128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
239784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23987128ae9fSMatthew G Knepley   default:
239982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
24007128ae9fSMatthew G Knepley   }
240184330215SMatthew G. Knepley   if (sf && !isInsert) {
2402ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2403ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
240484330215SMatthew G. Knepley 
2405ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
24067128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2407a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2408ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24097128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
241084330215SMatthew G. Knepley   } else if (s && isInsert) {
24117128ae9fSMatthew G Knepley   } else {
2412843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24137128ae9fSMatthew G Knepley   }
2414d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2415d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2416d4d07f1eSToby Isaac   }
241747c6ae99SBarry Smith   PetscFunctionReturn(0);
241847c6ae99SBarry Smith }
241947c6ae99SBarry Smith 
2420f089877aSRichard Tran Mills /*@
2421bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2422bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2423d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2424f089877aSRichard Tran Mills 
2425bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2426f089877aSRichard Tran Mills 
2427f089877aSRichard Tran Mills    Input Parameters:
2428f089877aSRichard Tran Mills +  dm - the DM object
2429bc0a1609SRichard Tran Mills .  g - the original local vector
2430bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2431f089877aSRichard Tran Mills 
2432bc0a1609SRichard Tran Mills    Output Parameter:
2433bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2434f089877aSRichard Tran Mills 
2435f089877aSRichard Tran Mills    Level: intermediate
2436f089877aSRichard Tran Mills 
2437bc0a1609SRichard Tran Mills    Notes:
2438bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2439bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2440bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2441bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2442bc0a1609SRichard Tran Mills 
2443bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2444bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2445f089877aSRichard Tran Mills 
2446f089877aSRichard Tran Mills @*/
2447f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2448f089877aSRichard Tran Mills {
2449f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2450f089877aSRichard Tran Mills 
2451f089877aSRichard Tran Mills   PetscFunctionBegin;
2452f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2453bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2454f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2455f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2456f089877aSRichard Tran Mills }
2457f089877aSRichard Tran Mills 
2458f089877aSRichard Tran Mills /*@
2459bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2460bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2461d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2462f089877aSRichard Tran Mills 
2463bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2464f089877aSRichard Tran Mills 
2465f089877aSRichard Tran Mills    Input Parameters:
2466bc0a1609SRichard Tran Mills +  da - the DM object
2467bc0a1609SRichard Tran Mills .  g - the original local vector
2468bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2469f089877aSRichard Tran Mills 
2470bc0a1609SRichard Tran Mills    Output Parameter:
2471bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2472f089877aSRichard Tran Mills 
2473f089877aSRichard Tran Mills    Level: intermediate
2474f089877aSRichard Tran Mills 
2475bc0a1609SRichard Tran Mills    Notes:
2476bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2477bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2478bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2479bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2480bc0a1609SRichard Tran Mills 
2481bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2482bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2483f089877aSRichard Tran Mills 
2484f089877aSRichard Tran Mills @*/
2485f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2486f089877aSRichard Tran Mills {
2487f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2488f089877aSRichard Tran Mills 
2489f089877aSRichard Tran Mills   PetscFunctionBegin;
2490f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2491bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2492f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2493f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2494f089877aSRichard Tran Mills }
2495f089877aSRichard Tran Mills 
2496f089877aSRichard Tran Mills 
249747c6ae99SBarry Smith /*@
249847c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
249947c6ae99SBarry Smith 
250047c6ae99SBarry Smith     Collective on DM
250147c6ae99SBarry Smith 
250247c6ae99SBarry Smith     Input Parameter:
250347c6ae99SBarry Smith +   dm - the DM object
250491d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
250547c6ae99SBarry Smith 
250647c6ae99SBarry Smith     Output Parameter:
250747c6ae99SBarry Smith .   dmc - the coarsened DM
250847c6ae99SBarry Smith 
250947c6ae99SBarry Smith     Level: developer
251047c6ae99SBarry Smith 
2511e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
251247c6ae99SBarry Smith 
251347c6ae99SBarry Smith @*/
25147087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
251547c6ae99SBarry Smith {
251647c6ae99SBarry Smith   PetscErrorCode    ierr;
2517b17ce1afSJed Brown   DMCoarsenHookLink link;
251847c6ae99SBarry Smith 
251947c6ae99SBarry Smith   PetscFunctionBegin;
2520171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2521fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
252247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
252347c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
25248892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2525a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
252643842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
25278cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2528644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
25290598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2530656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2531e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2532b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2533b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2534b17ce1afSJed Brown   }
253547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2536b17ce1afSJed Brown   PetscFunctionReturn(0);
2537b17ce1afSJed Brown }
2538b17ce1afSJed Brown 
2539bb9467b5SJed Brown /*@C
2540b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2541b17ce1afSJed Brown 
2542b17ce1afSJed Brown    Logically Collective
2543b17ce1afSJed Brown 
2544b17ce1afSJed Brown    Input Arguments:
2545b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2546b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2547b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
25480298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2549b17ce1afSJed Brown 
2550b17ce1afSJed Brown    Calling sequence of coarsenhook:
2551b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2552b17ce1afSJed Brown 
2553b17ce1afSJed Brown +  fine - fine level DM
2554b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2555b17ce1afSJed Brown -  ctx - optional user-defined function context
2556b17ce1afSJed Brown 
2557b17ce1afSJed Brown    Calling sequence for restricthook:
2558c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2559b17ce1afSJed Brown 
2560b17ce1afSJed Brown +  fine - fine level DM
2561b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2562c833c3b5SJed Brown .  rscale - scaling vector for restriction
2563c833c3b5SJed Brown .  inject - matrix restricting by injection
2564b17ce1afSJed Brown .  coarse - coarse level DM to update
2565b17ce1afSJed Brown -  ctx - optional user-defined function context
2566b17ce1afSJed Brown 
2567b17ce1afSJed Brown    Level: advanced
2568b17ce1afSJed Brown 
2569b17ce1afSJed Brown    Notes:
2570b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2571b17ce1afSJed Brown 
2572b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2573b17ce1afSJed Brown 
2574b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2575b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2576b17ce1afSJed Brown 
2577bb9467b5SJed Brown    This function is currently not available from Fortran.
2578bb9467b5SJed Brown 
2579dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2580b17ce1afSJed Brown @*/
2581b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2582b17ce1afSJed Brown {
2583b17ce1afSJed Brown   PetscErrorCode    ierr;
2584b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2585b17ce1afSJed Brown 
2586b17ce1afSJed Brown   PetscFunctionBegin;
2587b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
25881e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
25891e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
25901e3d8eccSJed Brown   }
259195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2592b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2593b17ce1afSJed Brown   link->restricthook = restricthook;
2594b17ce1afSJed Brown   link->ctx          = ctx;
25950298fd71SBarry Smith   link->next         = NULL;
2596b17ce1afSJed Brown   *p                 = link;
2597b17ce1afSJed Brown   PetscFunctionReturn(0);
2598b17ce1afSJed Brown }
2599b17ce1afSJed Brown 
2600dc822a44SJed Brown /*@C
2601dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2602dc822a44SJed Brown 
2603dc822a44SJed Brown    Logically Collective
2604dc822a44SJed Brown 
2605dc822a44SJed Brown    Input Arguments:
2606dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2607dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2608dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2609dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2610dc822a44SJed Brown 
2611dc822a44SJed Brown    Level: advanced
2612dc822a44SJed Brown 
2613dc822a44SJed Brown    Notes:
2614dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2615dc822a44SJed Brown 
2616dc822a44SJed Brown    This function is currently not available from Fortran.
2617dc822a44SJed Brown 
2618dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2619dc822a44SJed Brown @*/
2620dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2621dc822a44SJed Brown {
2622dc822a44SJed Brown   PetscErrorCode    ierr;
2623dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2624dc822a44SJed Brown 
2625dc822a44SJed Brown   PetscFunctionBegin;
2626dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2627dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2628dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2629dc822a44SJed Brown       link = *p;
2630dc822a44SJed Brown       *p = link->next;
2631dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2632dc822a44SJed Brown       break;
2633dc822a44SJed Brown     }
2634dc822a44SJed Brown   }
2635dc822a44SJed Brown   PetscFunctionReturn(0);
2636dc822a44SJed Brown }
2637dc822a44SJed Brown 
2638dc822a44SJed Brown 
2639b17ce1afSJed Brown /*@
2640b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2641b17ce1afSJed Brown 
2642b17ce1afSJed Brown    Collective if any hooks are
2643b17ce1afSJed Brown 
2644b17ce1afSJed Brown    Input Arguments:
2645b17ce1afSJed Brown +  fine - finer DM to use as a base
2646b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2647e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2648b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2649e91eccc2SStefano Zampini -  coarse - coarser DM to update
2650b17ce1afSJed Brown 
2651b17ce1afSJed Brown    Level: developer
2652b17ce1afSJed Brown 
2653b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2654b17ce1afSJed Brown @*/
2655b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2656b17ce1afSJed Brown {
2657b17ce1afSJed Brown   PetscErrorCode    ierr;
2658b17ce1afSJed Brown   DMCoarsenHookLink link;
2659b17ce1afSJed Brown 
2660b17ce1afSJed Brown   PetscFunctionBegin;
2661b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
26628865f1eaSKarl Rupp     if (link->restricthook) {
26638865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
26648865f1eaSKarl Rupp     }
2665b17ce1afSJed Brown   }
266647c6ae99SBarry Smith   PetscFunctionReturn(0);
266747c6ae99SBarry Smith }
266847c6ae99SBarry Smith 
2669bb9467b5SJed Brown /*@C
2670be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
26715dbd56e3SPeter Brune 
26725dbd56e3SPeter Brune    Logically Collective
26735dbd56e3SPeter Brune 
26745dbd56e3SPeter Brune    Input Arguments:
26755dbd56e3SPeter Brune +  global - global DM
2676ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
26775dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
26780298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
26795dbd56e3SPeter Brune 
2680ec4806b8SPeter Brune 
2681ec4806b8SPeter Brune    Calling sequence for ddhook:
2682ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2683ec4806b8SPeter Brune 
2684ec4806b8SPeter Brune +  global - global DM
2685ec4806b8SPeter Brune .  block  - block DM
2686ec4806b8SPeter Brune -  ctx - optional user-defined function context
2687ec4806b8SPeter Brune 
26885dbd56e3SPeter Brune    Calling sequence for restricthook:
2689ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
26905dbd56e3SPeter Brune 
26915dbd56e3SPeter Brune +  global - global DM
26925dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
26935dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2694ec4806b8SPeter Brune .  block  - block DM
26955dbd56e3SPeter Brune -  ctx - optional user-defined function context
26965dbd56e3SPeter Brune 
26975dbd56e3SPeter Brune    Level: advanced
26985dbd56e3SPeter Brune 
26995dbd56e3SPeter Brune    Notes:
2700ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
27015dbd56e3SPeter Brune 
27025dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
27035dbd56e3SPeter Brune 
27045dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2705ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
27065dbd56e3SPeter Brune 
2707bb9467b5SJed Brown    This function is currently not available from Fortran.
2708bb9467b5SJed Brown 
27095dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
27105dbd56e3SPeter Brune @*/
2711be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
27125dbd56e3SPeter Brune {
27135dbd56e3SPeter Brune   PetscErrorCode      ierr;
2714be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
27155dbd56e3SPeter Brune 
27165dbd56e3SPeter Brune   PetscFunctionBegin;
27175dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2718b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2719b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2720b3a6b972SJed Brown   }
272195dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
27225dbd56e3SPeter Brune   link->restricthook = restricthook;
2723be081cd6SPeter Brune   link->ddhook       = ddhook;
27245dbd56e3SPeter Brune   link->ctx          = ctx;
27250298fd71SBarry Smith   link->next         = NULL;
27265dbd56e3SPeter Brune   *p                 = link;
27275dbd56e3SPeter Brune   PetscFunctionReturn(0);
27285dbd56e3SPeter Brune }
27295dbd56e3SPeter Brune 
2730b3a6b972SJed Brown /*@C
2731b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2732b3a6b972SJed Brown 
2733b3a6b972SJed Brown    Logically Collective
2734b3a6b972SJed Brown 
2735b3a6b972SJed Brown    Input Arguments:
2736b3a6b972SJed Brown +  global - global DM
2737b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2738b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2739b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2740b3a6b972SJed Brown 
2741b3a6b972SJed Brown    Level: advanced
2742b3a6b972SJed Brown 
2743b3a6b972SJed Brown    Notes:
2744b3a6b972SJed Brown 
2745b3a6b972SJed Brown    This function is currently not available from Fortran.
2746b3a6b972SJed Brown 
2747b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2748b3a6b972SJed Brown @*/
2749b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2750b3a6b972SJed Brown {
2751b3a6b972SJed Brown   PetscErrorCode      ierr;
2752b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2753b3a6b972SJed Brown 
2754b3a6b972SJed Brown   PetscFunctionBegin;
2755b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2756b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2757b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2758b3a6b972SJed Brown       link = *p;
2759b3a6b972SJed Brown       *p = link->next;
2760b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2761b3a6b972SJed Brown       break;
2762b3a6b972SJed Brown     }
2763b3a6b972SJed Brown   }
2764b3a6b972SJed Brown   PetscFunctionReturn(0);
2765b3a6b972SJed Brown }
2766b3a6b972SJed Brown 
27675dbd56e3SPeter Brune /*@
2768be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
27695dbd56e3SPeter Brune 
27705dbd56e3SPeter Brune    Collective if any hooks are
27715dbd56e3SPeter Brune 
27725dbd56e3SPeter Brune    Input Arguments:
27735dbd56e3SPeter Brune +  fine - finer DM to use as a base
2774be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2775be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
27765dbd56e3SPeter Brune -  coarse - coarer DM to update
27775dbd56e3SPeter Brune 
27785dbd56e3SPeter Brune    Level: developer
27795dbd56e3SPeter Brune 
27805dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
27815dbd56e3SPeter Brune @*/
2782be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
27835dbd56e3SPeter Brune {
27845dbd56e3SPeter Brune   PetscErrorCode      ierr;
2785be081cd6SPeter Brune   DMSubDomainHookLink link;
27865dbd56e3SPeter Brune 
27875dbd56e3SPeter Brune   PetscFunctionBegin;
2788be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
27898865f1eaSKarl Rupp     if (link->restricthook) {
27908865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
27918865f1eaSKarl Rupp     }
27925dbd56e3SPeter Brune   }
27935dbd56e3SPeter Brune   PetscFunctionReturn(0);
27945dbd56e3SPeter Brune }
27955dbd56e3SPeter Brune 
27965fe1f584SPeter Brune /*@
27976a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
27985fe1f584SPeter Brune 
27995fe1f584SPeter Brune     Not Collective
28005fe1f584SPeter Brune 
28015fe1f584SPeter Brune     Input Parameter:
28025fe1f584SPeter Brune .   dm - the DM object
28035fe1f584SPeter Brune 
28045fe1f584SPeter Brune     Output Parameter:
28056a7d9d85SPeter Brune .   level - number of coarsenings
28065fe1f584SPeter Brune 
28075fe1f584SPeter Brune     Level: developer
28085fe1f584SPeter Brune 
28096a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
28105fe1f584SPeter Brune 
28115fe1f584SPeter Brune @*/
28125fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
28135fe1f584SPeter Brune {
28145fe1f584SPeter Brune   PetscFunctionBegin;
28155fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28165fe1f584SPeter Brune   *level = dm->leveldown;
28175fe1f584SPeter Brune   PetscFunctionReturn(0);
28185fe1f584SPeter Brune }
28195fe1f584SPeter Brune 
28205fe1f584SPeter Brune 
28215fe1f584SPeter Brune 
282247c6ae99SBarry Smith /*@C
282347c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
282447c6ae99SBarry Smith 
282547c6ae99SBarry Smith     Collective on DM
282647c6ae99SBarry Smith 
282747c6ae99SBarry Smith     Input Parameter:
282847c6ae99SBarry Smith +   dm - the DM object
282947c6ae99SBarry Smith -   nlevels - the number of levels of refinement
283047c6ae99SBarry Smith 
283147c6ae99SBarry Smith     Output Parameter:
283247c6ae99SBarry Smith .   dmf - the refined DM hierarchy
283347c6ae99SBarry Smith 
283447c6ae99SBarry Smith     Level: developer
283547c6ae99SBarry Smith 
2836e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
283747c6ae99SBarry Smith 
283847c6ae99SBarry Smith @*/
28397087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
284047c6ae99SBarry Smith {
284147c6ae99SBarry Smith   PetscErrorCode ierr;
284247c6ae99SBarry Smith 
284347c6ae99SBarry Smith   PetscFunctionBegin;
2844171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2845ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
284647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
284747c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
284847c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
284947c6ae99SBarry Smith   } else if (dm->ops->refine) {
285047c6ae99SBarry Smith     PetscInt i;
285147c6ae99SBarry Smith 
2852ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
285347c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2854ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
285547c6ae99SBarry Smith     }
2856ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
285747c6ae99SBarry Smith   PetscFunctionReturn(0);
285847c6ae99SBarry Smith }
285947c6ae99SBarry Smith 
286047c6ae99SBarry Smith /*@C
286147c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
286247c6ae99SBarry Smith 
286347c6ae99SBarry Smith     Collective on DM
286447c6ae99SBarry Smith 
286547c6ae99SBarry Smith     Input Parameter:
286647c6ae99SBarry Smith +   dm - the DM object
286747c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
286847c6ae99SBarry Smith 
286947c6ae99SBarry Smith     Output Parameter:
287047c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
287147c6ae99SBarry Smith 
287247c6ae99SBarry Smith     Level: developer
287347c6ae99SBarry Smith 
2874e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
287547c6ae99SBarry Smith 
287647c6ae99SBarry Smith @*/
28777087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
287847c6ae99SBarry Smith {
287947c6ae99SBarry Smith   PetscErrorCode ierr;
288047c6ae99SBarry Smith 
288147c6ae99SBarry Smith   PetscFunctionBegin;
2882171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2883ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
288447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
288547c6ae99SBarry Smith   PetscValidPointer(dmc,3);
288647c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
288747c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
288847c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
288947c6ae99SBarry Smith     PetscInt i;
289047c6ae99SBarry Smith 
2891ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
289247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2893ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
289447c6ae99SBarry Smith     }
2895ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
289647c6ae99SBarry Smith   PetscFunctionReturn(0);
289747c6ae99SBarry Smith }
289847c6ae99SBarry Smith 
289947c6ae99SBarry Smith /*@
2900e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
290147c6ae99SBarry Smith    grids associated with two DMs.
290247c6ae99SBarry Smith 
290347c6ae99SBarry Smith    Collective on DM
290447c6ae99SBarry Smith 
290547c6ae99SBarry Smith    Input Parameters:
290647c6ae99SBarry Smith +  dmc - the coarse grid DM
290747c6ae99SBarry Smith -  dmf - the fine grid DM
290847c6ae99SBarry Smith 
290947c6ae99SBarry Smith    Output Parameters:
291047c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
291147c6ae99SBarry Smith 
291247c6ae99SBarry Smith    Level: intermediate
291347c6ae99SBarry Smith 
291447c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
291547c6ae99SBarry Smith 
2916e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
291747c6ae99SBarry Smith @*/
2918e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
291947c6ae99SBarry Smith {
292047c6ae99SBarry Smith   PetscErrorCode ierr;
292147c6ae99SBarry Smith 
292247c6ae99SBarry Smith   PetscFunctionBegin;
2923171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2924171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
292547c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
292647c6ae99SBarry Smith   PetscFunctionReturn(0);
292747c6ae99SBarry Smith }
292847c6ae99SBarry Smith 
29291a266240SBarry Smith /*@C
29301a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
29311a266240SBarry Smith 
29321a266240SBarry Smith     Not Collective
29331a266240SBarry Smith 
29341a266240SBarry Smith     Input Parameters:
29351a266240SBarry Smith +   dm - the DM object
29361a266240SBarry Smith -   destroy - the destroy function
29371a266240SBarry Smith 
29381a266240SBarry Smith     Level: intermediate
29391a266240SBarry Smith 
2940e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
29411a266240SBarry Smith 
2942f07f9ceaSJed Brown @*/
29431a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
29441a266240SBarry Smith {
29451a266240SBarry Smith   PetscFunctionBegin;
2946171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29471a266240SBarry Smith   dm->ctxdestroy = destroy;
29481a266240SBarry Smith   PetscFunctionReturn(0);
29491a266240SBarry Smith }
29501a266240SBarry Smith 
2951b07ff414SBarry Smith /*@
29521b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
295347c6ae99SBarry Smith 
295447c6ae99SBarry Smith     Not Collective
295547c6ae99SBarry Smith 
295647c6ae99SBarry Smith     Input Parameters:
295747c6ae99SBarry Smith +   dm - the DM object
295847c6ae99SBarry Smith -   ctx - the user context
295947c6ae99SBarry Smith 
296047c6ae99SBarry Smith     Level: intermediate
296147c6ae99SBarry Smith 
2962e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
296347c6ae99SBarry Smith 
296447c6ae99SBarry Smith @*/
29651b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
296647c6ae99SBarry Smith {
296747c6ae99SBarry Smith   PetscFunctionBegin;
2968171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
296947c6ae99SBarry Smith   dm->ctx = ctx;
297047c6ae99SBarry Smith   PetscFunctionReturn(0);
297147c6ae99SBarry Smith }
297247c6ae99SBarry Smith 
297347c6ae99SBarry Smith /*@
29741b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
297547c6ae99SBarry Smith 
297647c6ae99SBarry Smith     Not Collective
297747c6ae99SBarry Smith 
297847c6ae99SBarry Smith     Input Parameter:
297947c6ae99SBarry Smith .   dm - the DM object
298047c6ae99SBarry Smith 
298147c6ae99SBarry Smith     Output Parameter:
298247c6ae99SBarry Smith .   ctx - the user context
298347c6ae99SBarry Smith 
298447c6ae99SBarry Smith     Level: intermediate
298547c6ae99SBarry Smith 
2986e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
298747c6ae99SBarry Smith 
298847c6ae99SBarry Smith @*/
29891b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
299047c6ae99SBarry Smith {
299147c6ae99SBarry Smith   PetscFunctionBegin;
2992171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29931b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
299447c6ae99SBarry Smith   PetscFunctionReturn(0);
299547c6ae99SBarry Smith }
299647c6ae99SBarry Smith 
299708da532bSDmitry Karpeev /*@C
2998df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
299908da532bSDmitry Karpeev 
300008da532bSDmitry Karpeev     Logically Collective on DM
300108da532bSDmitry Karpeev 
300208da532bSDmitry Karpeev     Input Parameter:
300308da532bSDmitry Karpeev +   dm - the DM object
30040298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
300508da532bSDmitry Karpeev 
300608da532bSDmitry Karpeev     Level: intermediate
300708da532bSDmitry Karpeev 
3008835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
300908da532bSDmitry Karpeev          DMSetJacobian()
301008da532bSDmitry Karpeev 
301108da532bSDmitry Karpeev @*/
301208da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
301308da532bSDmitry Karpeev {
301408da532bSDmitry Karpeev   PetscFunctionBegin;
301508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
301608da532bSDmitry Karpeev   PetscFunctionReturn(0);
301708da532bSDmitry Karpeev }
301808da532bSDmitry Karpeev 
301908da532bSDmitry Karpeev /*@
302008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
302108da532bSDmitry Karpeev 
302208da532bSDmitry Karpeev     Not Collective
302308da532bSDmitry Karpeev 
302408da532bSDmitry Karpeev     Input Parameter:
302508da532bSDmitry Karpeev .   dm - the DM object to destroy
302608da532bSDmitry Karpeev 
302708da532bSDmitry Karpeev     Output Parameter:
302808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
302908da532bSDmitry Karpeev 
303008da532bSDmitry Karpeev     Level: developer
303108da532bSDmitry Karpeev 
303274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
303308da532bSDmitry Karpeev 
303408da532bSDmitry Karpeev @*/
303508da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
303608da532bSDmitry Karpeev {
303708da532bSDmitry Karpeev   PetscFunctionBegin;
303808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
303908da532bSDmitry Karpeev   PetscFunctionReturn(0);
304008da532bSDmitry Karpeev }
304108da532bSDmitry Karpeev 
304208da532bSDmitry Karpeev /*@C
304308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
304408da532bSDmitry Karpeev 
304508da532bSDmitry Karpeev     Logically Collective on DM
304608da532bSDmitry Karpeev 
304708da532bSDmitry Karpeev     Input Parameters:
3048907376e6SBarry Smith .   dm - the DM object
304908da532bSDmitry Karpeev 
305008da532bSDmitry Karpeev     Output parameters:
305108da532bSDmitry Karpeev +   xl - lower bound
305208da532bSDmitry Karpeev -   xu - upper bound
305308da532bSDmitry Karpeev 
3054907376e6SBarry Smith     Level: advanced
3055907376e6SBarry Smith 
305695452b02SPatrick Sanan     Notes:
305795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
305808da532bSDmitry Karpeev 
305974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
306008da532bSDmitry Karpeev 
306108da532bSDmitry Karpeev @*/
306208da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
306308da532bSDmitry Karpeev {
306408da532bSDmitry Karpeev   PetscErrorCode ierr;
30655fd66863SKarl Rupp 
306608da532bSDmitry Karpeev   PetscFunctionBegin;
306708da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
306808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
306908da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
307008da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
30718865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
307208da532bSDmitry Karpeev   PetscFunctionReturn(0);
307308da532bSDmitry Karpeev }
307408da532bSDmitry Karpeev 
3075b0ae01b7SPeter Brune /*@
3076b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3077b0ae01b7SPeter Brune 
3078b0ae01b7SPeter Brune     Not Collective
3079b0ae01b7SPeter Brune 
3080b0ae01b7SPeter Brune     Input Parameter:
3081b0ae01b7SPeter Brune .   dm - the DM object
3082b0ae01b7SPeter Brune 
3083b0ae01b7SPeter Brune     Output Parameter:
3084b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3085b0ae01b7SPeter Brune 
3086b0ae01b7SPeter Brune     Level: developer
3087b0ae01b7SPeter Brune 
3088b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3089b0ae01b7SPeter Brune 
3090b0ae01b7SPeter Brune @*/
3091b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3092b0ae01b7SPeter Brune {
3093b0ae01b7SPeter Brune   PetscFunctionBegin;
3094b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3095b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3096b0ae01b7SPeter Brune }
3097b0ae01b7SPeter Brune 
30983ad4599aSBarry Smith /*@
30993ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
31003ad4599aSBarry Smith 
31013ad4599aSBarry Smith     Not Collective
31023ad4599aSBarry Smith 
31033ad4599aSBarry Smith     Input Parameter:
31043ad4599aSBarry Smith .   dm - the DM object
31053ad4599aSBarry Smith 
31063ad4599aSBarry Smith     Output Parameter:
31073ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
31083ad4599aSBarry Smith 
31093ad4599aSBarry Smith     Level: developer
31103ad4599aSBarry Smith 
31113ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
31123ad4599aSBarry Smith 
31133ad4599aSBarry Smith @*/
31143ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
31153ad4599aSBarry Smith {
31163ad4599aSBarry Smith   PetscFunctionBegin;
31173ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
31183ad4599aSBarry Smith   PetscFunctionReturn(0);
31193ad4599aSBarry Smith }
31203ad4599aSBarry Smith 
3121a7058e45SLawrence Mitchell 
3122a7058e45SLawrence Mitchell /*@
3123a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3124a7058e45SLawrence Mitchell 
3125a7058e45SLawrence Mitchell     Not Collective
3126a7058e45SLawrence Mitchell 
3127a7058e45SLawrence Mitchell     Input Parameter:
3128a7058e45SLawrence Mitchell .   dm - the DM object
3129a7058e45SLawrence Mitchell 
3130a7058e45SLawrence Mitchell     Output Parameter:
3131a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3132a7058e45SLawrence Mitchell 
3133a7058e45SLawrence Mitchell     Level: developer
3134a7058e45SLawrence Mitchell 
3135a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3136a7058e45SLawrence Mitchell 
3137a7058e45SLawrence Mitchell @*/
3138a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3139a7058e45SLawrence Mitchell {
31404a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3141a7058e45SLawrence Mitchell   PetscFunctionBegin;
31424a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
31434a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3144a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3145a7058e45SLawrence Mitchell }
3146a7058e45SLawrence Mitchell 
3147748fac09SDmitry Karpeev /*@C
314808da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
314908da532bSDmitry Karpeev 
315008da532bSDmitry Karpeev     Collective on DM
315108da532bSDmitry Karpeev 
315208da532bSDmitry Karpeev     Input Parameter:
315308da532bSDmitry Karpeev +   dm - the DM object
31540298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
315508da532bSDmitry Karpeev 
315608da532bSDmitry Karpeev     Level: developer
315708da532bSDmitry Karpeev 
315874e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
315908da532bSDmitry Karpeev 
316008da532bSDmitry Karpeev @*/
316108da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
316208da532bSDmitry Karpeev {
316308da532bSDmitry Karpeev   PetscErrorCode ierr;
31645fd66863SKarl Rupp 
316508da532bSDmitry Karpeev   PetscFunctionBegin;
316608da532bSDmitry Karpeev   if (x) {
316708da532bSDmitry Karpeev     if (!dm->x) {
316808da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
316908da532bSDmitry Karpeev     }
317008da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
31718865f1eaSKarl Rupp   } else if (dm->x) {
317208da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
317308da532bSDmitry Karpeev   }
317408da532bSDmitry Karpeev   PetscFunctionReturn(0);
317508da532bSDmitry Karpeev }
317608da532bSDmitry Karpeev 
31770298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3178264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3179264ace61SBarry Smith 
3180264ace61SBarry Smith /*@C
3181264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3182264ace61SBarry Smith 
3183264ace61SBarry Smith   Collective on DM
3184264ace61SBarry Smith 
3185264ace61SBarry Smith   Input Parameters:
3186264ace61SBarry Smith + dm     - The DM object
3187264ace61SBarry Smith - method - The name of the DM type
3188264ace61SBarry Smith 
3189264ace61SBarry Smith   Options Database Key:
3190264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3191264ace61SBarry Smith 
3192264ace61SBarry Smith   Notes:
3193e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3194264ace61SBarry Smith 
3195264ace61SBarry Smith   Level: intermediate
3196264ace61SBarry Smith 
3197264ace61SBarry Smith .keywords: DM, set, type
3198264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3199264ace61SBarry Smith @*/
320019fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3201264ace61SBarry Smith {
3202264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3203264ace61SBarry Smith   PetscBool      match;
3204264ace61SBarry Smith   PetscErrorCode ierr;
3205264ace61SBarry Smith 
3206264ace61SBarry Smith   PetscFunctionBegin;
3207264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3208251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3209264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3210264ace61SBarry Smith 
32110f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
32121c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3213ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3214264ace61SBarry Smith 
3215264ace61SBarry Smith   if (dm->ops->destroy) {
3216264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
32170298fd71SBarry Smith     dm->ops->destroy = NULL;
3218264ace61SBarry Smith   }
3219264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3220264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3221264ace61SBarry Smith   PetscFunctionReturn(0);
3222264ace61SBarry Smith }
3223264ace61SBarry Smith 
3224264ace61SBarry Smith /*@C
3225264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3226264ace61SBarry Smith 
3227264ace61SBarry Smith   Not Collective
3228264ace61SBarry Smith 
3229264ace61SBarry Smith   Input Parameter:
3230264ace61SBarry Smith . dm  - The DM
3231264ace61SBarry Smith 
3232264ace61SBarry Smith   Output Parameter:
3233264ace61SBarry Smith . type - The DM type name
3234264ace61SBarry Smith 
3235264ace61SBarry Smith   Level: intermediate
3236264ace61SBarry Smith 
3237264ace61SBarry Smith .keywords: DM, get, type, name
3238264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3239264ace61SBarry Smith @*/
324019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3241264ace61SBarry Smith {
3242264ace61SBarry Smith   PetscErrorCode ierr;
3243264ace61SBarry Smith 
3244264ace61SBarry Smith   PetscFunctionBegin;
3245264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3246c959eef4SJed Brown   PetscValidPointer(type,2);
3247607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3248264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3249264ace61SBarry Smith   PetscFunctionReturn(0);
3250264ace61SBarry Smith }
3251264ace61SBarry Smith 
325267a56275SMatthew G Knepley /*@C
325367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
325467a56275SMatthew G Knepley 
325567a56275SMatthew G Knepley   Collective on DM
325667a56275SMatthew G Knepley 
325767a56275SMatthew G Knepley   Input Parameters:
325867a56275SMatthew G Knepley + dm - the DM
325967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
326067a56275SMatthew G Knepley 
326167a56275SMatthew G Knepley   Output Parameter:
326267a56275SMatthew G Knepley . M - pointer to new DM
326367a56275SMatthew G Knepley 
326467a56275SMatthew G Knepley   Notes:
326567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
326667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
326767a56275SMatthew G Knepley   of the input DM.
326867a56275SMatthew G Knepley 
326967a56275SMatthew G Knepley   Level: intermediate
327067a56275SMatthew G Knepley 
327167a56275SMatthew G Knepley .seealso: DMCreate()
327267a56275SMatthew G Knepley @*/
327319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
327467a56275SMatthew G Knepley {
327567a56275SMatthew G Knepley   DM             B;
327667a56275SMatthew G Knepley   char           convname[256];
3277c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
327867a56275SMatthew G Knepley   PetscErrorCode ierr;
327967a56275SMatthew G Knepley 
328067a56275SMatthew G Knepley   PetscFunctionBegin;
328167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
328267a56275SMatthew G Knepley   PetscValidType(dm,1);
328367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3284251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3285c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3286c067b6caSMatthew G. Knepley   if (sametype) {
3287c067b6caSMatthew G. Knepley     *M   = dm;
3288c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3289c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3290c067b6caSMatthew G. Knepley   } else {
32910298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
329267a56275SMatthew G Knepley 
329367a56275SMatthew G Knepley     /*
329467a56275SMatthew G Knepley        Order of precedence:
329567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
329667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
329767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
329867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
329967a56275SMatthew G Knepley        5) Use a really basic converter.
330067a56275SMatthew G Knepley     */
330167a56275SMatthew G Knepley 
330267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3303a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3304a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3305a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3306a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3307a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
33080005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
330967a56275SMatthew G Knepley     if (conv) goto foundconv;
331067a56275SMatthew G Knepley 
331167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
331282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
331367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3314a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3315a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3316a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3317a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3318a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
33190005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
332067a56275SMatthew G Knepley     if (conv) {
3321fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
332267a56275SMatthew G Knepley       goto foundconv;
332367a56275SMatthew G Knepley     }
332467a56275SMatthew G Knepley 
332567a56275SMatthew G Knepley #if 0
332667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
332767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3328fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
332967a56275SMatthew G Knepley     if (conv) goto foundconv;
333067a56275SMatthew G Knepley 
333167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
333267a56275SMatthew G Knepley     if (dm->ops->convert) {
333367a56275SMatthew G Knepley       conv = dm->ops->convert;
333467a56275SMatthew G Knepley     }
333567a56275SMatthew G Knepley     if (conv) goto foundconv;
333667a56275SMatthew G Knepley #endif
333767a56275SMatthew G Knepley 
333867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
333982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
334067a56275SMatthew G Knepley 
334167a56275SMatthew G Knepley foundconv:
334267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
334367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
334412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
334590b157c4SStefano Zampini     {
334690b157c4SStefano Zampini       PetscBool             isper;
334712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
334812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
334990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
335090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
335112fa691eSMatthew G. Knepley     }
335267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
335367a56275SMatthew G Knepley   }
335467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
335567a56275SMatthew G Knepley   PetscFunctionReturn(0);
335667a56275SMatthew G Knepley }
3357264ace61SBarry Smith 
3358264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3359264ace61SBarry Smith 
3360264ace61SBarry Smith /*@C
33611c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
33621c84c290SBarry Smith 
33631c84c290SBarry Smith   Not Collective
33641c84c290SBarry Smith 
33651c84c290SBarry Smith   Input Parameters:
33661c84c290SBarry Smith + name        - The name of a new user-defined creation routine
33671c84c290SBarry Smith - create_func - The creation routine itself
33681c84c290SBarry Smith 
33691c84c290SBarry Smith   Notes:
33701c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
33711c84c290SBarry Smith 
33721c84c290SBarry Smith 
33731c84c290SBarry Smith   Sample usage:
33741c84c290SBarry Smith .vb
3375bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
33761c84c290SBarry Smith .ve
33771c84c290SBarry Smith 
33781c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
33791c84c290SBarry Smith .vb
33801c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
33811c84c290SBarry Smith     DMSetType(DM,"my_da");
33821c84c290SBarry Smith .ve
33831c84c290SBarry Smith    or at runtime via the option
33841c84c290SBarry Smith .vb
33851c84c290SBarry Smith     -da_type my_da
33861c84c290SBarry Smith .ve
3387264ace61SBarry Smith 
3388264ace61SBarry Smith   Level: advanced
33891c84c290SBarry Smith 
33901c84c290SBarry Smith .keywords: DM, register
3391bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
33921c84c290SBarry Smith 
3393264ace61SBarry Smith @*/
3394bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3395264ace61SBarry Smith {
3396264ace61SBarry Smith   PetscErrorCode ierr;
3397264ace61SBarry Smith 
3398264ace61SBarry Smith   PetscFunctionBegin;
3399a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3400264ace61SBarry Smith   PetscFunctionReturn(0);
3401264ace61SBarry Smith }
3402264ace61SBarry Smith 
3403b859378eSBarry Smith /*@C
340455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3405b859378eSBarry Smith 
3406b859378eSBarry Smith   Collective on PetscViewer
3407b859378eSBarry Smith 
3408b859378eSBarry Smith   Input Parameters:
3409b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3410b859378eSBarry Smith            some related function before a call to DMLoad().
3411b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3412b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3413b859378eSBarry Smith 
3414b859378eSBarry Smith    Level: intermediate
3415b859378eSBarry Smith 
3416b859378eSBarry Smith   Notes:
341755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3418b859378eSBarry Smith 
3419b859378eSBarry Smith   Notes for advanced users:
3420b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3421b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3422b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3423b859378eSBarry Smith   format is
3424b859378eSBarry Smith .vb
3425b859378eSBarry Smith      has not yet been determined
3426b859378eSBarry Smith .ve
3427b859378eSBarry Smith 
3428b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3429b859378eSBarry Smith @*/
3430b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3431b859378eSBarry Smith {
34329331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3433b859378eSBarry Smith   PetscErrorCode ierr;
3434b859378eSBarry Smith 
3435b859378eSBarry Smith   PetscFunctionBegin;
3436b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3437b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
343832c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
34399331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
34409331c7a4SMatthew G. Knepley   if (isbinary) {
34419331c7a4SMatthew G. Knepley     PetscInt classid;
34429331c7a4SMatthew G. Knepley     char     type[256];
3443b859378eSBarry Smith 
3444060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
34459200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3446060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
344732c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
34489331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
34499331c7a4SMatthew G. Knepley   } else if (ishdf5) {
34509331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
34519331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3452b859378eSBarry Smith   PetscFunctionReturn(0);
3453b859378eSBarry Smith }
3454b859378eSBarry Smith 
34557da65231SMatthew G Knepley /******************************** FEM Support **********************************/
34567da65231SMatthew G Knepley 
3457a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3458a6dfd86eSKarl Rupp {
34591d47ebbbSSatish Balay   PetscInt       f;
34601b30c384SMatthew G Knepley   PetscErrorCode ierr;
34611b30c384SMatthew G Knepley 
34627da65231SMatthew G Knepley   PetscFunctionBegin;
346374778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
34641d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
346557622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
34667da65231SMatthew G Knepley   }
34677da65231SMatthew G Knepley   PetscFunctionReturn(0);
34687da65231SMatthew G Knepley }
34697da65231SMatthew G Knepley 
3470a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3471a6dfd86eSKarl Rupp {
34721b30c384SMatthew G Knepley   PetscInt       f, g;
34737da65231SMatthew G Knepley   PetscErrorCode ierr;
34747da65231SMatthew G Knepley 
34757da65231SMatthew G Knepley   PetscFunctionBegin;
347674778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
34771d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
347874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
34791d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3480e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
34817da65231SMatthew G Knepley     }
348274778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
34837da65231SMatthew G Knepley   }
34847da65231SMatthew G Knepley   PetscFunctionReturn(0);
34857da65231SMatthew G Knepley }
3486e7c4fc90SDmitry Karpeev 
34876113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3488e759306cSMatthew G. Knepley {
34890c5b8624SToby Isaac   PetscInt          localSize, bs;
34900c5b8624SToby Isaac   PetscMPIInt       size;
34910c5b8624SToby Isaac   Vec               x, xglob;
34920c5b8624SToby Isaac   const PetscScalar *xarray;
3493e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3494e759306cSMatthew G. Knepley 
3495e759306cSMatthew G. Knepley   PetscFunctionBegin;
34969852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3497e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3498e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
34996113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
35000c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
35010c5b8624SToby Isaac   if (size > 1) {
35020c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
35030c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
35040c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
35050c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
35060c5b8624SToby Isaac   } else {
35070c5b8624SToby Isaac     xglob = x;
35080c5b8624SToby Isaac   }
35090c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
35100c5b8624SToby Isaac   if (size > 1) {
35110c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
35120c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
35130c5b8624SToby Isaac   }
3514e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3515e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3516e759306cSMatthew G. Knepley }
3517e759306cSMatthew G. Knepley 
351888ed4aceSMatthew G Knepley /*@
351988ed4aceSMatthew G Knepley   DMGetDefaultSection - Get the PetscSection encoding the local data layout for the DM.
352088ed4aceSMatthew G Knepley 
352188ed4aceSMatthew G Knepley   Input Parameter:
352288ed4aceSMatthew G Knepley . dm - The DM
352388ed4aceSMatthew G Knepley 
352488ed4aceSMatthew G Knepley   Output Parameter:
352588ed4aceSMatthew G Knepley . section - The PetscSection
352688ed4aceSMatthew G Knepley 
352788ed4aceSMatthew G Knepley   Level: intermediate
352888ed4aceSMatthew G Knepley 
352988ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
353088ed4aceSMatthew G Knepley 
353188ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
353288ed4aceSMatthew G Knepley @*/
35330adebc6cSBarry Smith PetscErrorCode DMGetDefaultSection(DM dm, PetscSection *section)
35340adebc6cSBarry Smith {
3535fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3536fd59a867SMatthew G. Knepley 
353788ed4aceSMatthew G Knepley   PetscFunctionBegin;
353888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
353988ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
35402f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
35412f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3542ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
35432f0f8703SMatthew G. Knepley   }
354488ed4aceSMatthew G Knepley   *section = dm->defaultSection;
354588ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
354688ed4aceSMatthew G Knepley }
354788ed4aceSMatthew G Knepley 
354888ed4aceSMatthew G Knepley /*@
354988ed4aceSMatthew G Knepley   DMSetDefaultSection - Set the PetscSection encoding the local data layout for the DM.
355088ed4aceSMatthew G Knepley 
355188ed4aceSMatthew G Knepley   Input Parameters:
355288ed4aceSMatthew G Knepley + dm - The DM
355388ed4aceSMatthew G Knepley - section - The PetscSection
355488ed4aceSMatthew G Knepley 
355588ed4aceSMatthew G Knepley   Level: intermediate
355688ed4aceSMatthew G Knepley 
355788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
355888ed4aceSMatthew G Knepley 
355988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultGlobalSection()
356088ed4aceSMatthew G Knepley @*/
35610adebc6cSBarry Smith PetscErrorCode DMSetDefaultSection(DM dm, PetscSection section)
35620adebc6cSBarry Smith {
3563c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3564af122d2aSMatthew G Knepley   PetscInt       f;
356588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
356688ed4aceSMatthew G Knepley 
356788ed4aceSMatthew G Knepley   PetscFunctionBegin;
356888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3569c473ab19SMatthew G. Knepley   if (section) {
35701d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
35711d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3572c473ab19SMatthew G. Knepley   }
357388ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
357488ed4aceSMatthew G Knepley   dm->defaultSection = section;
3575c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3576af122d2aSMatthew G Knepley   if (numFields) {
3577af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3578af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
35790f21e855SMatthew G. Knepley       PetscObject disc;
3580af122d2aSMatthew G Knepley       const char *name;
3581af122d2aSMatthew G Knepley 
3582af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
35830f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
35840f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3585af122d2aSMatthew G Knepley     }
3586af122d2aSMatthew G Knepley   }
35871d799100SJed Brown   /* The global section will be rebuilt in the next call to DMGetDefaultGlobalSection(). */
35881d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
358988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
359088ed4aceSMatthew G Knepley }
359188ed4aceSMatthew G Knepley 
35929435951eSToby Isaac /*@
35939435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
35949435951eSToby Isaac 
3595e228b242SToby Isaac   not collective
3596e228b242SToby Isaac 
35979435951eSToby Isaac   Input Parameter:
35989435951eSToby Isaac . dm - The DM
35999435951eSToby Isaac 
36009435951eSToby Isaac   Output Parameter:
36019435951eSToby 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.
36029435951eSToby 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.
36039435951eSToby Isaac 
36049435951eSToby Isaac   Level: advanced
36059435951eSToby Isaac 
36069435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
36079435951eSToby Isaac 
36089435951eSToby Isaac .seealso: DMSetDefaultConstraints()
36099435951eSToby Isaac @*/
36109435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
36119435951eSToby Isaac {
36129435951eSToby Isaac   PetscErrorCode ierr;
36139435951eSToby Isaac 
36149435951eSToby Isaac   PetscFunctionBegin;
36159435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36169435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
361745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
361845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
36199435951eSToby Isaac   PetscFunctionReturn(0);
36209435951eSToby Isaac }
36219435951eSToby Isaac 
36229435951eSToby Isaac /*@
36239435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
36249435951eSToby Isaac 
36259435951eSToby 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().
36269435951eSToby Isaac 
36279435951eSToby 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.
36289435951eSToby Isaac 
3629e228b242SToby Isaac   collective on dm
3630e228b242SToby Isaac 
36319435951eSToby Isaac   Input Parameters:
36329435951eSToby Isaac + dm - The DM
3633e228b242SToby 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).
3634e228b242SToby 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).
36359435951eSToby Isaac 
36369435951eSToby Isaac   Level: advanced
36379435951eSToby Isaac 
36389435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
36399435951eSToby Isaac 
36409435951eSToby Isaac .seealso: DMGetDefaultConstraints()
36419435951eSToby Isaac @*/
36429435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
36439435951eSToby Isaac {
3644e228b242SToby Isaac   PetscMPIInt result;
36459435951eSToby Isaac   PetscErrorCode ierr;
36469435951eSToby Isaac 
36479435951eSToby Isaac   PetscFunctionBegin;
36489435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3649e228b242SToby Isaac   if (section) {
3650e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3651e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3652f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3653e228b242SToby Isaac   }
3654e228b242SToby Isaac   if (mat) {
3655e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3656e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3657f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3658e228b242SToby Isaac   }
36599435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
36609435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
36619435951eSToby Isaac   dm->defaultConstraintSection = section;
36629435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
36639435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
36649435951eSToby Isaac   dm->defaultConstraintMat = mat;
36659435951eSToby Isaac   PetscFunctionReturn(0);
36669435951eSToby Isaac }
36679435951eSToby Isaac 
3668497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3669507e4973SMatthew G. Knepley /*
3670507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3671507e4973SMatthew G. Knepley 
3672507e4973SMatthew G. Knepley   Input Parameters:
3673507e4973SMatthew G. Knepley + dm - The DM
3674507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3675507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3676507e4973SMatthew G. Knepley 
3677507e4973SMatthew G. Knepley   Level: intermediate
3678507e4973SMatthew G. Knepley 
3679507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3680507e4973SMatthew G. Knepley */
3681f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3682507e4973SMatthew G. Knepley {
3683507e4973SMatthew G. Knepley   MPI_Comm        comm;
3684507e4973SMatthew G. Knepley   PetscLayout     layout;
3685507e4973SMatthew G. Knepley   const PetscInt *ranges;
3686507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3687507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3688507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3689507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3690507e4973SMatthew G. Knepley 
3691507e4973SMatthew G. Knepley   PetscFunctionBegin;
3692507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3693507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3694507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3695507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3696507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3697507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3698507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3699507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3700507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3701507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3702507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3703507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3704f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3705507e4973SMatthew G. Knepley 
3706507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3707507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3708507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3709507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3710507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3711507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3712507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3713507e4973SMatthew 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;}
3714507e4973SMatthew 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;}
3715507e4973SMatthew G. Knepley     if (gdof < 0) {
3716507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3717507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3718507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3719507e4973SMatthew G. Knepley 
3720507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3721507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3722507e4973SMatthew 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;}
3723507e4973SMatthew G. Knepley       }
3724507e4973SMatthew G. Knepley     }
3725507e4973SMatthew G. Knepley   }
3726507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3727507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3728b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3729507e4973SMatthew G. Knepley   if (!gvalid) {
3730507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3731507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3732507e4973SMatthew G. Knepley   }
3733507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3734507e4973SMatthew G. Knepley }
3735f741bcd2SMatthew G. Knepley #endif
3736507e4973SMatthew G. Knepley 
373788ed4aceSMatthew G Knepley /*@
373888ed4aceSMatthew G Knepley   DMGetDefaultGlobalSection - Get the PetscSection encoding the global data layout for the DM.
373988ed4aceSMatthew G Knepley 
37408b1ab98fSJed Brown   Collective on DM
37418b1ab98fSJed Brown 
374288ed4aceSMatthew G Knepley   Input Parameter:
374388ed4aceSMatthew G Knepley . dm - The DM
374488ed4aceSMatthew G Knepley 
374588ed4aceSMatthew G Knepley   Output Parameter:
374688ed4aceSMatthew G Knepley . section - The PetscSection
374788ed4aceSMatthew G Knepley 
374888ed4aceSMatthew G Knepley   Level: intermediate
374988ed4aceSMatthew G Knepley 
375088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
375188ed4aceSMatthew G Knepley 
375288ed4aceSMatthew G Knepley .seealso: DMSetDefaultSection(), DMGetDefaultSection()
375388ed4aceSMatthew G Knepley @*/
37540adebc6cSBarry Smith PetscErrorCode DMGetDefaultGlobalSection(DM dm, PetscSection *section)
37550adebc6cSBarry Smith {
375688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
375788ed4aceSMatthew G Knepley 
375888ed4aceSMatthew G Knepley   PetscFunctionBegin;
375988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
376088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
376188ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3762fd59a867SMatthew G. Knepley     PetscSection s;
3763fd59a867SMatthew G. Knepley 
3764fd59a867SMatthew G. Knepley     ierr = DMGetDefaultSection(dm, &s);CHKERRQ(ierr);
3765fd59a867SMatthew 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");
3766fd59a867SMatthew G. Knepley     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a default PetscSF in order to create a global PetscSection");
376715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3768cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3769ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3770685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
377188ed4aceSMatthew G Knepley   }
377288ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
377388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
377488ed4aceSMatthew G Knepley }
377588ed4aceSMatthew G Knepley 
3776b21d0597SMatthew G Knepley /*@
3777b21d0597SMatthew G Knepley   DMSetDefaultGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3778b21d0597SMatthew G Knepley 
3779b21d0597SMatthew G Knepley   Input Parameters:
3780b21d0597SMatthew G Knepley + dm - The DM
37815080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3782b21d0597SMatthew G Knepley 
3783b21d0597SMatthew G Knepley   Level: intermediate
3784b21d0597SMatthew G Knepley 
3785b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3786b21d0597SMatthew G Knepley 
3787b21d0597SMatthew G Knepley .seealso: DMGetDefaultGlobalSection(), DMSetDefaultSection()
3788b21d0597SMatthew G Knepley @*/
37890adebc6cSBarry Smith PetscErrorCode DMSetDefaultGlobalSection(DM dm, PetscSection section)
37900adebc6cSBarry Smith {
3791b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3792b21d0597SMatthew G Knepley 
3793b21d0597SMatthew G Knepley   PetscFunctionBegin;
3794b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37955080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
37961d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3797b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3798b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3799497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
3800f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3801507e4973SMatthew G. Knepley #endif
3802b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3803b21d0597SMatthew G Knepley }
3804b21d0597SMatthew G Knepley 
380588ed4aceSMatthew G Knepley /*@
380688ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
380788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
380888ed4aceSMatthew G Knepley 
380988ed4aceSMatthew G Knepley   Input Parameter:
381088ed4aceSMatthew G Knepley . dm - The DM
381188ed4aceSMatthew G Knepley 
381288ed4aceSMatthew G Knepley   Output Parameter:
381388ed4aceSMatthew G Knepley . sf - The PetscSF
381488ed4aceSMatthew G Knepley 
381588ed4aceSMatthew G Knepley   Level: intermediate
381688ed4aceSMatthew G Knepley 
381788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
381888ed4aceSMatthew G Knepley 
381988ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
382088ed4aceSMatthew G Knepley @*/
38210adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
38220adebc6cSBarry Smith {
382388ed4aceSMatthew G Knepley   PetscInt       nroots;
382488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
382588ed4aceSMatthew G Knepley 
382688ed4aceSMatthew G Knepley   PetscFunctionBegin;
382788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
382888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
38290298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
383088ed4aceSMatthew G Knepley   if (nroots < 0) {
383188ed4aceSMatthew G Knepley     PetscSection section, gSection;
383288ed4aceSMatthew G Knepley 
383388ed4aceSMatthew G Knepley     ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
383431ea6d37SMatthew G Knepley     if (section) {
383588ed4aceSMatthew G Knepley       ierr = DMGetDefaultGlobalSection(dm, &gSection);CHKERRQ(ierr);
383688ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
383731ea6d37SMatthew G Knepley     } else {
38380298fd71SBarry Smith       *sf = NULL;
383931ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
384031ea6d37SMatthew G Knepley     }
384188ed4aceSMatthew G Knepley   }
384288ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
384388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
384488ed4aceSMatthew G Knepley }
384588ed4aceSMatthew G Knepley 
384688ed4aceSMatthew G Knepley /*@
384788ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
384888ed4aceSMatthew G Knepley 
384988ed4aceSMatthew G Knepley   Input Parameters:
385088ed4aceSMatthew G Knepley + dm - The DM
385188ed4aceSMatthew G Knepley - sf - The PetscSF
385288ed4aceSMatthew G Knepley 
385388ed4aceSMatthew G Knepley   Level: intermediate
385488ed4aceSMatthew G Knepley 
385588ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
385688ed4aceSMatthew G Knepley 
385788ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
385888ed4aceSMatthew G Knepley @*/
38590adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
38600adebc6cSBarry Smith {
386188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
386288ed4aceSMatthew G Knepley 
386388ed4aceSMatthew G Knepley   PetscFunctionBegin;
386488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
386588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
386688ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
386788ed4aceSMatthew G Knepley   dm->defaultSF = sf;
386888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
386988ed4aceSMatthew G Knepley }
387088ed4aceSMatthew G Knepley 
387188ed4aceSMatthew G Knepley /*@C
387288ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
387388ed4aceSMatthew G Knepley   describing the data layout.
387488ed4aceSMatthew G Knepley 
387588ed4aceSMatthew G Knepley   Input Parameters:
387688ed4aceSMatthew G Knepley + dm - The DM
387788ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
387888ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
387988ed4aceSMatthew G Knepley 
388088ed4aceSMatthew G Knepley   Level: intermediate
388188ed4aceSMatthew G Knepley 
388288ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
388388ed4aceSMatthew G Knepley @*/
388488ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
388588ed4aceSMatthew G Knepley {
388682f516ccSBarry Smith   MPI_Comm       comm;
388788ed4aceSMatthew G Knepley   PetscLayout    layout;
388888ed4aceSMatthew G Knepley   const PetscInt *ranges;
388988ed4aceSMatthew G Knepley   PetscInt       *local;
389088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3891ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
389288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
389388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
389488ed4aceSMatthew G Knepley 
389588ed4aceSMatthew G Knepley   PetscFunctionBegin;
389682f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
389788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
389888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
389988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
390088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
390188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
390288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
390388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
390488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
390588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
390688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3907ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
39086636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
390988ed4aceSMatthew G Knepley 
39106636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39116636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3912235fbf56SMatthew 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));
39136636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
391488ed4aceSMatthew G Knepley   }
3915785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3916785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
391788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
39181f588964SMatthew G Knepley     const PetscInt *cind;
39196636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
392088ed4aceSMatthew G Knepley 
392188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
392288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
392388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
392488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
392588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39266636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
392788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
39286636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
39296636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
39306636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3931057b4bcdSMatthew 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);
39326636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
39336636e97aSMatthew G Knepley     }
393488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
393588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
393688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
393788ed4aceSMatthew G Knepley     }
393888ed4aceSMatthew G Knepley     if (gdof < 0) {
39396636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
394088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
394188ed4aceSMatthew G Knepley 
394205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
394331d3f06eSJed Brown         if (r < 0) r = -(r+2);
394405376888SMatthew 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);
394588ed4aceSMatthew G Knepley         remote[l].rank  = r;
394688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
394788ed4aceSMatthew G Knepley       }
394888ed4aceSMatthew G Knepley     } else {
39496636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
395088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
395188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
395288ed4aceSMatthew G Knepley       }
395388ed4aceSMatthew G Knepley     }
395488ed4aceSMatthew G Knepley   }
39556636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
395688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
395788ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
395888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
395988ed4aceSMatthew G Knepley }
3960af122d2aSMatthew G Knepley 
3961b21d0597SMatthew G Knepley /*@
3962b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3963b21d0597SMatthew G Knepley 
3964b21d0597SMatthew G Knepley   Input Parameter:
3965b21d0597SMatthew G Knepley . dm - The DM
3966b21d0597SMatthew G Knepley 
3967b21d0597SMatthew G Knepley   Output Parameter:
3968b21d0597SMatthew G Knepley . sf - The PetscSF
3969b21d0597SMatthew G Knepley 
3970b21d0597SMatthew G Knepley   Level: intermediate
3971b21d0597SMatthew G Knepley 
3972b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3973b21d0597SMatthew G Knepley 
3974057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3975b21d0597SMatthew G Knepley @*/
39760adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
39770adebc6cSBarry Smith {
3978b21d0597SMatthew G Knepley   PetscFunctionBegin;
3979b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3980b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3981b21d0597SMatthew G Knepley   *sf = dm->sf;
3982b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3983b21d0597SMatthew G Knepley }
3984b21d0597SMatthew G Knepley 
3985057b4bcdSMatthew G Knepley /*@
3986057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3987057b4bcdSMatthew G Knepley 
3988057b4bcdSMatthew G Knepley   Input Parameters:
3989057b4bcdSMatthew G Knepley + dm - The DM
3990057b4bcdSMatthew G Knepley - sf - The PetscSF
3991057b4bcdSMatthew G Knepley 
3992057b4bcdSMatthew G Knepley   Level: intermediate
3993057b4bcdSMatthew G Knepley 
3994057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3995057b4bcdSMatthew G Knepley @*/
39960adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
39970adebc6cSBarry Smith {
3998057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3999057b4bcdSMatthew G Knepley 
4000057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4001057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4002057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
4003057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4004057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
4005057b4bcdSMatthew G Knepley   dm->sf = sf;
4006057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4007057b4bcdSMatthew G Knepley }
4008057b4bcdSMatthew G Knepley 
40092764a2aaSMatthew G. Knepley /*@
40102764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
40112764a2aaSMatthew G. Knepley 
40122764a2aaSMatthew G. Knepley   Input Parameter:
40132764a2aaSMatthew G. Knepley . dm - The DM
40142764a2aaSMatthew G. Knepley 
40152764a2aaSMatthew G. Knepley   Output Parameter:
40162764a2aaSMatthew G. Knepley . prob - The PetscDS
40172764a2aaSMatthew G. Knepley 
40182764a2aaSMatthew G. Knepley   Level: developer
40192764a2aaSMatthew G. Knepley 
40202764a2aaSMatthew G. Knepley .seealso: DMSetDS()
40212764a2aaSMatthew G. Knepley @*/
40222764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4023af122d2aSMatthew G Knepley {
4024af122d2aSMatthew G Knepley   PetscFunctionBegin;
4025af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40260f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
40270f21e855SMatthew G. Knepley   *prob = dm->prob;
40280f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
40290f21e855SMatthew G. Knepley }
40300f21e855SMatthew G. Knepley 
40312764a2aaSMatthew G. Knepley /*@
40322764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
40332764a2aaSMatthew G. Knepley 
40342764a2aaSMatthew G. Knepley   Input Parameters:
40352764a2aaSMatthew G. Knepley + dm - The DM
40362764a2aaSMatthew G. Knepley - prob - The PetscDS
40372764a2aaSMatthew G. Knepley 
40382764a2aaSMatthew G. Knepley   Level: developer
40392764a2aaSMatthew G. Knepley 
40402764a2aaSMatthew G. Knepley .seealso: DMGetDS()
40412764a2aaSMatthew G. Knepley @*/
40422764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
40430f21e855SMatthew G. Knepley {
4044f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
40450f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40460f21e855SMatthew G. Knepley 
40470f21e855SMatthew G. Knepley   PetscFunctionBegin;
40480f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40492764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
405037316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
40512764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
40520f21e855SMatthew G. Knepley   dm->prob = prob;
4053f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4054f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
40550f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
40560f21e855SMatthew G. Knepley }
40570f21e855SMatthew G. Knepley 
40580f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
40590f21e855SMatthew G. Knepley {
40600f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40610f21e855SMatthew G. Knepley 
40620f21e855SMatthew G. Knepley   PetscFunctionBegin;
40630f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40642764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4065af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4066af122d2aSMatthew G Knepley }
4067af122d2aSMatthew G Knepley 
4068af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4069af122d2aSMatthew G Knepley {
40700f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4071af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4072af122d2aSMatthew G Knepley 
4073af122d2aSMatthew G Knepley   PetscFunctionBegin;
4074af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40752764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
40760f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
40770f21e855SMatthew G. Knepley     PetscContainer obj;
40780f21e855SMatthew G. Knepley 
40790f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
40802764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
40810f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4082af122d2aSMatthew G Knepley   }
4083af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4084af122d2aSMatthew G Knepley }
4085af122d2aSMatthew G Knepley 
4086c1929be8SMatthew G. Knepley /*@
4087c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4088c1929be8SMatthew G. Knepley 
4089c1929be8SMatthew G. Knepley   Not collective
4090c1929be8SMatthew G. Knepley 
4091c1929be8SMatthew G. Knepley   Input Parameters:
4092c1929be8SMatthew G. Knepley + dm - The DM
4093c1929be8SMatthew G. Knepley - f  - The field number
4094c1929be8SMatthew G. Knepley 
4095c1929be8SMatthew G. Knepley   Output Parameter:
4096c1929be8SMatthew G. Knepley . field - The discretization object
4097c1929be8SMatthew G. Knepley 
4098c1929be8SMatthew G. Knepley   Level: developer
4099c1929be8SMatthew G. Knepley 
4100c1929be8SMatthew G. Knepley .seealso: DMSetField()
4101c1929be8SMatthew G. Knepley @*/
4102af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4103af122d2aSMatthew G Knepley {
41040f21e855SMatthew G. Knepley   PetscErrorCode ierr;
41050f21e855SMatthew G. Knepley 
4106af122d2aSMatthew G Knepley   PetscFunctionBegin;
4107af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41082764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4109decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4110decb47aaSMatthew G. Knepley }
4111decb47aaSMatthew G. Knepley 
4112c1929be8SMatthew G. Knepley /*@
4113c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4114c1929be8SMatthew G. Knepley 
4115c1929be8SMatthew G. Knepley   Logically collective on DM
4116c1929be8SMatthew G. Knepley 
4117c1929be8SMatthew G. Knepley   Input Parameters:
4118c1929be8SMatthew G. Knepley + dm - The DM
4119c1929be8SMatthew G. Knepley . f  - The field number
4120c1929be8SMatthew G. Knepley - field - The discretization object
4121c1929be8SMatthew G. Knepley 
4122c1929be8SMatthew G. Knepley   Level: developer
4123c1929be8SMatthew G. Knepley 
4124c1929be8SMatthew G. Knepley .seealso: DMGetField()
4125c1929be8SMatthew G. Knepley @*/
4126decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4127decb47aaSMatthew G. Knepley {
4128decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4129decb47aaSMatthew G. Knepley 
4130decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4131decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41322764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4133af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4134af122d2aSMatthew G Knepley }
41356636e97aSMatthew G Knepley 
4136b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4137b64e0483SPeter Brune {
4138b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4139b64e0483SPeter Brune   PetscErrorCode ierr;
4140b64e0483SPeter Brune   Vec coords,ccoords;
41416dbf9973SLawrence Mitchell   Mat inject;
4142b64e0483SPeter Brune   PetscFunctionBegin;
4143b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4144b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4145b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4146b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4147b64e0483SPeter Brune   if (coords && !ccoords) {
4148b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
41496668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
41506dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
41512adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
41526dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4153b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4154b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4155b64e0483SPeter Brune   }
4156b64e0483SPeter Brune   PetscFunctionReturn(0);
4157b64e0483SPeter Brune }
4158b64e0483SPeter Brune 
415903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
416003dadc2fSPeter Brune {
416103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
416203dadc2fSPeter Brune   PetscErrorCode ierr;
416303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
416403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
416503dadc2fSPeter Brune   PetscFunctionBegin;
416603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
416703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
416803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
416903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
417003dadc2fSPeter Brune   if (coords && !ccoords) {
417103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
41726668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
417303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
417424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
417503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
417603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
417703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
417803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
417903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
418003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
418103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
418203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
418303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
418403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
418503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
418603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
418703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
418803dadc2fSPeter Brune   }
418903dadc2fSPeter Brune   PetscFunctionReturn(0);
419003dadc2fSPeter Brune }
419103dadc2fSPeter Brune 
4192c73cfb54SMatthew G. Knepley /*@
4193c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4194c73cfb54SMatthew G. Knepley 
4195c73cfb54SMatthew G. Knepley   Not collective
4196c73cfb54SMatthew G. Knepley 
4197c73cfb54SMatthew G. Knepley   Input Parameter:
4198c73cfb54SMatthew G. Knepley . dm - The DM
4199c73cfb54SMatthew G. Knepley 
4200c73cfb54SMatthew G. Knepley   Output Parameter:
4201c73cfb54SMatthew G. Knepley . dim - The topological dimension
4202c73cfb54SMatthew G. Knepley 
4203c73cfb54SMatthew G. Knepley   Level: beginner
4204c73cfb54SMatthew G. Knepley 
4205c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4206c73cfb54SMatthew G. Knepley @*/
4207c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4208c73cfb54SMatthew G. Knepley {
4209c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4210c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4211c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4212c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4213c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4214c73cfb54SMatthew G. Knepley }
4215c73cfb54SMatthew G. Knepley 
4216c73cfb54SMatthew G. Knepley /*@
4217c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4218c73cfb54SMatthew G. Knepley 
4219c73cfb54SMatthew G. Knepley   Collective on dm
4220c73cfb54SMatthew G. Knepley 
4221c73cfb54SMatthew G. Knepley   Input Parameters:
4222c73cfb54SMatthew G. Knepley + dm - The DM
4223c73cfb54SMatthew G. Knepley - dim - The topological dimension
4224c73cfb54SMatthew G. Knepley 
4225c73cfb54SMatthew G. Knepley   Level: beginner
4226c73cfb54SMatthew G. Knepley 
4227c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4228c73cfb54SMatthew G. Knepley @*/
4229c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4230c73cfb54SMatthew G. Knepley {
4231f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4232f17e8794SMatthew G. Knepley 
4233c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4234c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4235c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4236c73cfb54SMatthew G. Knepley   dm->dim = dim;
4237f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4238c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4239c73cfb54SMatthew G. Knepley }
4240c73cfb54SMatthew G. Knepley 
4241793f3fe5SMatthew G. Knepley /*@
4242793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4243793f3fe5SMatthew G. Knepley 
4244793f3fe5SMatthew G. Knepley   Collective on DM
4245793f3fe5SMatthew G. Knepley 
4246793f3fe5SMatthew G. Knepley   Input Parameters:
4247793f3fe5SMatthew G. Knepley + dm - the DM
4248793f3fe5SMatthew G. Knepley - dim - the dimension
4249793f3fe5SMatthew G. Knepley 
4250793f3fe5SMatthew G. Knepley   Output Parameters:
4251793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4252793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4253793f3fe5SMatthew G. Knepley 
4254793f3fe5SMatthew G. Knepley   Note:
4255793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4256793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4257793f3fe5SMatthew G. Knepley   then the interval is empty.
4258793f3fe5SMatthew G. Knepley 
4259793f3fe5SMatthew G. Knepley   Level: intermediate
4260793f3fe5SMatthew G. Knepley 
4261793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4262793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4263793f3fe5SMatthew G. Knepley @*/
4264793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4265793f3fe5SMatthew G. Knepley {
4266793f3fe5SMatthew G. Knepley   PetscInt       d;
4267793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4268793f3fe5SMatthew G. Knepley 
4269793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4270793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4271793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4272793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4273793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4274793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4275793f3fe5SMatthew G. Knepley }
4276793f3fe5SMatthew G. Knepley 
42776636e97aSMatthew G Knepley /*@
42786636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
42796636e97aSMatthew G Knepley 
42806636e97aSMatthew G Knepley   Collective on DM
42816636e97aSMatthew G Knepley 
42826636e97aSMatthew G Knepley   Input Parameters:
42836636e97aSMatthew G Knepley + dm - the DM
42846636e97aSMatthew G Knepley - c - coordinate vector
42856636e97aSMatthew G Knepley 
42866636e97aSMatthew G Knepley   Note:
42876636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
42886636e97aSMatthew G Knepley 
42896636e97aSMatthew G Knepley   Level: intermediate
42906636e97aSMatthew G Knepley 
42916636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42926636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
42936636e97aSMatthew G Knepley @*/
42946636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
42956636e97aSMatthew G Knepley {
42966636e97aSMatthew G Knepley   PetscErrorCode ierr;
42976636e97aSMatthew G Knepley 
42986636e97aSMatthew G Knepley   PetscFunctionBegin;
42996636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43006636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
43016636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
43026636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
43036636e97aSMatthew G Knepley   dm->coordinates = c;
43046636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4305b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
430603dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
43076636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43086636e97aSMatthew G Knepley }
43096636e97aSMatthew G Knepley 
43106636e97aSMatthew G Knepley /*@
43116636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
43126636e97aSMatthew G Knepley 
43136636e97aSMatthew G Knepley   Collective on DM
43146636e97aSMatthew G Knepley 
43156636e97aSMatthew G Knepley    Input Parameters:
43166636e97aSMatthew G Knepley +  dm - the DM
43176636e97aSMatthew G Knepley -  c - coordinate vector
43186636e97aSMatthew G Knepley 
43196636e97aSMatthew G Knepley   Note:
43206636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
43216636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
43226636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
43236636e97aSMatthew G Knepley 
43246636e97aSMatthew G Knepley   Level: intermediate
43256636e97aSMatthew G Knepley 
43266636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43276636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
43286636e97aSMatthew G Knepley @*/
43296636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
43306636e97aSMatthew G Knepley {
43316636e97aSMatthew G Knepley   PetscErrorCode ierr;
43326636e97aSMatthew G Knepley 
43336636e97aSMatthew G Knepley   PetscFunctionBegin;
43346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43356636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
43366636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
43376636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
43388865f1eaSKarl Rupp 
43396636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
43408865f1eaSKarl Rupp 
43416636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
43426636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43436636e97aSMatthew G Knepley }
43446636e97aSMatthew G Knepley 
43456636e97aSMatthew G Knepley /*@
43466636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
43476636e97aSMatthew G Knepley 
43486636e97aSMatthew G Knepley   Not Collective
43496636e97aSMatthew G Knepley 
43506636e97aSMatthew G Knepley   Input Parameter:
43516636e97aSMatthew G Knepley . dm - the DM
43526636e97aSMatthew G Knepley 
43536636e97aSMatthew G Knepley   Output Parameter:
43546636e97aSMatthew G Knepley . c - global coordinate vector
43556636e97aSMatthew G Knepley 
43566636e97aSMatthew G Knepley   Note:
43576636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
43586636e97aSMatthew G Knepley 
43596636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
43606636e97aSMatthew G Knepley 
43616636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
43626636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
43636636e97aSMatthew G Knepley 
43646636e97aSMatthew G Knepley   Level: intermediate
43656636e97aSMatthew G Knepley 
43666636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43676636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
43686636e97aSMatthew G Knepley @*/
43696636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
43706636e97aSMatthew G Knepley {
43716636e97aSMatthew G Knepley   PetscErrorCode ierr;
43726636e97aSMatthew G Knepley 
43736636e97aSMatthew G Knepley   PetscFunctionBegin;
43746636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43756636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43761f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
43770298fd71SBarry Smith     DM cdm = NULL;
43786636e97aSMatthew G Knepley 
43796636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43806636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
43816636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
43826636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43836636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43846636e97aSMatthew G Knepley   }
43856636e97aSMatthew G Knepley   *c = dm->coordinates;
43866636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43876636e97aSMatthew G Knepley }
43886636e97aSMatthew G Knepley 
43896636e97aSMatthew G Knepley /*@
43906636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
43916636e97aSMatthew G Knepley 
43926636e97aSMatthew G Knepley   Collective on DM
43936636e97aSMatthew G Knepley 
43946636e97aSMatthew G Knepley   Input Parameter:
43956636e97aSMatthew G Knepley . dm - the DM
43966636e97aSMatthew G Knepley 
43976636e97aSMatthew G Knepley   Output Parameter:
43986636e97aSMatthew G Knepley . c - coordinate vector
43996636e97aSMatthew G Knepley 
44006636e97aSMatthew G Knepley   Note:
44016636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
44026636e97aSMatthew G Knepley 
44036636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
44046636e97aSMatthew G Knepley 
44056636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
44066636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
44076636e97aSMatthew G Knepley 
44086636e97aSMatthew G Knepley   Level: intermediate
44096636e97aSMatthew G Knepley 
44106636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44116636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
44126636e97aSMatthew G Knepley @*/
44136636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
44146636e97aSMatthew G Knepley {
44156636e97aSMatthew G Knepley   PetscErrorCode ierr;
44166636e97aSMatthew G Knepley 
44176636e97aSMatthew G Knepley   PetscFunctionBegin;
44186636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44196636e97aSMatthew G Knepley   PetscValidPointer(c,2);
44201f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
44210298fd71SBarry Smith     DM cdm = NULL;
44226636e97aSMatthew G Knepley 
44236636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
44246636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
44256636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
44266636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44276636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44286636e97aSMatthew G Knepley   }
44296636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
44306636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44316636e97aSMatthew G Knepley }
44326636e97aSMatthew G Knepley 
4433f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
4434f19dbd58SToby Isaac {
4435f19dbd58SToby Isaac   PetscErrorCode ierr;
4436f19dbd58SToby Isaac 
4437f19dbd58SToby Isaac   PetscFunctionBegin;
4438f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4439f19dbd58SToby Isaac   PetscValidPointer(field,2);
4440f19dbd58SToby Isaac   if (!dm->coordinateField) {
4441f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
4442f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
4443f19dbd58SToby Isaac     }
4444f19dbd58SToby Isaac   }
4445f19dbd58SToby Isaac   *field = dm->coordinateField;
4446f19dbd58SToby Isaac   PetscFunctionReturn(0);
4447f19dbd58SToby Isaac }
4448f19dbd58SToby Isaac 
4449f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
4450f19dbd58SToby Isaac {
4451f19dbd58SToby Isaac   PetscErrorCode ierr;
4452f19dbd58SToby Isaac 
4453f19dbd58SToby Isaac   PetscFunctionBegin;
4454f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4455f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
4456c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
4457f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
4458f19dbd58SToby Isaac   dm->coordinateField = field;
4459f19dbd58SToby Isaac   PetscFunctionReturn(0);
4460f19dbd58SToby Isaac }
4461f19dbd58SToby Isaac 
44626636e97aSMatthew G Knepley /*@
44631cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
44646636e97aSMatthew G Knepley 
44656636e97aSMatthew G Knepley   Collective on DM
44666636e97aSMatthew G Knepley 
44676636e97aSMatthew G Knepley   Input Parameter:
44686636e97aSMatthew G Knepley . dm - the DM
44696636e97aSMatthew G Knepley 
44706636e97aSMatthew G Knepley   Output Parameter:
44716636e97aSMatthew G Knepley . cdm - coordinate DM
44726636e97aSMatthew G Knepley 
44736636e97aSMatthew G Knepley   Level: intermediate
44746636e97aSMatthew G Knepley 
44756636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44761cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
44776636e97aSMatthew G Knepley @*/
44786636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
44796636e97aSMatthew G Knepley {
44806636e97aSMatthew G Knepley   PetscErrorCode ierr;
44816636e97aSMatthew G Knepley 
44826636e97aSMatthew G Knepley   PetscFunctionBegin;
44836636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44846636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
44856636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
448682f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
44876636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
44886636e97aSMatthew G Knepley   }
44896636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
44906636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44916636e97aSMatthew G Knepley }
4492e87bb0d3SMatthew G Knepley 
44931cfe2091SMatthew G. Knepley /*@
44941cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
44951cfe2091SMatthew G. Knepley 
44961cfe2091SMatthew G. Knepley   Logically Collective on DM
44971cfe2091SMatthew G. Knepley 
44981cfe2091SMatthew G. Knepley   Input Parameters:
44991cfe2091SMatthew G. Knepley + dm - the DM
45001cfe2091SMatthew G. Knepley - cdm - coordinate DM
45011cfe2091SMatthew G. Knepley 
45021cfe2091SMatthew G. Knepley   Level: intermediate
45031cfe2091SMatthew G. Knepley 
45041cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
45051cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
45061cfe2091SMatthew G. Knepley @*/
45071cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
45081cfe2091SMatthew G. Knepley {
45091cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
45101cfe2091SMatthew G. Knepley 
45111cfe2091SMatthew G. Knepley   PetscFunctionBegin;
45121cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
45131cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4514f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
45151cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
45161cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
45171cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
45181cfe2091SMatthew G. Knepley }
45191cfe2091SMatthew G. Knepley 
452046e270d4SMatthew G. Knepley /*@
452146e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
452246e270d4SMatthew G. Knepley 
452346e270d4SMatthew G. Knepley   Not Collective
452446e270d4SMatthew G. Knepley 
452546e270d4SMatthew G. Knepley   Input Parameter:
452646e270d4SMatthew G. Knepley . dm - The DM object
452746e270d4SMatthew G. Knepley 
452846e270d4SMatthew G. Knepley   Output Parameter:
452946e270d4SMatthew G. Knepley . dim - The embedding dimension
453046e270d4SMatthew G. Knepley 
453146e270d4SMatthew G. Knepley   Level: intermediate
453246e270d4SMatthew G. Knepley 
453346e270d4SMatthew G. Knepley .keywords: mesh, coordinates
453446e270d4SMatthew G. Knepley .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
453546e270d4SMatthew G. Knepley @*/
453646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
453746e270d4SMatthew G. Knepley {
453846e270d4SMatthew G. Knepley   PetscFunctionBegin;
453946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
454046e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
45419a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
45429a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
45439a9a41abSToby Isaac   }
454446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
454546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
454646e270d4SMatthew G. Knepley }
454746e270d4SMatthew G. Knepley 
454846e270d4SMatthew G. Knepley /*@
454946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
455046e270d4SMatthew G. Knepley 
455146e270d4SMatthew G. Knepley   Not Collective
455246e270d4SMatthew G. Knepley 
455346e270d4SMatthew G. Knepley   Input Parameters:
455446e270d4SMatthew G. Knepley + dm  - The DM object
455546e270d4SMatthew G. Knepley - dim - The embedding dimension
455646e270d4SMatthew G. Knepley 
455746e270d4SMatthew G. Knepley   Level: intermediate
455846e270d4SMatthew G. Knepley 
455946e270d4SMatthew G. Knepley .keywords: mesh, coordinates
456046e270d4SMatthew G. Knepley .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
456146e270d4SMatthew G. Knepley @*/
456246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
456346e270d4SMatthew G. Knepley {
4564f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4565f17e8794SMatthew G. Knepley 
456646e270d4SMatthew G. Knepley   PetscFunctionBegin;
456746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
456846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4569f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
457046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
457146e270d4SMatthew G. Knepley }
457246e270d4SMatthew G. Knepley 
4573e8abe2deSMatthew G. Knepley /*@
4574e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4575e8abe2deSMatthew G. Knepley 
4576e8abe2deSMatthew G. Knepley   Not Collective
4577e8abe2deSMatthew G. Knepley 
4578e8abe2deSMatthew G. Knepley   Input Parameter:
4579e8abe2deSMatthew G. Knepley . dm - The DM object
4580e8abe2deSMatthew G. Knepley 
4581e8abe2deSMatthew G. Knepley   Output Parameter:
4582e8abe2deSMatthew G. Knepley . section - The PetscSection object
4583e8abe2deSMatthew G. Knepley 
4584e8abe2deSMatthew G. Knepley   Level: intermediate
4585e8abe2deSMatthew G. Knepley 
4586e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4587e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMGetDefaultSection(), DMSetDefaultSection()
4588e8abe2deSMatthew G. Knepley @*/
4589e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4590e8abe2deSMatthew G. Knepley {
4591e8abe2deSMatthew G. Knepley   DM             cdm;
4592e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4593e8abe2deSMatthew G. Knepley 
4594e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4595e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4596e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4597e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4598e8abe2deSMatthew G. Knepley   ierr = DMGetDefaultSection(cdm, section);CHKERRQ(ierr);
4599e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4600e8abe2deSMatthew G. Knepley }
4601e8abe2deSMatthew G. Knepley 
4602e8abe2deSMatthew G. Knepley /*@
4603e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4604e8abe2deSMatthew G. Knepley 
4605e8abe2deSMatthew G. Knepley   Not Collective
4606e8abe2deSMatthew G. Knepley 
4607e8abe2deSMatthew G. Knepley   Input Parameters:
4608e8abe2deSMatthew G. Knepley + dm      - The DM object
460946e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4610e8abe2deSMatthew G. Knepley - section - The PetscSection object
4611e8abe2deSMatthew G. Knepley 
4612e8abe2deSMatthew G. Knepley   Level: intermediate
4613e8abe2deSMatthew G. Knepley 
4614e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4615e8abe2deSMatthew G. Knepley .seealso: DMGetCoordinateSection(), DMGetDefaultSection(), DMSetDefaultSection()
4616e8abe2deSMatthew G. Knepley @*/
461746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4618e8abe2deSMatthew G. Knepley {
4619e8abe2deSMatthew G. Knepley   DM             cdm;
4620e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4621e8abe2deSMatthew G. Knepley 
4622e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4623e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
462446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4625e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4626e8abe2deSMatthew G. Knepley   ierr = DMSetDefaultSection(cdm, section);CHKERRQ(ierr);
462746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
46284c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
462946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
463046e270d4SMatthew G. Knepley 
463146e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
463246e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
463346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
463446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
463546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
463646e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
463746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
463846e270d4SMatthew G. Knepley     }
46396be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
464046e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
464146e270d4SMatthew G. Knepley   }
4642e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4643e8abe2deSMatthew G. Knepley }
4644e8abe2deSMatthew G. Knepley 
46455dc8c3f7SMatthew G. Knepley /*@C
464690b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
46475dc8c3f7SMatthew G. Knepley 
46485dc8c3f7SMatthew G. Knepley   Input Parameters:
464990b157c4SStefano Zampini . dm      - The DM object
465090b157c4SStefano Zampini 
465190b157c4SStefano Zampini   Output Parameters:
465290b157c4SStefano Zampini + per     - Whether the DM is periodic or not
46535dc8c3f7SMatthew 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
46545dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
46555dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
46565dc8c3f7SMatthew G. Knepley 
46575dc8c3f7SMatthew G. Knepley   Level: developer
46585dc8c3f7SMatthew G. Knepley 
46595dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
46605dc8c3f7SMatthew G. Knepley @*/
466190b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4662c6b900c6SMatthew G. Knepley {
4663c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4664c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
466590b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4666c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4667c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
46685dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4669c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4670c6b900c6SMatthew G. Knepley }
4671c6b900c6SMatthew G. Knepley 
46725dc8c3f7SMatthew G. Knepley /*@C
46735dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
46745dc8c3f7SMatthew G. Knepley 
46755dc8c3f7SMatthew G. Knepley   Input Parameters:
46765dc8c3f7SMatthew G. Knepley + dm      - The DM object
467790b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
46785dc8c3f7SMatthew 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
46795dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
46805dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
46815dc8c3f7SMatthew G. Knepley 
46825dc8c3f7SMatthew G. Knepley   Level: developer
46835dc8c3f7SMatthew G. Knepley 
46845dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
46855dc8c3f7SMatthew G. Knepley @*/
468690b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4687c6b900c6SMatthew G. Knepley {
4688c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4689c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4690c6b900c6SMatthew G. Knepley 
4691c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4692c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
469390b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
469490b157c4SStefano Zampini   if (maxCell) {
469590b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
469690b157c4SStefano Zampini     PetscValidPointer(L,4);
469790b157c4SStefano Zampini     PetscValidPointer(bd,5);
469890b157c4SStefano Zampini   }
46995dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
47005dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
470190b157c4SStefano Zampini   if (maxCell) {
47025dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
47035dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
470490b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
470590b157c4SStefano Zampini   } else {
470690b157c4SStefano Zampini     dm->periodic = per;
470790b157c4SStefano Zampini   }
4708c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4709c6b900c6SMatthew G. Knepley }
4710c6b900c6SMatthew G. Knepley 
47112e17dfb7SMatthew G. Knepley /*@
47122e17dfb7SMatthew 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.
47132e17dfb7SMatthew G. Knepley 
47142e17dfb7SMatthew G. Knepley   Input Parameters:
47152e17dfb7SMatthew G. Knepley + dm     - The DM
471665da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
471765da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
47182e17dfb7SMatthew G. Knepley 
47192e17dfb7SMatthew G. Knepley   Output Parameter:
47202e17dfb7SMatthew G. Knepley . out - The localized coordinate point
47212e17dfb7SMatthew G. Knepley 
47222e17dfb7SMatthew G. Knepley   Level: developer
47232e17dfb7SMatthew G. Knepley 
47242e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
47252e17dfb7SMatthew G. Knepley @*/
472665da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
47272e17dfb7SMatthew G. Knepley {
47282e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
47292e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
47302e17dfb7SMatthew G. Knepley 
47312e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47322e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
47332e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47342e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47352e17dfb7SMatthew G. Knepley   } else {
473665da65dcSMatthew G. Knepley     if (endpoint) {
473765da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4738da3333bfSMatthew 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)) {
4739da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
474065da65dcSMatthew G. Knepley         } else {
4741da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
474265da65dcSMatthew G. Knepley         }
474365da65dcSMatthew G. Knepley       }
474465da65dcSMatthew G. Knepley     } else {
47452e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
47461118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
47472e17dfb7SMatthew G. Knepley       }
47482e17dfb7SMatthew G. Knepley     }
474965da65dcSMatthew G. Knepley   }
47502e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47512e17dfb7SMatthew G. Knepley }
47522e17dfb7SMatthew G. Knepley 
47532e17dfb7SMatthew G. Knepley /*
47542e17dfb7SMatthew 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.
47552e17dfb7SMatthew G. Knepley 
47562e17dfb7SMatthew G. Knepley   Input Parameters:
47572e17dfb7SMatthew G. Knepley + dm     - The DM
47582e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
47592e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
47602e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
47612e17dfb7SMatthew G. Knepley 
47622e17dfb7SMatthew G. Knepley   Output Parameter:
47632e17dfb7SMatthew G. Knepley . out - The localized coordinate point
47642e17dfb7SMatthew G. Knepley 
47652e17dfb7SMatthew G. Knepley   Level: developer
47662e17dfb7SMatthew G. Knepley 
47672e17dfb7SMatthew 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
47682e17dfb7SMatthew G. Knepley 
47692e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
47702e17dfb7SMatthew G. Knepley */
47712e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
47722e17dfb7SMatthew G. Knepley {
47732e17dfb7SMatthew G. Knepley   PetscInt d;
47742e17dfb7SMatthew G. Knepley 
47752e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47762e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47772e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47782e17dfb7SMatthew G. Knepley   } else {
47792e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4780908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
47812e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
47822e17dfb7SMatthew G. Knepley       } else {
47832e17dfb7SMatthew G. Knepley         out[d] = in[d];
47842e17dfb7SMatthew G. Knepley       }
47852e17dfb7SMatthew G. Knepley     }
47862e17dfb7SMatthew G. Knepley   }
47872e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47882e17dfb7SMatthew G. Knepley }
47892e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
47902e17dfb7SMatthew G. Knepley {
47912e17dfb7SMatthew G. Knepley   PetscInt d;
47922e17dfb7SMatthew G. Knepley 
47932e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47942e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47952e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47962e17dfb7SMatthew G. Knepley   } else {
47972e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4798908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
47992e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
48002e17dfb7SMatthew G. Knepley       } else {
48012e17dfb7SMatthew G. Knepley         out[d] = in[d];
48022e17dfb7SMatthew G. Knepley       }
48032e17dfb7SMatthew G. Knepley     }
48042e17dfb7SMatthew G. Knepley   }
48052e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48062e17dfb7SMatthew G. Knepley }
48072e17dfb7SMatthew G. Knepley 
48082e17dfb7SMatthew G. Knepley /*
48092e17dfb7SMatthew 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.
48102e17dfb7SMatthew G. Knepley 
48112e17dfb7SMatthew G. Knepley   Input Parameters:
48122e17dfb7SMatthew G. Knepley + dm     - The DM
48132e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
48142e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
48152e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
48162e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
48172e17dfb7SMatthew G. Knepley 
48182e17dfb7SMatthew G. Knepley   Output Parameter:
48192e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
48202e17dfb7SMatthew G. Knepley 
48212e17dfb7SMatthew G. Knepley   Level: developer
48222e17dfb7SMatthew G. Knepley 
48232e17dfb7SMatthew 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
48242e17dfb7SMatthew G. Knepley 
48252e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
48262e17dfb7SMatthew G. Knepley */
48272e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
48282e17dfb7SMatthew G. Knepley {
48292e17dfb7SMatthew G. Knepley   PetscInt d;
48302e17dfb7SMatthew G. Knepley 
48312e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48322e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
48332e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
48342e17dfb7SMatthew G. Knepley   } else {
48352e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4836908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
48372e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
48382e17dfb7SMatthew G. Knepley       } else {
48392e17dfb7SMatthew G. Knepley         out[d] += in[d];
48402e17dfb7SMatthew G. Knepley       }
48412e17dfb7SMatthew G. Knepley     }
48422e17dfb7SMatthew G. Knepley   }
48432e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
48442e17dfb7SMatthew G. Knepley }
48452e17dfb7SMatthew G. Knepley 
484636447a5eSToby Isaac /*@
484736447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
484836447a5eSToby Isaac 
484936447a5eSToby Isaac   Input Parameter:
485036447a5eSToby Isaac . dm - The DM
485136447a5eSToby Isaac 
485236447a5eSToby Isaac   Output Parameter:
485336447a5eSToby Isaac   areLocalized - True if localized
485436447a5eSToby Isaac 
485536447a5eSToby Isaac   Level: developer
485636447a5eSToby Isaac 
485736447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
485836447a5eSToby Isaac @*/
485936447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
486036447a5eSToby Isaac {
486136447a5eSToby Isaac   DM             cdm;
486236447a5eSToby Isaac   PetscSection   coordSection;
486346a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
486446a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
486536447a5eSToby Isaac   PetscErrorCode ierr;
486636447a5eSToby Isaac 
486736447a5eSToby Isaac   PetscFunctionBegin;
486836447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
486946a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
487046a3a80fSLisandro Dalcin 
48718b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
487246a3a80fSLisandro Dalcin   if (!dm->periodic) PetscFunctionReturn(0); /* This is a hideous optimization hack! */
487346a3a80fSLisandro Dalcin 
487436447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
487536447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
487636447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
487746a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
487846a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
487946a3a80fSLisandro Dalcin 
488046a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
488136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
488236447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
488346a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
488446a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
488536447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
488646a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
488736447a5eSToby Isaac   }
488846a3a80fSLisandro Dalcin   ierr = MPIU_Allreduce(&alreadyLocalized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
488936447a5eSToby Isaac   PetscFunctionReturn(0);
489036447a5eSToby Isaac }
489136447a5eSToby Isaac 
489236447a5eSToby Isaac 
48932e17dfb7SMatthew G. Knepley /*@
4894492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
48952e17dfb7SMatthew G. Knepley 
48962e17dfb7SMatthew G. Knepley   Input Parameter:
48972e17dfb7SMatthew G. Knepley . dm - The DM
48982e17dfb7SMatthew G. Knepley 
48992e17dfb7SMatthew G. Knepley   Level: developer
49002e17dfb7SMatthew G. Knepley 
49012e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
49022e17dfb7SMatthew G. Knepley @*/
49032e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
49042e17dfb7SMatthew G. Knepley {
49052e17dfb7SMatthew G. Knepley   DM             cdm;
49062e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
49072e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
49083e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
49093e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4910e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
49113e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
49123e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
49132e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
49142e17dfb7SMatthew G. Knepley 
49152e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
49162e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
491792c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4918f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4919f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4920f7cbd40bSStefano Zampini 
49212e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
49222e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
49232e17dfb7SMatthew G. Knepley   {
49242e17dfb7SMatthew G. Knepley     PetscBool isplex;
49252e17dfb7SMatthew G. Knepley 
49262e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
49272e17dfb7SMatthew G. Knepley     if (isplex) {
49282e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
49293e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
493069291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
49313e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
49323e922f36SToby Isaac       newStart = vStart;
49333e922f36SToby Isaac       newEnd   = vEnd;
49343e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
49353e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
49363e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
49373e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
49383e922f36SToby Isaac       }
49392e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
49402e17dfb7SMatthew G. Knepley   }
49412e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
494243eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
49432e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
49443e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4945e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
49463e922f36SToby Isaac 
49472e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
49482e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
49492e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
49502e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
49513e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
49523e922f36SToby Isaac 
495369291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
49543e922f36SToby Isaac   localized = &anchor[bs];
49553e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
49563e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49573e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49583e922f36SToby Isaac 
49593e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
49603e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
49613e922f36SToby Isaac       PetscInt     b;
49623e922f36SToby Isaac 
49633e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
49643e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49653e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49663e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
49673e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
49683e922f36SToby Isaac         for (b = 0; b < bs; b++) {
49693e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
49703e922f36SToby Isaac         }
49713e922f36SToby Isaac         if (b < bs) break;
49723e922f36SToby Isaac       }
49733e922f36SToby Isaac       if (d < dof/bs) {
49743e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
49753e922f36SToby Isaac           PetscInt cdof;
49763e922f36SToby Isaac 
49773e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
49783e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
49793e922f36SToby Isaac         }
49803e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
49813e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
49823e922f36SToby Isaac       }
49833e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49843e922f36SToby Isaac     }
49853e922f36SToby Isaac   }
49863e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
49873e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
498869291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
49893e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
499069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
49913e922f36SToby Isaac     PetscFunctionReturn(0);
49923e922f36SToby Isaac   }
49932e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49942e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49952e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
49962e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
49972e17dfb7SMatthew G. Knepley   }
49982e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
49992e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
5000c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
50012e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
50022e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
50032e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
50042e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
5005c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
50062e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
50072e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
50082e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
50092e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
50102e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
50112e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
50122e17dfb7SMatthew G. Knepley   }
50133e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
50143e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
50153e922f36SToby Isaac 
50162e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
50172e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
50183e922f36SToby Isaac       PetscInt     b, cdof;
50192e17dfb7SMatthew G. Knepley 
50203e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
50213e922f36SToby Isaac       if (!cdof) continue;
50222e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50232e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
50242e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
50252e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
50262e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
50272e17dfb7SMatthew G. Knepley     }
50283e922f36SToby Isaac   }
502969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
503069291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
5031c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
50322e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
50332e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
50342e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
50352e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
50362e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
50372e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50382e17dfb7SMatthew G. Knepley }
50392e17dfb7SMatthew G. Knepley 
5040e87bb0d3SMatthew G Knepley /*@
50413a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5042e87bb0d3SMatthew G Knepley 
50433a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5044e87bb0d3SMatthew G Knepley 
5045e87bb0d3SMatthew G Knepley   Input Parameters:
5046e87bb0d3SMatthew G Knepley + dm - The DM
50473a93e3b7SToby Isaac . v - The Vec of points
504862a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
50493a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5050e87bb0d3SMatthew G Knepley 
505161e3bb9bSMatthew G Knepley   Output Parameter:
505262a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
505362a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
50543a93e3b7SToby Isaac 
5055e87bb0d3SMatthew G Knepley 
5056e87bb0d3SMatthew G Knepley   Level: developer
505761e3bb9bSMatthew G Knepley 
505862a38674SMatthew G. Knepley   Notes:
50593a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
506062a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
50613a93e3b7SToby Isaac 
50623a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
506362a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
50643a93e3b7SToby Isaac 
50653a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
50663a93e3b7SToby Isaac 
506762a38674SMatthew G. Knepley $    const PetscSFNode *cells;
506862a38674SMatthew G. Knepley $    PetscInt           nFound;
5069a6216909SToby Isaac $    const PetscInt    *found;
507062a38674SMatthew G. Knepley $
5071a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
50723a93e3b7SToby Isaac 
50733a93e3b7SToby 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
50743a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
50753a93e3b7SToby Isaac 
507661e3bb9bSMatthew G Knepley .keywords: point location, mesh
507762a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
507861e3bb9bSMatthew G Knepley @*/
507962a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5080e87bb0d3SMatthew G Knepley {
5081735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5082735aa83eSMatthew G Knepley 
5083e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5084e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5085e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5086e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
50873a93e3b7SToby Isaac   if (*cellSF) {
50883a93e3b7SToby Isaac     PetscMPIInt result;
50893a93e3b7SToby Isaac 
5090e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5091a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
50923a93e3b7SToby 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");
5093e0fc9d1bSMatthew G. Knepley   } else {
50943a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
50953a93e3b7SToby Isaac   }
509647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5097735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
509862a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
509982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
510047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5101e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5102e87bb0d3SMatthew G Knepley }
510314f150ffSMatthew G. Knepley 
5104f4d763aaSMatthew G. Knepley /*@
5105f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5106f4d763aaSMatthew G. Knepley 
5107f4d763aaSMatthew G. Knepley   Input Parameter:
5108f4d763aaSMatthew G. Knepley . dm - The original DM
5109f4d763aaSMatthew G. Knepley 
5110f4d763aaSMatthew G. Knepley   Output Parameter:
5111f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5112f4d763aaSMatthew G. Knepley 
5113f4d763aaSMatthew G. Knepley   Level: intermediate
5114f4d763aaSMatthew G. Knepley 
5115f4d763aaSMatthew G. Knepley .seealso: VecView(), DMGetDefaultGlobalSection()
5116f4d763aaSMatthew G. Knepley @*/
511714f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
511814f150ffSMatthew G. Knepley {
5119c26acbdeSMatthew G. Knepley   PetscSection   section;
51202d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
512114f150ffSMatthew G. Knepley   PetscErrorCode ierr;
512214f150ffSMatthew G. Knepley 
512314f150ffSMatthew G. Knepley   PetscFunctionBegin;
512414f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
512514f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5126c26acbdeSMatthew G. Knepley   ierr = DMGetDefaultSection(dm, &section);CHKERRQ(ierr);
5127c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5128127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
51292d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5130c26acbdeSMatthew G. Knepley     *odm = dm;
5131c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5132c26acbdeSMatthew G. Knepley   }
513314f150ffSMatthew G. Knepley   if (!dm->dmBC) {
51340305aff6SMatthew G. Knepley     PetscDS      ds;
5135c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
513614f150ffSMatthew G. Knepley     PetscSF      sf;
513714f150ffSMatthew G. Knepley 
513814f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
51390305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
51400305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
514114f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
514214f150ffSMatthew G. Knepley     ierr = DMSetDefaultSection(dm->dmBC, newSection);CHKERRQ(ierr);
514314f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
514414f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
514515b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
514614f150ffSMatthew G. Knepley     ierr = DMSetDefaultGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
514714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
514814f150ffSMatthew G. Knepley   }
514914f150ffSMatthew G. Knepley   *odm = dm->dmBC;
515014f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
515114f150ffSMatthew G. Knepley }
5152f4d763aaSMatthew G. Knepley 
5153f4d763aaSMatthew G. Knepley /*@
5154cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5155f4d763aaSMatthew G. Knepley 
5156f4d763aaSMatthew G. Knepley   Input Parameter:
5157f4d763aaSMatthew G. Knepley . dm - The original DM
5158f4d763aaSMatthew G. Knepley 
5159cdb7a50dSMatthew G. Knepley   Output Parameters:
5160cdb7a50dSMatthew G. Knepley + num - The output sequence number
5161cdb7a50dSMatthew G. Knepley - val - The output sequence value
5162f4d763aaSMatthew G. Knepley 
5163f4d763aaSMatthew G. Knepley   Level: intermediate
5164f4d763aaSMatthew G. Knepley 
5165f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5166f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5167f4d763aaSMatthew G. Knepley 
5168f4d763aaSMatthew G. Knepley .seealso: VecView()
5169f4d763aaSMatthew G. Knepley @*/
5170cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5171f4d763aaSMatthew G. Knepley {
5172f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5173f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5174cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5175cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5176f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5177f4d763aaSMatthew G. Knepley }
5178f4d763aaSMatthew G. Knepley 
5179f4d763aaSMatthew G. Knepley /*@
5180cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5181f4d763aaSMatthew G. Knepley 
5182f4d763aaSMatthew G. Knepley   Input Parameters:
5183f4d763aaSMatthew G. Knepley + dm - The original DM
5184cdb7a50dSMatthew G. Knepley . num - The output sequence number
5185cdb7a50dSMatthew G. Knepley - val - The output sequence value
5186f4d763aaSMatthew G. Knepley 
5187f4d763aaSMatthew G. Knepley   Level: intermediate
5188f4d763aaSMatthew G. Knepley 
5189f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5190f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5191f4d763aaSMatthew G. Knepley 
5192f4d763aaSMatthew G. Knepley .seealso: VecView()
5193f4d763aaSMatthew G. Knepley @*/
5194cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5195f4d763aaSMatthew G. Knepley {
5196f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5197f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5198f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5199cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5200cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5201cdb7a50dSMatthew G. Knepley }
5202cdb7a50dSMatthew G. Knepley 
5203cdb7a50dSMatthew G. Knepley /*@C
5204cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5205cdb7a50dSMatthew G. Knepley 
5206cdb7a50dSMatthew G. Knepley   Input Parameters:
5207cdb7a50dSMatthew G. Knepley + dm   - The original DM
5208cdb7a50dSMatthew G. Knepley . name - The sequence name
5209cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5210cdb7a50dSMatthew G. Knepley 
5211cdb7a50dSMatthew G. Knepley   Output Parameter:
5212cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5213cdb7a50dSMatthew G. Knepley 
5214cdb7a50dSMatthew G. Knepley   Level: intermediate
5215cdb7a50dSMatthew G. Knepley 
5216cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5217cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5218cdb7a50dSMatthew G. Knepley 
5219cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5220cdb7a50dSMatthew G. Knepley @*/
5221cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5222cdb7a50dSMatthew G. Knepley {
5223cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5224cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5225cdb7a50dSMatthew G. Knepley 
5226cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5227cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5228cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5229cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5230cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5231cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5232cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5233cdb7a50dSMatthew G. Knepley     PetscScalar value;
5234cdb7a50dSMatthew G. Knepley 
523539d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
52364aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5237cdb7a50dSMatthew G. Knepley #endif
5238cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5239f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5240f4d763aaSMatthew G. Knepley }
52418e4ac7eaSMatthew G. Knepley 
52428e4ac7eaSMatthew G. Knepley /*@
52438e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
52448e4ac7eaSMatthew G. Knepley 
52458e4ac7eaSMatthew G. Knepley   Not collective
52468e4ac7eaSMatthew G. Knepley 
52478e4ac7eaSMatthew G. Knepley   Input Parameter:
52488e4ac7eaSMatthew G. Knepley . dm - The DM
52498e4ac7eaSMatthew G. Knepley 
52508e4ac7eaSMatthew G. Knepley   Output Parameter:
52518e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
52528e4ac7eaSMatthew G. Knepley 
52538e4ac7eaSMatthew G. Knepley   Level: beginner
52548e4ac7eaSMatthew G. Knepley 
52558e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
52568e4ac7eaSMatthew G. Knepley @*/
52578e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
52588e4ac7eaSMatthew G. Knepley {
52598e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
52608e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52618e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
52628e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
52638e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
52648e4ac7eaSMatthew G. Knepley }
52658e4ac7eaSMatthew G. Knepley 
52668e4ac7eaSMatthew G. Knepley /*@
52678e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
52688e4ac7eaSMatthew G. Knepley 
52698e4ac7eaSMatthew G. Knepley   Collective on dm
52708e4ac7eaSMatthew G. Knepley 
52718e4ac7eaSMatthew G. Knepley   Input Parameters:
52728e4ac7eaSMatthew G. Knepley + dm - The DM
52738e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
52748e4ac7eaSMatthew G. Knepley 
52758e4ac7eaSMatthew G. Knepley   Level: beginner
52768e4ac7eaSMatthew G. Knepley 
52778e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
52788e4ac7eaSMatthew G. Knepley @*/
52798e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
52808e4ac7eaSMatthew G. Knepley {
52818e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
52828e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52838833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
52848e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
52858e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
52868e4ac7eaSMatthew G. Knepley }
5287c58f1c22SToby Isaac 
5288c58f1c22SToby Isaac 
5289c58f1c22SToby Isaac /*@C
5290c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5291c58f1c22SToby Isaac 
5292c58f1c22SToby Isaac   Not Collective
5293c58f1c22SToby Isaac 
5294c58f1c22SToby Isaac   Input Parameters:
5295c58f1c22SToby Isaac + dm   - The DM object
5296c58f1c22SToby Isaac - name - The label name
5297c58f1c22SToby Isaac 
5298c58f1c22SToby Isaac   Level: intermediate
5299c58f1c22SToby Isaac 
5300c58f1c22SToby Isaac .keywords: mesh
5301c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5302c58f1c22SToby Isaac @*/
5303c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5304c58f1c22SToby Isaac {
5305c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5306c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5307c58f1c22SToby Isaac   PetscErrorCode ierr;
5308c58f1c22SToby Isaac 
5309c58f1c22SToby Isaac   PetscFunctionBegin;
5310c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5311c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5312c58f1c22SToby Isaac   while (next) {
5313c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5314c58f1c22SToby Isaac     if (flg) break;
5315c58f1c22SToby Isaac     next = next->next;
5316c58f1c22SToby Isaac   }
5317c58f1c22SToby Isaac   if (!flg) {
5318c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5319c58f1c22SToby Isaac 
5320c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5321c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5322c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5323c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5324c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5325c58f1c22SToby Isaac   }
5326c58f1c22SToby Isaac   PetscFunctionReturn(0);
5327c58f1c22SToby Isaac }
5328c58f1c22SToby Isaac 
5329c58f1c22SToby Isaac /*@C
5330c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5331c58f1c22SToby Isaac 
5332c58f1c22SToby Isaac   Not Collective
5333c58f1c22SToby Isaac 
5334c58f1c22SToby Isaac   Input Parameters:
5335c58f1c22SToby Isaac + dm   - The DM object
5336c58f1c22SToby Isaac . name - The label name
5337c58f1c22SToby Isaac - point - The mesh point
5338c58f1c22SToby Isaac 
5339c58f1c22SToby Isaac   Output Parameter:
5340c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5341c58f1c22SToby Isaac 
5342c58f1c22SToby Isaac   Level: beginner
5343c58f1c22SToby Isaac 
5344c58f1c22SToby Isaac .keywords: mesh
5345c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5346c58f1c22SToby Isaac @*/
5347c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5348c58f1c22SToby Isaac {
5349c58f1c22SToby Isaac   DMLabel        label;
5350c58f1c22SToby Isaac   PetscErrorCode ierr;
5351c58f1c22SToby Isaac 
5352c58f1c22SToby Isaac   PetscFunctionBegin;
5353c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5354c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5355c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
535613903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5357c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5358c58f1c22SToby Isaac   PetscFunctionReturn(0);
5359c58f1c22SToby Isaac }
5360c58f1c22SToby Isaac 
5361c58f1c22SToby Isaac /*@C
5362c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5363c58f1c22SToby Isaac 
5364c58f1c22SToby Isaac   Not Collective
5365c58f1c22SToby Isaac 
5366c58f1c22SToby Isaac   Input Parameters:
5367c58f1c22SToby Isaac + dm   - The DM object
5368c58f1c22SToby Isaac . name - The label name
5369c58f1c22SToby Isaac . point - The mesh point
5370c58f1c22SToby Isaac - value - The label value for this point
5371c58f1c22SToby Isaac 
5372c58f1c22SToby Isaac   Output Parameter:
5373c58f1c22SToby Isaac 
5374c58f1c22SToby Isaac   Level: beginner
5375c58f1c22SToby Isaac 
5376c58f1c22SToby Isaac .keywords: mesh
5377c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5378c58f1c22SToby Isaac @*/
5379c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5380c58f1c22SToby Isaac {
5381c58f1c22SToby Isaac   DMLabel        label;
5382c58f1c22SToby Isaac   PetscErrorCode ierr;
5383c58f1c22SToby Isaac 
5384c58f1c22SToby Isaac   PetscFunctionBegin;
5385c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5386c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5387c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5388c58f1c22SToby Isaac   if (!label) {
5389c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5390c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5391c58f1c22SToby Isaac   }
5392c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5393c58f1c22SToby Isaac   PetscFunctionReturn(0);
5394c58f1c22SToby Isaac }
5395c58f1c22SToby Isaac 
5396c58f1c22SToby Isaac /*@C
5397c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5398c58f1c22SToby Isaac 
5399c58f1c22SToby Isaac   Not Collective
5400c58f1c22SToby Isaac 
5401c58f1c22SToby Isaac   Input Parameters:
5402c58f1c22SToby Isaac + dm   - The DM object
5403c58f1c22SToby Isaac . name - The label name
5404c58f1c22SToby Isaac . point - The mesh point
5405c58f1c22SToby Isaac - value - The label value for this point
5406c58f1c22SToby Isaac 
5407c58f1c22SToby Isaac   Output Parameter:
5408c58f1c22SToby Isaac 
5409c58f1c22SToby Isaac   Level: beginner
5410c58f1c22SToby Isaac 
5411c58f1c22SToby Isaac .keywords: mesh
5412c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5413c58f1c22SToby Isaac @*/
5414c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5415c58f1c22SToby Isaac {
5416c58f1c22SToby Isaac   DMLabel        label;
5417c58f1c22SToby Isaac   PetscErrorCode ierr;
5418c58f1c22SToby Isaac 
5419c58f1c22SToby Isaac   PetscFunctionBegin;
5420c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5421c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5422c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5423c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5424c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5425c58f1c22SToby Isaac   PetscFunctionReturn(0);
5426c58f1c22SToby Isaac }
5427c58f1c22SToby Isaac 
5428c58f1c22SToby Isaac /*@C
5429c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5430c58f1c22SToby Isaac 
5431c58f1c22SToby Isaac   Not Collective
5432c58f1c22SToby Isaac 
5433c58f1c22SToby Isaac   Input Parameters:
5434c58f1c22SToby Isaac + dm   - The DM object
5435c58f1c22SToby Isaac - name - The label name
5436c58f1c22SToby Isaac 
5437c58f1c22SToby Isaac   Output Parameter:
5438c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5439c58f1c22SToby Isaac 
5440c58f1c22SToby Isaac   Level: beginner
5441c58f1c22SToby Isaac 
5442c58f1c22SToby Isaac .keywords: mesh
5443c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5444c58f1c22SToby Isaac @*/
5445c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5446c58f1c22SToby Isaac {
5447c58f1c22SToby Isaac   DMLabel        label;
5448c58f1c22SToby Isaac   PetscErrorCode ierr;
5449c58f1c22SToby Isaac 
5450c58f1c22SToby Isaac   PetscFunctionBegin;
5451c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5452c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5453c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5454c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5455c58f1c22SToby Isaac   *size = 0;
5456c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5457c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5458c58f1c22SToby Isaac   PetscFunctionReturn(0);
5459c58f1c22SToby Isaac }
5460c58f1c22SToby Isaac 
5461c58f1c22SToby Isaac /*@C
5462c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5463c58f1c22SToby Isaac 
5464c58f1c22SToby Isaac   Not Collective
5465c58f1c22SToby Isaac 
5466c58f1c22SToby Isaac   Input Parameters:
5467c58f1c22SToby Isaac + mesh - The DM object
5468c58f1c22SToby Isaac - name - The label name
5469c58f1c22SToby Isaac 
5470c58f1c22SToby Isaac   Output Parameter:
5471c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5472c58f1c22SToby Isaac 
5473c58f1c22SToby Isaac   Level: beginner
5474c58f1c22SToby Isaac 
5475c58f1c22SToby Isaac .keywords: mesh
5476c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5477c58f1c22SToby Isaac @*/
5478c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5479c58f1c22SToby Isaac {
5480c58f1c22SToby Isaac   DMLabel        label;
5481c58f1c22SToby Isaac   PetscErrorCode ierr;
5482c58f1c22SToby Isaac 
5483c58f1c22SToby Isaac   PetscFunctionBegin;
5484c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5485c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5486c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5487c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5488c58f1c22SToby Isaac   *ids = NULL;
5489dab2e251SBlaise Bourdin  if (label) {
5490c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5491dab2e251SBlaise Bourdin   } else {
5492dab2e251SBlaise Bourdin     /* returning an empty IS */
5493dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5494dab2e251SBlaise Bourdin   }
5495c58f1c22SToby Isaac   PetscFunctionReturn(0);
5496c58f1c22SToby Isaac }
5497c58f1c22SToby Isaac 
5498c58f1c22SToby Isaac /*@C
5499c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5500c58f1c22SToby Isaac 
5501c58f1c22SToby Isaac   Not Collective
5502c58f1c22SToby Isaac 
5503c58f1c22SToby Isaac   Input Parameters:
5504c58f1c22SToby Isaac + dm - The DM object
5505c58f1c22SToby Isaac . name - The label name
5506c58f1c22SToby Isaac - value - The stratum value
5507c58f1c22SToby Isaac 
5508c58f1c22SToby Isaac   Output Parameter:
5509c58f1c22SToby Isaac . size - The stratum size
5510c58f1c22SToby Isaac 
5511c58f1c22SToby Isaac   Level: beginner
5512c58f1c22SToby Isaac 
5513c58f1c22SToby Isaac .keywords: mesh
5514c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5515c58f1c22SToby Isaac @*/
5516c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
5517c58f1c22SToby Isaac {
5518c58f1c22SToby Isaac   DMLabel        label;
5519c58f1c22SToby Isaac   PetscErrorCode ierr;
5520c58f1c22SToby Isaac 
5521c58f1c22SToby Isaac   PetscFunctionBegin;
5522c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5523c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5524c58f1c22SToby Isaac   PetscValidPointer(size, 4);
5525c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5526c58f1c22SToby Isaac   *size = 0;
5527c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5528c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5529c58f1c22SToby Isaac   PetscFunctionReturn(0);
5530c58f1c22SToby Isaac }
5531c58f1c22SToby Isaac 
5532c58f1c22SToby Isaac /*@C
5533c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5534c58f1c22SToby Isaac 
5535c58f1c22SToby Isaac   Not Collective
5536c58f1c22SToby Isaac 
5537c58f1c22SToby Isaac   Input Parameters:
5538c58f1c22SToby Isaac + dm - The DM object
5539c58f1c22SToby Isaac . name - The label name
5540c58f1c22SToby Isaac - value - The stratum value
5541c58f1c22SToby Isaac 
5542c58f1c22SToby Isaac   Output Parameter:
5543c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5544c58f1c22SToby Isaac 
5545c58f1c22SToby Isaac   Level: beginner
5546c58f1c22SToby Isaac 
5547c58f1c22SToby Isaac .keywords: mesh
5548c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5549c58f1c22SToby Isaac @*/
5550c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5551c58f1c22SToby Isaac {
5552c58f1c22SToby Isaac   DMLabel        label;
5553c58f1c22SToby Isaac   PetscErrorCode ierr;
5554c58f1c22SToby Isaac 
5555c58f1c22SToby Isaac   PetscFunctionBegin;
5556c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5557c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5558c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5559c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5560c58f1c22SToby Isaac   *points = NULL;
5561c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5562c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5563c58f1c22SToby Isaac   PetscFunctionReturn(0);
5564c58f1c22SToby Isaac }
5565c58f1c22SToby Isaac 
55664de306b1SToby Isaac /*@C
55679044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
55684de306b1SToby Isaac 
55694de306b1SToby Isaac   Not Collective
55704de306b1SToby Isaac 
55714de306b1SToby Isaac   Input Parameters:
55724de306b1SToby Isaac + dm - The DM object
55734de306b1SToby Isaac . name - The label name
55744de306b1SToby Isaac . value - The stratum value
55754de306b1SToby Isaac - points - The stratum points
55764de306b1SToby Isaac 
55774de306b1SToby Isaac   Level: beginner
55784de306b1SToby Isaac 
55794de306b1SToby Isaac .keywords: mesh
55804de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
55814de306b1SToby Isaac @*/
55824de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
55834de306b1SToby Isaac {
55844de306b1SToby Isaac   DMLabel        label;
55854de306b1SToby Isaac   PetscErrorCode ierr;
55864de306b1SToby Isaac 
55874de306b1SToby Isaac   PetscFunctionBegin;
55884de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55894de306b1SToby Isaac   PetscValidCharPointer(name, 2);
55904de306b1SToby Isaac   PetscValidPointer(points, 4);
55914de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
55924de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
55934de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
55944de306b1SToby Isaac   PetscFunctionReturn(0);
55954de306b1SToby Isaac }
55964de306b1SToby Isaac 
5597c58f1c22SToby Isaac /*@C
5598c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5599c58f1c22SToby Isaac 
5600c58f1c22SToby Isaac   Not Collective
5601c58f1c22SToby Isaac 
5602c58f1c22SToby Isaac   Input Parameters:
5603c58f1c22SToby Isaac + dm   - The DM object
5604c58f1c22SToby Isaac . name - The label name
5605c58f1c22SToby Isaac - value - The label value for this point
5606c58f1c22SToby Isaac 
5607c58f1c22SToby Isaac   Output Parameter:
5608c58f1c22SToby Isaac 
5609c58f1c22SToby Isaac   Level: beginner
5610c58f1c22SToby Isaac 
5611c58f1c22SToby Isaac .keywords: mesh
5612c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5613c58f1c22SToby Isaac @*/
5614c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5615c58f1c22SToby Isaac {
5616c58f1c22SToby Isaac   DMLabel        label;
5617c58f1c22SToby Isaac   PetscErrorCode ierr;
5618c58f1c22SToby Isaac 
5619c58f1c22SToby Isaac   PetscFunctionBegin;
5620c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5621c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5622c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5623c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5624c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5625c58f1c22SToby Isaac   PetscFunctionReturn(0);
5626c58f1c22SToby Isaac }
5627c58f1c22SToby Isaac 
5628c58f1c22SToby Isaac /*@
5629c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5630c58f1c22SToby Isaac 
5631c58f1c22SToby Isaac   Not Collective
5632c58f1c22SToby Isaac 
5633c58f1c22SToby Isaac   Input Parameter:
5634c58f1c22SToby Isaac . dm   - The DM object
5635c58f1c22SToby Isaac 
5636c58f1c22SToby Isaac   Output Parameter:
5637c58f1c22SToby Isaac . numLabels - the number of Labels
5638c58f1c22SToby Isaac 
5639c58f1c22SToby Isaac   Level: intermediate
5640c58f1c22SToby Isaac 
5641c58f1c22SToby Isaac .keywords: mesh
5642c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5643c58f1c22SToby Isaac @*/
5644c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5645c58f1c22SToby Isaac {
5646c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5647c58f1c22SToby Isaac   PetscInt  n    = 0;
5648c58f1c22SToby Isaac 
5649c58f1c22SToby Isaac   PetscFunctionBegin;
5650c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5651c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5652c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5653c58f1c22SToby Isaac   *numLabels = n;
5654c58f1c22SToby Isaac   PetscFunctionReturn(0);
5655c58f1c22SToby Isaac }
5656c58f1c22SToby Isaac 
5657c58f1c22SToby Isaac /*@C
5658c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5659c58f1c22SToby Isaac 
5660c58f1c22SToby Isaac   Not Collective
5661c58f1c22SToby Isaac 
5662c58f1c22SToby Isaac   Input Parameters:
5663c58f1c22SToby Isaac + dm - The DM object
5664c58f1c22SToby Isaac - n  - the label number
5665c58f1c22SToby Isaac 
5666c58f1c22SToby Isaac   Output Parameter:
5667c58f1c22SToby Isaac . name - the label name
5668c58f1c22SToby Isaac 
5669c58f1c22SToby Isaac   Level: intermediate
5670c58f1c22SToby Isaac 
5671c58f1c22SToby Isaac .keywords: mesh
5672c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5673c58f1c22SToby Isaac @*/
5674c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5675c58f1c22SToby Isaac {
5676c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5677c58f1c22SToby Isaac   PetscInt  l    = 0;
5678c58f1c22SToby Isaac 
5679c58f1c22SToby Isaac   PetscFunctionBegin;
5680c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5681c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5682c58f1c22SToby Isaac   while (next) {
5683c58f1c22SToby Isaac     if (l == n) {
5684c58f1c22SToby Isaac       *name = next->label->name;
5685c58f1c22SToby Isaac       PetscFunctionReturn(0);
5686c58f1c22SToby Isaac     }
5687c58f1c22SToby Isaac     ++l;
5688c58f1c22SToby Isaac     next = next->next;
5689c58f1c22SToby Isaac   }
5690c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5691c58f1c22SToby Isaac }
5692c58f1c22SToby Isaac 
5693c58f1c22SToby Isaac /*@C
5694c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5695c58f1c22SToby Isaac 
5696c58f1c22SToby Isaac   Not Collective
5697c58f1c22SToby Isaac 
5698c58f1c22SToby Isaac   Input Parameters:
5699c58f1c22SToby Isaac + dm   - The DM object
5700c58f1c22SToby Isaac - name - The label name
5701c58f1c22SToby Isaac 
5702c58f1c22SToby Isaac   Output Parameter:
5703c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5704c58f1c22SToby Isaac 
5705c58f1c22SToby Isaac   Level: intermediate
5706c58f1c22SToby Isaac 
5707c58f1c22SToby Isaac .keywords: mesh
5708c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5709c58f1c22SToby Isaac @*/
5710c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5711c58f1c22SToby Isaac {
5712c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5713c58f1c22SToby Isaac   PetscErrorCode ierr;
5714c58f1c22SToby Isaac 
5715c58f1c22SToby Isaac   PetscFunctionBegin;
5716c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5717c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5718c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5719c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5720c58f1c22SToby Isaac   while (next) {
5721c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5722c58f1c22SToby Isaac     if (*hasLabel) break;
5723c58f1c22SToby Isaac     next = next->next;
5724c58f1c22SToby Isaac   }
5725c58f1c22SToby Isaac   PetscFunctionReturn(0);
5726c58f1c22SToby Isaac }
5727c58f1c22SToby Isaac 
5728c58f1c22SToby Isaac /*@C
5729c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5730c58f1c22SToby Isaac 
5731c58f1c22SToby Isaac   Not Collective
5732c58f1c22SToby Isaac 
5733c58f1c22SToby Isaac   Input Parameters:
5734c58f1c22SToby Isaac + dm   - The DM object
5735c58f1c22SToby Isaac - name - The label name
5736c58f1c22SToby Isaac 
5737c58f1c22SToby Isaac   Output Parameter:
5738c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5739c58f1c22SToby Isaac 
5740c58f1c22SToby Isaac   Level: intermediate
5741c58f1c22SToby Isaac 
5742c58f1c22SToby Isaac .keywords: mesh
5743c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5744c58f1c22SToby Isaac @*/
5745c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5746c58f1c22SToby Isaac {
5747c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5748c58f1c22SToby Isaac   PetscBool      hasLabel;
5749c58f1c22SToby Isaac   PetscErrorCode ierr;
5750c58f1c22SToby Isaac 
5751c58f1c22SToby Isaac   PetscFunctionBegin;
5752c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5753c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5754c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5755c58f1c22SToby Isaac   *label = NULL;
5756c58f1c22SToby Isaac   while (next) {
5757c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5758c58f1c22SToby Isaac     if (hasLabel) {
5759c58f1c22SToby Isaac       *label = next->label;
5760c58f1c22SToby Isaac       break;
5761c58f1c22SToby Isaac     }
5762c58f1c22SToby Isaac     next = next->next;
5763c58f1c22SToby Isaac   }
5764c58f1c22SToby Isaac   PetscFunctionReturn(0);
5765c58f1c22SToby Isaac }
5766c58f1c22SToby Isaac 
5767c58f1c22SToby Isaac /*@C
5768c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5769c58f1c22SToby Isaac 
5770c58f1c22SToby Isaac   Not Collective
5771c58f1c22SToby Isaac 
5772c58f1c22SToby Isaac   Input Parameters:
5773c58f1c22SToby Isaac + dm - The DM object
5774c58f1c22SToby Isaac - n  - the label number
5775c58f1c22SToby Isaac 
5776c58f1c22SToby Isaac   Output Parameter:
5777c58f1c22SToby Isaac . label - the label
5778c58f1c22SToby Isaac 
5779c58f1c22SToby Isaac   Level: intermediate
5780c58f1c22SToby Isaac 
5781c58f1c22SToby Isaac .keywords: mesh
5782c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5783c58f1c22SToby Isaac @*/
5784c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5785c58f1c22SToby Isaac {
5786c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5787c58f1c22SToby Isaac   PetscInt    l    = 0;
5788c58f1c22SToby Isaac 
5789c58f1c22SToby Isaac   PetscFunctionBegin;
5790c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5791c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5792c58f1c22SToby Isaac   while (next) {
5793c58f1c22SToby Isaac     if (l == n) {
5794c58f1c22SToby Isaac       *label = next->label;
5795c58f1c22SToby Isaac       PetscFunctionReturn(0);
5796c58f1c22SToby Isaac     }
5797c58f1c22SToby Isaac     ++l;
5798c58f1c22SToby Isaac     next = next->next;
5799c58f1c22SToby Isaac   }
5800c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5801c58f1c22SToby Isaac }
5802c58f1c22SToby Isaac 
5803c58f1c22SToby Isaac /*@C
5804c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5805c58f1c22SToby Isaac 
5806c58f1c22SToby Isaac   Not Collective
5807c58f1c22SToby Isaac 
5808c58f1c22SToby Isaac   Input Parameters:
5809c58f1c22SToby Isaac + dm   - The DM object
5810c58f1c22SToby Isaac - label - The DMLabel
5811c58f1c22SToby Isaac 
5812c58f1c22SToby Isaac   Level: developer
5813c58f1c22SToby Isaac 
5814c58f1c22SToby Isaac .keywords: mesh
5815c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5816c58f1c22SToby Isaac @*/
5817c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5818c58f1c22SToby Isaac {
5819c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5820c58f1c22SToby Isaac   PetscBool      hasLabel;
5821c58f1c22SToby Isaac   PetscErrorCode ierr;
5822c58f1c22SToby Isaac 
5823c58f1c22SToby Isaac   PetscFunctionBegin;
5824c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5825c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5826c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5827c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5828c58f1c22SToby Isaac   tmpLabel->label  = label;
5829c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5830c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5831c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5832c58f1c22SToby Isaac   PetscFunctionReturn(0);
5833c58f1c22SToby Isaac }
5834c58f1c22SToby Isaac 
5835c58f1c22SToby Isaac /*@C
5836c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5837c58f1c22SToby Isaac 
5838c58f1c22SToby Isaac   Not Collective
5839c58f1c22SToby Isaac 
5840c58f1c22SToby Isaac   Input Parameters:
5841c58f1c22SToby Isaac + dm   - The DM object
5842c58f1c22SToby Isaac - name - The label name
5843c58f1c22SToby Isaac 
5844c58f1c22SToby Isaac   Output Parameter:
5845c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5846c58f1c22SToby Isaac 
5847c58f1c22SToby Isaac   Level: developer
5848c58f1c22SToby Isaac 
5849c58f1c22SToby Isaac .keywords: mesh
5850c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5851c58f1c22SToby Isaac @*/
5852c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5853c58f1c22SToby Isaac {
5854c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5855c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5856c58f1c22SToby Isaac   PetscBool      hasLabel;
5857c58f1c22SToby Isaac   PetscErrorCode ierr;
5858c58f1c22SToby Isaac 
5859c58f1c22SToby Isaac   PetscFunctionBegin;
5860c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5861c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5862c58f1c22SToby Isaac   *label = NULL;
5863c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5864c58f1c22SToby Isaac   while (next) {
5865c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5866c58f1c22SToby Isaac     if (hasLabel) {
5867c58f1c22SToby Isaac       if (last) last->next       = next->next;
5868c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5869c58f1c22SToby Isaac       next->next = NULL;
5870c58f1c22SToby Isaac       *label     = next->label;
5871c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5872c58f1c22SToby Isaac       if (hasLabel) {
5873c58f1c22SToby Isaac         dm->depthLabel = NULL;
5874c58f1c22SToby Isaac       }
5875c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5876c58f1c22SToby Isaac       break;
5877c58f1c22SToby Isaac     }
5878c58f1c22SToby Isaac     last = next;
5879c58f1c22SToby Isaac     next = next->next;
5880c58f1c22SToby Isaac   }
5881c58f1c22SToby Isaac   PetscFunctionReturn(0);
5882c58f1c22SToby Isaac }
5883c58f1c22SToby Isaac 
5884c58f1c22SToby Isaac /*@C
5885c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5886c58f1c22SToby Isaac 
5887c58f1c22SToby Isaac   Not Collective
5888c58f1c22SToby Isaac 
5889c58f1c22SToby Isaac   Input Parameters:
5890c58f1c22SToby Isaac + dm   - The DM object
5891c58f1c22SToby Isaac - name - The label name
5892c58f1c22SToby Isaac 
5893c58f1c22SToby Isaac   Output Parameter:
5894c58f1c22SToby Isaac . output - The flag for output
5895c58f1c22SToby Isaac 
5896c58f1c22SToby Isaac   Level: developer
5897c58f1c22SToby Isaac 
5898c58f1c22SToby Isaac .keywords: mesh
5899c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5900c58f1c22SToby Isaac @*/
5901c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5902c58f1c22SToby Isaac {
5903c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5904c58f1c22SToby Isaac   PetscErrorCode ierr;
5905c58f1c22SToby Isaac 
5906c58f1c22SToby Isaac   PetscFunctionBegin;
5907c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5908c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5909c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5910c58f1c22SToby Isaac   while (next) {
5911c58f1c22SToby Isaac     PetscBool flg;
5912c58f1c22SToby Isaac 
5913c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5914c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5915c58f1c22SToby Isaac     next = next->next;
5916c58f1c22SToby Isaac   }
5917c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5918c58f1c22SToby Isaac }
5919c58f1c22SToby Isaac 
5920c58f1c22SToby Isaac /*@C
5921c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5922c58f1c22SToby Isaac 
5923c58f1c22SToby Isaac   Not Collective
5924c58f1c22SToby Isaac 
5925c58f1c22SToby Isaac   Input Parameters:
5926c58f1c22SToby Isaac + dm     - The DM object
5927c58f1c22SToby Isaac . name   - The label name
5928c58f1c22SToby Isaac - output - The flag for output
5929c58f1c22SToby Isaac 
5930c58f1c22SToby Isaac   Level: developer
5931c58f1c22SToby Isaac 
5932c58f1c22SToby Isaac .keywords: mesh
5933c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5934c58f1c22SToby Isaac @*/
5935c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5936c58f1c22SToby Isaac {
5937c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5938c58f1c22SToby Isaac   PetscErrorCode ierr;
5939c58f1c22SToby Isaac 
5940c58f1c22SToby Isaac   PetscFunctionBegin;
5941c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5942c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5943c58f1c22SToby Isaac   while (next) {
5944c58f1c22SToby Isaac     PetscBool flg;
5945c58f1c22SToby Isaac 
5946c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5947c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5948c58f1c22SToby Isaac     next = next->next;
5949c58f1c22SToby Isaac   }
5950c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5951c58f1c22SToby Isaac }
5952c58f1c22SToby Isaac 
5953c58f1c22SToby Isaac 
5954c58f1c22SToby Isaac /*@
5955c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5956c58f1c22SToby Isaac 
5957c58f1c22SToby Isaac   Collective on DM
5958c58f1c22SToby Isaac 
5959c58f1c22SToby Isaac   Input Parameter:
59602e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5961c58f1c22SToby Isaac 
5962c58f1c22SToby Isaac   Output Parameter:
59632e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5964c58f1c22SToby Isaac 
5965c58f1c22SToby Isaac   Level: intermediate
5966c58f1c22SToby Isaac 
5967c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5968c58f1c22SToby Isaac 
5969c58f1c22SToby Isaac .keywords: mesh
5970c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5971c58f1c22SToby Isaac @*/
5972c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5973c58f1c22SToby Isaac {
5974c58f1c22SToby Isaac   PetscInt       numLabels, l;
5975c58f1c22SToby Isaac   PetscErrorCode ierr;
5976c58f1c22SToby Isaac 
5977c58f1c22SToby Isaac   PetscFunctionBegin;
5978c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5979c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5980c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5981c58f1c22SToby Isaac     DMLabel     label, labelNew;
5982c58f1c22SToby Isaac     const char *name;
5983c58f1c22SToby Isaac     PetscBool   flg;
5984c58f1c22SToby Isaac 
5985c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5986c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5987c58f1c22SToby Isaac     if (flg) continue;
59887d5acc75SStefano Zampini     ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
59897d5acc75SStefano Zampini     if (flg) continue;
5990c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5991c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5992c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5993c58f1c22SToby Isaac   }
5994c58f1c22SToby Isaac   PetscFunctionReturn(0);
5995c58f1c22SToby Isaac }
5996a8fb8f29SToby Isaac 
5997a8fb8f29SToby Isaac /*@
5998a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5999a8fb8f29SToby Isaac 
6000a8fb8f29SToby Isaac   Input Parameter:
6001a8fb8f29SToby Isaac . dm - The DM object
6002a8fb8f29SToby Isaac 
6003a8fb8f29SToby Isaac   Output Parameter:
6004a8fb8f29SToby Isaac . cdm - The coarse DM
6005a8fb8f29SToby Isaac 
6006a8fb8f29SToby Isaac   Level: intermediate
6007a8fb8f29SToby Isaac 
6008a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
6009a8fb8f29SToby Isaac @*/
6010a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
6011a8fb8f29SToby Isaac {
6012a8fb8f29SToby Isaac   PetscFunctionBegin;
6013a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6014a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
6015a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
6016a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6017a8fb8f29SToby Isaac }
6018a8fb8f29SToby Isaac 
6019a8fb8f29SToby Isaac /*@
6020a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
6021a8fb8f29SToby Isaac 
6022a8fb8f29SToby Isaac   Input Parameters:
6023a8fb8f29SToby Isaac + dm - The DM object
6024a8fb8f29SToby Isaac - cdm - The coarse DM
6025a8fb8f29SToby Isaac 
6026a8fb8f29SToby Isaac   Level: intermediate
6027a8fb8f29SToby Isaac 
6028a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
6029a8fb8f29SToby Isaac @*/
6030a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
6031a8fb8f29SToby Isaac {
6032a8fb8f29SToby Isaac   PetscErrorCode ierr;
6033a8fb8f29SToby Isaac 
6034a8fb8f29SToby Isaac   PetscFunctionBegin;
6035a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6036a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
6037a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
6038a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
6039a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6040a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6041a8fb8f29SToby Isaac }
6042a8fb8f29SToby Isaac 
604388bdff64SToby Isaac /*@
604488bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
604588bdff64SToby Isaac 
604688bdff64SToby Isaac   Input Parameter:
604788bdff64SToby Isaac . dm - The DM object
604888bdff64SToby Isaac 
604988bdff64SToby Isaac   Output Parameter:
605088bdff64SToby Isaac . fdm - The fine DM
605188bdff64SToby Isaac 
605288bdff64SToby Isaac   Level: intermediate
605388bdff64SToby Isaac 
605488bdff64SToby Isaac .seealso: DMSetFineDM()
605588bdff64SToby Isaac @*/
605688bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
605788bdff64SToby Isaac {
605888bdff64SToby Isaac   PetscFunctionBegin;
605988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
606088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
606188bdff64SToby Isaac   *fdm = dm->fineMesh;
606288bdff64SToby Isaac   PetscFunctionReturn(0);
606388bdff64SToby Isaac }
606488bdff64SToby Isaac 
606588bdff64SToby Isaac /*@
606688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
606788bdff64SToby Isaac 
606888bdff64SToby Isaac   Input Parameters:
606988bdff64SToby Isaac + dm - The DM object
607088bdff64SToby Isaac - fdm - The fine DM
607188bdff64SToby Isaac 
607288bdff64SToby Isaac   Level: intermediate
607388bdff64SToby Isaac 
607488bdff64SToby Isaac .seealso: DMGetFineDM()
607588bdff64SToby Isaac @*/
607688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
607788bdff64SToby Isaac {
607888bdff64SToby Isaac   PetscErrorCode ierr;
607988bdff64SToby Isaac 
608088bdff64SToby Isaac   PetscFunctionBegin;
608188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
608288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
608388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
608488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
608588bdff64SToby Isaac   dm->fineMesh = fdm;
608688bdff64SToby Isaac   PetscFunctionReturn(0);
608788bdff64SToby Isaac }
608888bdff64SToby Isaac 
6089a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6090a6ba4734SToby Isaac 
6091a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6092a6ba4734SToby Isaac {
6093a6ba4734SToby Isaac   PetscErrorCode ierr;
6094a6ba4734SToby Isaac 
6095a6ba4734SToby Isaac   PetscFunctionBegin;
6096e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6097a6ba4734SToby Isaac   PetscFunctionReturn(0);
6098a6ba4734SToby Isaac }
6099a6ba4734SToby Isaac 
6100a6ba4734SToby Isaac /*@C
6101a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6102a6ba4734SToby Isaac 
6103a6ba4734SToby Isaac   Input Parameters:
61044c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6105f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6106a6ba4734SToby Isaac . name        - The BC name
6107a6ba4734SToby Isaac . labelname   - The label defining constrained points
6108a6ba4734SToby Isaac . field       - The field to constrain
6109a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6110a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6111a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6112a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6113a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6114a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6115a6ba4734SToby Isaac 
6116a6ba4734SToby Isaac   Options Database Keys:
6117a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6118a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6119a6ba4734SToby Isaac 
6120a6ba4734SToby Isaac   Level: developer
6121a6ba4734SToby Isaac 
6122a6ba4734SToby Isaac .seealso: DMGetBoundary()
6123a6ba4734SToby Isaac @*/
6124a30ec4eaSSatish 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)
6125a6ba4734SToby Isaac {
6126a6ba4734SToby Isaac   PetscErrorCode ierr;
6127a6ba4734SToby Isaac 
6128a6ba4734SToby Isaac   PetscFunctionBegin;
6129a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6130f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6131a6ba4734SToby Isaac   PetscFunctionReturn(0);
6132a6ba4734SToby Isaac }
6133a6ba4734SToby Isaac 
6134a6ba4734SToby Isaac /*@
6135a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6136a6ba4734SToby Isaac 
6137a6ba4734SToby Isaac   Input Parameters:
6138a6ba4734SToby Isaac . dm - The mesh object
6139a6ba4734SToby Isaac 
6140a6ba4734SToby Isaac   Output Parameters:
6141a6ba4734SToby Isaac . numBd - The number of BC
6142a6ba4734SToby Isaac 
6143a6ba4734SToby Isaac   Level: intermediate
6144a6ba4734SToby Isaac 
6145a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6146a6ba4734SToby Isaac @*/
6147a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6148a6ba4734SToby Isaac {
614958ebd649SToby Isaac   PetscErrorCode ierr;
6150a6ba4734SToby Isaac 
6151a6ba4734SToby Isaac   PetscFunctionBegin;
6152a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
615358ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6154a6ba4734SToby Isaac   PetscFunctionReturn(0);
6155a6ba4734SToby Isaac }
6156a6ba4734SToby Isaac 
6157a6ba4734SToby Isaac /*@C
61581c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6159a6ba4734SToby Isaac 
6160a6ba4734SToby Isaac   Input Parameters:
6161a6ba4734SToby Isaac + dm          - The mesh object
6162a6ba4734SToby Isaac - bd          - The BC number
6163a6ba4734SToby Isaac 
6164a6ba4734SToby Isaac   Output Parameters:
6165f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6166a6ba4734SToby Isaac . name        - The BC name
6167a6ba4734SToby Isaac . labelname   - The label defining constrained points
6168a6ba4734SToby Isaac . field       - The field to constrain
6169a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6170a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6171a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6172a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6173a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6174a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6175a6ba4734SToby Isaac 
6176a6ba4734SToby Isaac   Options Database Keys:
6177a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6178a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6179a6ba4734SToby Isaac 
6180a6ba4734SToby Isaac   Level: developer
6181a6ba4734SToby Isaac 
6182a6ba4734SToby Isaac .seealso: DMAddBoundary()
6183a6ba4734SToby Isaac @*/
6184a30ec4eaSSatish 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)
6185a6ba4734SToby Isaac {
618658ebd649SToby Isaac   PetscErrorCode ierr;
6187a6ba4734SToby Isaac 
6188a6ba4734SToby Isaac   PetscFunctionBegin;
6189a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6190f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6191a6ba4734SToby Isaac   PetscFunctionReturn(0);
6192a6ba4734SToby Isaac }
6193a6ba4734SToby Isaac 
6194e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6195e6f8dbb6SToby Isaac {
6196dff059c6SToby Isaac   DMBoundary *lastnext;
6197e6f8dbb6SToby Isaac   DSBoundary dsbound;
6198e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6199e6f8dbb6SToby Isaac 
6200e6f8dbb6SToby Isaac   PetscFunctionBegin;
6201e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
620247a1f5adSToby Isaac   if (dm->boundary) {
620347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
620447a1f5adSToby Isaac 
620547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
620647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
620747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
620847a1f5adSToby Isaac     while (next) {
620947a1f5adSToby Isaac       DMBoundary b = next;
621047a1f5adSToby Isaac 
621147a1f5adSToby Isaac       next = b->next;
621247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6213a6ba4734SToby Isaac     }
621447a1f5adSToby Isaac     dm->boundary = NULL;
6215a6ba4734SToby Isaac   }
621647a1f5adSToby Isaac 
6217dff059c6SToby Isaac   lastnext = &(dm->boundary);
6218e6f8dbb6SToby Isaac   while (dsbound) {
6219e6f8dbb6SToby Isaac     DMBoundary dmbound;
6220e6f8dbb6SToby Isaac 
6221e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6222e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6223e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6224994fe344SLisandro 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);}
622547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6226dff059c6SToby Isaac     *lastnext = dmbound;
6227dff059c6SToby Isaac     lastnext = &(dmbound->next);
6228dff059c6SToby Isaac     dsbound = dsbound->next;
6229a6ba4734SToby Isaac   }
6230a6ba4734SToby Isaac   PetscFunctionReturn(0);
6231a6ba4734SToby Isaac }
6232a6ba4734SToby Isaac 
6233a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6234a6ba4734SToby Isaac {
6235b95f2879SToby Isaac   DMBoundary     b;
6236a6ba4734SToby Isaac   PetscErrorCode ierr;
6237a6ba4734SToby Isaac 
6238a6ba4734SToby Isaac   PetscFunctionBegin;
6239a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6240a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6241a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6242e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6243b95f2879SToby Isaac   b = dm->boundary;
6244a6ba4734SToby Isaac   while (b && !(*isBd)) {
6245e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6246e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
62473424c85cSToby Isaac 
62483424c85cSToby Isaac     if (label) {
6249a6ba4734SToby Isaac       PetscInt i;
6250a6ba4734SToby Isaac 
6251e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6252e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6253a6ba4734SToby Isaac       }
6254a6ba4734SToby Isaac     }
6255a6ba4734SToby Isaac     b = b->next;
6256a6ba4734SToby Isaac   }
6257a6ba4734SToby Isaac   PetscFunctionReturn(0);
6258a6ba4734SToby Isaac }
62594d6f44ffSToby Isaac 
62604d6f44ffSToby Isaac /*@C
62614d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
62624d6f44ffSToby Isaac 
62634d6f44ffSToby Isaac   Input Parameters:
62644d6f44ffSToby Isaac + dm      - The DM
62650709b2feSToby Isaac . time    - The time
62664d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
62674d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
62684d6f44ffSToby Isaac - mode    - The insertion mode for values
62694d6f44ffSToby Isaac 
62704d6f44ffSToby Isaac   Output Parameter:
62714d6f44ffSToby Isaac . X - vector
62724d6f44ffSToby Isaac 
62734d6f44ffSToby Isaac    Calling sequence of func:
62740709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
62754d6f44ffSToby Isaac 
62764d6f44ffSToby Isaac +  dim - The spatial dimension
62774d6f44ffSToby Isaac .  x   - The coordinates
62784d6f44ffSToby Isaac .  Nf  - The number of fields
62794d6f44ffSToby Isaac .  u   - The output field values
62804d6f44ffSToby Isaac -  ctx - optional user-defined function context
62814d6f44ffSToby Isaac 
62824d6f44ffSToby Isaac   Level: developer
62834d6f44ffSToby Isaac 
62842716604bSToby Isaac .seealso: DMComputeL2Diff()
62854d6f44ffSToby Isaac @*/
62860709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
62874d6f44ffSToby Isaac {
62884d6f44ffSToby Isaac   Vec            localX;
62894d6f44ffSToby Isaac   PetscErrorCode ierr;
62904d6f44ffSToby Isaac 
62914d6f44ffSToby Isaac   PetscFunctionBegin;
62924d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62934d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62940709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62954d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62964d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62974d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62984d6f44ffSToby Isaac   PetscFunctionReturn(0);
62994d6f44ffSToby Isaac }
63004d6f44ffSToby Isaac 
63010709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
63024d6f44ffSToby Isaac {
63034d6f44ffSToby Isaac   PetscErrorCode ierr;
63044d6f44ffSToby Isaac 
63054d6f44ffSToby Isaac   PetscFunctionBegin;
63064d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63074d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
63080918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
63090709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63104d6f44ffSToby Isaac   PetscFunctionReturn(0);
63114d6f44ffSToby Isaac }
63124d6f44ffSToby Isaac 
63132c53366bSMatthew 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)
63142c53366bSMatthew G. Knepley {
63152c53366bSMatthew G. Knepley   Vec            localX;
63162c53366bSMatthew G. Knepley   PetscErrorCode ierr;
63172c53366bSMatthew G. Knepley 
63182c53366bSMatthew G. Knepley   PetscFunctionBegin;
63192c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
63202c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
63212c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63222c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
63232c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
63242c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
63252c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
63262c53366bSMatthew G. Knepley }
63272c53366bSMatthew G. Knepley 
63281c531cf8SMatthew 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)
63294d6f44ffSToby Isaac {
63304d6f44ffSToby Isaac   PetscErrorCode ierr;
63314d6f44ffSToby Isaac 
63324d6f44ffSToby Isaac   PetscFunctionBegin;
63334d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63344d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
63350918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
63361c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
63374d6f44ffSToby Isaac   PetscFunctionReturn(0);
63384d6f44ffSToby Isaac }
63392716604bSToby Isaac 
63408c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
63418c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
63428c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
63438c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6344191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
63458c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
63468c6c5593SMatthew G. Knepley {
63478c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
63488c6c5593SMatthew G. Knepley 
63498c6c5593SMatthew G. Knepley   PetscFunctionBegin;
63508c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63518c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
63528c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
63530918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
63548c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
63558c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
63568c6c5593SMatthew G. Knepley }
63578c6c5593SMatthew G. Knepley 
63581c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
63598c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
63608c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
63618c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6362191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
63638c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
63648c6c5593SMatthew G. Knepley {
63658c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
63668c6c5593SMatthew G. Knepley 
63678c6c5593SMatthew G. Knepley   PetscFunctionBegin;
63688c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63698c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
63708c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
63710918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
63721c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
63738c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
63748c6c5593SMatthew G. Knepley }
63758c6c5593SMatthew G. Knepley 
63762716604bSToby Isaac /*@C
63772716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
63782716604bSToby Isaac 
63792716604bSToby Isaac   Input Parameters:
63802716604bSToby Isaac + dm    - The DM
63810709b2feSToby Isaac . time  - The time
63822716604bSToby Isaac . funcs - The functions to evaluate for each field component
63832716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6384574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
63852716604bSToby Isaac 
63862716604bSToby Isaac   Output Parameter:
63872716604bSToby Isaac . diff - The diff ||u - u_h||_2
63882716604bSToby Isaac 
63892716604bSToby Isaac   Level: developer
63902716604bSToby Isaac 
63911189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63922716604bSToby Isaac @*/
63930709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
63942716604bSToby Isaac {
63952716604bSToby Isaac   PetscErrorCode ierr;
63962716604bSToby Isaac 
63972716604bSToby Isaac   PetscFunctionBegin;
63982716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6399b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
64000918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
64010709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
64022716604bSToby Isaac   PetscFunctionReturn(0);
64032716604bSToby Isaac }
6404b698f381SToby Isaac 
6405b698f381SToby Isaac /*@C
6406b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6407b698f381SToby Isaac 
6408b698f381SToby Isaac   Input Parameters:
6409b698f381SToby Isaac + dm    - The DM
6410b698f381SToby Isaac , time  - The time
6411b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6412b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6413574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6414b698f381SToby Isaac - n     - The vector to project along
6415b698f381SToby Isaac 
6416b698f381SToby Isaac   Output Parameter:
6417b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6418b698f381SToby Isaac 
6419b698f381SToby Isaac   Level: developer
6420b698f381SToby Isaac 
6421b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6422b698f381SToby Isaac @*/
6423b698f381SToby 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)
6424b698f381SToby Isaac {
6425b698f381SToby Isaac   PetscErrorCode ierr;
6426b698f381SToby Isaac 
6427b698f381SToby Isaac   PetscFunctionBegin;
6428b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6429b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6430b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6431b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6432b698f381SToby Isaac   PetscFunctionReturn(0);
6433b698f381SToby Isaac }
6434b698f381SToby Isaac 
64352a16baeaSToby Isaac /*@C
64362a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
64372a16baeaSToby Isaac 
64382a16baeaSToby Isaac   Input Parameters:
64392a16baeaSToby Isaac + dm    - The DM
64402a16baeaSToby Isaac . time  - The time
64412a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
64422a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6443574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
64442a16baeaSToby Isaac 
64452a16baeaSToby Isaac   Output Parameter:
64462a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
64472a16baeaSToby Isaac 
64482a16baeaSToby Isaac   Level: developer
64492a16baeaSToby Isaac 
64501189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
64512a16baeaSToby Isaac @*/
64521189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
64532a16baeaSToby Isaac {
64542a16baeaSToby Isaac   PetscErrorCode ierr;
64552a16baeaSToby Isaac 
64562a16baeaSToby Isaac   PetscFunctionBegin;
64572a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64582a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
64590918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
64602a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
64612a16baeaSToby Isaac   PetscFunctionReturn(0);
64622a16baeaSToby Isaac }
64632a16baeaSToby Isaac 
6464df0b854cSToby Isaac /*@C
6465df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6466cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6467df0b854cSToby Isaac 
6468df0b854cSToby Isaac   Collective on dm
6469df0b854cSToby Isaac 
6470df0b854cSToby Isaac   Input parameters:
6471df0b854cSToby Isaac + dm - the pre-adaptation DM object
6472a1b0c543SToby Isaac - label - label with the flags
6473df0b854cSToby Isaac 
6474df0b854cSToby Isaac   Output parameters:
64750d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6476df0b854cSToby Isaac 
6477df0b854cSToby Isaac   Level: intermediate
64780d1cd5e0SMatthew G. Knepley 
64790d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6480df0b854cSToby Isaac @*/
64810d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6482df0b854cSToby Isaac {
6483df0b854cSToby Isaac   PetscErrorCode ierr;
6484df0b854cSToby Isaac 
6485df0b854cSToby Isaac   PetscFunctionBegin;
6486df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6487a1b0c543SToby Isaac   PetscValidPointer(label,2);
64880d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
64890d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
64906f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
64910d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
64920d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
64930d1cd5e0SMatthew G. Knepley }
64940d1cd5e0SMatthew G. Knepley 
64950d1cd5e0SMatthew G. Knepley /*@C
64960d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
64970d1cd5e0SMatthew G. Knepley 
64980d1cd5e0SMatthew G. Knepley   Input Parameters:
64990d1cd5e0SMatthew G. Knepley + dm - The DM object
65000d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
65016f25b0d8SLisandro 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_".
65020d1cd5e0SMatthew G. Knepley 
65030d1cd5e0SMatthew G. Knepley   Output Parameter:
65040d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
65050d1cd5e0SMatthew G. Knepley 
65060d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
65070d1cd5e0SMatthew G. Knepley 
65080d1cd5e0SMatthew G. Knepley   Level: advanced
65090d1cd5e0SMatthew G. Knepley 
65100d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
65110d1cd5e0SMatthew G. Knepley @*/
65120d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
65130d1cd5e0SMatthew G. Knepley {
65140d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
65150d1cd5e0SMatthew G. Knepley 
65160d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
65170d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
65180d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
65190d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
65200d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
65216f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
65226f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
65230d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6524df0b854cSToby Isaac   PetscFunctionReturn(0);
6525df0b854cSToby Isaac }
6526c4088d22SMatthew G. Knepley 
6527502a2867SDave May /*@C
6528502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6529502a2867SDave May 
6530502a2867SDave May  Not Collective
6531502a2867SDave May 
6532502a2867SDave May  Input Parameter:
6533502a2867SDave May  . dm    - The DM
6534502a2867SDave May 
6535502a2867SDave May  Output Parameter:
6536502a2867SDave May  . nranks - the number of neighbours
6537502a2867SDave May  . ranks - the neighbors ranks
6538502a2867SDave May 
6539502a2867SDave May  Notes:
6540502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6541502a2867SDave May 
6542502a2867SDave May  Level: beginner
6543502a2867SDave May 
6544dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6545502a2867SDave May @*/
6546502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6547502a2867SDave May {
6548502a2867SDave May   PetscErrorCode ierr;
6549502a2867SDave May 
6550502a2867SDave May   PetscFunctionBegin;
6551502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65520918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6553502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6554502a2867SDave May   PetscFunctionReturn(0);
6555502a2867SDave May }
6556502a2867SDave May 
6557531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6558531c7667SBarry Smith 
6559531c7667SBarry Smith /*
6560531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6561531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6562531c7667SBarry Smith */
6563531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6564531c7667SBarry Smith {
6565531c7667SBarry Smith   PetscErrorCode ierr;
6566531c7667SBarry Smith 
6567531c7667SBarry Smith   PetscFunctionBegin;
6568531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6569531c7667SBarry Smith     Vec x1local;
6570531c7667SBarry Smith     DM  dm;
6571531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6572531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6573531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6574531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6575531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6576531c7667SBarry Smith     x1   = x1local;
6577531c7667SBarry Smith   }
6578531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6579531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6580531c7667SBarry Smith     DM  dm;
6581531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6582531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6583531c7667SBarry Smith   }
6584531c7667SBarry Smith   PetscFunctionReturn(0);
6585531c7667SBarry Smith }
6586531c7667SBarry Smith 
6587531c7667SBarry Smith /*@
6588531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6589531c7667SBarry Smith 
6590531c7667SBarry Smith     Input Parameter:
6591531c7667SBarry Smith .    coloring - the MatFDColoring object
6592531c7667SBarry Smith 
659395452b02SPatrick Sanan     Developer Notes:
659495452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
6595531c7667SBarry Smith 
65961b266c99SBarry Smith     Level: advanced
65971b266c99SBarry Smith 
6598531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6599531c7667SBarry Smith @*/
6600531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6601531c7667SBarry Smith {
6602531c7667SBarry Smith   PetscFunctionBegin;
6603531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6604531c7667SBarry Smith   PetscFunctionReturn(0);
6605531c7667SBarry Smith }
6606*8320bc6fSPatrick Sanan 
6607*8320bc6fSPatrick Sanan /*@
6608*8320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
6609*8320bc6fSPatrick Sanan 
6610*8320bc6fSPatrick Sanan     Collective
6611*8320bc6fSPatrick Sanan 
6612*8320bc6fSPatrick Sanan     Input Parameters:
6613*8320bc6fSPatrick Sanan +    dm - the first DM
6614*8320bc6fSPatrick Sanan -    dm2 - the second DM
6615*8320bc6fSPatrick Sanan 
6616*8320bc6fSPatrick Sanan     Output Parameters:
6617*8320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
6618*8320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
6619*8320bc6fSPatrick Sanan 
6620*8320bc6fSPatrick Sanan     Notes:
6621*8320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
6622*8320bc6fSPatrick Sanan     of the same topology. This implies that the the section (field data) on one
6623*8320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
6624*8320bc6fSPatrick Sanan     Loosely speaking, compatibile DMs represent the same domain, with the same parallel
6625*8320bc6fSPatrick Sanan     decomposition, with different data.
6626*8320bc6fSPatrick Sanan 
6627*8320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
6628*8320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
6629*8320bc6fSPatrick Sanan 
6630*8320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
6631*8320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
6632*8320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
6633*8320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
6634*8320bc6fSPatrick Sanan 
6635*8320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
6636*8320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
6637*8320bc6fSPatrick Sanan .vb
6638*8320bc6fSPatrick Sanan   ...
6639*8320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
6640*8320bc6fSPatrick Sanan   if (set && compatible)  {
6641*8320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
6642*8320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
6643*8320bc6fSPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n);CHKERRQ(ierr);
6644*8320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
6645*8320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
6646*8320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
6647*8320bc6fSPatrick Sanan       }
6648*8320bc6fSPatrick Sanan     }
6649*8320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
6650*8320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
6651*8320bc6fSPatrick Sanan   } else {
6652*8320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
6653*8320bc6fSPatrick Sanan   }
6654*8320bc6fSPatrick Sanan   ...
6655*8320bc6fSPatrick Sanan .ve
6656*8320bc6fSPatrick Sanan 
6657*8320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
6658*8320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
6659*8320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
6660*8320bc6fSPatrick Sanan     always check the "set" output parameter.
6661*8320bc6fSPatrick Sanan 
6662*8320bc6fSPatrick Sanan     A DM is always compatible with itself.
6663*8320bc6fSPatrick Sanan 
6664*8320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
6665*8320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
6666*8320bc6fSPatrick Sanan     incompatible.
6667*8320bc6fSPatrick Sanan 
6668*8320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
6669*8320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
6670*8320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
6671*8320bc6fSPatrick Sanan 
6672*8320bc6fSPatrick Sanan     Developer Notes:
6673*8320bc6fSPatrick Sanan     Compatibility is assumed to be a symmetric concept; if DM A is compatible with DM B,
6674*8320bc6fSPatrick Sanan     the DM B is compatible with DM A. Thus, this function checks the implementations
6675*8320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
6676*8320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
6677*8320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
6678*8320bc6fSPatrick Sanan     logic for this function, to check for existing logic in the implementation
6679*8320bc6fSPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found; the logic of this
6680*8320bc6fSPatrick Sanan     function will then call that logic.
6681*8320bc6fSPatrick Sanan 
6682*8320bc6fSPatrick Sanan     Level: advanced
6683*8320bc6fSPatrick Sanan 
6684*8320bc6fSPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA()
6685*8320bc6fSPatrick Sanan @*/
6686*8320bc6fSPatrick Sanan 
6687*8320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
6688*8320bc6fSPatrick Sanan {
6689*8320bc6fSPatrick Sanan   PetscErrorCode ierr;
6690*8320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
6691*8320bc6fSPatrick Sanan   DMType         type,type2;
6692*8320bc6fSPatrick Sanan   PetscBool      sameType;
6693*8320bc6fSPatrick Sanan 
6694*8320bc6fSPatrick Sanan   PetscFunctionBegin;
6695*8320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6696*8320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
6697*8320bc6fSPatrick Sanan 
6698*8320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
6699*8320bc6fSPatrick Sanan   if (dm == dm2) {
6700*8320bc6fSPatrick Sanan     *set = PETSC_TRUE;
6701*8320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
6702*8320bc6fSPatrick Sanan     PetscFunctionReturn(0);
6703*8320bc6fSPatrick Sanan   }
6704*8320bc6fSPatrick Sanan 
6705*8320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
6706*8320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
6707*8320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
6708*8320bc6fSPatrick Sanan      determined by the implementation-specific logic */
6709*8320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
6710*8320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
6711*8320bc6fSPatrick Sanan     *set = PETSC_TRUE;
6712*8320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
6713*8320bc6fSPatrick Sanan     PetscFunctionReturn(0);
6714*8320bc6fSPatrick Sanan   }
6715*8320bc6fSPatrick Sanan 
6716*8320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
6717*8320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
6718*8320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
6719*8320bc6fSPatrick Sanan     if (*set) {
6720*8320bc6fSPatrick Sanan       PetscFunctionReturn(0);
6721*8320bc6fSPatrick Sanan     }
6722*8320bc6fSPatrick Sanan   }
6723*8320bc6fSPatrick Sanan 
6724*8320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
6725*8320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
6726*8320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
6727*8320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
6728*8320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
6729*8320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
6730*8320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
6731*8320bc6fSPatrick Sanan   } else {
6732*8320bc6fSPatrick Sanan     *set = PETSC_FALSE;
6733*8320bc6fSPatrick Sanan   }
6734*8320bc6fSPatrick Sanan   PetscFunctionReturn(0);
6735*8320bc6fSPatrick Sanan }
6736