xref: /petsc/src/dm/interface/dm.c (revision e87a4003e3e772bdbe5118ac0e5c086db060906d)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
50c312b8eSJed Brown #include <petscsf.h>
62764a2aaSMatthew G. Knepley #include <petscds.h>
747c6ae99SBarry Smith 
8732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
91ac00216SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction;
1067a56275SMatthew G Knepley 
11bff4a2f0SMatthew G. Knepley const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DM_BOUNDARY_",0};
12bff4a2f0SMatthew G. Knepley 
134a7a4c06SLawrence Mitchell static PetscErrorCode DMHasCreateInjection_Default(DM dm, PetscBool *flg)
144a7a4c06SLawrence Mitchell {
154a7a4c06SLawrence Mitchell   PetscFunctionBegin;
164a7a4c06SLawrence Mitchell   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
174a7a4c06SLawrence Mitchell   PetscValidPointer(flg,2);
184a7a4c06SLawrence Mitchell   *flg = PETSC_FALSE;
194a7a4c06SLawrence Mitchell   PetscFunctionReturn(0);
204a7a4c06SLawrence Mitchell }
214a7a4c06SLawrence Mitchell 
22a4121054SBarry Smith /*@
23de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
24a4121054SBarry Smith 
25a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
26a4121054SBarry Smith    error when you try to use the vector.
27a4121054SBarry Smith 
28a4121054SBarry Smith   Collective on MPI_Comm
29a4121054SBarry Smith 
30a4121054SBarry Smith   Input Parameter:
31a4121054SBarry Smith . comm - The communicator for the DM object
32a4121054SBarry Smith 
33a4121054SBarry Smith   Output Parameter:
34a4121054SBarry Smith . dm - The DM object
35a4121054SBarry Smith 
36a4121054SBarry Smith   Level: beginner
37a4121054SBarry Smith 
388472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
39a4121054SBarry Smith @*/
407087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
41a4121054SBarry Smith {
42a4121054SBarry Smith   DM             v;
43a4121054SBarry Smith   PetscErrorCode ierr;
44a4121054SBarry Smith 
45a4121054SBarry Smith   PetscFunctionBegin;
461411c6eeSJed Brown   PetscValidPointer(dm,2);
470298fd71SBarry Smith   *dm = NULL;
488b984314SMatthew G. Knepley   ierr = PetscSysInitializePackage();CHKERRQ(ierr);
49607a6623SBarry Smith   ierr = VecInitializePackage();CHKERRQ(ierr);
50607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
51607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
52a4121054SBarry Smith 
5373107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
54e7c4fc90SDmitry Karpeev 
550298fd71SBarry Smith   v->ltogmap                  = NULL;
561411c6eeSJed Brown   v->bs                       = 1;
57171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5888ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
5988ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->defaultSF);CHKERRQ(ierr);
60c58f1c22SToby Isaac   v->labels                   = NULL;
61c58f1c22SToby Isaac   v->depthLabel               = NULL;
620298fd71SBarry Smith   v->defaultSection           = NULL;
630298fd71SBarry Smith   v->defaultGlobalSection     = NULL;
64fba222abSToby Isaac   v->defaultConstraintSection = NULL;
65fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
66c6b900c6SMatthew G. Knepley   v->L                        = NULL;
67c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
685dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
699a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
70435a35e8SMatthew G Knepley   {
71435a35e8SMatthew G Knepley     PetscInt i;
72435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
730298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
74435a35e8SMatthew G Knepley     }
75435a35e8SMatthew G Knepley   }
762764a2aaSMatthew G. Knepley   ierr = PetscDSCreate(comm, &v->prob);CHKERRQ(ierr);
7714f150ffSMatthew G. Knepley   v->dmBC = NULL;
78a8fb8f29SToby Isaac   v->coarseMesh = NULL;
79f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
80cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
81c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
82b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
83bcc5ffd9SToby Isaac   ierr = PetscNew(&(v->labels));CHKERRQ(ierr);
84c58f1c22SToby Isaac   v->labels->refct = 1;
854a7a4c06SLawrence Mitchell 
864a7a4c06SLawrence Mitchell   v->ops->hascreateinjection = DMHasCreateInjection_Default;
874a7a4c06SLawrence Mitchell 
881411c6eeSJed Brown   *dm = v;
89a4121054SBarry Smith   PetscFunctionReturn(0);
90a4121054SBarry Smith }
91a4121054SBarry Smith 
9238221697SMatthew G. Knepley /*@
9338221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9438221697SMatthew G. Knepley 
9538221697SMatthew G. Knepley   Collective on MPI_Comm
9638221697SMatthew G. Knepley 
9738221697SMatthew G. Knepley   Input Parameter:
9838221697SMatthew G. Knepley . dm - The original DM object
9938221697SMatthew G. Knepley 
10038221697SMatthew G. Knepley   Output Parameter:
10138221697SMatthew G. Knepley . newdm  - The new DM object
10238221697SMatthew G. Knepley 
10338221697SMatthew G. Knepley   Level: beginner
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley .keywords: DM, topology, create
10638221697SMatthew G. Knepley @*/
10738221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
10838221697SMatthew G. Knepley {
10938221697SMatthew G. Knepley   PetscSF        sf;
11038221697SMatthew G. Knepley   Vec            coords;
11138221697SMatthew G. Knepley   void          *ctx;
112a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11338221697SMatthew G. Knepley   PetscErrorCode ierr;
11438221697SMatthew G. Knepley 
11538221697SMatthew G. Knepley   PetscFunctionBegin;
11638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
11838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
119c58f1c22SToby Isaac   ierr = PetscFree((*newdm)->labels);CHKERRQ(ierr);
120c58f1c22SToby Isaac   dm->labels->refct++;
121c58f1c22SToby Isaac   (*newdm)->labels = dm->labels;
122c58f1c22SToby Isaac   (*newdm)->depthLabel = dm->depthLabel;
1231de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1241de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
12538221697SMatthew G. Knepley   if (dm->ops->clone) {
12638221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
12738221697SMatthew G. Knepley   }
1283f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
12938221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13038221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13138221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
13238221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
133be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
134be4c1c3eSMatthew G. Knepley     DM           ncdm;
135be4c1c3eSMatthew G. Knepley     PetscSection cs;
1365a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
137be4c1c3eSMatthew G. Knepley 
138*e87a4003SBarry Smith     ierr = DMGetSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
139be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1405a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1415a0206caSToby Isaac     if (pEndMax >= 0) {
142be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
143*e87a4003SBarry Smith       ierr = DMSetSection(ncdm, cs);CHKERRQ(ierr);
144a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
145be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
146be4c1c3eSMatthew G. Knepley     }
147be4c1c3eSMatthew G. Knepley   }
148a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
149a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15038221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   if (coords) {
15238221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15338221697SMatthew G. Knepley   } else {
15438221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15538221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15638221697SMatthew G. Knepley   }
15790b157c4SStefano Zampini   {
15890b157c4SStefano Zampini     PetscBool             isper;
159c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1605dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16190b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16290b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
163c6b900c6SMatthew G. Knepley   }
16438221697SMatthew G. Knepley   PetscFunctionReturn(0);
16538221697SMatthew G. Knepley }
16638221697SMatthew G. Knepley 
1679a42bb27SBarry Smith /*@C
168564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1699a42bb27SBarry Smith 
1708472ad0fSDave May    Logically Collective on DM
1719a42bb27SBarry Smith 
1729a42bb27SBarry Smith    Input Parameter:
1739a42bb27SBarry Smith +  da - initial distributed array
174e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1759a42bb27SBarry Smith 
1769a42bb27SBarry Smith    Options Database:
177dd85299cSBarry Smith .   -dm_vec_type ctype
1789a42bb27SBarry Smith 
1799a42bb27SBarry Smith    Level: intermediate
1809a42bb27SBarry Smith 
1818472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType()
1829a42bb27SBarry Smith @*/
18319fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1849a42bb27SBarry Smith {
1859a42bb27SBarry Smith   PetscErrorCode ierr;
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith   PetscFunctionBegin;
1889a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1899a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1919a42bb27SBarry Smith   PetscFunctionReturn(0);
1929a42bb27SBarry Smith }
1939a42bb27SBarry Smith 
194c0dedaeaSBarry Smith /*@C
195c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
196c0dedaeaSBarry Smith 
1978472ad0fSDave May    Logically Collective on DM
198c0dedaeaSBarry Smith 
199c0dedaeaSBarry Smith    Input Parameter:
200c0dedaeaSBarry Smith .  da - initial distributed array
201c0dedaeaSBarry Smith 
202c0dedaeaSBarry Smith    Output Parameter:
203c0dedaeaSBarry Smith .  ctype - the vector type
204c0dedaeaSBarry Smith 
205c0dedaeaSBarry Smith    Level: intermediate
206c0dedaeaSBarry Smith 
2078472ad0fSDave May .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType
208c0dedaeaSBarry Smith @*/
209c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
210c0dedaeaSBarry Smith {
211c0dedaeaSBarry Smith   PetscFunctionBegin;
212c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
213c0dedaeaSBarry Smith   *ctype = da->vectype;
214c0dedaeaSBarry Smith   PetscFunctionReturn(0);
215c0dedaeaSBarry Smith }
216c0dedaeaSBarry Smith 
2175f1ad066SMatthew G Knepley /*@
21834f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2195f1ad066SMatthew G Knepley 
2205f1ad066SMatthew G Knepley   Not collective
2215f1ad066SMatthew G Knepley 
2225f1ad066SMatthew G Knepley   Input Parameter:
2235f1ad066SMatthew G Knepley . v - The Vec
2245f1ad066SMatthew G Knepley 
2255f1ad066SMatthew G Knepley   Output Parameter:
2265f1ad066SMatthew G Knepley . dm - The DM
2275f1ad066SMatthew G Knepley 
2285f1ad066SMatthew G Knepley   Level: intermediate
2295f1ad066SMatthew G Knepley 
2305f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2315f1ad066SMatthew G Knepley @*/
2325f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2335f1ad066SMatthew G Knepley {
2345f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   PetscFunctionBegin;
2375f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2385f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2395f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2405f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2415f1ad066SMatthew G Knepley }
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley /*@
244d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley   Not collective
2475f1ad066SMatthew G Knepley 
2485f1ad066SMatthew G Knepley   Input Parameters:
2495f1ad066SMatthew G Knepley + v - The Vec
2505f1ad066SMatthew G Knepley - dm - The DM
2515f1ad066SMatthew G Knepley 
252d9805387SMatthew 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.
253d9805387SMatthew G. Knepley 
2545f1ad066SMatthew G Knepley   Level: intermediate
2555f1ad066SMatthew G Knepley 
2565f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2575f1ad066SMatthew G Knepley @*/
2585f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2595f1ad066SMatthew G Knepley {
2605f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2615f1ad066SMatthew G Knepley 
2625f1ad066SMatthew G Knepley   PetscFunctionBegin;
2635f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
264d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2655f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2665f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2675f1ad066SMatthew G Knepley }
2685f1ad066SMatthew G Knepley 
269521d9a4cSLisandro Dalcin /*@C
2708f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2718f1509bcSBarry Smith 
2728f1509bcSBarry Smith    Logically Collective on DM
2738f1509bcSBarry Smith 
2748f1509bcSBarry Smith    Input Parameters:
2758f1509bcSBarry Smith +  dm - the DM context
2768f1509bcSBarry Smith -  ctype - the matrix type
2778f1509bcSBarry Smith 
2788f1509bcSBarry Smith    Options Database:
2798f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2808f1509bcSBarry Smith 
2818f1509bcSBarry Smith    Level: intermediate
2828f1509bcSBarry Smith 
2838f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2848f1509bcSBarry Smith           DMGetISColoringType()
2858f1509bcSBarry Smith @*/
2868f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2878f1509bcSBarry Smith {
2888f1509bcSBarry Smith   PetscErrorCode ierr;
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   PetscErrorCode ierr;
3188f1509bcSBarry Smith 
3198f1509bcSBarry Smith   PetscFunctionBegin;
3208f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3218f1509bcSBarry Smith   *ctype = dm->coloringtype;
3228f1509bcSBarry Smith   PetscFunctionReturn(0);
3238f1509bcSBarry Smith }
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith /*@C
3268f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3278f1509bcSBarry Smith 
3288f1509bcSBarry Smith    Logically Collective on DM
3298f1509bcSBarry Smith 
3308f1509bcSBarry Smith    Input Parameters:
331521d9a4cSLisandro Dalcin +  dm - the DM context
332a2b5a043SBarry Smith -  ctype - the matrix type
333521d9a4cSLisandro Dalcin 
334521d9a4cSLisandro Dalcin    Options Database:
335521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
336521d9a4cSLisandro Dalcin 
337521d9a4cSLisandro Dalcin    Level: intermediate
338521d9a4cSLisandro Dalcin 
339c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType()
340521d9a4cSLisandro Dalcin @*/
34119fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
342521d9a4cSLisandro Dalcin {
343521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34488f0584fSBarry Smith 
345521d9a4cSLisandro Dalcin   PetscFunctionBegin;
346521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
347521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
34819fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
349521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
350521d9a4cSLisandro Dalcin }
351521d9a4cSLisandro Dalcin 
352c0dedaeaSBarry Smith /*@C
353c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
354c0dedaeaSBarry Smith 
355c0dedaeaSBarry Smith    Logically Collective on DM
356c0dedaeaSBarry Smith 
357c0dedaeaSBarry Smith    Input Parameter:
358c0dedaeaSBarry Smith .  dm - the DM context
359c0dedaeaSBarry Smith 
360c0dedaeaSBarry Smith    Output Parameter:
361c0dedaeaSBarry Smith .  ctype - the matrix type
362c0dedaeaSBarry Smith 
363c0dedaeaSBarry Smith    Options Database:
364c0dedaeaSBarry Smith .   -dm_mat_type ctype
365c0dedaeaSBarry Smith 
366c0dedaeaSBarry Smith    Level: intermediate
367c0dedaeaSBarry Smith 
368c0dedaeaSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType()
369c0dedaeaSBarry Smith @*/
370c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
371c0dedaeaSBarry Smith {
372c0dedaeaSBarry Smith   PetscFunctionBegin;
373c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
374c0dedaeaSBarry Smith   *ctype = dm->mattype;
375c0dedaeaSBarry Smith   PetscFunctionReturn(0);
376c0dedaeaSBarry Smith }
377c0dedaeaSBarry Smith 
378c688c046SMatthew G Knepley /*@
37934f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
380c688c046SMatthew G Knepley 
381c688c046SMatthew G Knepley   Not collective
382c688c046SMatthew G Knepley 
383c688c046SMatthew G Knepley   Input Parameter:
384c688c046SMatthew G Knepley . A - The Mat
385c688c046SMatthew G Knepley 
386c688c046SMatthew G Knepley   Output Parameter:
387c688c046SMatthew G Knepley . dm - The DM
388c688c046SMatthew G Knepley 
389c688c046SMatthew G Knepley   Level: intermediate
390c688c046SMatthew G Knepley 
3918f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3928f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3938f1509bcSBarry Smith 
394c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
395c688c046SMatthew G Knepley @*/
396c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
397c688c046SMatthew G Knepley {
398c688c046SMatthew G Knepley   PetscErrorCode ierr;
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   PetscFunctionBegin;
401c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
402c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
403c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
404c688c046SMatthew G Knepley   PetscFunctionReturn(0);
405c688c046SMatthew G Knepley }
406c688c046SMatthew G Knepley 
407c688c046SMatthew G Knepley /*@
408c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
409c688c046SMatthew G Knepley 
410c688c046SMatthew G Knepley   Not collective
411c688c046SMatthew G Knepley 
412c688c046SMatthew G Knepley   Input Parameters:
413c688c046SMatthew G Knepley + A - The Mat
414c688c046SMatthew G Knepley - dm - The DM
415c688c046SMatthew G Knepley 
416c688c046SMatthew G Knepley   Level: intermediate
417c688c046SMatthew G Knepley 
4188f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4198f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4208f1509bcSBarry Smith 
4218f1509bcSBarry Smith 
422c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
423c688c046SMatthew G Knepley @*/
424c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
425c688c046SMatthew G Knepley {
426c688c046SMatthew G Knepley   PetscErrorCode ierr;
427c688c046SMatthew G Knepley 
428c688c046SMatthew G Knepley   PetscFunctionBegin;
429c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4308865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
431c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
432c688c046SMatthew G Knepley   PetscFunctionReturn(0);
433c688c046SMatthew G Knepley }
434c688c046SMatthew G Knepley 
4359a42bb27SBarry Smith /*@C
4369a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4376757b960SDave May    DM options in the database.
4389a42bb27SBarry Smith 
4398353ddbbSDave May    Logically Collective on DM
4409a42bb27SBarry Smith 
4419a42bb27SBarry Smith    Input Parameter:
4428353ddbbSDave May +  da - the DM context
4439a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4449a42bb27SBarry Smith 
4459a42bb27SBarry Smith    Notes:
4469a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4479a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4489a42bb27SBarry Smith 
4499a42bb27SBarry Smith    Level: advanced
4509a42bb27SBarry Smith 
4518353ddbbSDave May .keywords: DM, set, options, prefix, database
4529a42bb27SBarry Smith 
4539a42bb27SBarry Smith .seealso: DMSetFromOptions()
4549a42bb27SBarry Smith @*/
4557087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4569a42bb27SBarry Smith {
4579a42bb27SBarry Smith   PetscErrorCode ierr;
4589a42bb27SBarry Smith 
4599a42bb27SBarry Smith   PetscFunctionBegin;
4609a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4619a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
462691be533SLawrence Mitchell   if (dm->sf) {
463691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
464691be533SLawrence Mitchell   }
465691be533SLawrence Mitchell   if (dm->defaultSF) {
466691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->defaultSF,prefix);CHKERRQ(ierr);
467691be533SLawrence Mitchell   }
4689a42bb27SBarry Smith   PetscFunctionReturn(0);
4699a42bb27SBarry Smith }
4709a42bb27SBarry Smith 
47131697293SDave May /*@C
47231697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47331697293SDave May    DM options in the database.
47431697293SDave May 
47531697293SDave May    Logically Collective on DM
47631697293SDave May 
47731697293SDave May    Input Parameters:
47831697293SDave May +  dm - the DM context
47931697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48031697293SDave May 
48131697293SDave May    Notes:
48231697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48331697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48431697293SDave May 
48531697293SDave May    Level: advanced
48631697293SDave May 
48731697293SDave May .keywords: DM, append, options, prefix, database
48831697293SDave May 
48931697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49031697293SDave May @*/
49131697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49231697293SDave May {
49331697293SDave May   PetscErrorCode ierr;
49431697293SDave May 
49531697293SDave May   PetscFunctionBegin;
49631697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49731697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49831697293SDave May   PetscFunctionReturn(0);
49931697293SDave May }
50031697293SDave May 
50131697293SDave May /*@C
50231697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50331697293SDave May    DM options in the database.
50431697293SDave May 
50531697293SDave May    Not Collective
50631697293SDave May 
50731697293SDave May    Input Parameters:
50831697293SDave May .  dm - the DM context
50931697293SDave May 
51031697293SDave May    Output Parameters:
51131697293SDave May .  prefix - pointer to the prefix string used is returned
51231697293SDave May 
51331697293SDave May    Notes: On the fortran side, the user should pass in a string 'prefix' of
51431697293SDave May    sufficient length to hold the prefix.
51531697293SDave May 
51631697293SDave May    Level: advanced
51731697293SDave May 
51831697293SDave May .keywords: DM, set, options, prefix, database
51931697293SDave May 
52031697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52131697293SDave May @*/
52231697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52331697293SDave May {
52431697293SDave May   PetscErrorCode ierr;
52531697293SDave May 
52631697293SDave May   PetscFunctionBegin;
52731697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52831697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52931697293SDave May   PetscFunctionReturn(0);
53031697293SDave May }
53131697293SDave May 
53288bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53388bdff64SToby Isaac {
53488bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53588bdff64SToby Isaac   DMNamedVecLink nlink;
53688bdff64SToby Isaac   PetscErrorCode ierr;
53788bdff64SToby Isaac 
53888bdff64SToby Isaac   PetscFunctionBegin;
539aab5bcd8SJed Brown   *ncrefct = 0;
54088bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
54188bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54288bdff64SToby Isaac     if (dm->localin[i])  refct--;
54388bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54488bdff64SToby Isaac   }
54588bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54688bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54788bdff64SToby Isaac   if (dm->x) {
54888bdff64SToby Isaac     DM obj;
54988bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
55088bdff64SToby Isaac     if (obj == dm) refct--;
55188bdff64SToby Isaac   }
55288bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55388bdff64SToby Isaac     refct--;
55488bdff64SToby Isaac     if (recurseCoarse) {
55588bdff64SToby Isaac       PetscInt coarseCount;
55688bdff64SToby Isaac 
55788bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55888bdff64SToby Isaac       refct += coarseCount;
55988bdff64SToby Isaac     }
56088bdff64SToby Isaac   }
56188bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56288bdff64SToby Isaac     refct--;
56388bdff64SToby Isaac     if (recurseFine) {
56488bdff64SToby Isaac       PetscInt fineCount;
56588bdff64SToby Isaac 
56688bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56788bdff64SToby Isaac       refct += fineCount;
56888bdff64SToby Isaac     }
56988bdff64SToby Isaac   }
57088bdff64SToby Isaac   *ncrefct = refct;
57188bdff64SToby Isaac   PetscFunctionReturn(0);
57288bdff64SToby Isaac }
57388bdff64SToby Isaac 
574354557abSToby Isaac PetscErrorCode DMDestroyLabelLinkList(DM dm)
575354557abSToby Isaac {
576354557abSToby Isaac   PetscErrorCode ierr;
577354557abSToby Isaac 
578354557abSToby Isaac   PetscFunctionBegin;
579354557abSToby Isaac   if (!--(dm->labels->refct)) {
580354557abSToby Isaac     DMLabelLink next = dm->labels->next;
581354557abSToby Isaac 
582354557abSToby Isaac     /* destroy the labels */
583354557abSToby Isaac     while (next) {
584354557abSToby Isaac       DMLabelLink tmp = next->next;
585354557abSToby Isaac 
586354557abSToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
587354557abSToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
588354557abSToby Isaac       next = tmp;
589354557abSToby Isaac     }
590354557abSToby Isaac     ierr = PetscFree(dm->labels);CHKERRQ(ierr);
591354557abSToby Isaac   }
592354557abSToby Isaac   PetscFunctionReturn(0);
593354557abSToby Isaac }
594354557abSToby Isaac 
59547c6ae99SBarry Smith /*@
5968472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59747c6ae99SBarry Smith 
59847c6ae99SBarry Smith     Collective on DM
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith     Input Parameter:
60147c6ae99SBarry Smith .   dm - the DM object to destroy
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith     Level: developer
60447c6ae99SBarry Smith 
605e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith @*/
608fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60947c6ae99SBarry Smith {
61088bdff64SToby Isaac   PetscInt       i, cnt;
611dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
61247c6ae99SBarry Smith   PetscErrorCode ierr;
61347c6ae99SBarry Smith 
61447c6ae99SBarry Smith   PetscFunctionBegin;
6156bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6166bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61787e657c6SBarry Smith 
61888bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61988bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
62088bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
62188bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
622732e2eb9SMatthew G Knepley   /*
623732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
624732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
625732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
626732e2eb9SMatthew G Knepley   */
6276bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6286bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
629732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6306bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6316bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
632732e2eb9SMatthew G Knepley   }
633f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6340298fd71SBarry Smith   (*dm)->namedglobal = NULL;
635f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6362348bcf4SPeter Brune     nnext = nlink->next;
6372348bcf4SPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
6382348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6392348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6402348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6412348bcf4SPeter Brune   }
642f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6430298fd71SBarry Smith   (*dm)->namedlocal = NULL;
644f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
645f490541aSPeter Brune     nnext = nlink->next;
646f490541aSPeter Brune     if (nlink->status != DMVEC_STATUS_IN) SETERRQ1(((PetscObject)*dm)->comm,PETSC_ERR_ARG_WRONGSTATE,"DM still has Vec named '%s' checked out",nlink->name);
647f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
648f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
649f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
650f490541aSPeter Brune   }
6512348bcf4SPeter Brune 
652b17ce1afSJed Brown   /* Destroy the list of hooks */
653c833c3b5SJed Brown   {
654c833c3b5SJed Brown     DMCoarsenHookLink link,next;
655b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
656b17ce1afSJed Brown       next = link->next;
657b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
658b17ce1afSJed Brown     }
6590298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
660c833c3b5SJed Brown   }
661c833c3b5SJed Brown   {
662c833c3b5SJed Brown     DMRefineHookLink link,next;
663c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
664c833c3b5SJed Brown       next = link->next;
665c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
666c833c3b5SJed Brown     }
6670298fd71SBarry Smith     (*dm)->refinehook = NULL;
668c833c3b5SJed Brown   }
669be081cd6SPeter Brune   {
670be081cd6SPeter Brune     DMSubDomainHookLink link,next;
671be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
672be081cd6SPeter Brune       next = link->next;
673be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
674be081cd6SPeter Brune     }
6750298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
676be081cd6SPeter Brune   }
677baf369e7SPeter Brune   {
678baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
679baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
680baf369e7SPeter Brune       next = link->next;
681baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
682baf369e7SPeter Brune     }
6830298fd71SBarry Smith     (*dm)->gtolhook = NULL;
684baf369e7SPeter Brune   }
685d4d07f1eSToby Isaac   {
686d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
687d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
688d4d07f1eSToby Isaac       next = link->next;
689d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
690d4d07f1eSToby Isaac     }
691d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
692d4d07f1eSToby Isaac   }
693aa1993deSMatthew G Knepley   /* Destroy the work arrays */
694aa1993deSMatthew G Knepley   {
695aa1993deSMatthew G Knepley     DMWorkLink link,next;
696aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
697aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
698aa1993deSMatthew G Knepley       next = link->next;
699aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
700aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
701aa1993deSMatthew G Knepley     }
7020298fd71SBarry Smith     (*dm)->workin = NULL;
703aa1993deSMatthew G Knepley   }
704c58f1c22SToby Isaac   if (!--((*dm)->labels->refct)) {
705c58f1c22SToby Isaac     DMLabelLink next = (*dm)->labels->next;
706c58f1c22SToby Isaac 
707c58f1c22SToby Isaac     /* destroy the labels */
708c58f1c22SToby Isaac     while (next) {
709c58f1c22SToby Isaac       DMLabelLink tmp = next->next;
710c58f1c22SToby Isaac 
711c58f1c22SToby Isaac       ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
712c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
713c58f1c22SToby Isaac       next = tmp;
714c58f1c22SToby Isaac     }
715c58f1c22SToby Isaac     ierr = PetscFree((*dm)->labels);CHKERRQ(ierr);
716c58f1c22SToby Isaac   }
717e6f8dbb6SToby Isaac   {
718e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
719e6f8dbb6SToby Isaac     while (next) {
720e6f8dbb6SToby Isaac       DMBoundary b = next;
721e6f8dbb6SToby Isaac 
722e6f8dbb6SToby Isaac       next = b->next;
723e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
724e6f8dbb6SToby Isaac     }
725e6f8dbb6SToby Isaac   }
726b17ce1afSJed Brown 
72752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
72852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
72952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
73052536dc3SBarry Smith 
7311a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7321a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7331a266240SBarry Smith   }
73487e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
73571cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7364dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7376bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7386bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
739073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
74088ed4aceSMatthew G Knepley 
74188ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultSection);CHKERRQ(ierr);
74288ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&(*dm)->defaultGlobalSection);CHKERRQ(ierr);
7438b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
744fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
745fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
74688ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
74788ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->defaultSF);CHKERRQ(ierr);
748736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
749736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7508e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
751736995cdSBlaise Bourdin     }
752736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
753736995cdSBlaise Bourdin   }
75488bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
75588bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
75688bdff64SToby Isaac   }
757a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
75888bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
75988bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
76088bdff64SToby Isaac   }
76188bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
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 
992*e87a4003SBarry Smith     ierr = DMGetSection(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 
998*e87a4003SBarry Smith       ierr = DMGetGlobalSection(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 
108385afcc9aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
108485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1085d52bd9f3SBarry Smith 
10861f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1087d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
108885afcc9aSBarry Smith 
108985afcc9aSBarry Smith 
10903ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith @*/
1093e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
109447c6ae99SBarry Smith {
109547c6ae99SBarry Smith   PetscErrorCode ierr;
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith   PetscFunctionBegin;
1098171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1099171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1100cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110125296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1102cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110347c6ae99SBarry Smith   PetscFunctionReturn(0);
110447c6ae99SBarry Smith }
110547c6ae99SBarry Smith 
11063ad4599aSBarry Smith /*@
11073ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11083ad4599aSBarry Smith 
11093ad4599aSBarry Smith     Collective on DM
11103ad4599aSBarry Smith 
11113ad4599aSBarry Smith     Input Parameter:
11123ad4599aSBarry Smith +   dm1 - the DM object
11133ad4599aSBarry Smith -   dm2 - the second, finer DM object
11143ad4599aSBarry Smith 
11153ad4599aSBarry Smith     Output Parameter:
11163ad4599aSBarry Smith .  mat - the restriction
11173ad4599aSBarry Smith 
11183ad4599aSBarry Smith 
11193ad4599aSBarry Smith     Level: developer
11203ad4599aSBarry Smith 
11213ad4599aSBarry Smith     Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
11223ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11233ad4599aSBarry Smith 
11243ad4599aSBarry Smith 
11253ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11263ad4599aSBarry Smith 
11273ad4599aSBarry Smith @*/
11283ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11293ad4599aSBarry Smith {
11303ad4599aSBarry Smith   PetscErrorCode ierr;
11313ad4599aSBarry Smith 
11323ad4599aSBarry Smith   PetscFunctionBegin;
11333ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11343ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11353ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11367294143fSBarry Smith   if (!dm1->ops->createrestriction) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateRestriction not implemented for this type");
11373ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11383ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11393ad4599aSBarry Smith   PetscFunctionReturn(0);
11403ad4599aSBarry Smith }
11413ad4599aSBarry Smith 
114247c6ae99SBarry Smith /*@
11438472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114447c6ae99SBarry Smith 
114547c6ae99SBarry Smith     Collective on DM
114647c6ae99SBarry Smith 
114747c6ae99SBarry Smith     Input Parameter:
114847c6ae99SBarry Smith +   dm1 - the DM object
114947c6ae99SBarry Smith -   dm2 - the second, finer DM object
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Output Parameter:
11526dbf9973SLawrence Mitchell .   mat - the injection
115347c6ae99SBarry Smith 
115447c6ae99SBarry Smith     Level: developer
115547c6ae99SBarry Smith 
115685afcc9aSBarry Smith    Notes:  For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
115785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
115885afcc9aSBarry Smith 
1159e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
116047c6ae99SBarry Smith 
116147c6ae99SBarry Smith @*/
11626dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116347c6ae99SBarry Smith {
116447c6ae99SBarry Smith   PetscErrorCode ierr;
116547c6ae99SBarry Smith 
116647c6ae99SBarry Smith   PetscFunctionBegin;
1167171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1168171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11690c4c9125SBarry Smith   if (!dm1->ops->getinjection) SETERRQ(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DMCreateInjection not implemented for this type");
11706dbf9973SLawrence Mitchell   ierr = (*dm1->ops->getinjection)(dm1,dm2,mat);CHKERRQ(ierr);
117147c6ae99SBarry Smith   PetscFunctionReturn(0);
117247c6ae99SBarry Smith }
117347c6ae99SBarry Smith 
1174b412c318SBarry Smith /*@
1175bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1176bd041c0cSMatthew G. Knepley 
1177bd041c0cSMatthew G. Knepley   Collective on DM
1178bd041c0cSMatthew G. Knepley 
1179bd041c0cSMatthew G. Knepley   Input Parameter:
1180bd041c0cSMatthew G. Knepley + dm1 - the DM object
1181bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1182bd041c0cSMatthew G. Knepley 
1183bd041c0cSMatthew G. Knepley   Output Parameter:
1184bd041c0cSMatthew G. Knepley . mat - the interpolation
1185bd041c0cSMatthew G. Knepley 
1186bd041c0cSMatthew G. Knepley   Level: developer
1187bd041c0cSMatthew G. Knepley 
1188bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1189bd041c0cSMatthew G. Knepley @*/
1190bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1191bd041c0cSMatthew G. Knepley {
1192bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1193bd041c0cSMatthew G. Knepley 
1194bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1195bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1196bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
1197bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1198bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1199bd041c0cSMatthew G. Knepley }
1200bd041c0cSMatthew G. Knepley 
1201bd041c0cSMatthew G. Knepley /*@
1202b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
120347c6ae99SBarry Smith 
120447c6ae99SBarry Smith     Collective on DM
120547c6ae99SBarry Smith 
120647c6ae99SBarry Smith     Input Parameter:
120747c6ae99SBarry Smith +   dm - the DM object
12085bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
120947c6ae99SBarry Smith 
121047c6ae99SBarry Smith     Output Parameter:
121147c6ae99SBarry Smith .   coloring - the coloring
121247c6ae99SBarry Smith 
121347c6ae99SBarry Smith     Level: developer
121447c6ae99SBarry Smith 
1215b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
121647c6ae99SBarry Smith 
1217aab9d709SJed Brown @*/
1218b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
121947c6ae99SBarry Smith {
122047c6ae99SBarry Smith   PetscErrorCode ierr;
122147c6ae99SBarry Smith 
122247c6ae99SBarry Smith   PetscFunctionBegin;
1223171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1224ce94432eSBarry Smith   if (!dm->ops->getcoloring) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No coloring for this type of DM yet");
1225b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
122647c6ae99SBarry Smith   PetscFunctionReturn(0);
122747c6ae99SBarry Smith }
122847c6ae99SBarry Smith 
1229b412c318SBarry Smith /*@
12308472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
123147c6ae99SBarry Smith 
123247c6ae99SBarry Smith     Collective on DM
123347c6ae99SBarry Smith 
123447c6ae99SBarry Smith     Input Parameter:
1235b412c318SBarry Smith .   dm - the DM object
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith     Output Parameter:
123847c6ae99SBarry Smith .   mat - the empty Jacobian
123947c6ae99SBarry Smith 
1240073dac72SJed Brown     Level: beginner
124147c6ae99SBarry Smith 
124294013140SBarry Smith     Notes: This properly preallocates the number of nonzeros in the sparse matrix so you
124394013140SBarry Smith        do not need to do it yourself.
124494013140SBarry Smith 
124594013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12467889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
124794013140SBarry Smith 
124894013140SBarry 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
124994013140SBarry Smith        internally by PETSc.
125094013140SBarry Smith 
125194013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1252aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
125394013140SBarry Smith 
1254b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
125547c6ae99SBarry Smith 
1256aab9d709SJed Brown @*/
1257b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
125847c6ae99SBarry Smith {
125947c6ae99SBarry Smith   PetscErrorCode ierr;
126047c6ae99SBarry Smith 
126147c6ae99SBarry Smith   PetscFunctionBegin;
1262171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1263607a6623SBarry Smith   ierr = MatInitializePackage();CHKERRQ(ierr);
1264c7b7c8a4SJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1265c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1266b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
126747c6ae99SBarry Smith   PetscFunctionReturn(0);
126847c6ae99SBarry Smith }
126947c6ae99SBarry Smith 
1270732e2eb9SMatthew G Knepley /*@
1271950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1272732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1273732e2eb9SMatthew G Knepley 
12748472ad0fSDave May   Logically Collective on DM
1275732e2eb9SMatthew G Knepley 
1276732e2eb9SMatthew G Knepley   Input Parameter:
1277732e2eb9SMatthew G Knepley + dm - the DM
1278732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1279732e2eb9SMatthew G Knepley 
1280732e2eb9SMatthew G Knepley   Level: developer
1281b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1282732e2eb9SMatthew G Knepley @*/
1283732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1284732e2eb9SMatthew G Knepley {
1285732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1286732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1287732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1288732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1289732e2eb9SMatthew G Knepley }
1290732e2eb9SMatthew G Knepley 
1291b06ff27eSHong Zhang /*@
1292b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1293b06ff27eSHong Zhang     but the array for values will not be allocated.
1294b06ff27eSHong Zhang 
1295b06ff27eSHong Zhang   Logically Collective on DM
1296b06ff27eSHong Zhang 
1297b06ff27eSHong Zhang   Input Parameter:
1298b06ff27eSHong Zhang + dm - the DM
1299b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1300b06ff27eSHong Zhang 
1301b06ff27eSHong Zhang   Level: developer
1302b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1303b06ff27eSHong Zhang @*/
1304b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1305b06ff27eSHong Zhang {
1306b06ff27eSHong Zhang   PetscFunctionBegin;
1307b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1308b06ff27eSHong Zhang   dm->structure_only = only;
1309b06ff27eSHong Zhang   PetscFunctionReturn(0);
1310b06ff27eSHong Zhang }
1311b06ff27eSHong Zhang 
1312a89ea682SMatthew G Knepley /*@C
1313aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1314a89ea682SMatthew G Knepley 
1315a89ea682SMatthew G Knepley   Not Collective
1316a89ea682SMatthew G Knepley 
1317a89ea682SMatthew G Knepley   Input Parameters:
1318a89ea682SMatthew G Knepley + dm - the DM object
1319aa1993deSMatthew G Knepley . count - The minium size
132069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1321a89ea682SMatthew G Knepley 
1322a89ea682SMatthew G Knepley   Output Parameter:
1323a89ea682SMatthew G Knepley . array - the work array
1324a89ea682SMatthew G Knepley 
1325a89ea682SMatthew G Knepley   Level: developer
1326a89ea682SMatthew G Knepley 
1327a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1328a89ea682SMatthew G Knepley @*/
132969291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1330a89ea682SMatthew G Knepley {
1331a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1332aa1993deSMatthew G Knepley   DMWorkLink     link;
133369291d52SBarry Smith   PetscMPIInt    dsize;
1334a89ea682SMatthew G Knepley 
1335a89ea682SMatthew G Knepley   PetscFunctionBegin;
1336a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1337aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1338aa1993deSMatthew G Knepley   if (dm->workin) {
1339aa1993deSMatthew G Knepley     link       = dm->workin;
1340aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1341aa1993deSMatthew G Knepley   } else {
1342b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1343a89ea682SMatthew G Knepley   }
134469291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13455056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1346aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1347854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1348854ce69bSBarry Smith     link->bytes = dsize*count;
1349aa1993deSMatthew G Knepley   }
1350aa1993deSMatthew G Knepley   link->next   = dm->workout;
1351aa1993deSMatthew G Knepley   dm->workout  = link;
1352aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1353a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1354a89ea682SMatthew G Knepley }
1355a89ea682SMatthew G Knepley 
1356aa1993deSMatthew G Knepley /*@C
1357aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1358aa1993deSMatthew G Knepley 
1359aa1993deSMatthew G Knepley   Not Collective
1360aa1993deSMatthew G Knepley 
1361aa1993deSMatthew G Knepley   Input Parameters:
1362aa1993deSMatthew G Knepley + dm - the DM object
1363aa1993deSMatthew G Knepley . count - The minium size
136469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1365aa1993deSMatthew G Knepley 
1366aa1993deSMatthew G Knepley   Output Parameter:
1367aa1993deSMatthew G Knepley . array - the work array
1368aa1993deSMatthew G Knepley 
1369aa1993deSMatthew G Knepley   Level: developer
1370aa1993deSMatthew G Knepley 
137169291d52SBarry Smith   Developer Notes: count and dtype are ignored, they are only needed for DMGetWorkArray()
1372aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1373aa1993deSMatthew G Knepley @*/
137469291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1375aa1993deSMatthew G Knepley {
1376aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1377aa1993deSMatthew G Knepley 
1378aa1993deSMatthew G Knepley   PetscFunctionBegin;
1379aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1380aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1381aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1382aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1383aa1993deSMatthew G Knepley       *p           = link->next;
1384aa1993deSMatthew G Knepley       link->next   = dm->workin;
1385aa1993deSMatthew G Knepley       dm->workin   = link;
13860298fd71SBarry Smith       *(void**)mem = NULL;
1387aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1388aa1993deSMatthew G Knepley     }
1389aa1993deSMatthew G Knepley   }
1390aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1391aa1993deSMatthew G Knepley }
1392e7c4fc90SDmitry Karpeev 
1393435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1394435a35e8SMatthew G Knepley {
1395435a35e8SMatthew G Knepley   PetscFunctionBegin;
1396435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
139782f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1398435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1399435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1400435a35e8SMatthew G Knepley }
1401435a35e8SMatthew G Knepley 
14024f3b5142SJed Brown /*@C
14034d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14044d343eeaSMatthew G Knepley 
14054d343eeaSMatthew G Knepley   Not collective
14064d343eeaSMatthew G Knepley 
14074d343eeaSMatthew G Knepley   Input Parameter:
14084d343eeaSMatthew G Knepley . dm - the DM object
14094d343eeaSMatthew G Knepley 
14104d343eeaSMatthew G Knepley   Output Parameters:
14110298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14120298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14130298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14144d343eeaSMatthew G Knepley 
14154d343eeaSMatthew G Knepley   Level: intermediate
14164d343eeaSMatthew G Knepley 
141721c9b008SJed Brown   Notes:
141821c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
141921c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
142021c9b008SJed Brown   PetscFree().
142121c9b008SJed Brown 
14224d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14234d343eeaSMatthew G Knepley @*/
142437d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14254d343eeaSMatthew G Knepley {
142637d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14274d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14284d343eeaSMatthew G Knepley 
14294d343eeaSMatthew G Knepley   PetscFunctionBegin;
14304d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
143169ca1f37SDmitry Karpeev   if (numFields) {
143269ca1f37SDmitry Karpeev     PetscValidPointer(numFields,2);
143369ca1f37SDmitry Karpeev     *numFields = 0;
143469ca1f37SDmitry Karpeev   }
143537d0c07bSMatthew G Knepley   if (fieldNames) {
143637d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
14370298fd71SBarry Smith     *fieldNames = NULL;
143869ca1f37SDmitry Karpeev   }
143969ca1f37SDmitry Karpeev   if (fields) {
144069ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
14410298fd71SBarry Smith     *fields = NULL;
144269ca1f37SDmitry Karpeev   }
1443*e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
144437d0c07bSMatthew G Knepley   if (section) {
144537d0c07bSMatthew G Knepley     PetscInt *fieldSizes, **fieldIndices;
144637d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
144737d0c07bSMatthew G Knepley 
1448*e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
144937d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
1450dcca6d9dSJed Brown     ierr = PetscMalloc2(nF,&fieldSizes,nF,&fieldIndices);CHKERRQ(ierr);
145137d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
145237d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
145337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
145437d0c07bSMatthew G Knepley     }
145537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
145637d0c07bSMatthew G Knepley       PetscInt gdof;
145737d0c07bSMatthew G Knepley 
145837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
145937d0c07bSMatthew G Knepley       if (gdof > 0) {
146037d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
146137d0c07bSMatthew G Knepley           PetscInt fdof, fcdof;
146237d0c07bSMatthew G Knepley 
146337d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
146437d0c07bSMatthew G Knepley           ierr           = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
146537d0c07bSMatthew G Knepley           fieldSizes[f] += fdof-fcdof;
146637d0c07bSMatthew G Knepley         }
146737d0c07bSMatthew G Knepley       }
146837d0c07bSMatthew G Knepley     }
146937d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1470785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
147137d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
147237d0c07bSMatthew G Knepley     }
147337d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
147437d0c07bSMatthew G Knepley       PetscInt gdof, goff;
147537d0c07bSMatthew G Knepley 
147637d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
147737d0c07bSMatthew G Knepley       if (gdof > 0) {
147837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
147937d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
148037d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
148137d0c07bSMatthew G Knepley 
148237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
148337d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
148437d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
148537d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
148637d0c07bSMatthew G Knepley           }
148737d0c07bSMatthew G Knepley         }
148837d0c07bSMatthew G Knepley       }
148937d0c07bSMatthew G Knepley     }
14908865f1eaSKarl Rupp     if (numFields) *numFields = nF;
149137d0c07bSMatthew G Knepley     if (fieldNames) {
1492785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
149337d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
149437d0c07bSMatthew G Knepley         const char *fieldName;
149537d0c07bSMatthew G Knepley 
149637d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
149737d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
149837d0c07bSMatthew G Knepley       }
149937d0c07bSMatthew G Knepley     }
150037d0c07bSMatthew G Knepley     if (fields) {
1501785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
150237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
150382f516ccSBarry Smith         ierr = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
150437d0c07bSMatthew G Knepley       }
150537d0c07bSMatthew G Knepley     }
150637d0c07bSMatthew G Knepley     ierr = PetscFree2(fieldSizes,fieldIndices);CHKERRQ(ierr);
15078865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15088865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
150969ca1f37SDmitry Karpeev   }
15104d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15114d343eeaSMatthew G Knepley }
15124d343eeaSMatthew G Knepley 
151316621825SDmitry Karpeev 
151416621825SDmitry Karpeev /*@C
151516621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
151616621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
151716621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1518e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1519e7c4fc90SDmitry Karpeev 
1520e7c4fc90SDmitry Karpeev   Not collective
1521e7c4fc90SDmitry Karpeev 
1522e7c4fc90SDmitry Karpeev   Input Parameter:
1523e7c4fc90SDmitry Karpeev . dm - the DM object
1524e7c4fc90SDmitry Karpeev 
1525e7c4fc90SDmitry Karpeev   Output Parameters:
15260298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
15270298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
15280298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
15290298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1530e7c4fc90SDmitry Karpeev 
1531e7c4fc90SDmitry Karpeev   Level: intermediate
1532e7c4fc90SDmitry Karpeev 
1533e7c4fc90SDmitry Karpeev   Notes:
1534e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1535e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1536e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1537e7c4fc90SDmitry Karpeev 
1538e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1539e7c4fc90SDmitry Karpeev @*/
154016621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1541e7c4fc90SDmitry Karpeev {
1542e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1543e7c4fc90SDmitry Karpeev 
1544e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1545e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
15468865f1eaSKarl Rupp   if (len) {
15478865f1eaSKarl Rupp     PetscValidPointer(len,2);
15488865f1eaSKarl Rupp     *len = 0;
15498865f1eaSKarl Rupp   }
15508865f1eaSKarl Rupp   if (namelist) {
15518865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
15528865f1eaSKarl Rupp     *namelist = 0;
15538865f1eaSKarl Rupp   }
15548865f1eaSKarl Rupp   if (islist) {
15558865f1eaSKarl Rupp     PetscValidPointer(islist,4);
15568865f1eaSKarl Rupp     *islist = 0;
15578865f1eaSKarl Rupp   }
15588865f1eaSKarl Rupp   if (dmlist) {
15598865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
15608865f1eaSKarl Rupp     *dmlist = 0;
15618865f1eaSKarl Rupp   }
1562f3f0edfdSDmitry Karpeev   /*
1563f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1564f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1565f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1566f3f0edfdSDmitry Karpeev    */
1567ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
156816621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1569435a35e8SMatthew G Knepley     PetscSection section;
1570435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1571435a35e8SMatthew G Knepley 
1572*e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
1573435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1574435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1575f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
157603dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
157703dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
157803dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1579435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1580435a35e8SMatthew G Knepley         const char *fieldName;
1581435a35e8SMatthew G Knepley 
158203dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
158303dc3394SMatthew G. Knepley         if (namelist) {
1584435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1585435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1586435a35e8SMatthew G Knepley         }
158703dc3394SMatthew G. Knepley       }
1588435a35e8SMatthew G Knepley     } else {
158969ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1590e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
15910298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1592e7c4fc90SDmitry Karpeev     }
15938865f1eaSKarl Rupp   } else {
159416621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
159516621825SDmitry Karpeev   }
159616621825SDmitry Karpeev   PetscFunctionReturn(0);
159716621825SDmitry Karpeev }
159816621825SDmitry Karpeev 
1599564cec59SMatthew G. Knepley /*@
1600435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1601435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1602435a35e8SMatthew G Knepley 
1603435a35e8SMatthew G Knepley   Not collective
1604435a35e8SMatthew G Knepley 
1605435a35e8SMatthew G Knepley   Input Parameters:
16062adcc780SMatthew G. Knepley + dm        - The DM object
16072adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16082adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1609435a35e8SMatthew G Knepley 
1610435a35e8SMatthew G Knepley   Output Parameters:
16112adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16122adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1613435a35e8SMatthew G Knepley 
1614435a35e8SMatthew G Knepley   Level: intermediate
1615435a35e8SMatthew G Knepley 
1616435a35e8SMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1617435a35e8SMatthew G Knepley @*/
161837bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1619435a35e8SMatthew G Knepley {
1620435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1621435a35e8SMatthew G Knepley 
1622435a35e8SMatthew G Knepley   PetscFunctionBegin;
1623435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1624435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
16258865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
16268865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1627435a35e8SMatthew G Knepley   if (dm->ops->createsubdm) {
1628435a35e8SMatthew G Knepley     ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
162982f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateSubDM implementation defined");
1630435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1631435a35e8SMatthew G Knepley }
1632435a35e8SMatthew G Knepley 
16332adcc780SMatthew G. Knepley /*@C
16342adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
16352adcc780SMatthew G. Knepley 
16362adcc780SMatthew G. Knepley   Not collective
16372adcc780SMatthew G. Knepley 
16382adcc780SMatthew G. Knepley   Input Parameter:
16392adcc780SMatthew G. Knepley + dms - The DM objects
16402adcc780SMatthew G. Knepley - len - The number of DMs
16412adcc780SMatthew G. Knepley 
16422adcc780SMatthew G. Knepley   Output Parameters:
16432adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16442adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
16452adcc780SMatthew G. Knepley 
16462adcc780SMatthew G. Knepley   Level: intermediate
16472adcc780SMatthew G. Knepley 
16482adcc780SMatthew G. Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
16492adcc780SMatthew G. Knepley @*/
16502adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
16512adcc780SMatthew G. Knepley {
16522adcc780SMatthew G. Knepley   PetscInt       i;
16532adcc780SMatthew G. Knepley   PetscErrorCode ierr;
16542adcc780SMatthew G. Knepley 
16552adcc780SMatthew G. Knepley   PetscFunctionBegin;
16562adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
16572adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
16582adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
16592adcc780SMatthew G. Knepley   if (superdm) PetscValidPointer(superdm,4);
16602adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
16612adcc780SMatthew G. Knepley   if (len) {
16622adcc780SMatthew G. Knepley     if (dms[0]->ops->createsuperdm) {
16632adcc780SMatthew G. Knepley       ierr = (*dms[0]->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
16642adcc780SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) dms[0]), PETSC_ERR_SUP, "This type has no DMCreateSuperDM implementation defined");
16652adcc780SMatthew G. Knepley   }
16662adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
16672adcc780SMatthew G. Knepley }
16682adcc780SMatthew G. Knepley 
166916621825SDmitry Karpeev 
167016621825SDmitry Karpeev /*@C
16718d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
16728d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
16738d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
16748d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
16758d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
167616621825SDmitry Karpeev 
167716621825SDmitry Karpeev   Not collective
167816621825SDmitry Karpeev 
167916621825SDmitry Karpeev   Input Parameter:
168016621825SDmitry Karpeev . dm - the DM object
168116621825SDmitry Karpeev 
168216621825SDmitry Karpeev   Output Parameters:
16830298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
16840298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
16850298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
16860298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
16870298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
168816621825SDmitry Karpeev 
168916621825SDmitry Karpeev   Level: intermediate
169016621825SDmitry Karpeev 
169116621825SDmitry Karpeev   Notes:
169216621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
169316621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
169416621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
169516621825SDmitry Karpeev 
16968d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
169716621825SDmitry Karpeev @*/
16988d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
169916621825SDmitry Karpeev {
170016621825SDmitry Karpeev   PetscErrorCode      ierr;
1701be081cd6SPeter Brune   DMSubDomainHookLink link;
1702be081cd6SPeter Brune   PetscInt            i,l;
170316621825SDmitry Karpeev 
170416621825SDmitry Karpeev   PetscFunctionBegin;
170516621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
170614a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17070298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17080298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17090298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17100298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1711f3f0edfdSDmitry Karpeev   /*
1712f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1713f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1714f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1715f3f0edfdSDmitry Karpeev    */
1716ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
171716621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1718be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
171914a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1720f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1721be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1722be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1723be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1724be081cd6SPeter Brune         }
1725648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1726e7c4fc90SDmitry Karpeev       }
172714a18fd3SPeter Brune     }
172814a18fd3SPeter Brune     if (len) *len = l;
172914a18fd3SPeter Brune   }
1730e30e807fSPeter Brune   PetscFunctionReturn(0);
1731e30e807fSPeter Brune }
1732e30e807fSPeter Brune 
1733e30e807fSPeter Brune 
1734e30e807fSPeter Brune /*@C
1735e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1736e30e807fSPeter Brune 
1737e30e807fSPeter Brune   Not collective
1738e30e807fSPeter Brune 
1739e30e807fSPeter Brune   Input Parameters:
1740e30e807fSPeter Brune + dm - the DM object
1741e30e807fSPeter Brune . n  - the number of subdomain scatters
1742e30e807fSPeter Brune - subdms - the local subdomains
1743e30e807fSPeter Brune 
1744e30e807fSPeter Brune   Output Parameters:
1745e30e807fSPeter Brune + n     - the number of scatters returned
1746e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1747e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1748e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1749e30e807fSPeter Brune 
1750e30e807fSPeter Brune   Notes: This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1751e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1752e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1753e30e807fSPeter Brune   solution and residual data.
1754e30e807fSPeter Brune 
1755e30e807fSPeter Brune   Level: developer
1756e30e807fSPeter Brune 
1757e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1758e30e807fSPeter Brune @*/
1759e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1760e30e807fSPeter Brune {
1761e30e807fSPeter Brune   PetscErrorCode ierr;
1762e30e807fSPeter Brune 
1763e30e807fSPeter Brune   PetscFunctionBegin;
1764e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1765e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1766e30e807fSPeter Brune   if (dm->ops->createddscatters) {
1767e30e807fSPeter Brune     ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
17686668ed41SLisandro Dalcin   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "This type has no DMCreateDomainDecompositionScatter implementation defined");
1769e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1770e7c4fc90SDmitry Karpeev }
1771e7c4fc90SDmitry Karpeev 
177247c6ae99SBarry Smith /*@
177347c6ae99SBarry Smith   DMRefine - Refines a DM object
177447c6ae99SBarry Smith 
177547c6ae99SBarry Smith   Collective on DM
177647c6ae99SBarry Smith 
177747c6ae99SBarry Smith   Input Parameter:
177847c6ae99SBarry Smith + dm   - the DM object
177991d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
178047c6ae99SBarry Smith 
178147c6ae99SBarry Smith   Output Parameter:
17820298fd71SBarry Smith . dmf - the refined DM, or NULL
1783ae0a1c52SMatthew G Knepley 
17840298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
178547c6ae99SBarry Smith 
178647c6ae99SBarry Smith   Level: developer
178747c6ae99SBarry Smith 
1788e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
178947c6ae99SBarry Smith @*/
17907087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
179147c6ae99SBarry Smith {
179247c6ae99SBarry Smith   PetscErrorCode   ierr;
1793c833c3b5SJed Brown   DMRefineHookLink link;
179447c6ae99SBarry Smith 
179547c6ae99SBarry Smith   PetscFunctionBegin;
1796732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
17971ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1798fef3a512SBarry Smith   if (!dm->ops->refine) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot refine");
179947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18004057135bSMatthew G Knepley   if (*dmf) {
180143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18028865f1eaSKarl Rupp 
18038cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18048865f1eaSKarl Rupp 
1805644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18060598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1807656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18088865f1eaSKarl Rupp 
1809e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1810c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18118865f1eaSKarl Rupp       if (link->refinehook) {
18128865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18138865f1eaSKarl Rupp       }
1814c833c3b5SJed Brown     }
1815c833c3b5SJed Brown   }
18161ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1817c833c3b5SJed Brown   PetscFunctionReturn(0);
1818c833c3b5SJed Brown }
1819c833c3b5SJed Brown 
1820bb9467b5SJed Brown /*@C
1821c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1822c833c3b5SJed Brown 
1823c833c3b5SJed Brown    Logically Collective
1824c833c3b5SJed Brown 
1825c833c3b5SJed Brown    Input Arguments:
1826c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1827c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1828c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18290298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1830c833c3b5SJed Brown 
1831c833c3b5SJed Brown    Calling sequence of refinehook:
1832c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1833c833c3b5SJed Brown 
1834c833c3b5SJed Brown +  coarse - coarse level DM
1835c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1836c833c3b5SJed Brown -  ctx - optional user-defined function context
1837c833c3b5SJed Brown 
1838c833c3b5SJed Brown    Calling sequence for interphook:
1839c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1840c833c3b5SJed Brown 
1841c833c3b5SJed Brown +  coarse - coarse level DM
1842c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1843c833c3b5SJed Brown .  fine - fine level DM to update
1844c833c3b5SJed Brown -  ctx - optional user-defined function context
1845c833c3b5SJed Brown 
1846c833c3b5SJed Brown    Level: advanced
1847c833c3b5SJed Brown 
1848c833c3b5SJed Brown    Notes:
1849c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1850c833c3b5SJed Brown 
1851c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1852c833c3b5SJed Brown 
1853bb9467b5SJed Brown    This function is currently not available from Fortran.
1854bb9467b5SJed Brown 
1855c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1856c833c3b5SJed Brown @*/
1857c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1858c833c3b5SJed Brown {
1859c833c3b5SJed Brown   PetscErrorCode   ierr;
1860c833c3b5SJed Brown   DMRefineHookLink link,*p;
1861c833c3b5SJed Brown 
1862c833c3b5SJed Brown   PetscFunctionBegin;
1863c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
18643d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
18653d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
18663d8e3701SJed Brown   }
186795dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1868c833c3b5SJed Brown   link->refinehook = refinehook;
1869c833c3b5SJed Brown   link->interphook = interphook;
1870c833c3b5SJed Brown   link->ctx        = ctx;
18710298fd71SBarry Smith   link->next       = NULL;
1872c833c3b5SJed Brown   *p               = link;
1873c833c3b5SJed Brown   PetscFunctionReturn(0);
1874c833c3b5SJed Brown }
1875c833c3b5SJed Brown 
18763d8e3701SJed Brown /*@C
18773d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
18783d8e3701SJed Brown 
18793d8e3701SJed Brown    Logically Collective
18803d8e3701SJed Brown 
18813d8e3701SJed Brown    Input Arguments:
18823d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
18833d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
18843d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
18853d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
18863d8e3701SJed Brown 
18873d8e3701SJed Brown    Level: advanced
18883d8e3701SJed Brown 
18893d8e3701SJed Brown    Notes:
18903d8e3701SJed Brown    This function does nothing if the hook is not in the list.
18913d8e3701SJed Brown 
18923d8e3701SJed Brown    This function is currently not available from Fortran.
18933d8e3701SJed Brown 
18943d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
18953d8e3701SJed Brown @*/
18963d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
18973d8e3701SJed Brown {
18983d8e3701SJed Brown   PetscErrorCode   ierr;
18993d8e3701SJed Brown   DMRefineHookLink link,*p;
19003d8e3701SJed Brown 
19013d8e3701SJed Brown   PetscFunctionBegin;
19023d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19033d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19043d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19053d8e3701SJed Brown       link = *p;
19063d8e3701SJed Brown       *p = link->next;
19073d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19083d8e3701SJed Brown       break;
19093d8e3701SJed Brown     }
19103d8e3701SJed Brown   }
19113d8e3701SJed Brown   PetscFunctionReturn(0);
19123d8e3701SJed Brown }
19133d8e3701SJed Brown 
1914c833c3b5SJed Brown /*@
1915c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1916c833c3b5SJed Brown 
1917c833c3b5SJed Brown    Collective if any hooks are
1918c833c3b5SJed Brown 
1919c833c3b5SJed Brown    Input Arguments:
1920c833c3b5SJed Brown +  coarse - coarser DM to use as a base
1921e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
1922c833c3b5SJed Brown -  fine - finer DM to update
1923c833c3b5SJed Brown 
1924c833c3b5SJed Brown    Level: developer
1925c833c3b5SJed Brown 
1926c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
1927c833c3b5SJed Brown @*/
1928c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
1929c833c3b5SJed Brown {
1930c833c3b5SJed Brown   PetscErrorCode   ierr;
1931c833c3b5SJed Brown   DMRefineHookLink link;
1932c833c3b5SJed Brown 
1933c833c3b5SJed Brown   PetscFunctionBegin;
1934c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
19358865f1eaSKarl Rupp     if (link->interphook) {
19368865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
19378865f1eaSKarl Rupp     }
19384057135bSMatthew G Knepley   }
193947c6ae99SBarry Smith   PetscFunctionReturn(0);
194047c6ae99SBarry Smith }
194147c6ae99SBarry Smith 
1942eb3f98d2SBarry Smith /*@
1943eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
1944eb3f98d2SBarry Smith 
1945eb3f98d2SBarry Smith     Not Collective
1946eb3f98d2SBarry Smith 
1947eb3f98d2SBarry Smith     Input Parameter:
1948eb3f98d2SBarry Smith .   dm - the DM object
1949eb3f98d2SBarry Smith 
1950eb3f98d2SBarry Smith     Output Parameter:
1951eb3f98d2SBarry Smith .   level - number of refinements
1952eb3f98d2SBarry Smith 
1953eb3f98d2SBarry Smith     Level: developer
1954eb3f98d2SBarry Smith 
19556a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1956eb3f98d2SBarry Smith 
1957eb3f98d2SBarry Smith @*/
1958eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
1959eb3f98d2SBarry Smith {
1960eb3f98d2SBarry Smith   PetscFunctionBegin;
1961eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1962eb3f98d2SBarry Smith   *level = dm->levelup;
1963eb3f98d2SBarry Smith   PetscFunctionReturn(0);
1964eb3f98d2SBarry Smith }
1965eb3f98d2SBarry Smith 
1966fef3a512SBarry Smith /*@
1967fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
1968fef3a512SBarry Smith 
1969fef3a512SBarry Smith     Not Collective
1970fef3a512SBarry Smith 
1971fef3a512SBarry Smith     Input Parameter:
1972fef3a512SBarry Smith +   dm - the DM object
1973fef3a512SBarry Smith -   level - number of refinements
1974fef3a512SBarry Smith 
1975fef3a512SBarry Smith     Level: advanced
1976fef3a512SBarry Smith 
1977fef3a512SBarry Smith     Notes: This value is used by PCMG to determine how many multigrid levels to use
1978fef3a512SBarry Smith 
1979fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
1980fef3a512SBarry Smith 
1981fef3a512SBarry Smith @*/
1982fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
1983fef3a512SBarry Smith {
1984fef3a512SBarry Smith   PetscFunctionBegin;
1985fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1986fef3a512SBarry Smith   dm->levelup = level;
1987fef3a512SBarry Smith   PetscFunctionReturn(0);
1988fef3a512SBarry Smith }
1989fef3a512SBarry Smith 
1990bb9467b5SJed Brown /*@C
1991baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
1992baf369e7SPeter Brune 
1993baf369e7SPeter Brune    Logically Collective
1994baf369e7SPeter Brune 
1995baf369e7SPeter Brune    Input Arguments:
1996baf369e7SPeter Brune +  dm - the DM
1997baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
1998baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
19990298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2000baf369e7SPeter Brune 
2001baf369e7SPeter Brune    Calling sequence for beginhook:
2002baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2003baf369e7SPeter Brune 
2004baf369e7SPeter Brune +  dm - global DM
2005baf369e7SPeter Brune .  g - global vector
2006baf369e7SPeter Brune .  mode - mode
2007baf369e7SPeter Brune .  l - local vector
2008baf369e7SPeter Brune -  ctx - optional user-defined function context
2009baf369e7SPeter Brune 
2010baf369e7SPeter Brune 
2011baf369e7SPeter Brune    Calling sequence for endhook:
2012ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2013baf369e7SPeter Brune 
2014baf369e7SPeter Brune +  global - global DM
2015baf369e7SPeter Brune -  ctx - optional user-defined function context
2016baf369e7SPeter Brune 
2017baf369e7SPeter Brune    Level: advanced
2018baf369e7SPeter Brune 
2019baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2020baf369e7SPeter Brune @*/
2021baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2022baf369e7SPeter Brune {
2023baf369e7SPeter Brune   PetscErrorCode          ierr;
2024baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2025baf369e7SPeter Brune 
2026baf369e7SPeter Brune   PetscFunctionBegin;
2027baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2028baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
202995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2030baf369e7SPeter Brune   link->beginhook = beginhook;
2031baf369e7SPeter Brune   link->endhook   = endhook;
2032baf369e7SPeter Brune   link->ctx       = ctx;
20330298fd71SBarry Smith   link->next      = NULL;
2034baf369e7SPeter Brune   *p              = link;
2035baf369e7SPeter Brune   PetscFunctionReturn(0);
2036baf369e7SPeter Brune }
2037baf369e7SPeter Brune 
20384c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
20394c274da1SToby Isaac {
20404c274da1SToby Isaac   Mat cMat;
20414c274da1SToby Isaac   Vec cVec;
20424c274da1SToby Isaac   PetscSection section, cSec;
20434c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
20444c274da1SToby Isaac   PetscErrorCode ierr;
20454c274da1SToby Isaac 
20464c274da1SToby Isaac   PetscFunctionBegin;
20474c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
20484c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
20494c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
20505db9a05bSToby Isaac     PetscInt nRows;
20515db9a05bSToby Isaac 
20525db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
20535db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2054*e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
20557711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
20564c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
20574c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
20584c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
20594c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
20604c274da1SToby Isaac       if (dof) {
20614c274da1SToby Isaac         PetscScalar *vals;
20624c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
20634c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
20644c274da1SToby Isaac       }
20654c274da1SToby Isaac     }
20664c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
20674c274da1SToby Isaac   }
20684c274da1SToby Isaac   PetscFunctionReturn(0);
20694c274da1SToby Isaac }
20704c274da1SToby Isaac 
207147c6ae99SBarry Smith /*@
207247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
207347c6ae99SBarry Smith 
207447c6ae99SBarry Smith     Neighbor-wise Collective on DM
207547c6ae99SBarry Smith 
207647c6ae99SBarry Smith     Input Parameters:
207747c6ae99SBarry Smith +   dm - the DM object
207847c6ae99SBarry Smith .   g - the global vector
207947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
208047c6ae99SBarry Smith -   l - the local vector
208147c6ae99SBarry Smith 
208247c6ae99SBarry Smith 
208347c6ae99SBarry Smith     Level: beginner
208447c6ae99SBarry Smith 
2085e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
208647c6ae99SBarry Smith 
208747c6ae99SBarry Smith @*/
20887087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
208947c6ae99SBarry Smith {
20907128ae9fSMatthew G Knepley   PetscSF                 sf;
209147c6ae99SBarry Smith   PetscErrorCode          ierr;
2092baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
209347c6ae99SBarry Smith 
209447c6ae99SBarry Smith   PetscFunctionBegin;
2095171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2096baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
20978865f1eaSKarl Rupp     if (link->beginhook) {
20988865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
20998865f1eaSKarl Rupp     }
2100baf369e7SPeter Brune   }
21017128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21027128ae9fSMatthew G Knepley   if (sf) {
2103ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2104ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
21057128ae9fSMatthew G Knepley 
210682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21077128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2108ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21097128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21107128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2111ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21127128ae9fSMatthew G Knepley   } else {
2113843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21147128ae9fSMatthew G Knepley   }
211547c6ae99SBarry Smith   PetscFunctionReturn(0);
211647c6ae99SBarry Smith }
211747c6ae99SBarry Smith 
211847c6ae99SBarry Smith /*@
211947c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
212047c6ae99SBarry Smith 
212147c6ae99SBarry Smith     Neighbor-wise Collective on DM
212247c6ae99SBarry Smith 
212347c6ae99SBarry Smith     Input Parameters:
212447c6ae99SBarry Smith +   dm - the DM object
212547c6ae99SBarry Smith .   g - the global vector
212647c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
212747c6ae99SBarry Smith -   l - the local vector
212847c6ae99SBarry Smith 
212947c6ae99SBarry Smith 
213047c6ae99SBarry Smith     Level: beginner
213147c6ae99SBarry Smith 
2132e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
213347c6ae99SBarry Smith 
213447c6ae99SBarry Smith @*/
21357087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
213647c6ae99SBarry Smith {
21377128ae9fSMatthew G Knepley   PetscSF                 sf;
213847c6ae99SBarry Smith   PetscErrorCode          ierr;
2139ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2140ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2141baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
214247c6ae99SBarry Smith 
214347c6ae99SBarry Smith   PetscFunctionBegin;
2144171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
21457128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
21467128ae9fSMatthew G Knepley   if (sf) {
214782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
21487128ae9fSMatthew G Knepley 
21497128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2150ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
21517128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
21527128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2153ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
21547128ae9fSMatthew G Knepley   } else {
2155843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
21567128ae9fSMatthew G Knepley   }
21574c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2158baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2159baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2160baf369e7SPeter Brune   }
216147c6ae99SBarry Smith   PetscFunctionReturn(0);
216247c6ae99SBarry Smith }
216347c6ae99SBarry Smith 
2164d4d07f1eSToby Isaac /*@C
2165d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2166d4d07f1eSToby Isaac 
2167d4d07f1eSToby Isaac    Logically Collective
2168d4d07f1eSToby Isaac 
2169d4d07f1eSToby Isaac    Input Arguments:
2170d4d07f1eSToby Isaac +  dm - the DM
2171d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2172d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2173d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2174d4d07f1eSToby Isaac 
2175d4d07f1eSToby Isaac    Calling sequence for beginhook:
2176d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2177d4d07f1eSToby Isaac 
2178d4d07f1eSToby Isaac +  dm - global DM
2179d4d07f1eSToby Isaac .  l - local vector
2180d4d07f1eSToby Isaac .  mode - mode
2181d4d07f1eSToby Isaac .  g - global vector
2182d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2183d4d07f1eSToby Isaac 
2184d4d07f1eSToby Isaac 
2185d4d07f1eSToby Isaac    Calling sequence for endhook:
2186d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2187d4d07f1eSToby Isaac 
2188d4d07f1eSToby Isaac +  global - global DM
2189d4d07f1eSToby Isaac .  l - local vector
2190d4d07f1eSToby Isaac .  mode - mode
2191d4d07f1eSToby Isaac .  g - global vector
2192d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2193d4d07f1eSToby Isaac 
2194d4d07f1eSToby Isaac    Level: advanced
2195d4d07f1eSToby Isaac 
2196d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2197d4d07f1eSToby Isaac @*/
2198d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2199d4d07f1eSToby Isaac {
2200d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2201d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2202d4d07f1eSToby Isaac 
2203d4d07f1eSToby Isaac   PetscFunctionBegin;
2204d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2205d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
220695dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2207d4d07f1eSToby Isaac   link->beginhook = beginhook;
2208d4d07f1eSToby Isaac   link->endhook   = endhook;
2209d4d07f1eSToby Isaac   link->ctx       = ctx;
2210d4d07f1eSToby Isaac   link->next      = NULL;
2211d4d07f1eSToby Isaac   *p              = link;
2212d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2213d4d07f1eSToby Isaac }
2214d4d07f1eSToby Isaac 
22154c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
22164c274da1SToby Isaac {
22174c274da1SToby Isaac   Mat cMat;
22184c274da1SToby Isaac   Vec cVec;
22194c274da1SToby Isaac   PetscSection section, cSec;
22204c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22214c274da1SToby Isaac   PetscErrorCode ierr;
22224c274da1SToby Isaac 
22234c274da1SToby Isaac   PetscFunctionBegin;
22244c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22254c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22264c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
22275db9a05bSToby Isaac     PetscInt nRows;
22285db9a05bSToby Isaac 
22295db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22305db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
2231*e87a4003SBarry Smith     ierr = DMGetSection(dm,&section);CHKERRQ(ierr);
22327711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22334c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22344c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22354c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22364c274da1SToby Isaac       if (dof) {
22374c274da1SToby Isaac         PetscInt d;
22384c274da1SToby Isaac         PetscScalar *vals;
22394c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
22404c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
22414c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
22424c274da1SToby Isaac          * we just extracted */
22434c274da1SToby Isaac         for (d = 0; d < dof; d++) {
22444c274da1SToby Isaac           vals[d] = 0.;
22454c274da1SToby Isaac         }
22464c274da1SToby Isaac       }
22474c274da1SToby Isaac     }
22484c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
22494c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22504c274da1SToby Isaac   }
22514c274da1SToby Isaac   PetscFunctionReturn(0);
22524c274da1SToby Isaac }
22534c274da1SToby Isaac 
225447c6ae99SBarry Smith /*@
22559a42bb27SBarry Smith     DMLocalToGlobalBegin - updates global vectors from local vectors
22569a42bb27SBarry Smith 
22579a42bb27SBarry Smith     Neighbor-wise Collective on DM
22589a42bb27SBarry Smith 
22599a42bb27SBarry Smith     Input Parameters:
22609a42bb27SBarry Smith +   dm - the DM object
2261f6813fd5SJed Brown .   l - the local vector
22621eb28f2eSBarry 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.
22631eb28f2eSBarry Smith -   g - the global vector
22649a42bb27SBarry Smith 
2265d96308ebSBarry Smith     Notes: In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
226684330215SMatthew 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.
22679a42bb27SBarry Smith 
22689a42bb27SBarry Smith     Level: beginner
22699a42bb27SBarry Smith 
2270e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
22719a42bb27SBarry Smith 
22729a42bb27SBarry Smith @*/
22737087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
22749a42bb27SBarry Smith {
22757128ae9fSMatthew G Knepley   PetscSF                 sf;
227684330215SMatthew G. Knepley   PetscSection            s, gs;
2277d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2278ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2279ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
228084330215SMatthew G. Knepley   PetscBool               isInsert;
228184330215SMatthew G. Knepley   PetscErrorCode          ierr;
22829a42bb27SBarry Smith 
22839a42bb27SBarry Smith   PetscFunctionBegin;
2284171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2285d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2286d4d07f1eSToby Isaac     if (link->beginhook) {
2287d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2288d4d07f1eSToby Isaac     }
2289d4d07f1eSToby Isaac   }
22904c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
22917128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2292*e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
22937128ae9fSMatthew G Knepley   switch (mode) {
22947128ae9fSMatthew G Knepley   case INSERT_VALUES:
22957128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2296304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
229784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
22987128ae9fSMatthew G Knepley   case ADD_VALUES:
22997128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2300304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
230184330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23027128ae9fSMatthew G Knepley   default:
230382f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23047128ae9fSMatthew G Knepley   }
230584330215SMatthew G. Knepley   if (sf && !isInsert) {
2306ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23077128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2308a9b180a6SBarry Smith     ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2309ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
231084330215SMatthew G. Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
231184330215SMatthew G. Knepley   } else if (s && isInsert) {
231284330215SMatthew G. Knepley     PetscInt gStart, pStart, pEnd, p;
231384330215SMatthew G. Knepley 
2314*e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
231584330215SMatthew G. Knepley     ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
231684330215SMatthew G. Knepley     ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
2317ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
231884330215SMatthew G. Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
231984330215SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2320b3b16f48SMatthew G. Knepley       PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
232184330215SMatthew G. Knepley 
232284330215SMatthew G. Knepley       ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
232303442857SMatthew G. Knepley       ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
232484330215SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2325b3b16f48SMatthew G. Knepley       ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
232684330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
232784330215SMatthew G. Knepley       ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2328b3b16f48SMatthew G. Knepley       /* Ignore off-process data and points with no global data */
232903442857SMatthew G. Knepley       if (!gdof || goff < 0) continue;
2330b3b16f48SMatthew 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);
2331b3b16f48SMatthew G. Knepley       /* If no constraints are enforced in the global vector */
2332b3b16f48SMatthew G. Knepley       if (!gcdof) {
233384330215SMatthew G. Knepley         for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2334b3b16f48SMatthew G. Knepley       /* If constraints are enforced in the global vector */
2335b3b16f48SMatthew G. Knepley       } else if (cdof == gcdof) {
233684330215SMatthew G. Knepley         const PetscInt *cdofs;
233784330215SMatthew G. Knepley         PetscInt        cind = 0;
233884330215SMatthew G. Knepley 
233984330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2340b3b16f48SMatthew G. Knepley         for (d = 0, e = 0; d < dof; ++d) {
234184330215SMatthew G. Knepley           if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2342b3b16f48SMatthew G. Knepley           gArray[goff-gStart+e++] = lArray[off+d];
234384330215SMatthew G. Knepley         }
2344b3b16f48SMatthew 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);
234584330215SMatthew G. Knepley     }
2346ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
23477128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
23487128ae9fSMatthew G Knepley   } else {
2349843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
23507128ae9fSMatthew G Knepley   }
23519a42bb27SBarry Smith   PetscFunctionReturn(0);
23529a42bb27SBarry Smith }
23539a42bb27SBarry Smith 
23549a42bb27SBarry Smith /*@
23559a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
235647c6ae99SBarry Smith 
235747c6ae99SBarry Smith     Neighbor-wise Collective on DM
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith     Input Parameters:
236047c6ae99SBarry Smith +   dm - the DM object
2361f6813fd5SJed Brown .   l - the local vector
236247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2363f6813fd5SJed Brown -   g - the global vector
236447c6ae99SBarry Smith 
236547c6ae99SBarry Smith 
236647c6ae99SBarry Smith     Level: beginner
236747c6ae99SBarry Smith 
2368e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
236947c6ae99SBarry Smith 
237047c6ae99SBarry Smith @*/
23717087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
237247c6ae99SBarry Smith {
23737128ae9fSMatthew G Knepley   PetscSF                 sf;
237484330215SMatthew G. Knepley   PetscSection            s;
2375d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
237684330215SMatthew G. Knepley   PetscBool               isInsert;
237784330215SMatthew G. Knepley   PetscErrorCode          ierr;
237847c6ae99SBarry Smith 
237947c6ae99SBarry Smith   PetscFunctionBegin;
2380171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23817128ae9fSMatthew G Knepley   ierr = DMGetDefaultSF(dm, &sf);CHKERRQ(ierr);
2382*e87a4003SBarry Smith   ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
23837128ae9fSMatthew G Knepley   switch (mode) {
23847128ae9fSMatthew G Knepley   case INSERT_VALUES:
23857128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
238684330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
23877128ae9fSMatthew G Knepley   case ADD_VALUES:
23887128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
238984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
23907128ae9fSMatthew G Knepley   default:
239182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23927128ae9fSMatthew G Knepley   }
239384330215SMatthew G. Knepley   if (sf && !isInsert) {
2394ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2395ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
239684330215SMatthew G. Knepley 
2397ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
23987128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2399a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2400ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
24017128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
240284330215SMatthew G. Knepley   } else if (s && isInsert) {
24037128ae9fSMatthew G Knepley   } else {
2404843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
24057128ae9fSMatthew G Knepley   }
2406d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2407d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2408d4d07f1eSToby Isaac   }
240947c6ae99SBarry Smith   PetscFunctionReturn(0);
241047c6ae99SBarry Smith }
241147c6ae99SBarry Smith 
2412f089877aSRichard Tran Mills /*@
2413bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2414bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2415d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2416f089877aSRichard Tran Mills 
2417bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2418f089877aSRichard Tran Mills 
2419f089877aSRichard Tran Mills    Input Parameters:
2420f089877aSRichard Tran Mills +  dm - the DM object
2421bc0a1609SRichard Tran Mills .  g - the original local vector
2422bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2423f089877aSRichard Tran Mills 
2424bc0a1609SRichard Tran Mills    Output Parameter:
2425bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2426f089877aSRichard Tran Mills 
2427f089877aSRichard Tran Mills    Level: intermediate
2428f089877aSRichard Tran Mills 
2429bc0a1609SRichard Tran Mills    Notes:
2430bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2431bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2432bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2433bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2434bc0a1609SRichard Tran Mills 
2435bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, begin
2436bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2437f089877aSRichard Tran Mills 
2438f089877aSRichard Tran Mills @*/
2439f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2440f089877aSRichard Tran Mills {
2441f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2442f089877aSRichard Tran Mills 
2443f089877aSRichard Tran Mills   PetscFunctionBegin;
2444f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2445bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2446f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2447f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2448f089877aSRichard Tran Mills }
2449f089877aSRichard Tran Mills 
2450f089877aSRichard Tran Mills /*@
2451bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2452bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2453d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2454f089877aSRichard Tran Mills 
2455bc0a1609SRichard Tran Mills    Neighbor-wise Collective on DM and Vec
2456f089877aSRichard Tran Mills 
2457f089877aSRichard Tran Mills    Input Parameters:
2458bc0a1609SRichard Tran Mills +  da - the DM object
2459bc0a1609SRichard Tran Mills .  g - the original local vector
2460bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2461f089877aSRichard Tran Mills 
2462bc0a1609SRichard Tran Mills    Output Parameter:
2463bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2464f089877aSRichard Tran Mills 
2465f089877aSRichard Tran Mills    Level: intermediate
2466f089877aSRichard Tran Mills 
2467bc0a1609SRichard Tran Mills    Notes:
2468bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2469bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2470bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2471bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2472bc0a1609SRichard Tran Mills 
2473bc0a1609SRichard Tran Mills .keywords: DM, local-to-local, end
2474bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2475f089877aSRichard Tran Mills 
2476f089877aSRichard Tran Mills @*/
2477f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2478f089877aSRichard Tran Mills {
2479f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2480f089877aSRichard Tran Mills 
2481f089877aSRichard Tran Mills   PetscFunctionBegin;
2482f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2483bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2484f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2485f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2486f089877aSRichard Tran Mills }
2487f089877aSRichard Tran Mills 
2488f089877aSRichard Tran Mills 
248947c6ae99SBarry Smith /*@
249047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
249147c6ae99SBarry Smith 
249247c6ae99SBarry Smith     Collective on DM
249347c6ae99SBarry Smith 
249447c6ae99SBarry Smith     Input Parameter:
249547c6ae99SBarry Smith +   dm - the DM object
249691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
249747c6ae99SBarry Smith 
249847c6ae99SBarry Smith     Output Parameter:
249947c6ae99SBarry Smith .   dmc - the coarsened DM
250047c6ae99SBarry Smith 
250147c6ae99SBarry Smith     Level: developer
250247c6ae99SBarry Smith 
2503e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
250447c6ae99SBarry Smith 
250547c6ae99SBarry Smith @*/
25067087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
250747c6ae99SBarry Smith {
250847c6ae99SBarry Smith   PetscErrorCode    ierr;
2509b17ce1afSJed Brown   DMCoarsenHookLink link;
251047c6ae99SBarry Smith 
251147c6ae99SBarry Smith   PetscFunctionBegin;
2512171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2513fef3a512SBarry Smith   if (!dm->ops->coarsen) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM cannot coarsen");
251447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
251547c6ae99SBarry Smith   ierr                      = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
25168892b4c3SMatthew G. Knepley   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2517a8fb8f29SToby Isaac   ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
251843842a1eSJed Brown   (*dmc)->ops->creatematrix = dm->ops->creatematrix;
25198cd211a4SJed Brown   ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2520644e2e5bSBarry Smith   (*dmc)->ctx               = dm->ctx;
25210598a293SJed Brown   (*dmc)->levelup           = dm->levelup;
2522656b349aSBarry Smith   (*dmc)->leveldown         = dm->leveldown + 1;
2523e4b4b23bSJed Brown   ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2524b17ce1afSJed Brown   for (link=dm->coarsenhook; link; link=link->next) {
2525b17ce1afSJed Brown     if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2526b17ce1afSJed Brown   }
252747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2528b17ce1afSJed Brown   PetscFunctionReturn(0);
2529b17ce1afSJed Brown }
2530b17ce1afSJed Brown 
2531bb9467b5SJed Brown /*@C
2532b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2533b17ce1afSJed Brown 
2534b17ce1afSJed Brown    Logically Collective
2535b17ce1afSJed Brown 
2536b17ce1afSJed Brown    Input Arguments:
2537b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2538b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2539b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
25400298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2541b17ce1afSJed Brown 
2542b17ce1afSJed Brown    Calling sequence of coarsenhook:
2543b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2544b17ce1afSJed Brown 
2545b17ce1afSJed Brown +  fine - fine level DM
2546b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2547b17ce1afSJed Brown -  ctx - optional user-defined function context
2548b17ce1afSJed Brown 
2549b17ce1afSJed Brown    Calling sequence for restricthook:
2550c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2551b17ce1afSJed Brown 
2552b17ce1afSJed Brown +  fine - fine level DM
2553b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2554c833c3b5SJed Brown .  rscale - scaling vector for restriction
2555c833c3b5SJed Brown .  inject - matrix restricting by injection
2556b17ce1afSJed Brown .  coarse - coarse level DM to update
2557b17ce1afSJed Brown -  ctx - optional user-defined function context
2558b17ce1afSJed Brown 
2559b17ce1afSJed Brown    Level: advanced
2560b17ce1afSJed Brown 
2561b17ce1afSJed Brown    Notes:
2562b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2563b17ce1afSJed Brown 
2564b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2565b17ce1afSJed Brown 
2566b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2567b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2568b17ce1afSJed Brown 
2569bb9467b5SJed Brown    This function is currently not available from Fortran.
2570bb9467b5SJed Brown 
2571dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2572b17ce1afSJed Brown @*/
2573b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2574b17ce1afSJed Brown {
2575b17ce1afSJed Brown   PetscErrorCode    ierr;
2576b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2577b17ce1afSJed Brown 
2578b17ce1afSJed Brown   PetscFunctionBegin;
2579b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
25801e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
25811e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
25821e3d8eccSJed Brown   }
258395dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2584b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2585b17ce1afSJed Brown   link->restricthook = restricthook;
2586b17ce1afSJed Brown   link->ctx          = ctx;
25870298fd71SBarry Smith   link->next         = NULL;
2588b17ce1afSJed Brown   *p                 = link;
2589b17ce1afSJed Brown   PetscFunctionReturn(0);
2590b17ce1afSJed Brown }
2591b17ce1afSJed Brown 
2592dc822a44SJed Brown /*@C
2593dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2594dc822a44SJed Brown 
2595dc822a44SJed Brown    Logically Collective
2596dc822a44SJed Brown 
2597dc822a44SJed Brown    Input Arguments:
2598dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2599dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2600dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2601dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2602dc822a44SJed Brown 
2603dc822a44SJed Brown    Level: advanced
2604dc822a44SJed Brown 
2605dc822a44SJed Brown    Notes:
2606dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2607dc822a44SJed Brown 
2608dc822a44SJed Brown    This function is currently not available from Fortran.
2609dc822a44SJed Brown 
2610dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2611dc822a44SJed Brown @*/
2612dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2613dc822a44SJed Brown {
2614dc822a44SJed Brown   PetscErrorCode    ierr;
2615dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2616dc822a44SJed Brown 
2617dc822a44SJed Brown   PetscFunctionBegin;
2618dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2619dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2620dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2621dc822a44SJed Brown       link = *p;
2622dc822a44SJed Brown       *p = link->next;
2623dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2624dc822a44SJed Brown       break;
2625dc822a44SJed Brown     }
2626dc822a44SJed Brown   }
2627dc822a44SJed Brown   PetscFunctionReturn(0);
2628dc822a44SJed Brown }
2629dc822a44SJed Brown 
2630dc822a44SJed Brown 
2631b17ce1afSJed Brown /*@
2632b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2633b17ce1afSJed Brown 
2634b17ce1afSJed Brown    Collective if any hooks are
2635b17ce1afSJed Brown 
2636b17ce1afSJed Brown    Input Arguments:
2637b17ce1afSJed Brown +  fine - finer DM to use as a base
2638b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2639e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2640b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2641e91eccc2SStefano Zampini -  coarse - coarser DM to update
2642b17ce1afSJed Brown 
2643b17ce1afSJed Brown    Level: developer
2644b17ce1afSJed Brown 
2645b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2646b17ce1afSJed Brown @*/
2647b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2648b17ce1afSJed Brown {
2649b17ce1afSJed Brown   PetscErrorCode    ierr;
2650b17ce1afSJed Brown   DMCoarsenHookLink link;
2651b17ce1afSJed Brown 
2652b17ce1afSJed Brown   PetscFunctionBegin;
2653b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
26548865f1eaSKarl Rupp     if (link->restricthook) {
26558865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
26568865f1eaSKarl Rupp     }
2657b17ce1afSJed Brown   }
265847c6ae99SBarry Smith   PetscFunctionReturn(0);
265947c6ae99SBarry Smith }
266047c6ae99SBarry Smith 
2661bb9467b5SJed Brown /*@C
2662be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
26635dbd56e3SPeter Brune 
26645dbd56e3SPeter Brune    Logically Collective
26655dbd56e3SPeter Brune 
26665dbd56e3SPeter Brune    Input Arguments:
26675dbd56e3SPeter Brune +  global - global DM
2668ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
26695dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
26700298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
26715dbd56e3SPeter Brune 
2672ec4806b8SPeter Brune 
2673ec4806b8SPeter Brune    Calling sequence for ddhook:
2674ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2675ec4806b8SPeter Brune 
2676ec4806b8SPeter Brune +  global - global DM
2677ec4806b8SPeter Brune .  block  - block DM
2678ec4806b8SPeter Brune -  ctx - optional user-defined function context
2679ec4806b8SPeter Brune 
26805dbd56e3SPeter Brune    Calling sequence for restricthook:
2681ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
26825dbd56e3SPeter Brune 
26835dbd56e3SPeter Brune +  global - global DM
26845dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
26855dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2686ec4806b8SPeter Brune .  block  - block DM
26875dbd56e3SPeter Brune -  ctx - optional user-defined function context
26885dbd56e3SPeter Brune 
26895dbd56e3SPeter Brune    Level: advanced
26905dbd56e3SPeter Brune 
26915dbd56e3SPeter Brune    Notes:
2692ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
26935dbd56e3SPeter Brune 
26945dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
26955dbd56e3SPeter Brune 
26965dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2697ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
26985dbd56e3SPeter Brune 
2699bb9467b5SJed Brown    This function is currently not available from Fortran.
2700bb9467b5SJed Brown 
27015dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
27025dbd56e3SPeter Brune @*/
2703be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
27045dbd56e3SPeter Brune {
27055dbd56e3SPeter Brune   PetscErrorCode      ierr;
2706be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
27075dbd56e3SPeter Brune 
27085dbd56e3SPeter Brune   PetscFunctionBegin;
27095dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2710b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2711b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2712b3a6b972SJed Brown   }
271395dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
27145dbd56e3SPeter Brune   link->restricthook = restricthook;
2715be081cd6SPeter Brune   link->ddhook       = ddhook;
27165dbd56e3SPeter Brune   link->ctx          = ctx;
27170298fd71SBarry Smith   link->next         = NULL;
27185dbd56e3SPeter Brune   *p                 = link;
27195dbd56e3SPeter Brune   PetscFunctionReturn(0);
27205dbd56e3SPeter Brune }
27215dbd56e3SPeter Brune 
2722b3a6b972SJed Brown /*@C
2723b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
2724b3a6b972SJed Brown 
2725b3a6b972SJed Brown    Logically Collective
2726b3a6b972SJed Brown 
2727b3a6b972SJed Brown    Input Arguments:
2728b3a6b972SJed Brown +  global - global DM
2729b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
2730b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
2731b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2732b3a6b972SJed Brown 
2733b3a6b972SJed Brown    Level: advanced
2734b3a6b972SJed Brown 
2735b3a6b972SJed Brown    Notes:
2736b3a6b972SJed Brown 
2737b3a6b972SJed Brown    This function is currently not available from Fortran.
2738b3a6b972SJed Brown 
2739b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2740b3a6b972SJed Brown @*/
2741b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
2742b3a6b972SJed Brown {
2743b3a6b972SJed Brown   PetscErrorCode      ierr;
2744b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
2745b3a6b972SJed Brown 
2746b3a6b972SJed Brown   PetscFunctionBegin;
2747b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2748b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2749b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2750b3a6b972SJed Brown       link = *p;
2751b3a6b972SJed Brown       *p = link->next;
2752b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2753b3a6b972SJed Brown       break;
2754b3a6b972SJed Brown     }
2755b3a6b972SJed Brown   }
2756b3a6b972SJed Brown   PetscFunctionReturn(0);
2757b3a6b972SJed Brown }
2758b3a6b972SJed Brown 
27595dbd56e3SPeter Brune /*@
2760be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
27615dbd56e3SPeter Brune 
27625dbd56e3SPeter Brune    Collective if any hooks are
27635dbd56e3SPeter Brune 
27645dbd56e3SPeter Brune    Input Arguments:
27655dbd56e3SPeter Brune +  fine - finer DM to use as a base
2766be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
2767be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
27685dbd56e3SPeter Brune -  coarse - coarer DM to update
27695dbd56e3SPeter Brune 
27705dbd56e3SPeter Brune    Level: developer
27715dbd56e3SPeter Brune 
27725dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
27735dbd56e3SPeter Brune @*/
2774be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
27755dbd56e3SPeter Brune {
27765dbd56e3SPeter Brune   PetscErrorCode      ierr;
2777be081cd6SPeter Brune   DMSubDomainHookLink link;
27785dbd56e3SPeter Brune 
27795dbd56e3SPeter Brune   PetscFunctionBegin;
2780be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
27818865f1eaSKarl Rupp     if (link->restricthook) {
27828865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
27838865f1eaSKarl Rupp     }
27845dbd56e3SPeter Brune   }
27855dbd56e3SPeter Brune   PetscFunctionReturn(0);
27865dbd56e3SPeter Brune }
27875dbd56e3SPeter Brune 
27885fe1f584SPeter Brune /*@
27896a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
27905fe1f584SPeter Brune 
27915fe1f584SPeter Brune     Not Collective
27925fe1f584SPeter Brune 
27935fe1f584SPeter Brune     Input Parameter:
27945fe1f584SPeter Brune .   dm - the DM object
27955fe1f584SPeter Brune 
27965fe1f584SPeter Brune     Output Parameter:
27976a7d9d85SPeter Brune .   level - number of coarsenings
27985fe1f584SPeter Brune 
27995fe1f584SPeter Brune     Level: developer
28005fe1f584SPeter Brune 
28016a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
28025fe1f584SPeter Brune 
28035fe1f584SPeter Brune @*/
28045fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
28055fe1f584SPeter Brune {
28065fe1f584SPeter Brune   PetscFunctionBegin;
28075fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28085fe1f584SPeter Brune   *level = dm->leveldown;
28095fe1f584SPeter Brune   PetscFunctionReturn(0);
28105fe1f584SPeter Brune }
28115fe1f584SPeter Brune 
28125fe1f584SPeter Brune 
28135fe1f584SPeter Brune 
281447c6ae99SBarry Smith /*@C
281547c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
281647c6ae99SBarry Smith 
281747c6ae99SBarry Smith     Collective on DM
281847c6ae99SBarry Smith 
281947c6ae99SBarry Smith     Input Parameter:
282047c6ae99SBarry Smith +   dm - the DM object
282147c6ae99SBarry Smith -   nlevels - the number of levels of refinement
282247c6ae99SBarry Smith 
282347c6ae99SBarry Smith     Output Parameter:
282447c6ae99SBarry Smith .   dmf - the refined DM hierarchy
282547c6ae99SBarry Smith 
282647c6ae99SBarry Smith     Level: developer
282747c6ae99SBarry Smith 
2828e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
282947c6ae99SBarry Smith 
283047c6ae99SBarry Smith @*/
28317087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
283247c6ae99SBarry Smith {
283347c6ae99SBarry Smith   PetscErrorCode ierr;
283447c6ae99SBarry Smith 
283547c6ae99SBarry Smith   PetscFunctionBegin;
2836171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2837ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
283847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
283947c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
284047c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
284147c6ae99SBarry Smith   } else if (dm->ops->refine) {
284247c6ae99SBarry Smith     PetscInt i;
284347c6ae99SBarry Smith 
2844ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
284547c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2846ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
284747c6ae99SBarry Smith     }
2848ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
284947c6ae99SBarry Smith   PetscFunctionReturn(0);
285047c6ae99SBarry Smith }
285147c6ae99SBarry Smith 
285247c6ae99SBarry Smith /*@C
285347c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
285447c6ae99SBarry Smith 
285547c6ae99SBarry Smith     Collective on DM
285647c6ae99SBarry Smith 
285747c6ae99SBarry Smith     Input Parameter:
285847c6ae99SBarry Smith +   dm - the DM object
285947c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
286047c6ae99SBarry Smith 
286147c6ae99SBarry Smith     Output Parameter:
286247c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
286347c6ae99SBarry Smith 
286447c6ae99SBarry Smith     Level: developer
286547c6ae99SBarry Smith 
2866e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
286747c6ae99SBarry Smith 
286847c6ae99SBarry Smith @*/
28697087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
287047c6ae99SBarry Smith {
287147c6ae99SBarry Smith   PetscErrorCode ierr;
287247c6ae99SBarry Smith 
287347c6ae99SBarry Smith   PetscFunctionBegin;
2874171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2875ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
287647c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
287747c6ae99SBarry Smith   PetscValidPointer(dmc,3);
287847c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
287947c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
288047c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
288147c6ae99SBarry Smith     PetscInt i;
288247c6ae99SBarry Smith 
2883ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
288447c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
2885ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
288647c6ae99SBarry Smith     }
2887ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
288847c6ae99SBarry Smith   PetscFunctionReturn(0);
288947c6ae99SBarry Smith }
289047c6ae99SBarry Smith 
289147c6ae99SBarry Smith /*@
2892e727c939SJed Brown    DMCreateAggregates - Gets the aggregates that map between
289347c6ae99SBarry Smith    grids associated with two DMs.
289447c6ae99SBarry Smith 
289547c6ae99SBarry Smith    Collective on DM
289647c6ae99SBarry Smith 
289747c6ae99SBarry Smith    Input Parameters:
289847c6ae99SBarry Smith +  dmc - the coarse grid DM
289947c6ae99SBarry Smith -  dmf - the fine grid DM
290047c6ae99SBarry Smith 
290147c6ae99SBarry Smith    Output Parameters:
290247c6ae99SBarry Smith .  rest - the restriction matrix (transpose of the projection matrix)
290347c6ae99SBarry Smith 
290447c6ae99SBarry Smith    Level: intermediate
290547c6ae99SBarry Smith 
290647c6ae99SBarry Smith .keywords: interpolation, restriction, multigrid
290747c6ae99SBarry Smith 
2908e727c939SJed Brown .seealso: DMRefine(), DMCreateInjection(), DMCreateInterpolation()
290947c6ae99SBarry Smith @*/
2910e727c939SJed Brown PetscErrorCode  DMCreateAggregates(DM dmc, DM dmf, Mat *rest)
291147c6ae99SBarry Smith {
291247c6ae99SBarry Smith   PetscErrorCode ierr;
291347c6ae99SBarry Smith 
291447c6ae99SBarry Smith   PetscFunctionBegin;
2915171400e9SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
2916171400e9SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
291747c6ae99SBarry Smith   ierr = (*dmc->ops->getaggregates)(dmc, dmf, rest);CHKERRQ(ierr);
291847c6ae99SBarry Smith   PetscFunctionReturn(0);
291947c6ae99SBarry Smith }
292047c6ae99SBarry Smith 
29211a266240SBarry Smith /*@C
29221a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
29231a266240SBarry Smith 
29241a266240SBarry Smith     Not Collective
29251a266240SBarry Smith 
29261a266240SBarry Smith     Input Parameters:
29271a266240SBarry Smith +   dm - the DM object
29281a266240SBarry Smith -   destroy - the destroy function
29291a266240SBarry Smith 
29301a266240SBarry Smith     Level: intermediate
29311a266240SBarry Smith 
2932e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
29331a266240SBarry Smith 
2934f07f9ceaSJed Brown @*/
29351a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
29361a266240SBarry Smith {
29371a266240SBarry Smith   PetscFunctionBegin;
2938171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29391a266240SBarry Smith   dm->ctxdestroy = destroy;
29401a266240SBarry Smith   PetscFunctionReturn(0);
29411a266240SBarry Smith }
29421a266240SBarry Smith 
2943b07ff414SBarry Smith /*@
29441b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
294547c6ae99SBarry Smith 
294647c6ae99SBarry Smith     Not Collective
294747c6ae99SBarry Smith 
294847c6ae99SBarry Smith     Input Parameters:
294947c6ae99SBarry Smith +   dm - the DM object
295047c6ae99SBarry Smith -   ctx - the user context
295147c6ae99SBarry Smith 
295247c6ae99SBarry Smith     Level: intermediate
295347c6ae99SBarry Smith 
2954e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
295547c6ae99SBarry Smith 
295647c6ae99SBarry Smith @*/
29571b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
295847c6ae99SBarry Smith {
295947c6ae99SBarry Smith   PetscFunctionBegin;
2960171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
296147c6ae99SBarry Smith   dm->ctx = ctx;
296247c6ae99SBarry Smith   PetscFunctionReturn(0);
296347c6ae99SBarry Smith }
296447c6ae99SBarry Smith 
296547c6ae99SBarry Smith /*@
29661b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
296747c6ae99SBarry Smith 
296847c6ae99SBarry Smith     Not Collective
296947c6ae99SBarry Smith 
297047c6ae99SBarry Smith     Input Parameter:
297147c6ae99SBarry Smith .   dm - the DM object
297247c6ae99SBarry Smith 
297347c6ae99SBarry Smith     Output Parameter:
297447c6ae99SBarry Smith .   ctx - the user context
297547c6ae99SBarry Smith 
297647c6ae99SBarry Smith     Level: intermediate
297747c6ae99SBarry Smith 
2978e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
297947c6ae99SBarry Smith 
298047c6ae99SBarry Smith @*/
29811b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
298247c6ae99SBarry Smith {
298347c6ae99SBarry Smith   PetscFunctionBegin;
2984171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
29851b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
298647c6ae99SBarry Smith   PetscFunctionReturn(0);
298747c6ae99SBarry Smith }
298847c6ae99SBarry Smith 
298908da532bSDmitry Karpeev /*@C
2990df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
299108da532bSDmitry Karpeev 
299208da532bSDmitry Karpeev     Logically Collective on DM
299308da532bSDmitry Karpeev 
299408da532bSDmitry Karpeev     Input Parameter:
299508da532bSDmitry Karpeev +   dm - the DM object
29960298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
299708da532bSDmitry Karpeev 
299808da532bSDmitry Karpeev     Level: intermediate
299908da532bSDmitry Karpeev 
3000835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
300108da532bSDmitry Karpeev          DMSetJacobian()
300208da532bSDmitry Karpeev 
300308da532bSDmitry Karpeev @*/
300408da532bSDmitry Karpeev PetscErrorCode  DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
300508da532bSDmitry Karpeev {
300608da532bSDmitry Karpeev   PetscFunctionBegin;
300708da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
300808da532bSDmitry Karpeev   PetscFunctionReturn(0);
300908da532bSDmitry Karpeev }
301008da532bSDmitry Karpeev 
301108da532bSDmitry Karpeev /*@
301208da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
301308da532bSDmitry Karpeev 
301408da532bSDmitry Karpeev     Not Collective
301508da532bSDmitry Karpeev 
301608da532bSDmitry Karpeev     Input Parameter:
301708da532bSDmitry Karpeev .   dm - the DM object to destroy
301808da532bSDmitry Karpeev 
301908da532bSDmitry Karpeev     Output Parameter:
302008da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
302108da532bSDmitry Karpeev 
302208da532bSDmitry Karpeev     Level: developer
302308da532bSDmitry Karpeev 
302474e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
302508da532bSDmitry Karpeev 
302608da532bSDmitry Karpeev @*/
302708da532bSDmitry Karpeev PetscErrorCode  DMHasVariableBounds(DM dm,PetscBool  *flg)
302808da532bSDmitry Karpeev {
302908da532bSDmitry Karpeev   PetscFunctionBegin;
303008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
303108da532bSDmitry Karpeev   PetscFunctionReturn(0);
303208da532bSDmitry Karpeev }
303308da532bSDmitry Karpeev 
303408da532bSDmitry Karpeev /*@C
303508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
303608da532bSDmitry Karpeev 
303708da532bSDmitry Karpeev     Logically Collective on DM
303808da532bSDmitry Karpeev 
303908da532bSDmitry Karpeev     Input Parameters:
3040907376e6SBarry Smith .   dm - the DM object
304108da532bSDmitry Karpeev 
304208da532bSDmitry Karpeev     Output parameters:
304308da532bSDmitry Karpeev +   xl - lower bound
304408da532bSDmitry Karpeev -   xu - upper bound
304508da532bSDmitry Karpeev 
3046907376e6SBarry Smith     Level: advanced
3047907376e6SBarry Smith 
3048907376e6SBarry Smith     Notes: This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
304908da532bSDmitry Karpeev 
305074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
305108da532bSDmitry Karpeev 
305208da532bSDmitry Karpeev @*/
305308da532bSDmitry Karpeev PetscErrorCode  DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
305408da532bSDmitry Karpeev {
305508da532bSDmitry Karpeev   PetscErrorCode ierr;
30565fd66863SKarl Rupp 
305708da532bSDmitry Karpeev   PetscFunctionBegin;
305808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
305908da532bSDmitry Karpeev   PetscValidHeaderSpecific(xu,VEC_CLASSID,2);
306008da532bSDmitry Karpeev   if (dm->ops->computevariablebounds) {
306108da532bSDmitry Karpeev     ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
30628865f1eaSKarl Rupp   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "This DM is incapable of computing variable bounds.");
306308da532bSDmitry Karpeev   PetscFunctionReturn(0);
306408da532bSDmitry Karpeev }
306508da532bSDmitry Karpeev 
3066b0ae01b7SPeter Brune /*@
3067b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3068b0ae01b7SPeter Brune 
3069b0ae01b7SPeter Brune     Not Collective
3070b0ae01b7SPeter Brune 
3071b0ae01b7SPeter Brune     Input Parameter:
3072b0ae01b7SPeter Brune .   dm - the DM object
3073b0ae01b7SPeter Brune 
3074b0ae01b7SPeter Brune     Output Parameter:
3075b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3076b0ae01b7SPeter Brune 
3077b0ae01b7SPeter Brune     Level: developer
3078b0ae01b7SPeter Brune 
3079b0ae01b7SPeter Brune .seealso DMHasFunction(), DMCreateColoring()
3080b0ae01b7SPeter Brune 
3081b0ae01b7SPeter Brune @*/
3082b0ae01b7SPeter Brune PetscErrorCode  DMHasColoring(DM dm,PetscBool  *flg)
3083b0ae01b7SPeter Brune {
3084b0ae01b7SPeter Brune   PetscFunctionBegin;
3085b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3086b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3087b0ae01b7SPeter Brune }
3088b0ae01b7SPeter Brune 
30893ad4599aSBarry Smith /*@
30903ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
30913ad4599aSBarry Smith 
30923ad4599aSBarry Smith     Not Collective
30933ad4599aSBarry Smith 
30943ad4599aSBarry Smith     Input Parameter:
30953ad4599aSBarry Smith .   dm - the DM object
30963ad4599aSBarry Smith 
30973ad4599aSBarry Smith     Output Parameter:
30983ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
30993ad4599aSBarry Smith 
31003ad4599aSBarry Smith     Level: developer
31013ad4599aSBarry Smith 
31023ad4599aSBarry Smith .seealso DMHasFunction(), DMCreateRestriction()
31033ad4599aSBarry Smith 
31043ad4599aSBarry Smith @*/
31053ad4599aSBarry Smith PetscErrorCode  DMHasCreateRestriction(DM dm,PetscBool  *flg)
31063ad4599aSBarry Smith {
31073ad4599aSBarry Smith   PetscFunctionBegin;
31083ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
31093ad4599aSBarry Smith   PetscFunctionReturn(0);
31103ad4599aSBarry Smith }
31113ad4599aSBarry Smith 
3112a7058e45SLawrence Mitchell 
3113a7058e45SLawrence Mitchell /*@
3114a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3115a7058e45SLawrence Mitchell 
3116a7058e45SLawrence Mitchell     Not Collective
3117a7058e45SLawrence Mitchell 
3118a7058e45SLawrence Mitchell     Input Parameter:
3119a7058e45SLawrence Mitchell .   dm - the DM object
3120a7058e45SLawrence Mitchell 
3121a7058e45SLawrence Mitchell     Output Parameter:
3122a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3123a7058e45SLawrence Mitchell 
3124a7058e45SLawrence Mitchell     Level: developer
3125a7058e45SLawrence Mitchell 
3126a7058e45SLawrence Mitchell .seealso DMHasFunction(), DMCreateInjection()
3127a7058e45SLawrence Mitchell 
3128a7058e45SLawrence Mitchell @*/
3129a7058e45SLawrence Mitchell PetscErrorCode  DMHasCreateInjection(DM dm,PetscBool  *flg)
3130a7058e45SLawrence Mitchell {
31314a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
3132a7058e45SLawrence Mitchell   PetscFunctionBegin;
31334a7a4c06SLawrence Mitchell   if (!dm->ops->hascreateinjection) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DMHasCreateInjection not implemented for this type");
31344a7a4c06SLawrence Mitchell   ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
3135a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3136a7058e45SLawrence Mitchell }
3137a7058e45SLawrence Mitchell 
3138748fac09SDmitry Karpeev /*@C
313908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
314008da532bSDmitry Karpeev 
314108da532bSDmitry Karpeev     Collective on DM
314208da532bSDmitry Karpeev 
314308da532bSDmitry Karpeev     Input Parameter:
314408da532bSDmitry Karpeev +   dm - the DM object
31450298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
314608da532bSDmitry Karpeev 
314708da532bSDmitry Karpeev     Level: developer
314808da532bSDmitry Karpeev 
314974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
315008da532bSDmitry Karpeev 
315108da532bSDmitry Karpeev @*/
315208da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
315308da532bSDmitry Karpeev {
315408da532bSDmitry Karpeev   PetscErrorCode ierr;
31555fd66863SKarl Rupp 
315608da532bSDmitry Karpeev   PetscFunctionBegin;
315708da532bSDmitry Karpeev   if (x) {
315808da532bSDmitry Karpeev     if (!dm->x) {
315908da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
316008da532bSDmitry Karpeev     }
316108da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
31628865f1eaSKarl Rupp   } else if (dm->x) {
316308da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
316408da532bSDmitry Karpeev   }
316508da532bSDmitry Karpeev   PetscFunctionReturn(0);
316608da532bSDmitry Karpeev }
316708da532bSDmitry Karpeev 
31680298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3169264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3170264ace61SBarry Smith 
3171264ace61SBarry Smith /*@C
3172264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3173264ace61SBarry Smith 
3174264ace61SBarry Smith   Collective on DM
3175264ace61SBarry Smith 
3176264ace61SBarry Smith   Input Parameters:
3177264ace61SBarry Smith + dm     - The DM object
3178264ace61SBarry Smith - method - The name of the DM type
3179264ace61SBarry Smith 
3180264ace61SBarry Smith   Options Database Key:
3181264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3182264ace61SBarry Smith 
3183264ace61SBarry Smith   Notes:
3184e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3185264ace61SBarry Smith 
3186264ace61SBarry Smith   Level: intermediate
3187264ace61SBarry Smith 
3188264ace61SBarry Smith .keywords: DM, set, type
3189264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3190264ace61SBarry Smith @*/
319119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3192264ace61SBarry Smith {
3193264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3194264ace61SBarry Smith   PetscBool      match;
3195264ace61SBarry Smith   PetscErrorCode ierr;
3196264ace61SBarry Smith 
3197264ace61SBarry Smith   PetscFunctionBegin;
3198264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3199251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3200264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3201264ace61SBarry Smith 
32020f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
32031c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3204ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3205264ace61SBarry Smith 
3206264ace61SBarry Smith   if (dm->ops->destroy) {
3207264ace61SBarry Smith     ierr             = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
32080298fd71SBarry Smith     dm->ops->destroy = NULL;
3209264ace61SBarry Smith   }
3210264ace61SBarry Smith   ierr = (*r)(dm);CHKERRQ(ierr);
3211264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3212264ace61SBarry Smith   PetscFunctionReturn(0);
3213264ace61SBarry Smith }
3214264ace61SBarry Smith 
3215264ace61SBarry Smith /*@C
3216264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3217264ace61SBarry Smith 
3218264ace61SBarry Smith   Not Collective
3219264ace61SBarry Smith 
3220264ace61SBarry Smith   Input Parameter:
3221264ace61SBarry Smith . dm  - The DM
3222264ace61SBarry Smith 
3223264ace61SBarry Smith   Output Parameter:
3224264ace61SBarry Smith . type - The DM type name
3225264ace61SBarry Smith 
3226264ace61SBarry Smith   Level: intermediate
3227264ace61SBarry Smith 
3228264ace61SBarry Smith .keywords: DM, get, type, name
3229264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3230264ace61SBarry Smith @*/
323119fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3232264ace61SBarry Smith {
3233264ace61SBarry Smith   PetscErrorCode ierr;
3234264ace61SBarry Smith 
3235264ace61SBarry Smith   PetscFunctionBegin;
3236264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3237c959eef4SJed Brown   PetscValidPointer(type,2);
3238607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3239264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3240264ace61SBarry Smith   PetscFunctionReturn(0);
3241264ace61SBarry Smith }
3242264ace61SBarry Smith 
324367a56275SMatthew G Knepley /*@C
324467a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
324567a56275SMatthew G Knepley 
324667a56275SMatthew G Knepley   Collective on DM
324767a56275SMatthew G Knepley 
324867a56275SMatthew G Knepley   Input Parameters:
324967a56275SMatthew G Knepley + dm - the DM
325067a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
325167a56275SMatthew G Knepley 
325267a56275SMatthew G Knepley   Output Parameter:
325367a56275SMatthew G Knepley . M - pointer to new DM
325467a56275SMatthew G Knepley 
325567a56275SMatthew G Knepley   Notes:
325667a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
325767a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
325867a56275SMatthew G Knepley   of the input DM.
325967a56275SMatthew G Knepley 
326067a56275SMatthew G Knepley   Level: intermediate
326167a56275SMatthew G Knepley 
326267a56275SMatthew G Knepley .seealso: DMCreate()
326367a56275SMatthew G Knepley @*/
326419fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
326567a56275SMatthew G Knepley {
326667a56275SMatthew G Knepley   DM             B;
326767a56275SMatthew G Knepley   char           convname[256];
3268c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
326967a56275SMatthew G Knepley   PetscErrorCode ierr;
327067a56275SMatthew G Knepley 
327167a56275SMatthew G Knepley   PetscFunctionBegin;
327267a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
327367a56275SMatthew G Knepley   PetscValidType(dm,1);
327467a56275SMatthew G Knepley   PetscValidPointer(M,3);
3275251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3276c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3277c067b6caSMatthew G. Knepley   if (sametype) {
3278c067b6caSMatthew G. Knepley     *M   = dm;
3279c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3280c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3281c067b6caSMatthew G. Knepley   } else {
32820298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
328367a56275SMatthew G Knepley 
328467a56275SMatthew G Knepley     /*
328567a56275SMatthew G Knepley        Order of precedence:
328667a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
328767a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
328867a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
328967a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
329067a56275SMatthew G Knepley        5) Use a really basic converter.
329167a56275SMatthew G Knepley     */
329267a56275SMatthew G Knepley 
329367a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3294a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3295a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3296a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3297a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3298a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
32990005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
330067a56275SMatthew G Knepley     if (conv) goto foundconv;
330167a56275SMatthew G Knepley 
330267a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
330382f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
330467a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3305a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3306a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3307a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3308a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3309a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
33100005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
331167a56275SMatthew G Knepley     if (conv) {
3312fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
331367a56275SMatthew G Knepley       goto foundconv;
331467a56275SMatthew G Knepley     }
331567a56275SMatthew G Knepley 
331667a56275SMatthew G Knepley #if 0
331767a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
331867a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3319fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
332067a56275SMatthew G Knepley     if (conv) goto foundconv;
332167a56275SMatthew G Knepley 
332267a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
332367a56275SMatthew G Knepley     if (dm->ops->convert) {
332467a56275SMatthew G Knepley       conv = dm->ops->convert;
332567a56275SMatthew G Knepley     }
332667a56275SMatthew G Knepley     if (conv) goto foundconv;
332767a56275SMatthew G Knepley #endif
332867a56275SMatthew G Knepley 
332967a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
333082f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
333167a56275SMatthew G Knepley 
333267a56275SMatthew G Knepley foundconv:
333367a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
333467a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
333512fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
333690b157c4SStefano Zampini     {
333790b157c4SStefano Zampini       PetscBool             isper;
333812fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
333912fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
334090b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
334190b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
334212fa691eSMatthew G. Knepley     }
334367a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
334467a56275SMatthew G Knepley   }
334567a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
334667a56275SMatthew G Knepley   PetscFunctionReturn(0);
334767a56275SMatthew G Knepley }
3348264ace61SBarry Smith 
3349264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3350264ace61SBarry Smith 
3351264ace61SBarry Smith /*@C
33521c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
33531c84c290SBarry Smith 
33541c84c290SBarry Smith   Not Collective
33551c84c290SBarry Smith 
33561c84c290SBarry Smith   Input Parameters:
33571c84c290SBarry Smith + name        - The name of a new user-defined creation routine
33581c84c290SBarry Smith - create_func - The creation routine itself
33591c84c290SBarry Smith 
33601c84c290SBarry Smith   Notes:
33611c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
33621c84c290SBarry Smith 
33631c84c290SBarry Smith 
33641c84c290SBarry Smith   Sample usage:
33651c84c290SBarry Smith .vb
3366bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
33671c84c290SBarry Smith .ve
33681c84c290SBarry Smith 
33691c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
33701c84c290SBarry Smith .vb
33711c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
33721c84c290SBarry Smith     DMSetType(DM,"my_da");
33731c84c290SBarry Smith .ve
33741c84c290SBarry Smith    or at runtime via the option
33751c84c290SBarry Smith .vb
33761c84c290SBarry Smith     -da_type my_da
33771c84c290SBarry Smith .ve
3378264ace61SBarry Smith 
3379264ace61SBarry Smith   Level: advanced
33801c84c290SBarry Smith 
33811c84c290SBarry Smith .keywords: DM, register
3382bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
33831c84c290SBarry Smith 
3384264ace61SBarry Smith @*/
3385bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3386264ace61SBarry Smith {
3387264ace61SBarry Smith   PetscErrorCode ierr;
3388264ace61SBarry Smith 
3389264ace61SBarry Smith   PetscFunctionBegin;
3390a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3391264ace61SBarry Smith   PetscFunctionReturn(0);
3392264ace61SBarry Smith }
3393264ace61SBarry Smith 
3394b859378eSBarry Smith /*@C
339555849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3396b859378eSBarry Smith 
3397b859378eSBarry Smith   Collective on PetscViewer
3398b859378eSBarry Smith 
3399b859378eSBarry Smith   Input Parameters:
3400b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3401b859378eSBarry Smith            some related function before a call to DMLoad().
3402b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3403b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3404b859378eSBarry Smith 
3405b859378eSBarry Smith    Level: intermediate
3406b859378eSBarry Smith 
3407b859378eSBarry Smith   Notes:
340855849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3409b859378eSBarry Smith 
3410b859378eSBarry Smith   Notes for advanced users:
3411b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3412b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3413b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3414b859378eSBarry Smith   format is
3415b859378eSBarry Smith .vb
3416b859378eSBarry Smith      has not yet been determined
3417b859378eSBarry Smith .ve
3418b859378eSBarry Smith 
3419b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3420b859378eSBarry Smith @*/
3421b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3422b859378eSBarry Smith {
34239331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3424b859378eSBarry Smith   PetscErrorCode ierr;
3425b859378eSBarry Smith 
3426b859378eSBarry Smith   PetscFunctionBegin;
3427b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3428b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
342932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
34309331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
34319331c7a4SMatthew G. Knepley   if (isbinary) {
34329331c7a4SMatthew G. Knepley     PetscInt classid;
34339331c7a4SMatthew G. Knepley     char     type[256];
3434b859378eSBarry Smith 
3435060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
34369200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3437060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
343832c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
34399331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
34409331c7a4SMatthew G. Knepley   } else if (ishdf5) {
34419331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
34429331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3443b859378eSBarry Smith   PetscFunctionReturn(0);
3444b859378eSBarry Smith }
3445b859378eSBarry Smith 
34467da65231SMatthew G Knepley /******************************** FEM Support **********************************/
34477da65231SMatthew G Knepley 
3448a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3449a6dfd86eSKarl Rupp {
34501d47ebbbSSatish Balay   PetscInt       f;
34511b30c384SMatthew G Knepley   PetscErrorCode ierr;
34521b30c384SMatthew G Knepley 
34537da65231SMatthew G Knepley   PetscFunctionBegin;
345474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
34551d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
345657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
34577da65231SMatthew G Knepley   }
34587da65231SMatthew G Knepley   PetscFunctionReturn(0);
34597da65231SMatthew G Knepley }
34607da65231SMatthew G Knepley 
3461a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3462a6dfd86eSKarl Rupp {
34631b30c384SMatthew G Knepley   PetscInt       f, g;
34647da65231SMatthew G Knepley   PetscErrorCode ierr;
34657da65231SMatthew G Knepley 
34667da65231SMatthew G Knepley   PetscFunctionBegin;
346774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
34681d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
346974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
34701d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3471e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
34727da65231SMatthew G Knepley     }
347374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
34747da65231SMatthew G Knepley   }
34757da65231SMatthew G Knepley   PetscFunctionReturn(0);
34767da65231SMatthew G Knepley }
3477e7c4fc90SDmitry Karpeev 
34786113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3479e759306cSMatthew G. Knepley {
34800c5b8624SToby Isaac   PetscInt          localSize, bs;
34810c5b8624SToby Isaac   PetscMPIInt       size;
34820c5b8624SToby Isaac   Vec               x, xglob;
34830c5b8624SToby Isaac   const PetscScalar *xarray;
3484e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3485e759306cSMatthew G. Knepley 
3486e759306cSMatthew G. Knepley   PetscFunctionBegin;
34879852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3488e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3489e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
34906113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
34910c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
34920c5b8624SToby Isaac   if (size > 1) {
34930c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
34940c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
34950c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
34960c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
34970c5b8624SToby Isaac   } else {
34980c5b8624SToby Isaac     xglob = x;
34990c5b8624SToby Isaac   }
35000c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
35010c5b8624SToby Isaac   if (size > 1) {
35020c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
35030c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
35040c5b8624SToby Isaac   }
3505e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3506e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3507e759306cSMatthew G. Knepley }
3508e759306cSMatthew G. Knepley 
350988ed4aceSMatthew G Knepley /*@
3510*e87a4003SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.
351188ed4aceSMatthew G Knepley 
351288ed4aceSMatthew G Knepley   Input Parameter:
351388ed4aceSMatthew G Knepley . dm - The DM
351488ed4aceSMatthew G Knepley 
351588ed4aceSMatthew G Knepley   Output Parameter:
351688ed4aceSMatthew G Knepley . section - The PetscSection
351788ed4aceSMatthew G Knepley 
351888ed4aceSMatthew G Knepley   Level: intermediate
351988ed4aceSMatthew G Knepley 
352088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
352188ed4aceSMatthew G Knepley 
3522*e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
352388ed4aceSMatthew G Knepley @*/
3524*e87a4003SBarry Smith PetscErrorCode DMGetSection(DM dm, PetscSection *section)
35250adebc6cSBarry Smith {
3526fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3527fd59a867SMatthew G. Knepley 
352888ed4aceSMatthew G Knepley   PetscFunctionBegin;
352988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
353088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
35312f0f8703SMatthew G. Knepley   if (!dm->defaultSection && dm->ops->createdefaultsection) {
35322f0f8703SMatthew G. Knepley     ierr = (*dm->ops->createdefaultsection)(dm);CHKERRQ(ierr);
3533ae71db08SMatthew G. Knepley     if (dm->defaultSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->defaultSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
35342f0f8703SMatthew G. Knepley   }
353588ed4aceSMatthew G Knepley   *section = dm->defaultSection;
353688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
353788ed4aceSMatthew G Knepley }
353888ed4aceSMatthew G Knepley 
353988ed4aceSMatthew G Knepley /*@
3540*e87a4003SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.
354188ed4aceSMatthew G Knepley 
354288ed4aceSMatthew G Knepley   Input Parameters:
354388ed4aceSMatthew G Knepley + dm - The DM
354488ed4aceSMatthew G Knepley - section - The PetscSection
354588ed4aceSMatthew G Knepley 
354688ed4aceSMatthew G Knepley   Level: intermediate
354788ed4aceSMatthew G Knepley 
354888ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
354988ed4aceSMatthew G Knepley 
3550*e87a4003SBarry Smith .seealso: DMSetSection(), DMGetGlobalSection()
355188ed4aceSMatthew G Knepley @*/
3552*e87a4003SBarry Smith PetscErrorCode DMSetSection(DM dm, PetscSection section)
35530adebc6cSBarry Smith {
3554c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3555af122d2aSMatthew G Knepley   PetscInt       f;
355688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
355788ed4aceSMatthew G Knepley 
355888ed4aceSMatthew G Knepley   PetscFunctionBegin;
355988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3560c473ab19SMatthew G. Knepley   if (section) {
35611d799100SJed Brown     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
35621d799100SJed Brown     ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3563c473ab19SMatthew G. Knepley   }
356488ed4aceSMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultSection);CHKERRQ(ierr);
356588ed4aceSMatthew G Knepley   dm->defaultSection = section;
3566c473ab19SMatthew G. Knepley   if (section) {ierr = PetscSectionGetNumFields(dm->defaultSection, &numFields);CHKERRQ(ierr);}
3567af122d2aSMatthew G Knepley   if (numFields) {
3568af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
3569af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
35700f21e855SMatthew G. Knepley       PetscObject disc;
3571af122d2aSMatthew G Knepley       const char *name;
3572af122d2aSMatthew G Knepley 
3573af122d2aSMatthew G Knepley       ierr = PetscSectionGetFieldName(dm->defaultSection, f, &name);CHKERRQ(ierr);
35740f21e855SMatthew G. Knepley       ierr = DMGetField(dm, f, &disc);CHKERRQ(ierr);
35750f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
3576af122d2aSMatthew G Knepley     }
3577af122d2aSMatthew G Knepley   }
3578*e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
35791d799100SJed Brown   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
358088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
358188ed4aceSMatthew G Knepley }
358288ed4aceSMatthew G Knepley 
35839435951eSToby Isaac /*@
35849435951eSToby Isaac   DMGetDefaultConstraints - Get the PetscSection and Mat the specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
35859435951eSToby Isaac 
3586e228b242SToby Isaac   not collective
3587e228b242SToby Isaac 
35889435951eSToby Isaac   Input Parameter:
35899435951eSToby Isaac . dm - The DM
35909435951eSToby Isaac 
35919435951eSToby Isaac   Output Parameter:
35929435951eSToby 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.
35939435951eSToby 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.
35949435951eSToby Isaac 
35959435951eSToby Isaac   Level: advanced
35969435951eSToby Isaac 
35979435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
35989435951eSToby Isaac 
35999435951eSToby Isaac .seealso: DMSetDefaultConstraints()
36009435951eSToby Isaac @*/
36019435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
36029435951eSToby Isaac {
36039435951eSToby Isaac   PetscErrorCode ierr;
36049435951eSToby Isaac 
36059435951eSToby Isaac   PetscFunctionBegin;
36069435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
36079435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
360845a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
360945a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
36109435951eSToby Isaac   PetscFunctionReturn(0);
36119435951eSToby Isaac }
36129435951eSToby Isaac 
36139435951eSToby Isaac /*@
36149435951eSToby Isaac   DMSetDefaultConstraints - Set the PetscSection and Mat the specify the local constraint interpolation.
36159435951eSToby Isaac 
36169435951eSToby 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().
36179435951eSToby Isaac 
36189435951eSToby 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.
36199435951eSToby Isaac 
3620e228b242SToby Isaac   collective on dm
3621e228b242SToby Isaac 
36229435951eSToby Isaac   Input Parameters:
36239435951eSToby Isaac + dm - The DM
3624e228b242SToby 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).
3625e228b242SToby 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).
36269435951eSToby Isaac 
36279435951eSToby Isaac   Level: advanced
36289435951eSToby Isaac 
36299435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
36309435951eSToby Isaac 
36319435951eSToby Isaac .seealso: DMGetDefaultConstraints()
36329435951eSToby Isaac @*/
36339435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
36349435951eSToby Isaac {
3635e228b242SToby Isaac   PetscMPIInt result;
36369435951eSToby Isaac   PetscErrorCode ierr;
36379435951eSToby Isaac 
36389435951eSToby Isaac   PetscFunctionBegin;
36399435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3640e228b242SToby Isaac   if (section) {
3641e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
3642e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
3643f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
3644e228b242SToby Isaac   }
3645e228b242SToby Isaac   if (mat) {
3646e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
3647e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
3648f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
3649e228b242SToby Isaac   }
36509435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
36519435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
36529435951eSToby Isaac   dm->defaultConstraintSection = section;
36539435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
36549435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
36559435951eSToby Isaac   dm->defaultConstraintMat = mat;
36569435951eSToby Isaac   PetscFunctionReturn(0);
36579435951eSToby Isaac }
36589435951eSToby Isaac 
3659f741bcd2SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3660507e4973SMatthew G. Knepley /*
3661507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
3662507e4973SMatthew G. Knepley 
3663507e4973SMatthew G. Knepley   Input Parameters:
3664507e4973SMatthew G. Knepley + dm - The DM
3665507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
3666507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
3667507e4973SMatthew G. Knepley 
3668507e4973SMatthew G. Knepley   Level: intermediate
3669507e4973SMatthew G. Knepley 
3670507e4973SMatthew G. Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
3671507e4973SMatthew G. Knepley */
3672f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
3673507e4973SMatthew G. Knepley {
3674507e4973SMatthew G. Knepley   MPI_Comm        comm;
3675507e4973SMatthew G. Knepley   PetscLayout     layout;
3676507e4973SMatthew G. Knepley   const PetscInt *ranges;
3677507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
3678507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
3679507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
3680507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
3681507e4973SMatthew G. Knepley 
3682507e4973SMatthew G. Knepley   PetscFunctionBegin;
3683507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
3684507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3685507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
3686507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
3687507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
3688507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
3689507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
3690507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
3691507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
3692507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
3693507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3694507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
3695f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
3696507e4973SMatthew G. Knepley 
3697507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
3698507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
3699507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
3700507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
3701507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3702507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
3703507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
3704507e4973SMatthew 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;}
3705507e4973SMatthew 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;}
3706507e4973SMatthew G. Knepley     if (gdof < 0) {
3707507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
3708507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
3709507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
3710507e4973SMatthew G. Knepley 
3711507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
3712507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
3713507e4973SMatthew 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;}
3714507e4973SMatthew G. Knepley       }
3715507e4973SMatthew G. Knepley     }
3716507e4973SMatthew G. Knepley   }
3717507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
3718507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
3719b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
3720507e4973SMatthew G. Knepley   if (!gvalid) {
3721507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
3722507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
3723507e4973SMatthew G. Knepley   }
3724507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
3725507e4973SMatthew G. Knepley }
3726f741bcd2SMatthew G. Knepley #endif
3727507e4973SMatthew G. Knepley 
372888ed4aceSMatthew G Knepley /*@
3729*e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
373088ed4aceSMatthew G Knepley 
37318b1ab98fSJed Brown   Collective on DM
37328b1ab98fSJed Brown 
373388ed4aceSMatthew G Knepley   Input Parameter:
373488ed4aceSMatthew G Knepley . dm - The DM
373588ed4aceSMatthew G Knepley 
373688ed4aceSMatthew G Knepley   Output Parameter:
373788ed4aceSMatthew G Knepley . section - The PetscSection
373888ed4aceSMatthew G Knepley 
373988ed4aceSMatthew G Knepley   Level: intermediate
374088ed4aceSMatthew G Knepley 
374188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
374288ed4aceSMatthew G Knepley 
3743*e87a4003SBarry Smith .seealso: DMSetSection(), DMGetSection()
374488ed4aceSMatthew G Knepley @*/
3745*e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
37460adebc6cSBarry Smith {
374788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
374888ed4aceSMatthew G Knepley 
374988ed4aceSMatthew G Knepley   PetscFunctionBegin;
375088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
375188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
375288ed4aceSMatthew G Knepley   if (!dm->defaultGlobalSection) {
3753fd59a867SMatthew G. Knepley     PetscSection s;
3754fd59a867SMatthew G. Knepley 
3755*e87a4003SBarry Smith     ierr = DMGetSection(dm, &s);CHKERRQ(ierr);
3756fd59a867SMatthew 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");
3757fd59a867SMatthew 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");
375815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->defaultGlobalSection);CHKERRQ(ierr);
3759cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
3760ce94432eSBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->defaultGlobalSection, &dm->map);CHKERRQ(ierr);
3761685405a1SBarry Smith     ierr = PetscSectionViewFromOptions(dm->defaultGlobalSection, NULL, "-global_section_view");CHKERRQ(ierr);
376288ed4aceSMatthew G Knepley   }
376388ed4aceSMatthew G Knepley   *section = dm->defaultGlobalSection;
376488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
376588ed4aceSMatthew G Knepley }
376688ed4aceSMatthew G Knepley 
3767b21d0597SMatthew G Knepley /*@
3768*e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
3769b21d0597SMatthew G Knepley 
3770b21d0597SMatthew G Knepley   Input Parameters:
3771b21d0597SMatthew G Knepley + dm - The DM
37725080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
3773b21d0597SMatthew G Knepley 
3774b21d0597SMatthew G Knepley   Level: intermediate
3775b21d0597SMatthew G Knepley 
3776b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
3777b21d0597SMatthew G Knepley 
3778*e87a4003SBarry Smith .seealso: DMGetGlobalSection(), DMSetSection()
3779b21d0597SMatthew G Knepley @*/
3780*e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
37810adebc6cSBarry Smith {
3782b21d0597SMatthew G Knepley   PetscErrorCode ierr;
3783b21d0597SMatthew G Knepley 
3784b21d0597SMatthew G Knepley   PetscFunctionBegin;
3785b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
37865080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
37871d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
3788b21d0597SMatthew G Knepley   ierr = PetscSectionDestroy(&dm->defaultGlobalSection);CHKERRQ(ierr);
3789b21d0597SMatthew G Knepley   dm->defaultGlobalSection = section;
3790507e4973SMatthew G. Knepley #ifdef PETSC_USE_DEBUG
3791f741bcd2SMatthew G. Knepley   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->defaultSection, section);CHKERRQ(ierr);}
3792507e4973SMatthew G. Knepley #endif
3793b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3794b21d0597SMatthew G Knepley }
3795b21d0597SMatthew G Knepley 
379688ed4aceSMatthew G Knepley /*@
379788ed4aceSMatthew G Knepley   DMGetDefaultSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
379888ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
379988ed4aceSMatthew G Knepley 
380088ed4aceSMatthew G Knepley   Input Parameter:
380188ed4aceSMatthew G Knepley . dm - The DM
380288ed4aceSMatthew G Knepley 
380388ed4aceSMatthew G Knepley   Output Parameter:
380488ed4aceSMatthew G Knepley . sf - The PetscSF
380588ed4aceSMatthew G Knepley 
380688ed4aceSMatthew G Knepley   Level: intermediate
380788ed4aceSMatthew G Knepley 
380888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
380988ed4aceSMatthew G Knepley 
381088ed4aceSMatthew G Knepley .seealso: DMSetDefaultSF(), DMCreateDefaultSF()
381188ed4aceSMatthew G Knepley @*/
38120adebc6cSBarry Smith PetscErrorCode DMGetDefaultSF(DM dm, PetscSF *sf)
38130adebc6cSBarry Smith {
381488ed4aceSMatthew G Knepley   PetscInt       nroots;
381588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
381688ed4aceSMatthew G Knepley 
381788ed4aceSMatthew G Knepley   PetscFunctionBegin;
381888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
381988ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
38200298fd71SBarry Smith   ierr = PetscSFGetGraph(dm->defaultSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
382188ed4aceSMatthew G Knepley   if (nroots < 0) {
382288ed4aceSMatthew G Knepley     PetscSection section, gSection;
382388ed4aceSMatthew G Knepley 
3824*e87a4003SBarry Smith     ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
382531ea6d37SMatthew G Knepley     if (section) {
3826*e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
382788ed4aceSMatthew G Knepley       ierr = DMCreateDefaultSF(dm, section, gSection);CHKERRQ(ierr);
382831ea6d37SMatthew G Knepley     } else {
38290298fd71SBarry Smith       *sf = NULL;
383031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
383131ea6d37SMatthew G Knepley     }
383288ed4aceSMatthew G Knepley   }
383388ed4aceSMatthew G Knepley   *sf = dm->defaultSF;
383488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
383588ed4aceSMatthew G Knepley }
383688ed4aceSMatthew G Knepley 
383788ed4aceSMatthew G Knepley /*@
383888ed4aceSMatthew G Knepley   DMSetDefaultSF - Set the PetscSF encoding the parallel dof overlap for the DM
383988ed4aceSMatthew G Knepley 
384088ed4aceSMatthew G Knepley   Input Parameters:
384188ed4aceSMatthew G Knepley + dm - The DM
384288ed4aceSMatthew G Knepley - sf - The PetscSF
384388ed4aceSMatthew G Knepley 
384488ed4aceSMatthew G Knepley   Level: intermediate
384588ed4aceSMatthew G Knepley 
384688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
384788ed4aceSMatthew G Knepley 
384888ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMCreateDefaultSF()
384988ed4aceSMatthew G Knepley @*/
38500adebc6cSBarry Smith PetscErrorCode DMSetDefaultSF(DM dm, PetscSF sf)
38510adebc6cSBarry Smith {
385288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
385388ed4aceSMatthew G Knepley 
385488ed4aceSMatthew G Knepley   PetscFunctionBegin;
385588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
385688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
385788ed4aceSMatthew G Knepley   ierr          = PetscSFDestroy(&dm->defaultSF);CHKERRQ(ierr);
385888ed4aceSMatthew G Knepley   dm->defaultSF = sf;
385988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
386088ed4aceSMatthew G Knepley }
386188ed4aceSMatthew G Knepley 
386288ed4aceSMatthew G Knepley /*@C
386388ed4aceSMatthew G Knepley   DMCreateDefaultSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
386488ed4aceSMatthew G Knepley   describing the data layout.
386588ed4aceSMatthew G Knepley 
386688ed4aceSMatthew G Knepley   Input Parameters:
386788ed4aceSMatthew G Knepley + dm - The DM
386888ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
386988ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
387088ed4aceSMatthew G Knepley 
387188ed4aceSMatthew G Knepley   Level: intermediate
387288ed4aceSMatthew G Knepley 
387388ed4aceSMatthew G Knepley .seealso: DMGetDefaultSF(), DMSetDefaultSF()
387488ed4aceSMatthew G Knepley @*/
387588ed4aceSMatthew G Knepley PetscErrorCode DMCreateDefaultSF(DM dm, PetscSection localSection, PetscSection globalSection)
387688ed4aceSMatthew G Knepley {
387782f516ccSBarry Smith   MPI_Comm       comm;
387888ed4aceSMatthew G Knepley   PetscLayout    layout;
387988ed4aceSMatthew G Knepley   const PetscInt *ranges;
388088ed4aceSMatthew G Knepley   PetscInt       *local;
388188ed4aceSMatthew G Knepley   PetscSFNode    *remote;
3882ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
388388ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
388488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
388588ed4aceSMatthew G Knepley 
388688ed4aceSMatthew G Knepley   PetscFunctionBegin;
388782f516ccSBarry Smith   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
388888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
388988ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
389088ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
389188ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
389288ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
389388ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
389488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
389588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
389688ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
389788ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
3898ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
38996636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
390088ed4aceSMatthew G Knepley 
39016636e97aSMatthew G Knepley     ierr     = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39026636e97aSMatthew G Knepley     ierr     = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
3903235fbf56SMatthew 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));
39046636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
390588ed4aceSMatthew G Knepley   }
3906785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
3907785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
390888ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
39091f588964SMatthew G Knepley     const PetscInt *cind;
39106636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
391188ed4aceSMatthew G Knepley 
391288ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
391388ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
391488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
391588ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
391688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
39176636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
391888ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
39196636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
39206636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
39216636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
3922057b4bcdSMatthew 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);
39236636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
39246636e97aSMatthew G Knepley     }
392588ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
392688ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
392788ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
392888ed4aceSMatthew G Knepley     }
392988ed4aceSMatthew G Knepley     if (gdof < 0) {
39306636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
393188ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
393288ed4aceSMatthew G Knepley 
393305376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
393431d3f06eSJed Brown         if (r < 0) r = -(r+2);
393505376888SMatthew 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);
393688ed4aceSMatthew G Knepley         remote[l].rank  = r;
393788ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
393888ed4aceSMatthew G Knepley       }
393988ed4aceSMatthew G Knepley     } else {
39406636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
394188ed4aceSMatthew G Knepley         remote[l].rank  = rank;
394288ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
394388ed4aceSMatthew G Knepley       }
394488ed4aceSMatthew G Knepley     }
394588ed4aceSMatthew G Knepley   }
39466636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
394788ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
394888ed4aceSMatthew G Knepley   ierr = PetscSFSetGraph(dm->defaultSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
394988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
395088ed4aceSMatthew G Knepley }
3951af122d2aSMatthew G Knepley 
3952b21d0597SMatthew G Knepley /*@
3953b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
3954b21d0597SMatthew G Knepley 
3955b21d0597SMatthew G Knepley   Input Parameter:
3956b21d0597SMatthew G Knepley . dm - The DM
3957b21d0597SMatthew G Knepley 
3958b21d0597SMatthew G Knepley   Output Parameter:
3959b21d0597SMatthew G Knepley . sf - The PetscSF
3960b21d0597SMatthew G Knepley 
3961b21d0597SMatthew G Knepley   Level: intermediate
3962b21d0597SMatthew G Knepley 
3963b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
3964b21d0597SMatthew G Knepley 
3965057b4bcdSMatthew G Knepley .seealso: DMSetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3966b21d0597SMatthew G Knepley @*/
39670adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
39680adebc6cSBarry Smith {
3969b21d0597SMatthew G Knepley   PetscFunctionBegin;
3970b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3971b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
3972b21d0597SMatthew G Knepley   *sf = dm->sf;
3973b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
3974b21d0597SMatthew G Knepley }
3975b21d0597SMatthew G Knepley 
3976057b4bcdSMatthew G Knepley /*@
3977057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
3978057b4bcdSMatthew G Knepley 
3979057b4bcdSMatthew G Knepley   Input Parameters:
3980057b4bcdSMatthew G Knepley + dm - The DM
3981057b4bcdSMatthew G Knepley - sf - The PetscSF
3982057b4bcdSMatthew G Knepley 
3983057b4bcdSMatthew G Knepley   Level: intermediate
3984057b4bcdSMatthew G Knepley 
3985057b4bcdSMatthew G Knepley .seealso: DMGetPointSF(), DMGetDefaultSF(), DMSetDefaultSF(), DMCreateDefaultSF()
3986057b4bcdSMatthew G Knepley @*/
39870adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
39880adebc6cSBarry Smith {
3989057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
3990057b4bcdSMatthew G Knepley 
3991057b4bcdSMatthew G Knepley   PetscFunctionBegin;
3992057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3993057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 1);
3994057b4bcdSMatthew G Knepley   ierr   = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
3995057b4bcdSMatthew G Knepley   ierr   = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
3996057b4bcdSMatthew G Knepley   dm->sf = sf;
3997057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
3998057b4bcdSMatthew G Knepley }
3999057b4bcdSMatthew G Knepley 
40002764a2aaSMatthew G. Knepley /*@
40012764a2aaSMatthew G. Knepley   DMGetDS - Get the PetscDS
40022764a2aaSMatthew G. Knepley 
40032764a2aaSMatthew G. Knepley   Input Parameter:
40042764a2aaSMatthew G. Knepley . dm - The DM
40052764a2aaSMatthew G. Knepley 
40062764a2aaSMatthew G. Knepley   Output Parameter:
40072764a2aaSMatthew G. Knepley . prob - The PetscDS
40082764a2aaSMatthew G. Knepley 
40092764a2aaSMatthew G. Knepley   Level: developer
40102764a2aaSMatthew G. Knepley 
40112764a2aaSMatthew G. Knepley .seealso: DMSetDS()
40122764a2aaSMatthew G. Knepley @*/
40132764a2aaSMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4014af122d2aSMatthew G Knepley {
4015af122d2aSMatthew G Knepley   PetscFunctionBegin;
4016af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40170f21e855SMatthew G. Knepley   PetscValidPointer(prob, 2);
40180f21e855SMatthew G. Knepley   *prob = dm->prob;
40190f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
40200f21e855SMatthew G. Knepley }
40210f21e855SMatthew G. Knepley 
40222764a2aaSMatthew G. Knepley /*@
40232764a2aaSMatthew G. Knepley   DMSetDS - Set the PetscDS
40242764a2aaSMatthew G. Knepley 
40252764a2aaSMatthew G. Knepley   Input Parameters:
40262764a2aaSMatthew G. Knepley + dm - The DM
40272764a2aaSMatthew G. Knepley - prob - The PetscDS
40282764a2aaSMatthew G. Knepley 
40292764a2aaSMatthew G. Knepley   Level: developer
40302764a2aaSMatthew G. Knepley 
40312764a2aaSMatthew G. Knepley .seealso: DMGetDS()
40322764a2aaSMatthew G. Knepley @*/
40332764a2aaSMatthew G. Knepley PetscErrorCode DMSetDS(DM dm, PetscDS prob)
40340f21e855SMatthew G. Knepley {
4035f17e8794SMatthew G. Knepley   PetscInt       dimEmbed;
40360f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40370f21e855SMatthew G. Knepley 
40380f21e855SMatthew G. Knepley   PetscFunctionBegin;
40390f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40402764a2aaSMatthew G. Knepley   PetscValidHeaderSpecific(prob, PETSCDS_CLASSID, 2);
404137316535SToby Isaac   ierr = PetscObjectReference((PetscObject) prob);CHKERRQ(ierr);
40422764a2aaSMatthew G. Knepley   ierr = PetscDSDestroy(&dm->prob);CHKERRQ(ierr);
40430f21e855SMatthew G. Knepley   dm->prob = prob;
4044f17e8794SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
4045f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
40460f21e855SMatthew G. Knepley   PetscFunctionReturn(0);
40470f21e855SMatthew G. Knepley }
40480f21e855SMatthew G. Knepley 
40490f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
40500f21e855SMatthew G. Knepley {
40510f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40520f21e855SMatthew G. Knepley 
40530f21e855SMatthew G. Knepley   PetscFunctionBegin;
40540f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40552764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, numFields);CHKERRQ(ierr);
4056af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4057af122d2aSMatthew G Knepley }
4058af122d2aSMatthew G Knepley 
4059af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4060af122d2aSMatthew G Knepley {
40610f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4062af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4063af122d2aSMatthew G Knepley 
4064af122d2aSMatthew G Knepley   PetscFunctionBegin;
4065af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40662764a2aaSMatthew G. Knepley   ierr = PetscDSGetNumFields(dm->prob, &Nf);CHKERRQ(ierr);
40670f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
40680f21e855SMatthew G. Knepley     PetscContainer obj;
40690f21e855SMatthew G. Knepley 
40700f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
40712764a2aaSMatthew G. Knepley     ierr = PetscDSSetDiscretization(dm->prob, f, (PetscObject) obj);CHKERRQ(ierr);
40720f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4073af122d2aSMatthew G Knepley   }
4074af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4075af122d2aSMatthew G Knepley }
4076af122d2aSMatthew G Knepley 
4077c1929be8SMatthew G. Knepley /*@
4078c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4079c1929be8SMatthew G. Knepley 
4080c1929be8SMatthew G. Knepley   Not collective
4081c1929be8SMatthew G. Knepley 
4082c1929be8SMatthew G. Knepley   Input Parameters:
4083c1929be8SMatthew G. Knepley + dm - The DM
4084c1929be8SMatthew G. Knepley - f  - The field number
4085c1929be8SMatthew G. Knepley 
4086c1929be8SMatthew G. Knepley   Output Parameter:
4087c1929be8SMatthew G. Knepley . field - The discretization object
4088c1929be8SMatthew G. Knepley 
4089c1929be8SMatthew G. Knepley   Level: developer
4090c1929be8SMatthew G. Knepley 
4091c1929be8SMatthew G. Knepley .seealso: DMSetField()
4092c1929be8SMatthew G. Knepley @*/
4093af122d2aSMatthew G Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, PetscObject *field)
4094af122d2aSMatthew G Knepley {
40950f21e855SMatthew G. Knepley   PetscErrorCode ierr;
40960f21e855SMatthew G. Knepley 
4097af122d2aSMatthew G Knepley   PetscFunctionBegin;
4098af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40992764a2aaSMatthew G. Knepley   ierr = PetscDSGetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4100decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4101decb47aaSMatthew G. Knepley }
4102decb47aaSMatthew G. Knepley 
4103c1929be8SMatthew G. Knepley /*@
4104c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4105c1929be8SMatthew G. Knepley 
4106c1929be8SMatthew G. Knepley   Logically collective on DM
4107c1929be8SMatthew G. Knepley 
4108c1929be8SMatthew G. Knepley   Input Parameters:
4109c1929be8SMatthew G. Knepley + dm - The DM
4110c1929be8SMatthew G. Knepley . f  - The field number
4111c1929be8SMatthew G. Knepley - field - The discretization object
4112c1929be8SMatthew G. Knepley 
4113c1929be8SMatthew G. Knepley   Level: developer
4114c1929be8SMatthew G. Knepley 
4115c1929be8SMatthew G. Knepley .seealso: DMGetField()
4116c1929be8SMatthew G. Knepley @*/
4117decb47aaSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, PetscObject field)
4118decb47aaSMatthew G. Knepley {
4119decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4120decb47aaSMatthew G. Knepley 
4121decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4122decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
41232764a2aaSMatthew G. Knepley   ierr = PetscDSSetDiscretization(dm->prob, f, field);CHKERRQ(ierr);
4124af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4125af122d2aSMatthew G Knepley }
41266636e97aSMatthew G Knepley 
4127b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
4128b64e0483SPeter Brune {
4129b64e0483SPeter Brune   DM dm_coord,dmc_coord;
4130b64e0483SPeter Brune   PetscErrorCode ierr;
4131b64e0483SPeter Brune   Vec coords,ccoords;
41326dbf9973SLawrence Mitchell   Mat inject;
4133b64e0483SPeter Brune   PetscFunctionBegin;
4134b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
4135b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
4136b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
4137b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
4138b64e0483SPeter Brune   if (coords && !ccoords) {
4139b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
41406668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
41416dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
41422adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
41436dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
4144b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
4145b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
4146b64e0483SPeter Brune   }
4147b64e0483SPeter Brune   PetscFunctionReturn(0);
4148b64e0483SPeter Brune }
4149b64e0483SPeter Brune 
415003dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
415103dadc2fSPeter Brune {
415203dadc2fSPeter Brune   DM dm_coord,subdm_coord;
415303dadc2fSPeter Brune   PetscErrorCode ierr;
415403dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
415503dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
415603dadc2fSPeter Brune   PetscFunctionBegin;
415703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
415803dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
415903dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
416003dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
416103dadc2fSPeter Brune   if (coords && !ccoords) {
416203dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
41636668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
416403dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
416524640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
416603dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
416703dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
416803dadc2fSPeter Brune     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
416903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
417003dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
417103dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
417203dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
417303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
417403dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
417503dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
417603dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
417703dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
417803dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
417903dadc2fSPeter Brune   }
418003dadc2fSPeter Brune   PetscFunctionReturn(0);
418103dadc2fSPeter Brune }
418203dadc2fSPeter Brune 
4183c73cfb54SMatthew G. Knepley /*@
4184c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
4185c73cfb54SMatthew G. Knepley 
4186c73cfb54SMatthew G. Knepley   Not collective
4187c73cfb54SMatthew G. Knepley 
4188c73cfb54SMatthew G. Knepley   Input Parameter:
4189c73cfb54SMatthew G. Knepley . dm - The DM
4190c73cfb54SMatthew G. Knepley 
4191c73cfb54SMatthew G. Knepley   Output Parameter:
4192c73cfb54SMatthew G. Knepley . dim - The topological dimension
4193c73cfb54SMatthew G. Knepley 
4194c73cfb54SMatthew G. Knepley   Level: beginner
4195c73cfb54SMatthew G. Knepley 
4196c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
4197c73cfb54SMatthew G. Knepley @*/
4198c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
4199c73cfb54SMatthew G. Knepley {
4200c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4201c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4202c73cfb54SMatthew G. Knepley   PetscValidPointer(dim, 2);
4203c73cfb54SMatthew G. Knepley   *dim = dm->dim;
4204c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4205c73cfb54SMatthew G. Knepley }
4206c73cfb54SMatthew G. Knepley 
4207c73cfb54SMatthew G. Knepley /*@
4208c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
4209c73cfb54SMatthew G. Knepley 
4210c73cfb54SMatthew G. Knepley   Collective on dm
4211c73cfb54SMatthew G. Knepley 
4212c73cfb54SMatthew G. Knepley   Input Parameters:
4213c73cfb54SMatthew G. Knepley + dm - The DM
4214c73cfb54SMatthew G. Knepley - dim - The topological dimension
4215c73cfb54SMatthew G. Knepley 
4216c73cfb54SMatthew G. Knepley   Level: beginner
4217c73cfb54SMatthew G. Knepley 
4218c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
4219c73cfb54SMatthew G. Knepley @*/
4220c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
4221c73cfb54SMatthew G. Knepley {
4222f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4223f17e8794SMatthew G. Knepley 
4224c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
4225c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4226c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
4227c73cfb54SMatthew G. Knepley   dm->dim = dim;
4228f17e8794SMatthew G. Knepley   if (dm->prob->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dim);CHKERRQ(ierr);}
4229c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
4230c73cfb54SMatthew G. Knepley }
4231c73cfb54SMatthew G. Knepley 
4232793f3fe5SMatthew G. Knepley /*@
4233793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
4234793f3fe5SMatthew G. Knepley 
4235793f3fe5SMatthew G. Knepley   Collective on DM
4236793f3fe5SMatthew G. Knepley 
4237793f3fe5SMatthew G. Knepley   Input Parameters:
4238793f3fe5SMatthew G. Knepley + dm - the DM
4239793f3fe5SMatthew G. Knepley - dim - the dimension
4240793f3fe5SMatthew G. Knepley 
4241793f3fe5SMatthew G. Knepley   Output Parameters:
4242793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
4243793f3fe5SMatthew G. Knepley . pEnd - The first point following points of the given dimension
4244793f3fe5SMatthew G. Knepley 
4245793f3fe5SMatthew G. Knepley   Note:
4246793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
4247793f3fe5SMatthew G. Knepley   http://arxiv.org/abs/0908.4427. If not points exist of this dimension in the storage scheme,
4248793f3fe5SMatthew G. Knepley   then the interval is empty.
4249793f3fe5SMatthew G. Knepley 
4250793f3fe5SMatthew G. Knepley   Level: intermediate
4251793f3fe5SMatthew G. Knepley 
4252793f3fe5SMatthew G. Knepley .keywords: point, Hasse Diagram, dimension
4253793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
4254793f3fe5SMatthew G. Knepley @*/
4255793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
4256793f3fe5SMatthew G. Knepley {
4257793f3fe5SMatthew G. Knepley   PetscInt       d;
4258793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
4259793f3fe5SMatthew G. Knepley 
4260793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
4261793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4262793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
4263793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
4264793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
4265793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
4266793f3fe5SMatthew G. Knepley }
4267793f3fe5SMatthew G. Knepley 
42686636e97aSMatthew G Knepley /*@
42696636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
42706636e97aSMatthew G Knepley 
42716636e97aSMatthew G Knepley   Collective on DM
42726636e97aSMatthew G Knepley 
42736636e97aSMatthew G Knepley   Input Parameters:
42746636e97aSMatthew G Knepley + dm - the DM
42756636e97aSMatthew G Knepley - c - coordinate vector
42766636e97aSMatthew G Knepley 
42776636e97aSMatthew G Knepley   Note:
42786636e97aSMatthew G Knepley   The coordinates do include those for ghost points, which are in the local vector
42796636e97aSMatthew G Knepley 
42806636e97aSMatthew G Knepley   Level: intermediate
42816636e97aSMatthew G Knepley 
42826636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
42836636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLoca(), DMGetCoordinateDM()
42846636e97aSMatthew G Knepley @*/
42856636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
42866636e97aSMatthew G Knepley {
42876636e97aSMatthew G Knepley   PetscErrorCode ierr;
42886636e97aSMatthew G Knepley 
42896636e97aSMatthew G Knepley   PetscFunctionBegin;
42906636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
42916636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
42926636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
42936636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
42946636e97aSMatthew G Knepley   dm->coordinates = c;
42956636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
4296b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
429703dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
42986636e97aSMatthew G Knepley   PetscFunctionReturn(0);
42996636e97aSMatthew G Knepley }
43006636e97aSMatthew G Knepley 
43016636e97aSMatthew G Knepley /*@
43026636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
43036636e97aSMatthew G Knepley 
43046636e97aSMatthew G Knepley   Collective on DM
43056636e97aSMatthew G Knepley 
43066636e97aSMatthew G Knepley    Input Parameters:
43076636e97aSMatthew G Knepley +  dm - the DM
43086636e97aSMatthew G Knepley -  c - coordinate vector
43096636e97aSMatthew G Knepley 
43106636e97aSMatthew G Knepley   Note:
43116636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
43126636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
43136636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
43146636e97aSMatthew G Knepley 
43156636e97aSMatthew G Knepley   Level: intermediate
43166636e97aSMatthew G Knepley 
43176636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43186636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
43196636e97aSMatthew G Knepley @*/
43206636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
43216636e97aSMatthew G Knepley {
43226636e97aSMatthew G Knepley   PetscErrorCode ierr;
43236636e97aSMatthew G Knepley 
43246636e97aSMatthew G Knepley   PetscFunctionBegin;
43256636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
43276636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
43286636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
43298865f1eaSKarl Rupp 
43306636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
43318865f1eaSKarl Rupp 
43326636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
43336636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43346636e97aSMatthew G Knepley }
43356636e97aSMatthew G Knepley 
43366636e97aSMatthew G Knepley /*@
43376636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
43386636e97aSMatthew G Knepley 
43396636e97aSMatthew G Knepley   Not Collective
43406636e97aSMatthew G Knepley 
43416636e97aSMatthew G Knepley   Input Parameter:
43426636e97aSMatthew G Knepley . dm - the DM
43436636e97aSMatthew G Knepley 
43446636e97aSMatthew G Knepley   Output Parameter:
43456636e97aSMatthew G Knepley . c - global coordinate vector
43466636e97aSMatthew G Knepley 
43476636e97aSMatthew G Knepley   Note:
43486636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
43496636e97aSMatthew G Knepley 
43506636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
43516636e97aSMatthew G Knepley 
43526636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
43536636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
43546636e97aSMatthew G Knepley 
43556636e97aSMatthew G Knepley   Level: intermediate
43566636e97aSMatthew G Knepley 
43576636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
43586636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
43596636e97aSMatthew G Knepley @*/
43606636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
43616636e97aSMatthew G Knepley {
43626636e97aSMatthew G Knepley   PetscErrorCode ierr;
43636636e97aSMatthew G Knepley 
43646636e97aSMatthew G Knepley   PetscFunctionBegin;
43656636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
43666636e97aSMatthew G Knepley   PetscValidPointer(c,2);
43671f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
43680298fd71SBarry Smith     DM cdm = NULL;
43696636e97aSMatthew G Knepley 
43706636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
43716636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
43726636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
43736636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43746636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
43756636e97aSMatthew G Knepley   }
43766636e97aSMatthew G Knepley   *c = dm->coordinates;
43776636e97aSMatthew G Knepley   PetscFunctionReturn(0);
43786636e97aSMatthew G Knepley }
43796636e97aSMatthew G Knepley 
43806636e97aSMatthew G Knepley /*@
43816636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
43826636e97aSMatthew G Knepley 
43836636e97aSMatthew G Knepley   Collective on DM
43846636e97aSMatthew G Knepley 
43856636e97aSMatthew G Knepley   Input Parameter:
43866636e97aSMatthew G Knepley . dm - the DM
43876636e97aSMatthew G Knepley 
43886636e97aSMatthew G Knepley   Output Parameter:
43896636e97aSMatthew G Knepley . c - coordinate vector
43906636e97aSMatthew G Knepley 
43916636e97aSMatthew G Knepley   Note:
43926636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
43936636e97aSMatthew G Knepley 
43946636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
43956636e97aSMatthew G Knepley 
43966636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
43976636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
43986636e97aSMatthew G Knepley 
43996636e97aSMatthew G Knepley   Level: intermediate
44006636e97aSMatthew G Knepley 
44016636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44026636e97aSMatthew G Knepley .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
44036636e97aSMatthew G Knepley @*/
44046636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
44056636e97aSMatthew G Knepley {
44066636e97aSMatthew G Knepley   PetscErrorCode ierr;
44076636e97aSMatthew G Knepley 
44086636e97aSMatthew G Knepley   PetscFunctionBegin;
44096636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44106636e97aSMatthew G Knepley   PetscValidPointer(c,2);
44111f588964SMatthew G Knepley   if (!dm->coordinatesLocal && dm->coordinates) {
44120298fd71SBarry Smith     DM cdm = NULL;
44136636e97aSMatthew G Knepley 
44146636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
44156636e97aSMatthew G Knepley     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
44166636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
44176636e97aSMatthew G Knepley     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44186636e97aSMatthew G Knepley     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
44196636e97aSMatthew G Knepley   }
44206636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
44216636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44226636e97aSMatthew G Knepley }
44236636e97aSMatthew G Knepley 
44246636e97aSMatthew G Knepley /*@
44251cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
44266636e97aSMatthew G Knepley 
44276636e97aSMatthew G Knepley   Collective on DM
44286636e97aSMatthew G Knepley 
44296636e97aSMatthew G Knepley   Input Parameter:
44306636e97aSMatthew G Knepley . dm - the DM
44316636e97aSMatthew G Knepley 
44326636e97aSMatthew G Knepley   Output Parameter:
44336636e97aSMatthew G Knepley . cdm - coordinate DM
44346636e97aSMatthew G Knepley 
44356636e97aSMatthew G Knepley   Level: intermediate
44366636e97aSMatthew G Knepley 
44376636e97aSMatthew G Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44381cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
44396636e97aSMatthew G Knepley @*/
44406636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
44416636e97aSMatthew G Knepley {
44426636e97aSMatthew G Knepley   PetscErrorCode ierr;
44436636e97aSMatthew G Knepley 
44446636e97aSMatthew G Knepley   PetscFunctionBegin;
44456636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44466636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
44476636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
444882f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
44496636e97aSMatthew G Knepley     ierr = (*dm->ops->createcoordinatedm)(dm, &dm->coordinateDM);CHKERRQ(ierr);
44506636e97aSMatthew G Knepley   }
44516636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
44526636e97aSMatthew G Knepley   PetscFunctionReturn(0);
44536636e97aSMatthew G Knepley }
4454e87bb0d3SMatthew G Knepley 
44551cfe2091SMatthew G. Knepley /*@
44561cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
44571cfe2091SMatthew G. Knepley 
44581cfe2091SMatthew G. Knepley   Logically Collective on DM
44591cfe2091SMatthew G. Knepley 
44601cfe2091SMatthew G. Knepley   Input Parameters:
44611cfe2091SMatthew G. Knepley + dm - the DM
44621cfe2091SMatthew G. Knepley - cdm - coordinate DM
44631cfe2091SMatthew G. Knepley 
44641cfe2091SMatthew G. Knepley   Level: intermediate
44651cfe2091SMatthew G. Knepley 
44661cfe2091SMatthew G. Knepley .keywords: distributed array, get, corners, nodes, local indices, coordinates
44671cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
44681cfe2091SMatthew G. Knepley @*/
44691cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
44701cfe2091SMatthew G. Knepley {
44711cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
44721cfe2091SMatthew G. Knepley 
44731cfe2091SMatthew G. Knepley   PetscFunctionBegin;
44741cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
44751cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
4476f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
44771cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
44781cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
44791cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
44801cfe2091SMatthew G. Knepley }
44811cfe2091SMatthew G. Knepley 
448246e270d4SMatthew G. Knepley /*@
448346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
448446e270d4SMatthew G. Knepley 
448546e270d4SMatthew G. Knepley   Not Collective
448646e270d4SMatthew G. Knepley 
448746e270d4SMatthew G. Knepley   Input Parameter:
448846e270d4SMatthew G. Knepley . dm - The DM object
448946e270d4SMatthew G. Knepley 
449046e270d4SMatthew G. Knepley   Output Parameter:
449146e270d4SMatthew G. Knepley . dim - The embedding dimension
449246e270d4SMatthew G. Knepley 
449346e270d4SMatthew G. Knepley   Level: intermediate
449446e270d4SMatthew G. Knepley 
449546e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4496*e87a4003SBarry Smith .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetSection(), DMSetSection()
449746e270d4SMatthew G. Knepley @*/
449846e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
449946e270d4SMatthew G. Knepley {
450046e270d4SMatthew G. Knepley   PetscFunctionBegin;
450146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
450246e270d4SMatthew G. Knepley   PetscValidPointer(dim, 2);
45039a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
45049a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
45059a9a41abSToby Isaac   }
450646e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
450746e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
450846e270d4SMatthew G. Knepley }
450946e270d4SMatthew G. Knepley 
451046e270d4SMatthew G. Knepley /*@
451146e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
451246e270d4SMatthew G. Knepley 
451346e270d4SMatthew G. Knepley   Not Collective
451446e270d4SMatthew G. Knepley 
451546e270d4SMatthew G. Knepley   Input Parameters:
451646e270d4SMatthew G. Knepley + dm  - The DM object
451746e270d4SMatthew G. Knepley - dim - The embedding dimension
451846e270d4SMatthew G. Knepley 
451946e270d4SMatthew G. Knepley   Level: intermediate
452046e270d4SMatthew G. Knepley 
452146e270d4SMatthew G. Knepley .keywords: mesh, coordinates
4522*e87a4003SBarry Smith .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetSection(), DMSetSection()
452346e270d4SMatthew G. Knepley @*/
452446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
452546e270d4SMatthew G. Knepley {
4526f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
4527f17e8794SMatthew G. Knepley 
452846e270d4SMatthew G. Knepley   PetscFunctionBegin;
452946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
453046e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
4531f17e8794SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(dm->prob, dm->dimEmbed);CHKERRQ(ierr);
453246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
453346e270d4SMatthew G. Knepley }
453446e270d4SMatthew G. Knepley 
4535e8abe2deSMatthew G. Knepley /*@
4536e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
4537e8abe2deSMatthew G. Knepley 
4538e8abe2deSMatthew G. Knepley   Not Collective
4539e8abe2deSMatthew G. Knepley 
4540e8abe2deSMatthew G. Knepley   Input Parameter:
4541e8abe2deSMatthew G. Knepley . dm - The DM object
4542e8abe2deSMatthew G. Knepley 
4543e8abe2deSMatthew G. Knepley   Output Parameter:
4544e8abe2deSMatthew G. Knepley . section - The PetscSection object
4545e8abe2deSMatthew G. Knepley 
4546e8abe2deSMatthew G. Knepley   Level: intermediate
4547e8abe2deSMatthew G. Knepley 
4548e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4549*e87a4003SBarry Smith .seealso: DMGetCoordinateDM(), DMGetSection(), DMSetSection()
4550e8abe2deSMatthew G. Knepley @*/
4551e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
4552e8abe2deSMatthew G. Knepley {
4553e8abe2deSMatthew G. Knepley   DM             cdm;
4554e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4555e8abe2deSMatthew G. Knepley 
4556e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4557e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4558e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
4559e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4560*e87a4003SBarry Smith   ierr = DMGetSection(cdm, section);CHKERRQ(ierr);
4561e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4562e8abe2deSMatthew G. Knepley }
4563e8abe2deSMatthew G. Knepley 
4564e8abe2deSMatthew G. Knepley /*@
4565e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
4566e8abe2deSMatthew G. Knepley 
4567e8abe2deSMatthew G. Knepley   Not Collective
4568e8abe2deSMatthew G. Knepley 
4569e8abe2deSMatthew G. Knepley   Input Parameters:
4570e8abe2deSMatthew G. Knepley + dm      - The DM object
457146e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
4572e8abe2deSMatthew G. Knepley - section - The PetscSection object
4573e8abe2deSMatthew G. Knepley 
4574e8abe2deSMatthew G. Knepley   Level: intermediate
4575e8abe2deSMatthew G. Knepley 
4576e8abe2deSMatthew G. Knepley .keywords: mesh, coordinates
4577*e87a4003SBarry Smith .seealso: DMGetCoordinateSection(), DMGetSection(), DMSetSection()
4578e8abe2deSMatthew G. Knepley @*/
457946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
4580e8abe2deSMatthew G. Knepley {
4581e8abe2deSMatthew G. Knepley   DM             cdm;
4582e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
4583e8abe2deSMatthew G. Knepley 
4584e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
4585e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
458646e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
4587e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
4588*e87a4003SBarry Smith   ierr = DMSetSection(cdm, section);CHKERRQ(ierr);
458946e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
45904c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
459146e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
459246e270d4SMatthew G. Knepley 
459346e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
459446e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
459546e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
459646e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
459746e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
459846e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
459946e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
460046e270d4SMatthew G. Knepley     }
46016be882deSMatthew G. Knepley     if (d < 0) d = PETSC_DEFAULT;
460246e270d4SMatthew G. Knepley     ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);
460346e270d4SMatthew G. Knepley   }
4604e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
4605e8abe2deSMatthew G. Knepley }
4606e8abe2deSMatthew G. Knepley 
46075dc8c3f7SMatthew G. Knepley /*@C
460890b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
46095dc8c3f7SMatthew G. Knepley 
46105dc8c3f7SMatthew G. Knepley   Input Parameters:
461190b157c4SStefano Zampini . dm      - The DM object
461290b157c4SStefano Zampini 
461390b157c4SStefano Zampini   Output Parameters:
461490b157c4SStefano Zampini + per     - Whether the DM is periodic or not
46155dc8c3f7SMatthew 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
46165dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
46175dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
46185dc8c3f7SMatthew G. Knepley 
46195dc8c3f7SMatthew G. Knepley   Level: developer
46205dc8c3f7SMatthew G. Knepley 
46215dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
46225dc8c3f7SMatthew G. Knepley @*/
462390b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
4624c6b900c6SMatthew G. Knepley {
4625c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4626c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
462790b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
4628c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
4629c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
46305dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
4631c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4632c6b900c6SMatthew G. Knepley }
4633c6b900c6SMatthew G. Knepley 
46345dc8c3f7SMatthew G. Knepley /*@C
46355dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
46365dc8c3f7SMatthew G. Knepley 
46375dc8c3f7SMatthew G. Knepley   Input Parameters:
46385dc8c3f7SMatthew G. Knepley + dm      - The DM object
463990b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
46405dc8c3f7SMatthew 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
46415dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
46425dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
46435dc8c3f7SMatthew G. Knepley 
46445dc8c3f7SMatthew G. Knepley   Level: developer
46455dc8c3f7SMatthew G. Knepley 
46465dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
46475dc8c3f7SMatthew G. Knepley @*/
464890b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
4649c6b900c6SMatthew G. Knepley {
4650c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
4651c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
4652c6b900c6SMatthew G. Knepley 
4653c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
4654c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
465590b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
465690b157c4SStefano Zampini   if (maxCell) {
465790b157c4SStefano Zampini     PetscValidPointer(maxCell,3);
465890b157c4SStefano Zampini     PetscValidPointer(L,4);
465990b157c4SStefano Zampini     PetscValidPointer(bd,5);
466090b157c4SStefano Zampini   }
46615dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
46625dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
466390b157c4SStefano Zampini   if (maxCell) {
46645dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
46655dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
466690b157c4SStefano Zampini     dm->periodic = PETSC_TRUE;
466790b157c4SStefano Zampini   } else {
466890b157c4SStefano Zampini     dm->periodic = per;
466990b157c4SStefano Zampini   }
4670c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
4671c6b900c6SMatthew G. Knepley }
4672c6b900c6SMatthew G. Knepley 
46732e17dfb7SMatthew G. Knepley /*@
46742e17dfb7SMatthew 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.
46752e17dfb7SMatthew G. Knepley 
46762e17dfb7SMatthew G. Knepley   Input Parameters:
46772e17dfb7SMatthew G. Knepley + dm     - The DM
467865da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
467965da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
46802e17dfb7SMatthew G. Knepley 
46812e17dfb7SMatthew G. Knepley   Output Parameter:
46822e17dfb7SMatthew G. Knepley . out - The localized coordinate point
46832e17dfb7SMatthew G. Knepley 
46842e17dfb7SMatthew G. Knepley   Level: developer
46852e17dfb7SMatthew G. Knepley 
46862e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
46872e17dfb7SMatthew G. Knepley @*/
468865da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
46892e17dfb7SMatthew G. Knepley {
46902e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
46912e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
46922e17dfb7SMatthew G. Knepley 
46932e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
46942e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
46952e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
46962e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
46972e17dfb7SMatthew G. Knepley   } else {
469865da65dcSMatthew G. Knepley     if (endpoint) {
469965da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
4700da3333bfSMatthew 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)) {
4701da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
470265da65dcSMatthew G. Knepley         } else {
4703da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
470465da65dcSMatthew G. Knepley         }
470565da65dcSMatthew G. Knepley       }
470665da65dcSMatthew G. Knepley     } else {
47072e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
47081118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
47092e17dfb7SMatthew G. Knepley       }
47102e17dfb7SMatthew G. Knepley     }
471165da65dcSMatthew G. Knepley   }
47122e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47132e17dfb7SMatthew G. Knepley }
47142e17dfb7SMatthew G. Knepley 
47152e17dfb7SMatthew G. Knepley /*
47162e17dfb7SMatthew 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.
47172e17dfb7SMatthew G. Knepley 
47182e17dfb7SMatthew G. Knepley   Input Parameters:
47192e17dfb7SMatthew G. Knepley + dm     - The DM
47202e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
47212e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
47222e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
47232e17dfb7SMatthew G. Knepley 
47242e17dfb7SMatthew G. Knepley   Output Parameter:
47252e17dfb7SMatthew G. Knepley . out - The localized coordinate point
47262e17dfb7SMatthew G. Knepley 
47272e17dfb7SMatthew G. Knepley   Level: developer
47282e17dfb7SMatthew G. Knepley 
47292e17dfb7SMatthew 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
47302e17dfb7SMatthew G. Knepley 
47312e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
47322e17dfb7SMatthew G. Knepley */
47332e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
47342e17dfb7SMatthew G. Knepley {
47352e17dfb7SMatthew G. Knepley   PetscInt d;
47362e17dfb7SMatthew G. Knepley 
47372e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47382e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47392e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47402e17dfb7SMatthew G. Knepley   } else {
47412e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4742908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
47432e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
47442e17dfb7SMatthew G. Knepley       } else {
47452e17dfb7SMatthew G. Knepley         out[d] = in[d];
47462e17dfb7SMatthew G. Knepley       }
47472e17dfb7SMatthew G. Knepley     }
47482e17dfb7SMatthew G. Knepley   }
47492e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47502e17dfb7SMatthew G. Knepley }
47512e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
47522e17dfb7SMatthew G. Knepley {
47532e17dfb7SMatthew G. Knepley   PetscInt d;
47542e17dfb7SMatthew G. Knepley 
47552e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
47562e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
47572e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
47582e17dfb7SMatthew G. Knepley   } else {
47592e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
4760908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
47612e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
47622e17dfb7SMatthew G. Knepley       } else {
47632e17dfb7SMatthew G. Knepley         out[d] = in[d];
47642e17dfb7SMatthew G. Knepley       }
47652e17dfb7SMatthew G. Knepley     }
47662e17dfb7SMatthew G. Knepley   }
47672e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
47682e17dfb7SMatthew G. Knepley }
47692e17dfb7SMatthew G. Knepley 
47702e17dfb7SMatthew G. Knepley /*
47712e17dfb7SMatthew 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.
47722e17dfb7SMatthew G. Knepley 
47732e17dfb7SMatthew G. Knepley   Input Parameters:
47742e17dfb7SMatthew G. Knepley + dm     - The DM
47752e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
47762e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
47772e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
47782e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
47792e17dfb7SMatthew G. Knepley 
47802e17dfb7SMatthew G. Knepley   Output Parameter:
47812e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
47822e17dfb7SMatthew G. Knepley 
47832e17dfb7SMatthew G. Knepley   Level: developer
47842e17dfb7SMatthew G. Knepley 
47852e17dfb7SMatthew 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
47862e17dfb7SMatthew G. Knepley 
47872e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
47882e17dfb7SMatthew G. Knepley */
47892e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar 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) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
47992e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(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 
480836447a5eSToby Isaac /*@
480936447a5eSToby Isaac   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
481036447a5eSToby Isaac 
481136447a5eSToby Isaac   Input Parameter:
481236447a5eSToby Isaac . dm - The DM
481336447a5eSToby Isaac 
481436447a5eSToby Isaac   Output Parameter:
481536447a5eSToby Isaac   areLocalized - True if localized
481636447a5eSToby Isaac 
481736447a5eSToby Isaac   Level: developer
481836447a5eSToby Isaac 
481936447a5eSToby Isaac .seealso: DMLocalizeCoordinates()
482036447a5eSToby Isaac @*/
482136447a5eSToby Isaac PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
482236447a5eSToby Isaac {
482336447a5eSToby Isaac   DM             cdm;
482436447a5eSToby Isaac   PetscSection   coordSection;
482546a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
482646a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
482736447a5eSToby Isaac   PetscErrorCode ierr;
482836447a5eSToby Isaac 
482936447a5eSToby Isaac   PetscFunctionBegin;
483036447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
483146a3a80fSLisandro Dalcin   PetscValidPointer(areLocalized, 2);
483246a3a80fSLisandro Dalcin 
48338b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
483446a3a80fSLisandro Dalcin   if (!dm->periodic) PetscFunctionReturn(0); /* This is a hideous optimization hack! */
483546a3a80fSLisandro Dalcin 
483636447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
483736447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
483836447a5eSToby Isaac   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
483946a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
484046a3a80fSLisandro Dalcin   if (!isPlex) SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
484146a3a80fSLisandro Dalcin 
484246a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
484336447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
484436447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
484546a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
484646a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
484736447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
484846a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
484936447a5eSToby Isaac   }
485046a3a80fSLisandro Dalcin   ierr = MPIU_Allreduce(&alreadyLocalized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
485136447a5eSToby Isaac   PetscFunctionReturn(0);
485236447a5eSToby Isaac }
485336447a5eSToby Isaac 
485436447a5eSToby Isaac 
48552e17dfb7SMatthew G. Knepley /*@
4856492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
48572e17dfb7SMatthew G. Knepley 
48582e17dfb7SMatthew G. Knepley   Input Parameter:
48592e17dfb7SMatthew G. Knepley . dm - The DM
48602e17dfb7SMatthew G. Knepley 
48612e17dfb7SMatthew G. Knepley   Level: developer
48622e17dfb7SMatthew G. Knepley 
48632e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
48642e17dfb7SMatthew G. Knepley @*/
48652e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
48662e17dfb7SMatthew G. Knepley {
48672e17dfb7SMatthew G. Knepley   DM             cdm;
48682e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
48692e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
48703e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
48713e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
4872e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
48733e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
48743e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
48752e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
48762e17dfb7SMatthew G. Knepley 
48772e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
48782e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
487992c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
4880f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
4881f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
4882f7cbd40bSStefano Zampini 
48832e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
48842e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
48852e17dfb7SMatthew G. Knepley   {
48862e17dfb7SMatthew G. Knepley     PetscBool isplex;
48872e17dfb7SMatthew G. Knepley 
48882e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
48892e17dfb7SMatthew G. Knepley     if (isplex) {
48902e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
48913e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
489269291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
48933e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
48943e922f36SToby Isaac       newStart = vStart;
48953e922f36SToby Isaac       newEnd   = vEnd;
48963e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
48973e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
48983e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
48993e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
49003e922f36SToby Isaac       }
49012e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
49022e17dfb7SMatthew G. Knepley   }
49032e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
490443eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
49052e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
49063e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
4907e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
49083e922f36SToby Isaac 
49092e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
49102e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
49112e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
49122e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
49133e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
49143e922f36SToby Isaac 
491569291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
49163e922f36SToby Isaac   localized = &anchor[bs];
49173e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
49183e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49193e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49203e922f36SToby Isaac 
49213e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
49223e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
49233e922f36SToby Isaac       PetscInt     b;
49243e922f36SToby Isaac 
49253e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
49263e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49273e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49283e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
49293e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
49303e922f36SToby Isaac         for (b = 0; b < bs; b++) {
49313e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
49323e922f36SToby Isaac         }
49333e922f36SToby Isaac         if (b < bs) break;
49343e922f36SToby Isaac       }
49353e922f36SToby Isaac       if (d < dof/bs) {
49363e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
49373e922f36SToby Isaac           PetscInt cdof;
49383e922f36SToby Isaac 
49393e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
49403e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
49413e922f36SToby Isaac         }
49423e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
49433e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
49443e922f36SToby Isaac       }
49453e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49463e922f36SToby Isaac     }
49473e922f36SToby Isaac   }
49483e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
49493e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
495069291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
49513e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
495269291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
49533e922f36SToby Isaac     PetscFunctionReturn(0);
49543e922f36SToby Isaac   }
49552e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49562e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49572e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection,     v,  dof);CHKERRQ(ierr);
49582e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
49592e17dfb7SMatthew G. Knepley   }
49602e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
49612e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
4962c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
49632e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
49642e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec,         bs);CHKERRQ(ierr);
49652e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
49662e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
4967c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49682e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
49692e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
49702e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
49712e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
49722e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
49732e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
49742e17dfb7SMatthew G. Knepley   }
49753e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
49763e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
49773e922f36SToby Isaac 
49782e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
49792e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
49803e922f36SToby Isaac       PetscInt     b, cdof;
49812e17dfb7SMatthew G. Knepley 
49823e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
49833e922f36SToby Isaac       if (!cdof) continue;
49842e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49852e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
49862e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
49872e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
49882e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
49892e17dfb7SMatthew G. Knepley     }
49903e922f36SToby Isaac   }
499169291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
499269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
4993c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
49942e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
49952e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
49962e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
49972e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
49982e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
49992e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
50002e17dfb7SMatthew G. Knepley }
50012e17dfb7SMatthew G. Knepley 
5002e87bb0d3SMatthew G Knepley /*@
50033a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
5004e87bb0d3SMatthew G Knepley 
50053a93e3b7SToby Isaac   Collective on Vec v (see explanation below)
5006e87bb0d3SMatthew G Knepley 
5007e87bb0d3SMatthew G Knepley   Input Parameters:
5008e87bb0d3SMatthew G Knepley + dm - The DM
50093a93e3b7SToby Isaac . v - The Vec of points
501062a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
50113a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
5012e87bb0d3SMatthew G Knepley 
501361e3bb9bSMatthew G Knepley   Output Parameter:
501462a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
501562a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
50163a93e3b7SToby Isaac 
5017e87bb0d3SMatthew G Knepley 
5018e87bb0d3SMatthew G Knepley   Level: developer
501961e3bb9bSMatthew G Knepley 
502062a38674SMatthew G. Knepley   Notes:
50213a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
502262a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
50233a93e3b7SToby Isaac 
50243a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
502562a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
50263a93e3b7SToby Isaac 
50273a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
50283a93e3b7SToby Isaac 
502962a38674SMatthew G. Knepley $    const PetscSFNode *cells;
503062a38674SMatthew G. Knepley $    PetscInt           nFound;
503162a38674SMatthew G. Knepley $    const PetscSFNode *found;
503262a38674SMatthew G. Knepley $
503362a38674SMatthew G. Knepley $    PetscSFGetGraph(cells,NULL,&nFound,&found,&cells);
50343a93e3b7SToby Isaac 
50353a93e3b7SToby 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
50363a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
50373a93e3b7SToby Isaac 
503861e3bb9bSMatthew G Knepley .keywords: point location, mesh
503962a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
504061e3bb9bSMatthew G Knepley @*/
504162a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
5042e87bb0d3SMatthew G Knepley {
5043735aa83eSMatthew G Knepley   PetscErrorCode ierr;
5044735aa83eSMatthew G Knepley 
5045e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
5046e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5047e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
5048e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
50493a93e3b7SToby Isaac   if (*cellSF) {
50503a93e3b7SToby Isaac     PetscMPIInt result;
50513a93e3b7SToby Isaac 
5052e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
5053a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
50543a93e3b7SToby 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");
5055e0fc9d1bSMatthew G. Knepley   } else {
50563a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
50573a93e3b7SToby Isaac   }
505847a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5059735aa83eSMatthew G Knepley   if (dm->ops->locatepoints) {
506062a38674SMatthew G. Knepley     ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
506182f516ccSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
506247a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
5063e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
5064e87bb0d3SMatthew G Knepley }
506514f150ffSMatthew G. Knepley 
5066f4d763aaSMatthew G. Knepley /*@
5067f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
5068f4d763aaSMatthew G. Knepley 
5069f4d763aaSMatthew G. Knepley   Input Parameter:
5070f4d763aaSMatthew G. Knepley . dm - The original DM
5071f4d763aaSMatthew G. Knepley 
5072f4d763aaSMatthew G. Knepley   Output Parameter:
5073f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
5074f4d763aaSMatthew G. Knepley 
5075f4d763aaSMatthew G. Knepley   Level: intermediate
5076f4d763aaSMatthew G. Knepley 
5077*e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
5078f4d763aaSMatthew G. Knepley @*/
507914f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
508014f150ffSMatthew G. Knepley {
5081c26acbdeSMatthew G. Knepley   PetscSection   section;
50822d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
508314f150ffSMatthew G. Knepley   PetscErrorCode ierr;
508414f150ffSMatthew G. Knepley 
508514f150ffSMatthew G. Knepley   PetscFunctionBegin;
508614f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
508714f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
5088*e87a4003SBarry Smith   ierr = DMGetSection(dm, &section);CHKERRQ(ierr);
5089c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
5090127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
50912d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
5092c26acbdeSMatthew G. Knepley     *odm = dm;
5093c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
5094c26acbdeSMatthew G. Knepley   }
509514f150ffSMatthew G. Knepley   if (!dm->dmBC) {
50960305aff6SMatthew G. Knepley     PetscDS      ds;
5097c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
509814f150ffSMatthew G. Knepley     PetscSF      sf;
509914f150ffSMatthew G. Knepley 
510014f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
51010305aff6SMatthew G. Knepley     ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
51020305aff6SMatthew G. Knepley     ierr = DMSetDS(dm->dmBC, ds);CHKERRQ(ierr);
510314f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
5104*e87a4003SBarry Smith     ierr = DMSetSection(dm->dmBC, newSection);CHKERRQ(ierr);
510514f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
510614f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
510715b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
5108*e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
510914f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
511014f150ffSMatthew G. Knepley   }
511114f150ffSMatthew G. Knepley   *odm = dm->dmBC;
511214f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
511314f150ffSMatthew G. Knepley }
5114f4d763aaSMatthew G. Knepley 
5115f4d763aaSMatthew G. Knepley /*@
5116cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
5117f4d763aaSMatthew G. Knepley 
5118f4d763aaSMatthew G. Knepley   Input Parameter:
5119f4d763aaSMatthew G. Knepley . dm - The original DM
5120f4d763aaSMatthew G. Knepley 
5121cdb7a50dSMatthew G. Knepley   Output Parameters:
5122cdb7a50dSMatthew G. Knepley + num - The output sequence number
5123cdb7a50dSMatthew G. Knepley - val - The output sequence value
5124f4d763aaSMatthew G. Knepley 
5125f4d763aaSMatthew G. Knepley   Level: intermediate
5126f4d763aaSMatthew G. Knepley 
5127f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5128f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5129f4d763aaSMatthew G. Knepley 
5130f4d763aaSMatthew G. Knepley .seealso: VecView()
5131f4d763aaSMatthew G. Knepley @*/
5132cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
5133f4d763aaSMatthew G. Knepley {
5134f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5135f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5136cdb7a50dSMatthew G. Knepley   if (num) {PetscValidPointer(num,2); *num = dm->outputSequenceNum;}
5137cdb7a50dSMatthew G. Knepley   if (val) {PetscValidPointer(val,3);*val = dm->outputSequenceVal;}
5138f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5139f4d763aaSMatthew G. Knepley }
5140f4d763aaSMatthew G. Knepley 
5141f4d763aaSMatthew G. Knepley /*@
5142cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
5143f4d763aaSMatthew G. Knepley 
5144f4d763aaSMatthew G. Knepley   Input Parameters:
5145f4d763aaSMatthew G. Knepley + dm - The original DM
5146cdb7a50dSMatthew G. Knepley . num - The output sequence number
5147cdb7a50dSMatthew G. Knepley - val - The output sequence value
5148f4d763aaSMatthew G. Knepley 
5149f4d763aaSMatthew G. Knepley   Level: intermediate
5150f4d763aaSMatthew G. Knepley 
5151f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5152f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5153f4d763aaSMatthew G. Knepley 
5154f4d763aaSMatthew G. Knepley .seealso: VecView()
5155f4d763aaSMatthew G. Knepley @*/
5156cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
5157f4d763aaSMatthew G. Knepley {
5158f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
5159f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5160f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
5161cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
5162cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
5163cdb7a50dSMatthew G. Knepley }
5164cdb7a50dSMatthew G. Knepley 
5165cdb7a50dSMatthew G. Knepley /*@C
5166cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
5167cdb7a50dSMatthew G. Knepley 
5168cdb7a50dSMatthew G. Knepley   Input Parameters:
5169cdb7a50dSMatthew G. Knepley + dm   - The original DM
5170cdb7a50dSMatthew G. Knepley . name - The sequence name
5171cdb7a50dSMatthew G. Knepley - num  - The output sequence number
5172cdb7a50dSMatthew G. Knepley 
5173cdb7a50dSMatthew G. Knepley   Output Parameter:
5174cdb7a50dSMatthew G. Knepley . val  - The output sequence value
5175cdb7a50dSMatthew G. Knepley 
5176cdb7a50dSMatthew G. Knepley   Level: intermediate
5177cdb7a50dSMatthew G. Knepley 
5178cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
5179cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
5180cdb7a50dSMatthew G. Knepley 
5181cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
5182cdb7a50dSMatthew G. Knepley @*/
5183cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
5184cdb7a50dSMatthew G. Knepley {
5185cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
5186cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
5187cdb7a50dSMatthew G. Knepley 
5188cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
5189cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5190cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
5191cdb7a50dSMatthew G. Knepley   PetscValidPointer(val,4);
5192cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
5193cdb7a50dSMatthew G. Knepley   if (ishdf5) {
5194cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
5195cdb7a50dSMatthew G. Knepley     PetscScalar value;
5196cdb7a50dSMatthew G. Knepley 
519739d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
51984aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
5199cdb7a50dSMatthew G. Knepley #endif
5200cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
5201f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
5202f4d763aaSMatthew G. Knepley }
52038e4ac7eaSMatthew G. Knepley 
52048e4ac7eaSMatthew G. Knepley /*@
52058e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
52068e4ac7eaSMatthew G. Knepley 
52078e4ac7eaSMatthew G. Knepley   Not collective
52088e4ac7eaSMatthew G. Knepley 
52098e4ac7eaSMatthew G. Knepley   Input Parameter:
52108e4ac7eaSMatthew G. Knepley . dm - The DM
52118e4ac7eaSMatthew G. Knepley 
52128e4ac7eaSMatthew G. Knepley   Output Parameter:
52138e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
52148e4ac7eaSMatthew G. Knepley 
52158e4ac7eaSMatthew G. Knepley   Level: beginner
52168e4ac7eaSMatthew G. Knepley 
52178e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
52188e4ac7eaSMatthew G. Knepley @*/
52198e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
52208e4ac7eaSMatthew G. Knepley {
52218e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
52228e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52238e4ac7eaSMatthew G. Knepley   PetscValidPointer(useNatural, 2);
52248e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
52258e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
52268e4ac7eaSMatthew G. Knepley }
52278e4ac7eaSMatthew G. Knepley 
52288e4ac7eaSMatthew G. Knepley /*@
52298e4ac7eaSMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order on distribution
52308e4ac7eaSMatthew G. Knepley 
52318e4ac7eaSMatthew G. Knepley   Collective on dm
52328e4ac7eaSMatthew G. Knepley 
52338e4ac7eaSMatthew G. Knepley   Input Parameters:
52348e4ac7eaSMatthew G. Knepley + dm - The DM
52358e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
52368e4ac7eaSMatthew G. Knepley 
52378e4ac7eaSMatthew G. Knepley   Level: beginner
52388e4ac7eaSMatthew G. Knepley 
52398e4ac7eaSMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate()
52408e4ac7eaSMatthew G. Knepley @*/
52418e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
52428e4ac7eaSMatthew G. Knepley {
52438e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
52448e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
52458833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
52468e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
52478e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
52488e4ac7eaSMatthew G. Knepley }
5249c58f1c22SToby Isaac 
5250c58f1c22SToby Isaac 
5251c58f1c22SToby Isaac /*@C
5252c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
5253c58f1c22SToby Isaac 
5254c58f1c22SToby Isaac   Not Collective
5255c58f1c22SToby Isaac 
5256c58f1c22SToby Isaac   Input Parameters:
5257c58f1c22SToby Isaac + dm   - The DM object
5258c58f1c22SToby Isaac - name - The label name
5259c58f1c22SToby Isaac 
5260c58f1c22SToby Isaac   Level: intermediate
5261c58f1c22SToby Isaac 
5262c58f1c22SToby Isaac .keywords: mesh
5263c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5264c58f1c22SToby Isaac @*/
5265c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
5266c58f1c22SToby Isaac {
5267c58f1c22SToby Isaac   DMLabelLink    next  = dm->labels->next;
5268c58f1c22SToby Isaac   PetscBool      flg   = PETSC_FALSE;
5269c58f1c22SToby Isaac   PetscErrorCode ierr;
5270c58f1c22SToby Isaac 
5271c58f1c22SToby Isaac   PetscFunctionBegin;
5272c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5273c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5274c58f1c22SToby Isaac   while (next) {
5275c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5276c58f1c22SToby Isaac     if (flg) break;
5277c58f1c22SToby Isaac     next = next->next;
5278c58f1c22SToby Isaac   }
5279c58f1c22SToby Isaac   if (!flg) {
5280c58f1c22SToby Isaac     DMLabelLink tmpLabel;
5281c58f1c22SToby Isaac 
5282c58f1c22SToby Isaac     ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5283c58f1c22SToby Isaac     ierr = DMLabelCreate(name, &tmpLabel->label);CHKERRQ(ierr);
5284c58f1c22SToby Isaac     tmpLabel->output = PETSC_TRUE;
5285c58f1c22SToby Isaac     tmpLabel->next   = dm->labels->next;
5286c58f1c22SToby Isaac     dm->labels->next = tmpLabel;
5287c58f1c22SToby Isaac   }
5288c58f1c22SToby Isaac   PetscFunctionReturn(0);
5289c58f1c22SToby Isaac }
5290c58f1c22SToby Isaac 
5291c58f1c22SToby Isaac /*@C
5292c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
5293c58f1c22SToby Isaac 
5294c58f1c22SToby Isaac   Not Collective
5295c58f1c22SToby Isaac 
5296c58f1c22SToby Isaac   Input Parameters:
5297c58f1c22SToby Isaac + dm   - The DM object
5298c58f1c22SToby Isaac . name - The label name
5299c58f1c22SToby Isaac - point - The mesh point
5300c58f1c22SToby Isaac 
5301c58f1c22SToby Isaac   Output Parameter:
5302c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
5303c58f1c22SToby Isaac 
5304c58f1c22SToby Isaac   Level: beginner
5305c58f1c22SToby Isaac 
5306c58f1c22SToby Isaac .keywords: mesh
5307c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
5308c58f1c22SToby Isaac @*/
5309c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
5310c58f1c22SToby Isaac {
5311c58f1c22SToby Isaac   DMLabel        label;
5312c58f1c22SToby Isaac   PetscErrorCode ierr;
5313c58f1c22SToby Isaac 
5314c58f1c22SToby Isaac   PetscFunctionBegin;
5315c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5316c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5317c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
531813903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
5319c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
5320c58f1c22SToby Isaac   PetscFunctionReturn(0);
5321c58f1c22SToby Isaac }
5322c58f1c22SToby Isaac 
5323c58f1c22SToby Isaac /*@C
5324c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
5325c58f1c22SToby Isaac 
5326c58f1c22SToby Isaac   Not Collective
5327c58f1c22SToby Isaac 
5328c58f1c22SToby Isaac   Input Parameters:
5329c58f1c22SToby Isaac + dm   - The DM object
5330c58f1c22SToby Isaac . name - The label name
5331c58f1c22SToby Isaac . point - The mesh point
5332c58f1c22SToby Isaac - value - The label value for this point
5333c58f1c22SToby Isaac 
5334c58f1c22SToby Isaac   Output Parameter:
5335c58f1c22SToby Isaac 
5336c58f1c22SToby Isaac   Level: beginner
5337c58f1c22SToby Isaac 
5338c58f1c22SToby Isaac .keywords: mesh
5339c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
5340c58f1c22SToby Isaac @*/
5341c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5342c58f1c22SToby Isaac {
5343c58f1c22SToby Isaac   DMLabel        label;
5344c58f1c22SToby Isaac   PetscErrorCode ierr;
5345c58f1c22SToby Isaac 
5346c58f1c22SToby Isaac   PetscFunctionBegin;
5347c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5348c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5349c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5350c58f1c22SToby Isaac   if (!label) {
5351c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
5352c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5353c58f1c22SToby Isaac   }
5354c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
5355c58f1c22SToby Isaac   PetscFunctionReturn(0);
5356c58f1c22SToby Isaac }
5357c58f1c22SToby Isaac 
5358c58f1c22SToby Isaac /*@C
5359c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
5360c58f1c22SToby Isaac 
5361c58f1c22SToby Isaac   Not Collective
5362c58f1c22SToby Isaac 
5363c58f1c22SToby Isaac   Input Parameters:
5364c58f1c22SToby Isaac + dm   - The DM object
5365c58f1c22SToby Isaac . name - The label name
5366c58f1c22SToby Isaac . point - The mesh point
5367c58f1c22SToby Isaac - value - The label value for this point
5368c58f1c22SToby Isaac 
5369c58f1c22SToby Isaac   Output Parameter:
5370c58f1c22SToby Isaac 
5371c58f1c22SToby Isaac   Level: beginner
5372c58f1c22SToby Isaac 
5373c58f1c22SToby Isaac .keywords: mesh
5374c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
5375c58f1c22SToby Isaac @*/
5376c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
5377c58f1c22SToby Isaac {
5378c58f1c22SToby Isaac   DMLabel        label;
5379c58f1c22SToby Isaac   PetscErrorCode ierr;
5380c58f1c22SToby Isaac 
5381c58f1c22SToby Isaac   PetscFunctionBegin;
5382c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5383c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5384c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5385c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5386c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
5387c58f1c22SToby Isaac   PetscFunctionReturn(0);
5388c58f1c22SToby Isaac }
5389c58f1c22SToby Isaac 
5390c58f1c22SToby Isaac /*@C
5391c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
5392c58f1c22SToby Isaac 
5393c58f1c22SToby Isaac   Not Collective
5394c58f1c22SToby Isaac 
5395c58f1c22SToby Isaac   Input Parameters:
5396c58f1c22SToby Isaac + dm   - The DM object
5397c58f1c22SToby Isaac - name - The label name
5398c58f1c22SToby Isaac 
5399c58f1c22SToby Isaac   Output Parameter:
5400c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
5401c58f1c22SToby Isaac 
5402c58f1c22SToby Isaac   Level: beginner
5403c58f1c22SToby Isaac 
5404c58f1c22SToby Isaac .keywords: mesh
5405c58f1c22SToby Isaac .seealso: DMLabeGetNumValues(), DMSetLabelValue()
5406c58f1c22SToby Isaac @*/
5407c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
5408c58f1c22SToby Isaac {
5409c58f1c22SToby Isaac   DMLabel        label;
5410c58f1c22SToby Isaac   PetscErrorCode ierr;
5411c58f1c22SToby Isaac 
5412c58f1c22SToby Isaac   PetscFunctionBegin;
5413c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5414c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5415c58f1c22SToby Isaac   PetscValidPointer(size, 3);
5416c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5417c58f1c22SToby Isaac   *size = 0;
5418c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5419c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
5420c58f1c22SToby Isaac   PetscFunctionReturn(0);
5421c58f1c22SToby Isaac }
5422c58f1c22SToby Isaac 
5423c58f1c22SToby Isaac /*@C
5424c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
5425c58f1c22SToby Isaac 
5426c58f1c22SToby Isaac   Not Collective
5427c58f1c22SToby Isaac 
5428c58f1c22SToby Isaac   Input Parameters:
5429c58f1c22SToby Isaac + mesh - The DM object
5430c58f1c22SToby Isaac - name - The label name
5431c58f1c22SToby Isaac 
5432c58f1c22SToby Isaac   Output Parameter:
5433c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
5434c58f1c22SToby Isaac 
5435c58f1c22SToby Isaac   Level: beginner
5436c58f1c22SToby Isaac 
5437c58f1c22SToby Isaac .keywords: mesh
5438c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
5439c58f1c22SToby Isaac @*/
5440c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
5441c58f1c22SToby Isaac {
5442c58f1c22SToby Isaac   DMLabel        label;
5443c58f1c22SToby Isaac   PetscErrorCode ierr;
5444c58f1c22SToby Isaac 
5445c58f1c22SToby Isaac   PetscFunctionBegin;
5446c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5447c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5448c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
5449c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5450c58f1c22SToby Isaac   *ids = NULL;
5451dab2e251SBlaise Bourdin  if (label) {
5452c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
5453dab2e251SBlaise Bourdin   } else {
5454dab2e251SBlaise Bourdin     /* returning an empty IS */
5455dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
5456dab2e251SBlaise Bourdin   }
5457c58f1c22SToby Isaac   PetscFunctionReturn(0);
5458c58f1c22SToby Isaac }
5459c58f1c22SToby Isaac 
5460c58f1c22SToby Isaac /*@C
5461c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
5462c58f1c22SToby Isaac 
5463c58f1c22SToby Isaac   Not Collective
5464c58f1c22SToby Isaac 
5465c58f1c22SToby Isaac   Input Parameters:
5466c58f1c22SToby Isaac + dm - The DM object
5467c58f1c22SToby Isaac . name - The label name
5468c58f1c22SToby Isaac - value - The stratum value
5469c58f1c22SToby Isaac 
5470c58f1c22SToby Isaac   Output Parameter:
5471c58f1c22SToby Isaac . size - The stratum size
5472c58f1c22SToby Isaac 
5473c58f1c22SToby Isaac   Level: beginner
5474c58f1c22SToby Isaac 
5475c58f1c22SToby Isaac .keywords: mesh
5476c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
5477c58f1c22SToby Isaac @*/
5478c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
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(size, 4);
5487c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5488c58f1c22SToby Isaac   *size = 0;
5489c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5490c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
5491c58f1c22SToby Isaac   PetscFunctionReturn(0);
5492c58f1c22SToby Isaac }
5493c58f1c22SToby Isaac 
5494c58f1c22SToby Isaac /*@C
5495c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
5496c58f1c22SToby Isaac 
5497c58f1c22SToby Isaac   Not Collective
5498c58f1c22SToby Isaac 
5499c58f1c22SToby Isaac   Input Parameters:
5500c58f1c22SToby Isaac + dm - The DM object
5501c58f1c22SToby Isaac . name - The label name
5502c58f1c22SToby Isaac - value - The stratum value
5503c58f1c22SToby Isaac 
5504c58f1c22SToby Isaac   Output Parameter:
5505c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
5506c58f1c22SToby Isaac 
5507c58f1c22SToby Isaac   Level: beginner
5508c58f1c22SToby Isaac 
5509c58f1c22SToby Isaac .keywords: mesh
5510c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
5511c58f1c22SToby Isaac @*/
5512c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
5513c58f1c22SToby Isaac {
5514c58f1c22SToby Isaac   DMLabel        label;
5515c58f1c22SToby Isaac   PetscErrorCode ierr;
5516c58f1c22SToby Isaac 
5517c58f1c22SToby Isaac   PetscFunctionBegin;
5518c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5519c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5520c58f1c22SToby Isaac   PetscValidPointer(points, 4);
5521c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5522c58f1c22SToby Isaac   *points = NULL;
5523c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5524c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
5525c58f1c22SToby Isaac   PetscFunctionReturn(0);
5526c58f1c22SToby Isaac }
5527c58f1c22SToby Isaac 
55284de306b1SToby Isaac /*@C
55294de306b1SToby Isaac   DMGetStratumIS - Set the points in a label stratum
55304de306b1SToby Isaac 
55314de306b1SToby Isaac   Not Collective
55324de306b1SToby Isaac 
55334de306b1SToby Isaac   Input Parameters:
55344de306b1SToby Isaac + dm - The DM object
55354de306b1SToby Isaac . name - The label name
55364de306b1SToby Isaac . value - The stratum value
55374de306b1SToby Isaac - points - The stratum points
55384de306b1SToby Isaac 
55394de306b1SToby Isaac   Level: beginner
55404de306b1SToby Isaac 
55414de306b1SToby Isaac .keywords: mesh
55424de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
55434de306b1SToby Isaac @*/
55444de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
55454de306b1SToby Isaac {
55464de306b1SToby Isaac   DMLabel        label;
55474de306b1SToby Isaac   PetscErrorCode ierr;
55484de306b1SToby Isaac 
55494de306b1SToby Isaac   PetscFunctionBegin;
55504de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
55514de306b1SToby Isaac   PetscValidCharPointer(name, 2);
55524de306b1SToby Isaac   PetscValidPointer(points, 4);
55534de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
55544de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
55554de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
55564de306b1SToby Isaac   PetscFunctionReturn(0);
55574de306b1SToby Isaac }
55584de306b1SToby Isaac 
5559c58f1c22SToby Isaac /*@C
5560c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
5561c58f1c22SToby Isaac 
5562c58f1c22SToby Isaac   Not Collective
5563c58f1c22SToby Isaac 
5564c58f1c22SToby Isaac   Input Parameters:
5565c58f1c22SToby Isaac + dm   - The DM object
5566c58f1c22SToby Isaac . name - The label name
5567c58f1c22SToby Isaac - value - The label value for this point
5568c58f1c22SToby Isaac 
5569c58f1c22SToby Isaac   Output Parameter:
5570c58f1c22SToby Isaac 
5571c58f1c22SToby Isaac   Level: beginner
5572c58f1c22SToby Isaac 
5573c58f1c22SToby Isaac .keywords: mesh
5574c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
5575c58f1c22SToby Isaac @*/
5576c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
5577c58f1c22SToby Isaac {
5578c58f1c22SToby Isaac   DMLabel        label;
5579c58f1c22SToby Isaac   PetscErrorCode ierr;
5580c58f1c22SToby Isaac 
5581c58f1c22SToby Isaac   PetscFunctionBegin;
5582c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5583c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5584c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
5585c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
5586c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
5587c58f1c22SToby Isaac   PetscFunctionReturn(0);
5588c58f1c22SToby Isaac }
5589c58f1c22SToby Isaac 
5590c58f1c22SToby Isaac /*@
5591c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
5592c58f1c22SToby Isaac 
5593c58f1c22SToby Isaac   Not Collective
5594c58f1c22SToby Isaac 
5595c58f1c22SToby Isaac   Input Parameter:
5596c58f1c22SToby Isaac . dm   - The DM object
5597c58f1c22SToby Isaac 
5598c58f1c22SToby Isaac   Output Parameter:
5599c58f1c22SToby Isaac . numLabels - the number of Labels
5600c58f1c22SToby Isaac 
5601c58f1c22SToby Isaac   Level: intermediate
5602c58f1c22SToby Isaac 
5603c58f1c22SToby Isaac .keywords: mesh
5604c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5605c58f1c22SToby Isaac @*/
5606c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
5607c58f1c22SToby Isaac {
5608c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5609c58f1c22SToby Isaac   PetscInt  n    = 0;
5610c58f1c22SToby Isaac 
5611c58f1c22SToby Isaac   PetscFunctionBegin;
5612c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5613c58f1c22SToby Isaac   PetscValidPointer(numLabels, 2);
5614c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
5615c58f1c22SToby Isaac   *numLabels = n;
5616c58f1c22SToby Isaac   PetscFunctionReturn(0);
5617c58f1c22SToby Isaac }
5618c58f1c22SToby Isaac 
5619c58f1c22SToby Isaac /*@C
5620c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
5621c58f1c22SToby Isaac 
5622c58f1c22SToby Isaac   Not Collective
5623c58f1c22SToby Isaac 
5624c58f1c22SToby Isaac   Input Parameters:
5625c58f1c22SToby Isaac + dm - The DM object
5626c58f1c22SToby Isaac - n  - the label number
5627c58f1c22SToby Isaac 
5628c58f1c22SToby Isaac   Output Parameter:
5629c58f1c22SToby Isaac . name - the label name
5630c58f1c22SToby Isaac 
5631c58f1c22SToby Isaac   Level: intermediate
5632c58f1c22SToby Isaac 
5633c58f1c22SToby Isaac .keywords: mesh
5634c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5635c58f1c22SToby Isaac @*/
5636c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
5637c58f1c22SToby Isaac {
5638c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5639c58f1c22SToby Isaac   PetscInt  l    = 0;
5640c58f1c22SToby Isaac 
5641c58f1c22SToby Isaac   PetscFunctionBegin;
5642c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5643c58f1c22SToby Isaac   PetscValidPointer(name, 3);
5644c58f1c22SToby Isaac   while (next) {
5645c58f1c22SToby Isaac     if (l == n) {
5646c58f1c22SToby Isaac       *name = next->label->name;
5647c58f1c22SToby Isaac       PetscFunctionReturn(0);
5648c58f1c22SToby Isaac     }
5649c58f1c22SToby Isaac     ++l;
5650c58f1c22SToby Isaac     next = next->next;
5651c58f1c22SToby Isaac   }
5652c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5653c58f1c22SToby Isaac }
5654c58f1c22SToby Isaac 
5655c58f1c22SToby Isaac /*@C
5656c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
5657c58f1c22SToby Isaac 
5658c58f1c22SToby Isaac   Not Collective
5659c58f1c22SToby Isaac 
5660c58f1c22SToby Isaac   Input Parameters:
5661c58f1c22SToby Isaac + dm   - The DM object
5662c58f1c22SToby Isaac - name - The label name
5663c58f1c22SToby Isaac 
5664c58f1c22SToby Isaac   Output Parameter:
5665c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
5666c58f1c22SToby Isaac 
5667c58f1c22SToby Isaac   Level: intermediate
5668c58f1c22SToby Isaac 
5669c58f1c22SToby Isaac .keywords: mesh
5670c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5671c58f1c22SToby Isaac @*/
5672c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
5673c58f1c22SToby Isaac {
5674c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5675c58f1c22SToby Isaac   PetscErrorCode ierr;
5676c58f1c22SToby Isaac 
5677c58f1c22SToby Isaac   PetscFunctionBegin;
5678c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5679c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5680c58f1c22SToby Isaac   PetscValidPointer(hasLabel, 3);
5681c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
5682c58f1c22SToby Isaac   while (next) {
5683c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, hasLabel);CHKERRQ(ierr);
5684c58f1c22SToby Isaac     if (*hasLabel) break;
5685c58f1c22SToby Isaac     next = next->next;
5686c58f1c22SToby Isaac   }
5687c58f1c22SToby Isaac   PetscFunctionReturn(0);
5688c58f1c22SToby Isaac }
5689c58f1c22SToby Isaac 
5690c58f1c22SToby Isaac /*@C
5691c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
5692c58f1c22SToby Isaac 
5693c58f1c22SToby Isaac   Not Collective
5694c58f1c22SToby Isaac 
5695c58f1c22SToby Isaac   Input Parameters:
5696c58f1c22SToby Isaac + dm   - The DM object
5697c58f1c22SToby Isaac - name - The label name
5698c58f1c22SToby Isaac 
5699c58f1c22SToby Isaac   Output Parameter:
5700c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5701c58f1c22SToby Isaac 
5702c58f1c22SToby Isaac   Level: intermediate
5703c58f1c22SToby Isaac 
5704c58f1c22SToby Isaac .keywords: mesh
5705c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5706c58f1c22SToby Isaac @*/
5707c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
5708c58f1c22SToby Isaac {
5709c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5710c58f1c22SToby Isaac   PetscBool      hasLabel;
5711c58f1c22SToby Isaac   PetscErrorCode ierr;
5712c58f1c22SToby Isaac 
5713c58f1c22SToby Isaac   PetscFunctionBegin;
5714c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5715c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
5716c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5717c58f1c22SToby Isaac   *label = NULL;
5718c58f1c22SToby Isaac   while (next) {
5719c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5720c58f1c22SToby Isaac     if (hasLabel) {
5721c58f1c22SToby Isaac       *label = next->label;
5722c58f1c22SToby Isaac       break;
5723c58f1c22SToby Isaac     }
5724c58f1c22SToby Isaac     next = next->next;
5725c58f1c22SToby Isaac   }
5726c58f1c22SToby Isaac   PetscFunctionReturn(0);
5727c58f1c22SToby Isaac }
5728c58f1c22SToby Isaac 
5729c58f1c22SToby Isaac /*@C
5730c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
5731c58f1c22SToby Isaac 
5732c58f1c22SToby Isaac   Not Collective
5733c58f1c22SToby Isaac 
5734c58f1c22SToby Isaac   Input Parameters:
5735c58f1c22SToby Isaac + dm - The DM object
5736c58f1c22SToby Isaac - n  - the label number
5737c58f1c22SToby Isaac 
5738c58f1c22SToby Isaac   Output Parameter:
5739c58f1c22SToby Isaac . label - the label
5740c58f1c22SToby Isaac 
5741c58f1c22SToby Isaac   Level: intermediate
5742c58f1c22SToby Isaac 
5743c58f1c22SToby Isaac .keywords: mesh
5744c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5745c58f1c22SToby Isaac @*/
5746c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
5747c58f1c22SToby Isaac {
5748c58f1c22SToby Isaac   DMLabelLink next = dm->labels->next;
5749c58f1c22SToby Isaac   PetscInt    l    = 0;
5750c58f1c22SToby Isaac 
5751c58f1c22SToby Isaac   PetscFunctionBegin;
5752c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5753c58f1c22SToby Isaac   PetscValidPointer(label, 3);
5754c58f1c22SToby Isaac   while (next) {
5755c58f1c22SToby Isaac     if (l == n) {
5756c58f1c22SToby Isaac       *label = next->label;
5757c58f1c22SToby Isaac       PetscFunctionReturn(0);
5758c58f1c22SToby Isaac     }
5759c58f1c22SToby Isaac     ++l;
5760c58f1c22SToby Isaac     next = next->next;
5761c58f1c22SToby Isaac   }
5762c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
5763c58f1c22SToby Isaac }
5764c58f1c22SToby Isaac 
5765c58f1c22SToby Isaac /*@C
5766c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
5767c58f1c22SToby Isaac 
5768c58f1c22SToby Isaac   Not Collective
5769c58f1c22SToby Isaac 
5770c58f1c22SToby Isaac   Input Parameters:
5771c58f1c22SToby Isaac + dm   - The DM object
5772c58f1c22SToby Isaac - label - The DMLabel
5773c58f1c22SToby Isaac 
5774c58f1c22SToby Isaac   Level: developer
5775c58f1c22SToby Isaac 
5776c58f1c22SToby Isaac .keywords: mesh
5777c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5778c58f1c22SToby Isaac @*/
5779c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
5780c58f1c22SToby Isaac {
5781c58f1c22SToby Isaac   DMLabelLink    tmpLabel;
5782c58f1c22SToby Isaac   PetscBool      hasLabel;
5783c58f1c22SToby Isaac   PetscErrorCode ierr;
5784c58f1c22SToby Isaac 
5785c58f1c22SToby Isaac   PetscFunctionBegin;
5786c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5787c58f1c22SToby Isaac   ierr = DMHasLabel(dm, label->name, &hasLabel);CHKERRQ(ierr);
5788c58f1c22SToby Isaac   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", label->name);
5789c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
5790c58f1c22SToby Isaac   tmpLabel->label  = label;
5791c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
5792c58f1c22SToby Isaac   tmpLabel->next   = dm->labels->next;
5793c58f1c22SToby Isaac   dm->labels->next = tmpLabel;
5794c58f1c22SToby Isaac   PetscFunctionReturn(0);
5795c58f1c22SToby Isaac }
5796c58f1c22SToby Isaac 
5797c58f1c22SToby Isaac /*@C
5798c58f1c22SToby Isaac   DMRemoveLabel - Remove the label from this mesh
5799c58f1c22SToby Isaac 
5800c58f1c22SToby Isaac   Not Collective
5801c58f1c22SToby Isaac 
5802c58f1c22SToby Isaac   Input Parameters:
5803c58f1c22SToby Isaac + dm   - The DM object
5804c58f1c22SToby Isaac - name - The label name
5805c58f1c22SToby Isaac 
5806c58f1c22SToby Isaac   Output Parameter:
5807c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
5808c58f1c22SToby Isaac 
5809c58f1c22SToby Isaac   Level: developer
5810c58f1c22SToby Isaac 
5811c58f1c22SToby Isaac .keywords: mesh
5812c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5813c58f1c22SToby Isaac @*/
5814c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
5815c58f1c22SToby Isaac {
5816c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5817c58f1c22SToby Isaac   DMLabelLink    last = NULL;
5818c58f1c22SToby Isaac   PetscBool      hasLabel;
5819c58f1c22SToby Isaac   PetscErrorCode ierr;
5820c58f1c22SToby Isaac 
5821c58f1c22SToby Isaac   PetscFunctionBegin;
5822c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5823c58f1c22SToby Isaac   ierr   = DMHasLabel(dm, name, &hasLabel);CHKERRQ(ierr);
5824c58f1c22SToby Isaac   *label = NULL;
5825c58f1c22SToby Isaac   if (!hasLabel) PetscFunctionReturn(0);
5826c58f1c22SToby Isaac   while (next) {
5827c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &hasLabel);CHKERRQ(ierr);
5828c58f1c22SToby Isaac     if (hasLabel) {
5829c58f1c22SToby Isaac       if (last) last->next       = next->next;
5830c58f1c22SToby Isaac       else      dm->labels->next = next->next;
5831c58f1c22SToby Isaac       next->next = NULL;
5832c58f1c22SToby Isaac       *label     = next->label;
5833c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
5834c58f1c22SToby Isaac       if (hasLabel) {
5835c58f1c22SToby Isaac         dm->depthLabel = NULL;
5836c58f1c22SToby Isaac       }
5837c58f1c22SToby Isaac       ierr = PetscFree(next);CHKERRQ(ierr);
5838c58f1c22SToby Isaac       break;
5839c58f1c22SToby Isaac     }
5840c58f1c22SToby Isaac     last = next;
5841c58f1c22SToby Isaac     next = next->next;
5842c58f1c22SToby Isaac   }
5843c58f1c22SToby Isaac   PetscFunctionReturn(0);
5844c58f1c22SToby Isaac }
5845c58f1c22SToby Isaac 
5846c58f1c22SToby Isaac /*@C
5847c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
5848c58f1c22SToby Isaac 
5849c58f1c22SToby Isaac   Not Collective
5850c58f1c22SToby Isaac 
5851c58f1c22SToby Isaac   Input Parameters:
5852c58f1c22SToby Isaac + dm   - The DM object
5853c58f1c22SToby Isaac - name - The label name
5854c58f1c22SToby Isaac 
5855c58f1c22SToby Isaac   Output Parameter:
5856c58f1c22SToby Isaac . output - The flag for output
5857c58f1c22SToby Isaac 
5858c58f1c22SToby Isaac   Level: developer
5859c58f1c22SToby Isaac 
5860c58f1c22SToby Isaac .keywords: mesh
5861c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5862c58f1c22SToby Isaac @*/
5863c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
5864c58f1c22SToby Isaac {
5865c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5866c58f1c22SToby Isaac   PetscErrorCode ierr;
5867c58f1c22SToby Isaac 
5868c58f1c22SToby Isaac   PetscFunctionBegin;
5869c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5870c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5871c58f1c22SToby Isaac   PetscValidPointer(output, 3);
5872c58f1c22SToby Isaac   while (next) {
5873c58f1c22SToby Isaac     PetscBool flg;
5874c58f1c22SToby Isaac 
5875c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5876c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
5877c58f1c22SToby Isaac     next = next->next;
5878c58f1c22SToby Isaac   }
5879c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5880c58f1c22SToby Isaac }
5881c58f1c22SToby Isaac 
5882c58f1c22SToby Isaac /*@C
5883c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
5884c58f1c22SToby Isaac 
5885c58f1c22SToby Isaac   Not Collective
5886c58f1c22SToby Isaac 
5887c58f1c22SToby Isaac   Input Parameters:
5888c58f1c22SToby Isaac + dm     - The DM object
5889c58f1c22SToby Isaac . name   - The label name
5890c58f1c22SToby Isaac - output - The flag for output
5891c58f1c22SToby Isaac 
5892c58f1c22SToby Isaac   Level: developer
5893c58f1c22SToby Isaac 
5894c58f1c22SToby Isaac .keywords: mesh
5895c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
5896c58f1c22SToby Isaac @*/
5897c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
5898c58f1c22SToby Isaac {
5899c58f1c22SToby Isaac   DMLabelLink    next = dm->labels->next;
5900c58f1c22SToby Isaac   PetscErrorCode ierr;
5901c58f1c22SToby Isaac 
5902c58f1c22SToby Isaac   PetscFunctionBegin;
5903c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5904c58f1c22SToby Isaac   PetscValidPointer(name, 2);
5905c58f1c22SToby Isaac   while (next) {
5906c58f1c22SToby Isaac     PetscBool flg;
5907c58f1c22SToby Isaac 
5908c58f1c22SToby Isaac     ierr = PetscStrcmp(name, next->label->name, &flg);CHKERRQ(ierr);
5909c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
5910c58f1c22SToby Isaac     next = next->next;
5911c58f1c22SToby Isaac   }
5912c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
5913c58f1c22SToby Isaac }
5914c58f1c22SToby Isaac 
5915c58f1c22SToby Isaac 
5916c58f1c22SToby Isaac /*@
5917c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
5918c58f1c22SToby Isaac 
5919c58f1c22SToby Isaac   Collective on DM
5920c58f1c22SToby Isaac 
5921c58f1c22SToby Isaac   Input Parameter:
59222e17dfb7SMatthew G. Knepley . dmA - The DM object with initial labels
5923c58f1c22SToby Isaac 
5924c58f1c22SToby Isaac   Output Parameter:
59252e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
5926c58f1c22SToby Isaac 
5927c58f1c22SToby Isaac   Level: intermediate
5928c58f1c22SToby Isaac 
5929c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
5930c58f1c22SToby Isaac 
5931c58f1c22SToby Isaac .keywords: mesh
5932c58f1c22SToby Isaac .seealso: DMCopyCoordinates(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection()
5933c58f1c22SToby Isaac @*/
5934c58f1c22SToby Isaac PetscErrorCode DMCopyLabels(DM dmA, DM dmB)
5935c58f1c22SToby Isaac {
5936c58f1c22SToby Isaac   PetscInt       numLabels, l;
5937c58f1c22SToby Isaac   PetscErrorCode ierr;
5938c58f1c22SToby Isaac 
5939c58f1c22SToby Isaac   PetscFunctionBegin;
5940c58f1c22SToby Isaac   if (dmA == dmB) PetscFunctionReturn(0);
5941c58f1c22SToby Isaac   ierr = DMGetNumLabels(dmA, &numLabels);CHKERRQ(ierr);
5942c58f1c22SToby Isaac   for (l = 0; l < numLabels; ++l) {
5943c58f1c22SToby Isaac     DMLabel     label, labelNew;
5944c58f1c22SToby Isaac     const char *name;
5945c58f1c22SToby Isaac     PetscBool   flg;
5946c58f1c22SToby Isaac 
5947c58f1c22SToby Isaac     ierr = DMGetLabelName(dmA, l, &name);CHKERRQ(ierr);
5948c58f1c22SToby Isaac     ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
5949c58f1c22SToby Isaac     if (flg) continue;
5950c58f1c22SToby Isaac     ierr = DMGetLabel(dmA, name, &label);CHKERRQ(ierr);
5951c58f1c22SToby Isaac     ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
5952c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
5953c58f1c22SToby Isaac   }
5954c58f1c22SToby Isaac   PetscFunctionReturn(0);
5955c58f1c22SToby Isaac }
5956a8fb8f29SToby Isaac 
5957a8fb8f29SToby Isaac /*@
5958a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
5959a8fb8f29SToby Isaac 
5960a8fb8f29SToby Isaac   Input Parameter:
5961a8fb8f29SToby Isaac . dm - The DM object
5962a8fb8f29SToby Isaac 
5963a8fb8f29SToby Isaac   Output Parameter:
5964a8fb8f29SToby Isaac . cdm - The coarse DM
5965a8fb8f29SToby Isaac 
5966a8fb8f29SToby Isaac   Level: intermediate
5967a8fb8f29SToby Isaac 
5968a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
5969a8fb8f29SToby Isaac @*/
5970a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
5971a8fb8f29SToby Isaac {
5972a8fb8f29SToby Isaac   PetscFunctionBegin;
5973a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5974a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
5975a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
5976a8fb8f29SToby Isaac   PetscFunctionReturn(0);
5977a8fb8f29SToby Isaac }
5978a8fb8f29SToby Isaac 
5979a8fb8f29SToby Isaac /*@
5980a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
5981a8fb8f29SToby Isaac 
5982a8fb8f29SToby Isaac   Input Parameters:
5983a8fb8f29SToby Isaac + dm - The DM object
5984a8fb8f29SToby Isaac - cdm - The coarse DM
5985a8fb8f29SToby Isaac 
5986a8fb8f29SToby Isaac   Level: intermediate
5987a8fb8f29SToby Isaac 
5988a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
5989a8fb8f29SToby Isaac @*/
5990a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
5991a8fb8f29SToby Isaac {
5992a8fb8f29SToby Isaac   PetscErrorCode ierr;
5993a8fb8f29SToby Isaac 
5994a8fb8f29SToby Isaac   PetscFunctionBegin;
5995a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5996a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
5997a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
5998a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
5999a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
6000a8fb8f29SToby Isaac   PetscFunctionReturn(0);
6001a8fb8f29SToby Isaac }
6002a8fb8f29SToby Isaac 
600388bdff64SToby Isaac /*@
600488bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
600588bdff64SToby Isaac 
600688bdff64SToby Isaac   Input Parameter:
600788bdff64SToby Isaac . dm - The DM object
600888bdff64SToby Isaac 
600988bdff64SToby Isaac   Output Parameter:
601088bdff64SToby Isaac . fdm - The fine DM
601188bdff64SToby Isaac 
601288bdff64SToby Isaac   Level: intermediate
601388bdff64SToby Isaac 
601488bdff64SToby Isaac .seealso: DMSetFineDM()
601588bdff64SToby Isaac @*/
601688bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
601788bdff64SToby Isaac {
601888bdff64SToby Isaac   PetscFunctionBegin;
601988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
602088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
602188bdff64SToby Isaac   *fdm = dm->fineMesh;
602288bdff64SToby Isaac   PetscFunctionReturn(0);
602388bdff64SToby Isaac }
602488bdff64SToby Isaac 
602588bdff64SToby Isaac /*@
602688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
602788bdff64SToby Isaac 
602888bdff64SToby Isaac   Input Parameters:
602988bdff64SToby Isaac + dm - The DM object
603088bdff64SToby Isaac - fdm - The fine DM
603188bdff64SToby Isaac 
603288bdff64SToby Isaac   Level: intermediate
603388bdff64SToby Isaac 
603488bdff64SToby Isaac .seealso: DMGetFineDM()
603588bdff64SToby Isaac @*/
603688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
603788bdff64SToby Isaac {
603888bdff64SToby Isaac   PetscErrorCode ierr;
603988bdff64SToby Isaac 
604088bdff64SToby Isaac   PetscFunctionBegin;
604188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
604288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
604388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
604488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
604588bdff64SToby Isaac   dm->fineMesh = fdm;
604688bdff64SToby Isaac   PetscFunctionReturn(0);
604788bdff64SToby Isaac }
604888bdff64SToby Isaac 
6049a6ba4734SToby Isaac /*=== DMBoundary code ===*/
6050a6ba4734SToby Isaac 
6051a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
6052a6ba4734SToby Isaac {
6053a6ba4734SToby Isaac   PetscErrorCode ierr;
6054a6ba4734SToby Isaac 
6055a6ba4734SToby Isaac   PetscFunctionBegin;
6056e6f8dbb6SToby Isaac   ierr = PetscDSCopyBoundary(dm->prob,dmNew->prob);CHKERRQ(ierr);
6057a6ba4734SToby Isaac   PetscFunctionReturn(0);
6058a6ba4734SToby Isaac }
6059a6ba4734SToby Isaac 
6060a6ba4734SToby Isaac /*@C
6061a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
6062a6ba4734SToby Isaac 
6063a6ba4734SToby Isaac   Input Parameters:
60644c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
6065f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6066a6ba4734SToby Isaac . name        - The BC name
6067a6ba4734SToby Isaac . labelname   - The label defining constrained points
6068a6ba4734SToby Isaac . field       - The field to constrain
6069a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6070a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6071a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6072a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6073a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6074a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6075a6ba4734SToby Isaac 
6076a6ba4734SToby Isaac   Options Database Keys:
6077a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6078a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6079a6ba4734SToby Isaac 
6080a6ba4734SToby Isaac   Level: developer
6081a6ba4734SToby Isaac 
6082a6ba4734SToby Isaac .seealso: DMGetBoundary()
6083a6ba4734SToby Isaac @*/
6084a30ec4eaSSatish 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)
6085a6ba4734SToby Isaac {
6086a6ba4734SToby Isaac   PetscErrorCode ierr;
6087a6ba4734SToby Isaac 
6088a6ba4734SToby Isaac   PetscFunctionBegin;
6089a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6090f971fd6bSMatthew G. Knepley   ierr = PetscDSAddBoundary(dm->prob,type,name,labelname,field,numcomps,comps,bcFunc,numids,ids,ctx);CHKERRQ(ierr);
6091a6ba4734SToby Isaac   PetscFunctionReturn(0);
6092a6ba4734SToby Isaac }
6093a6ba4734SToby Isaac 
6094a6ba4734SToby Isaac /*@
6095a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
6096a6ba4734SToby Isaac 
6097a6ba4734SToby Isaac   Input Parameters:
6098a6ba4734SToby Isaac . dm - The mesh object
6099a6ba4734SToby Isaac 
6100a6ba4734SToby Isaac   Output Parameters:
6101a6ba4734SToby Isaac . numBd - The number of BC
6102a6ba4734SToby Isaac 
6103a6ba4734SToby Isaac   Level: intermediate
6104a6ba4734SToby Isaac 
6105a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
6106a6ba4734SToby Isaac @*/
6107a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
6108a6ba4734SToby Isaac {
610958ebd649SToby Isaac   PetscErrorCode ierr;
6110a6ba4734SToby Isaac 
6111a6ba4734SToby Isaac   PetscFunctionBegin;
6112a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
611358ebd649SToby Isaac   ierr = PetscDSGetNumBoundary(dm->prob,numBd);CHKERRQ(ierr);
6114a6ba4734SToby Isaac   PetscFunctionReturn(0);
6115a6ba4734SToby Isaac }
6116a6ba4734SToby Isaac 
6117a6ba4734SToby Isaac /*@C
61181c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
6119a6ba4734SToby Isaac 
6120a6ba4734SToby Isaac   Input Parameters:
6121a6ba4734SToby Isaac + dm          - The mesh object
6122a6ba4734SToby Isaac - bd          - The BC number
6123a6ba4734SToby Isaac 
6124a6ba4734SToby Isaac   Output Parameters:
6125f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
6126a6ba4734SToby Isaac . name        - The BC name
6127a6ba4734SToby Isaac . labelname   - The label defining constrained points
6128a6ba4734SToby Isaac . field       - The field to constrain
6129a6ba4734SToby Isaac . numcomps    - The number of constrained field components
6130a6ba4734SToby Isaac . comps       - An array of constrained component numbers
6131a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
6132a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
6133a6ba4734SToby Isaac . ids         - An array of ids for constrained points
6134a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
6135a6ba4734SToby Isaac 
6136a6ba4734SToby Isaac   Options Database Keys:
6137a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
6138a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
6139a6ba4734SToby Isaac 
6140a6ba4734SToby Isaac   Level: developer
6141a6ba4734SToby Isaac 
6142a6ba4734SToby Isaac .seealso: DMAddBoundary()
6143a6ba4734SToby Isaac @*/
6144a30ec4eaSSatish 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)
6145a6ba4734SToby Isaac {
614658ebd649SToby Isaac   PetscErrorCode ierr;
6147a6ba4734SToby Isaac 
6148a6ba4734SToby Isaac   PetscFunctionBegin;
6149a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6150f971fd6bSMatthew G. Knepley   ierr = PetscDSGetBoundary(dm->prob,bd,type,name,labelname,field,numcomps,comps,func,numids,ids,ctx);CHKERRQ(ierr);
6151a6ba4734SToby Isaac   PetscFunctionReturn(0);
6152a6ba4734SToby Isaac }
6153a6ba4734SToby Isaac 
6154e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
6155e6f8dbb6SToby Isaac {
6156dff059c6SToby Isaac   DMBoundary *lastnext;
6157e6f8dbb6SToby Isaac   DSBoundary dsbound;
6158e6f8dbb6SToby Isaac   PetscErrorCode ierr;
6159e6f8dbb6SToby Isaac 
6160e6f8dbb6SToby Isaac   PetscFunctionBegin;
6161e6f8dbb6SToby Isaac   dsbound = dm->prob->boundary;
616247a1f5adSToby Isaac   if (dm->boundary) {
616347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
616447a1f5adSToby Isaac 
616547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
616647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
616747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
616847a1f5adSToby Isaac     while (next) {
616947a1f5adSToby Isaac       DMBoundary b = next;
617047a1f5adSToby Isaac 
617147a1f5adSToby Isaac       next = b->next;
617247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
6173a6ba4734SToby Isaac     }
617447a1f5adSToby Isaac     dm->boundary = NULL;
6175a6ba4734SToby Isaac   }
617647a1f5adSToby Isaac 
6177dff059c6SToby Isaac   lastnext = &(dm->boundary);
6178e6f8dbb6SToby Isaac   while (dsbound) {
6179e6f8dbb6SToby Isaac     DMBoundary dmbound;
6180e6f8dbb6SToby Isaac 
6181e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
6182e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
6183e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
6184e6f8dbb6SToby Isaac     if (!dmbound->label) PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);
618547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
6186dff059c6SToby Isaac     *lastnext = dmbound;
6187dff059c6SToby Isaac     lastnext = &(dmbound->next);
6188dff059c6SToby Isaac     dsbound = dsbound->next;
6189a6ba4734SToby Isaac   }
6190a6ba4734SToby Isaac   PetscFunctionReturn(0);
6191a6ba4734SToby Isaac }
6192a6ba4734SToby Isaac 
6193a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
6194a6ba4734SToby Isaac {
6195b95f2879SToby Isaac   DMBoundary     b;
6196a6ba4734SToby Isaac   PetscErrorCode ierr;
6197a6ba4734SToby Isaac 
6198a6ba4734SToby Isaac   PetscFunctionBegin;
6199a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6200a6ba4734SToby Isaac   PetscValidPointer(isBd, 3);
6201a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
6202e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
6203b95f2879SToby Isaac   b = dm->boundary;
6204a6ba4734SToby Isaac   while (b && !(*isBd)) {
6205e6f8dbb6SToby Isaac     DMLabel    label = b->label;
6206e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
62073424c85cSToby Isaac 
62083424c85cSToby Isaac     if (label) {
6209a6ba4734SToby Isaac       PetscInt i;
6210a6ba4734SToby Isaac 
6211e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
6212e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
6213a6ba4734SToby Isaac       }
6214a6ba4734SToby Isaac     }
6215a6ba4734SToby Isaac     b = b->next;
6216a6ba4734SToby Isaac   }
6217a6ba4734SToby Isaac   PetscFunctionReturn(0);
6218a6ba4734SToby Isaac }
62194d6f44ffSToby Isaac 
62204d6f44ffSToby Isaac /*@C
62214d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
62224d6f44ffSToby Isaac 
62234d6f44ffSToby Isaac   Input Parameters:
62244d6f44ffSToby Isaac + dm      - The DM
62250709b2feSToby Isaac . time    - The time
62264d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
62274d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
62284d6f44ffSToby Isaac - mode    - The insertion mode for values
62294d6f44ffSToby Isaac 
62304d6f44ffSToby Isaac   Output Parameter:
62314d6f44ffSToby Isaac . X - vector
62324d6f44ffSToby Isaac 
62334d6f44ffSToby Isaac    Calling sequence of func:
62340709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
62354d6f44ffSToby Isaac 
62364d6f44ffSToby Isaac +  dim - The spatial dimension
62374d6f44ffSToby Isaac .  x   - The coordinates
62384d6f44ffSToby Isaac .  Nf  - The number of fields
62394d6f44ffSToby Isaac .  u   - The output field values
62404d6f44ffSToby Isaac -  ctx - optional user-defined function context
62414d6f44ffSToby Isaac 
62424d6f44ffSToby Isaac   Level: developer
62434d6f44ffSToby Isaac 
62442716604bSToby Isaac .seealso: DMComputeL2Diff()
62454d6f44ffSToby Isaac @*/
62460709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
62474d6f44ffSToby Isaac {
62484d6f44ffSToby Isaac   Vec            localX;
62494d6f44ffSToby Isaac   PetscErrorCode ierr;
62504d6f44ffSToby Isaac 
62514d6f44ffSToby Isaac   PetscFunctionBegin;
62524d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62534d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62540709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62554d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62564d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62574d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62584d6f44ffSToby Isaac   PetscFunctionReturn(0);
62594d6f44ffSToby Isaac }
62604d6f44ffSToby Isaac 
62610709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
62624d6f44ffSToby Isaac {
62634d6f44ffSToby Isaac   PetscErrorCode ierr;
62644d6f44ffSToby Isaac 
62654d6f44ffSToby Isaac   PetscFunctionBegin;
62664d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62674d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62680918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
62690709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62704d6f44ffSToby Isaac   PetscFunctionReturn(0);
62714d6f44ffSToby Isaac }
62724d6f44ffSToby Isaac 
62732c53366bSMatthew 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)
62742c53366bSMatthew G. Knepley {
62752c53366bSMatthew G. Knepley   Vec            localX;
62762c53366bSMatthew G. Knepley   PetscErrorCode ierr;
62772c53366bSMatthew G. Knepley 
62782c53366bSMatthew G. Knepley   PetscFunctionBegin;
62792c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
62802c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
62812c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62822c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
62832c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
62842c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
62852c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
62862c53366bSMatthew G. Knepley }
62872c53366bSMatthew G. Knepley 
62881c531cf8SMatthew 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)
62894d6f44ffSToby Isaac {
62904d6f44ffSToby Isaac   PetscErrorCode ierr;
62914d6f44ffSToby Isaac 
62924d6f44ffSToby Isaac   PetscFunctionBegin;
62934d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62944d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
62950918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
62961c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
62974d6f44ffSToby Isaac   PetscFunctionReturn(0);
62984d6f44ffSToby Isaac }
62992716604bSToby Isaac 
63008c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
63018c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
63028c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
63038c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6304191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
63058c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
63068c6c5593SMatthew G. Knepley {
63078c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
63088c6c5593SMatthew G. Knepley 
63098c6c5593SMatthew G. Knepley   PetscFunctionBegin;
63108c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63118c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
63128c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
63130918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
63148c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
63158c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
63168c6c5593SMatthew G. Knepley }
63178c6c5593SMatthew G. Knepley 
63181c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
63198c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
63208c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
63218c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
6322191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
63238c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
63248c6c5593SMatthew G. Knepley {
63258c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
63268c6c5593SMatthew G. Knepley 
63278c6c5593SMatthew G. Knepley   PetscFunctionBegin;
63288c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
63298c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
63308c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
63310918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
63321c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
63338c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
63348c6c5593SMatthew G. Knepley }
63358c6c5593SMatthew G. Knepley 
63362716604bSToby Isaac /*@C
63372716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
63382716604bSToby Isaac 
63392716604bSToby Isaac   Input Parameters:
63402716604bSToby Isaac + dm    - The DM
63410709b2feSToby Isaac . time  - The time
63422716604bSToby Isaac . funcs - The functions to evaluate for each field component
63432716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6344574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
63452716604bSToby Isaac 
63462716604bSToby Isaac   Output Parameter:
63472716604bSToby Isaac . diff - The diff ||u - u_h||_2
63482716604bSToby Isaac 
63492716604bSToby Isaac   Level: developer
63502716604bSToby Isaac 
63511189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
63522716604bSToby Isaac @*/
63530709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
63542716604bSToby Isaac {
63552716604bSToby Isaac   PetscErrorCode ierr;
63562716604bSToby Isaac 
63572716604bSToby Isaac   PetscFunctionBegin;
63582716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6359b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
63600918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
63610709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
63622716604bSToby Isaac   PetscFunctionReturn(0);
63632716604bSToby Isaac }
6364b698f381SToby Isaac 
6365b698f381SToby Isaac /*@C
6366b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
6367b698f381SToby Isaac 
6368b698f381SToby Isaac   Input Parameters:
6369b698f381SToby Isaac + dm    - The DM
6370b698f381SToby Isaac , time  - The time
6371b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
6372b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6373574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
6374b698f381SToby Isaac - n     - The vector to project along
6375b698f381SToby Isaac 
6376b698f381SToby Isaac   Output Parameter:
6377b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
6378b698f381SToby Isaac 
6379b698f381SToby Isaac   Level: developer
6380b698f381SToby Isaac 
6381b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
6382b698f381SToby Isaac @*/
6383b698f381SToby 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)
6384b698f381SToby Isaac {
6385b698f381SToby Isaac   PetscErrorCode ierr;
6386b698f381SToby Isaac 
6387b698f381SToby Isaac   PetscFunctionBegin;
6388b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6389b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
6390b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
6391b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
6392b698f381SToby Isaac   PetscFunctionReturn(0);
6393b698f381SToby Isaac }
6394b698f381SToby Isaac 
63952a16baeaSToby Isaac /*@C
63962a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
63972a16baeaSToby Isaac 
63982a16baeaSToby Isaac   Input Parameters:
63992a16baeaSToby Isaac + dm    - The DM
64002a16baeaSToby Isaac . time  - The time
64012a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
64022a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
6403574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
64042a16baeaSToby Isaac 
64052a16baeaSToby Isaac   Output Parameter:
64062a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
64072a16baeaSToby Isaac 
64082a16baeaSToby Isaac   Level: developer
64092a16baeaSToby Isaac 
64101189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
64112a16baeaSToby Isaac @*/
64121189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
64132a16baeaSToby Isaac {
64142a16baeaSToby Isaac   PetscErrorCode ierr;
64152a16baeaSToby Isaac 
64162a16baeaSToby Isaac   PetscFunctionBegin;
64172a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
64182a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
64190918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
64202a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
64212a16baeaSToby Isaac   PetscFunctionReturn(0);
64222a16baeaSToby Isaac }
64232a16baeaSToby Isaac 
6424df0b854cSToby Isaac /*@C
6425df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
6426cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
6427df0b854cSToby Isaac 
6428df0b854cSToby Isaac   Collective on dm
6429df0b854cSToby Isaac 
6430df0b854cSToby Isaac   Input parameters:
6431df0b854cSToby Isaac + dm - the pre-adaptation DM object
6432a1b0c543SToby Isaac - label - label with the flags
6433df0b854cSToby Isaac 
6434df0b854cSToby Isaac   Output parameters:
64350d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
6436df0b854cSToby Isaac 
6437df0b854cSToby Isaac   Level: intermediate
64380d1cd5e0SMatthew G. Knepley 
64390d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
6440df0b854cSToby Isaac @*/
64410d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
6442df0b854cSToby Isaac {
6443df0b854cSToby Isaac   PetscErrorCode ierr;
6444df0b854cSToby Isaac 
6445df0b854cSToby Isaac   PetscFunctionBegin;
6446df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6447a1b0c543SToby Isaac   PetscValidPointer(label,2);
64480d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
64490d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
64506f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
64510d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
64520d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
64530d1cd5e0SMatthew G. Knepley }
64540d1cd5e0SMatthew G. Knepley 
64550d1cd5e0SMatthew G. Knepley /*@C
64560d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
64570d1cd5e0SMatthew G. Knepley 
64580d1cd5e0SMatthew G. Knepley   Input Parameters:
64590d1cd5e0SMatthew G. Knepley + dm - The DM object
64600d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
64616f25b0d8SLisandro 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_".
64620d1cd5e0SMatthew G. Knepley 
64630d1cd5e0SMatthew G. Knepley   Output Parameter:
64640d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
64650d1cd5e0SMatthew G. Knepley 
64660d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
64670d1cd5e0SMatthew G. Knepley 
64680d1cd5e0SMatthew G. Knepley   Level: advanced
64690d1cd5e0SMatthew G. Knepley 
64700d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
64710d1cd5e0SMatthew G. Knepley @*/
64720d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
64730d1cd5e0SMatthew G. Knepley {
64740d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
64750d1cd5e0SMatthew G. Knepley 
64760d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
64770d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
64780d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
64790d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
64800d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
64816f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
64826f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
64830d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
6484df0b854cSToby Isaac   PetscFunctionReturn(0);
6485df0b854cSToby Isaac }
6486c4088d22SMatthew G. Knepley 
6487502a2867SDave May /*@C
6488502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
6489502a2867SDave May 
6490502a2867SDave May  Not Collective
6491502a2867SDave May 
6492502a2867SDave May  Input Parameter:
6493502a2867SDave May  . dm    - The DM
6494502a2867SDave May 
6495502a2867SDave May  Output Parameter:
6496502a2867SDave May  . nranks - the number of neighbours
6497502a2867SDave May  . ranks - the neighbors ranks
6498502a2867SDave May 
6499502a2867SDave May  Notes:
6500502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
6501502a2867SDave May 
6502502a2867SDave May  Level: beginner
6503502a2867SDave May 
6504dee935c1SDave May  .seealso: DMDAGetNeighbors(), PetscSFGetRanks()
6505502a2867SDave May @*/
6506502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
6507502a2867SDave May {
6508502a2867SDave May   PetscErrorCode ierr;
6509502a2867SDave May 
6510502a2867SDave May   PetscFunctionBegin;
6511502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
65120918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
6513502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
6514502a2867SDave May   PetscFunctionReturn(0);
6515502a2867SDave May }
6516502a2867SDave May 
6517531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
6518531c7667SBarry Smith 
6519531c7667SBarry Smith /*
6520531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
6521531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
6522531c7667SBarry Smith */
6523531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
6524531c7667SBarry Smith {
6525531c7667SBarry Smith   PetscErrorCode ierr;
6526531c7667SBarry Smith 
6527531c7667SBarry Smith   PetscFunctionBegin;
6528531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6529531c7667SBarry Smith     Vec x1local;
6530531c7667SBarry Smith     DM  dm;
6531531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6532531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
6533531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
6534531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6535531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
6536531c7667SBarry Smith     x1   = x1local;
6537531c7667SBarry Smith   }
6538531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
6539531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
6540531c7667SBarry Smith     DM  dm;
6541531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
6542531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
6543531c7667SBarry Smith   }
6544531c7667SBarry Smith   PetscFunctionReturn(0);
6545531c7667SBarry Smith }
6546531c7667SBarry Smith 
6547531c7667SBarry Smith /*@
6548531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
6549531c7667SBarry Smith 
6550531c7667SBarry Smith     Input Parameter:
6551531c7667SBarry Smith .    coloring - the MatFDColoring object
6552531c7667SBarry Smith 
6553531c7667SBarry Smith     Developer Notes: this routine exists because the PETSc Mat library does not know about the DM objects
6554531c7667SBarry Smith 
65551b266c99SBarry Smith     Level: advanced
65561b266c99SBarry Smith 
6557531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
6558531c7667SBarry Smith @*/
6559531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
6560531c7667SBarry Smith {
6561531c7667SBarry Smith   PetscFunctionBegin;
6562531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
6563531c7667SBarry Smith   PetscFunctionReturn(0);
6564531c7667SBarry Smith }
6565