xref: /petsc/src/dm/interface/dm.c (revision 5d80c0bf20dfdf3e47e6174603b3725e9d56818d)
1af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
2c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
3e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
43e922f36SToby Isaac #include <petscdmplex.h>
5f19dbd58SToby Isaac #include <petscdmfield.h>
60c312b8eSJed Brown #include <petscsf.h>
72764a2aaSMatthew G. Knepley #include <petscds.h>
847c6ae99SBarry Smith 
9732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
10d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
115a84ad33SLisandro Dalcin PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix;
1267a56275SMatthew G Knepley 
13e249ee26SToby Isaac const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_",0};
1440967b3bSMatthew G. Knepley const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_",0};
15bff4a2f0SMatthew G. Knepley 
16a4121054SBarry Smith /*@
17de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
18a4121054SBarry Smith 
19a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
20a4121054SBarry Smith    error when you try to use the vector.
21a4121054SBarry Smith 
22d083f849SBarry Smith   Collective
23a4121054SBarry Smith 
24a4121054SBarry Smith   Input Parameter:
25a4121054SBarry Smith . comm - The communicator for the DM object
26a4121054SBarry Smith 
27a4121054SBarry Smith   Output Parameter:
28a4121054SBarry Smith . dm - The DM object
29a4121054SBarry Smith 
30a4121054SBarry Smith   Level: beginner
31a4121054SBarry Smith 
328472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
33a4121054SBarry Smith @*/
347087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
35a4121054SBarry Smith {
36a4121054SBarry Smith   DM             v;
37e5e52638SMatthew G. Knepley   PetscDS        ds;
38a4121054SBarry Smith   PetscErrorCode ierr;
39a4121054SBarry Smith 
40a4121054SBarry Smith   PetscFunctionBegin;
411411c6eeSJed Brown   PetscValidPointer(dm,2);
420298fd71SBarry Smith   *dm = NULL;
43607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
44a4121054SBarry Smith 
4573107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
46e7c4fc90SDmitry Karpeev 
4749be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
4849be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
490298fd71SBarry Smith   v->ltogmap                  = NULL;
501411c6eeSJed Brown   v->bs                       = 1;
51171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5288ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
531bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
54c58f1c22SToby Isaac   v->labels                   = NULL;
5534aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
5634aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
57c58f1c22SToby Isaac   v->depthLabel               = NULL;
581bb6d2a8SBarry Smith   v->localSection             = NULL;
591bb6d2a8SBarry Smith   v->globalSection            = NULL;
60fba222abSToby Isaac   v->defaultConstraintSection = NULL;
61fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
62c6b900c6SMatthew G. Knepley   v->L                        = NULL;
63c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
645dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
659a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
6696173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
67435a35e8SMatthew G Knepley   {
68435a35e8SMatthew G Knepley     PetscInt i;
69435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
700298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
71f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
72435a35e8SMatthew G Knepley     }
73435a35e8SMatthew G Knepley   }
74e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
75b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
76e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);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);
834a7a4c06SLawrence Mitchell 
841411c6eeSJed Brown   *dm = v;
85a4121054SBarry Smith   PetscFunctionReturn(0);
86a4121054SBarry Smith }
87a4121054SBarry Smith 
8838221697SMatthew G. Knepley /*@
8938221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9038221697SMatthew G. Knepley 
91d083f849SBarry Smith   Collective
9238221697SMatthew G. Knepley 
9338221697SMatthew G. Knepley   Input Parameter:
9438221697SMatthew G. Knepley . dm - The original DM object
9538221697SMatthew G. Knepley 
9638221697SMatthew G. Knepley   Output Parameter:
9738221697SMatthew G. Knepley . newdm  - The new DM object
9838221697SMatthew G. Knepley 
9938221697SMatthew G. Knepley   Level: beginner
10038221697SMatthew G. Knepley 
1011bb6d2a8SBarry Smith   Notes: For some DM this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1021bb6d2a8SBarry Smith          DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does
1031bb6d2a8SBarry Smith          share the PetscSection of the original DM
1041bb6d2a8SBarry Smith 
1051bb6d2a8SBarry Smith .seealso: DMDestry(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1061bb6d2a8SBarry Smith 
10738221697SMatthew G. Knepley @*/
10838221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
10938221697SMatthew G. Knepley {
11038221697SMatthew G. Knepley   PetscSF        sf;
11138221697SMatthew G. Knepley   Vec            coords;
11238221697SMatthew G. Knepley   void          *ctx;
113a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
11438221697SMatthew G. Knepley   PetscErrorCode ierr;
11538221697SMatthew G. Knepley 
11638221697SMatthew G. Knepley   PetscFunctionBegin;
11738221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
11838221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
11938221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
120*5d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
121ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
122ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
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 
13892fd8e1eSJed Brown     ierr = DMGetLocalSection(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);
14383bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
14492fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
145a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
146be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley     }
148be4c1c3eSMatthew G. Knepley   }
149a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
150a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15138221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
15238221697SMatthew G. Knepley   if (coords) {
15338221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
15438221697SMatthew G. Knepley   } else {
15538221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
15638221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
15738221697SMatthew G. Knepley   }
15890b157c4SStefano Zampini   {
15990b157c4SStefano Zampini     PetscBool             isper;
160c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1615dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
16290b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
16390b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
164c6b900c6SMatthew G. Knepley   }
16534aa8a36SMatthew G. Knepley   {
16634aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
16734aa8a36SMatthew G. Knepley 
16834aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
16934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17034aa8a36SMatthew G. Knepley   }
17138221697SMatthew G. Knepley   PetscFunctionReturn(0);
17238221697SMatthew G. Knepley }
17338221697SMatthew G. Knepley 
1749a42bb27SBarry Smith /*@C
175564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1769a42bb27SBarry Smith 
177d083f849SBarry Smith    Logically Collective on da
1789a42bb27SBarry Smith 
1799a42bb27SBarry Smith    Input Parameter:
1809a42bb27SBarry Smith +  da - initial distributed array
181e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1829a42bb27SBarry Smith 
1839a42bb27SBarry Smith    Options Database:
184dd85299cSBarry Smith .   -dm_vec_type ctype
1859a42bb27SBarry Smith 
1869a42bb27SBarry Smith    Level: intermediate
1879a42bb27SBarry Smith 
188a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1899a42bb27SBarry Smith @*/
19019fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1919a42bb27SBarry Smith {
1929a42bb27SBarry Smith   PetscErrorCode ierr;
1939a42bb27SBarry Smith 
1949a42bb27SBarry Smith   PetscFunctionBegin;
1959a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
1969a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
19719fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
1989a42bb27SBarry Smith   PetscFunctionReturn(0);
1999a42bb27SBarry Smith }
2009a42bb27SBarry Smith 
201c0dedaeaSBarry Smith /*@C
202c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
203c0dedaeaSBarry Smith 
204d083f849SBarry Smith    Logically Collective on da
205c0dedaeaSBarry Smith 
206c0dedaeaSBarry Smith    Input Parameter:
207c0dedaeaSBarry Smith .  da - initial distributed array
208c0dedaeaSBarry Smith 
209c0dedaeaSBarry Smith    Output Parameter:
210c0dedaeaSBarry Smith .  ctype - the vector type
211c0dedaeaSBarry Smith 
212c0dedaeaSBarry Smith    Level: intermediate
213c0dedaeaSBarry Smith 
214a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
215c0dedaeaSBarry Smith @*/
216c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
217c0dedaeaSBarry Smith {
218c0dedaeaSBarry Smith   PetscFunctionBegin;
219c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
220c0dedaeaSBarry Smith   *ctype = da->vectype;
221c0dedaeaSBarry Smith   PetscFunctionReturn(0);
222c0dedaeaSBarry Smith }
223c0dedaeaSBarry Smith 
2245f1ad066SMatthew G Knepley /*@
22534f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2265f1ad066SMatthew G Knepley 
2275f1ad066SMatthew G Knepley   Not collective
2285f1ad066SMatthew G Knepley 
2295f1ad066SMatthew G Knepley   Input Parameter:
2305f1ad066SMatthew G Knepley . v - The Vec
2315f1ad066SMatthew G Knepley 
2325f1ad066SMatthew G Knepley   Output Parameter:
2335f1ad066SMatthew G Knepley . dm - The DM
2345f1ad066SMatthew G Knepley 
2355f1ad066SMatthew G Knepley   Level: intermediate
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2385f1ad066SMatthew G Knepley @*/
2395f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2405f1ad066SMatthew G Knepley {
2415f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   PetscFunctionBegin;
2445f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2455f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2465f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2475f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2485f1ad066SMatthew G Knepley }
2495f1ad066SMatthew G Knepley 
2505f1ad066SMatthew G Knepley /*@
251d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2525f1ad066SMatthew G Knepley 
2535f1ad066SMatthew G Knepley   Not collective
2545f1ad066SMatthew G Knepley 
2555f1ad066SMatthew G Knepley   Input Parameters:
2565f1ad066SMatthew G Knepley + v - The Vec
2575f1ad066SMatthew G Knepley - dm - The DM
2585f1ad066SMatthew G Knepley 
259d9805387SMatthew 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.
260d9805387SMatthew G. Knepley 
2615f1ad066SMatthew G Knepley   Level: intermediate
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2645f1ad066SMatthew G Knepley @*/
2655f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2665f1ad066SMatthew G Knepley {
2675f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2685f1ad066SMatthew G Knepley 
2695f1ad066SMatthew G Knepley   PetscFunctionBegin;
2705f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
271d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2725f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2735f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2745f1ad066SMatthew G Knepley }
2755f1ad066SMatthew G Knepley 
276521d9a4cSLisandro Dalcin /*@C
2778f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2788f1509bcSBarry Smith 
279d083f849SBarry Smith    Logically Collective on dm
2808f1509bcSBarry Smith 
2818f1509bcSBarry Smith    Input Parameters:
2828f1509bcSBarry Smith +  dm - the DM context
2838f1509bcSBarry Smith -  ctype - the matrix type
2848f1509bcSBarry Smith 
2858f1509bcSBarry Smith    Options Database:
2868f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2878f1509bcSBarry Smith 
2888f1509bcSBarry Smith    Level: intermediate
2898f1509bcSBarry Smith 
2908f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2918f1509bcSBarry Smith           DMGetISColoringType()
2928f1509bcSBarry Smith @*/
2938f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
2948f1509bcSBarry Smith {
2958f1509bcSBarry Smith   PetscFunctionBegin;
2968f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2978f1509bcSBarry Smith   dm->coloringtype = ctype;
2988f1509bcSBarry Smith   PetscFunctionReturn(0);
2998f1509bcSBarry Smith }
3008f1509bcSBarry Smith 
3018f1509bcSBarry Smith /*@C
3028f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
303521d9a4cSLisandro Dalcin 
304d083f849SBarry Smith    Logically Collective on dm
305521d9a4cSLisandro Dalcin 
306521d9a4cSLisandro Dalcin    Input Parameter:
3078f1509bcSBarry Smith .  dm - the DM context
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith    Output Parameter:
3108f1509bcSBarry Smith .  ctype - the matrix type
3118f1509bcSBarry Smith 
3128f1509bcSBarry Smith    Options Database:
3138f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3148f1509bcSBarry Smith 
3158f1509bcSBarry Smith    Level: intermediate
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3188f1509bcSBarry Smith           DMGetISColoringType()
3198f1509bcSBarry Smith @*/
3208f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3218f1509bcSBarry Smith {
3228f1509bcSBarry Smith   PetscFunctionBegin;
3238f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3248f1509bcSBarry Smith   *ctype = dm->coloringtype;
3258f1509bcSBarry Smith   PetscFunctionReturn(0);
3268f1509bcSBarry Smith }
3278f1509bcSBarry Smith 
3288f1509bcSBarry Smith /*@C
3298f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3308f1509bcSBarry Smith 
331d083f849SBarry Smith    Logically Collective on dm
3328f1509bcSBarry Smith 
3338f1509bcSBarry Smith    Input Parameters:
334521d9a4cSLisandro Dalcin +  dm - the DM context
335a2b5a043SBarry Smith -  ctype - the matrix type
336521d9a4cSLisandro Dalcin 
337521d9a4cSLisandro Dalcin    Options Database:
338521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
339521d9a4cSLisandro Dalcin 
340521d9a4cSLisandro Dalcin    Level: intermediate
341521d9a4cSLisandro Dalcin 
342a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
343521d9a4cSLisandro Dalcin @*/
34419fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
345521d9a4cSLisandro Dalcin {
346521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
34788f0584fSBarry Smith 
348521d9a4cSLisandro Dalcin   PetscFunctionBegin;
349521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
350521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35119fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
352521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
353521d9a4cSLisandro Dalcin }
354521d9a4cSLisandro Dalcin 
355c0dedaeaSBarry Smith /*@C
356c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
357c0dedaeaSBarry Smith 
358d083f849SBarry Smith    Logically Collective on dm
359c0dedaeaSBarry Smith 
360c0dedaeaSBarry Smith    Input Parameter:
361c0dedaeaSBarry Smith .  dm - the DM context
362c0dedaeaSBarry Smith 
363c0dedaeaSBarry Smith    Output Parameter:
364c0dedaeaSBarry Smith .  ctype - the matrix type
365c0dedaeaSBarry Smith 
366c0dedaeaSBarry Smith    Options Database:
367c0dedaeaSBarry Smith .   -dm_mat_type ctype
368c0dedaeaSBarry Smith 
369c0dedaeaSBarry Smith    Level: intermediate
370c0dedaeaSBarry Smith 
371a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
372c0dedaeaSBarry Smith @*/
373c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
374c0dedaeaSBarry Smith {
375c0dedaeaSBarry Smith   PetscFunctionBegin;
376c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
377c0dedaeaSBarry Smith   *ctype = dm->mattype;
378c0dedaeaSBarry Smith   PetscFunctionReturn(0);
379c0dedaeaSBarry Smith }
380c0dedaeaSBarry Smith 
381c688c046SMatthew G Knepley /*@
38234f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
383c688c046SMatthew G Knepley 
384c688c046SMatthew G Knepley   Not collective
385c688c046SMatthew G Knepley 
386c688c046SMatthew G Knepley   Input Parameter:
387c688c046SMatthew G Knepley . A - The Mat
388c688c046SMatthew G Knepley 
389c688c046SMatthew G Knepley   Output Parameter:
390c688c046SMatthew G Knepley . dm - The DM
391c688c046SMatthew G Knepley 
392c688c046SMatthew G Knepley   Level: intermediate
393c688c046SMatthew G Knepley 
3948f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
3958f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
3968f1509bcSBarry Smith 
397c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
398c688c046SMatthew G Knepley @*/
399c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
400c688c046SMatthew G Knepley {
401c688c046SMatthew G Knepley   PetscErrorCode ierr;
402c688c046SMatthew G Knepley 
403c688c046SMatthew G Knepley   PetscFunctionBegin;
404c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
405c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
406c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
407c688c046SMatthew G Knepley   PetscFunctionReturn(0);
408c688c046SMatthew G Knepley }
409c688c046SMatthew G Knepley 
410c688c046SMatthew G Knepley /*@
411c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
412c688c046SMatthew G Knepley 
413c688c046SMatthew G Knepley   Not collective
414c688c046SMatthew G Knepley 
415c688c046SMatthew G Knepley   Input Parameters:
416c688c046SMatthew G Knepley + A - The Mat
417c688c046SMatthew G Knepley - dm - The DM
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley   Level: intermediate
420c688c046SMatthew G Knepley 
4218f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4228f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4238f1509bcSBarry Smith 
4248f1509bcSBarry Smith 
425c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
426c688c046SMatthew G Knepley @*/
427c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
428c688c046SMatthew G Knepley {
429c688c046SMatthew G Knepley   PetscErrorCode ierr;
430c688c046SMatthew G Knepley 
431c688c046SMatthew G Knepley   PetscFunctionBegin;
432c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4338865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
434c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
435c688c046SMatthew G Knepley   PetscFunctionReturn(0);
436c688c046SMatthew G Knepley }
437c688c046SMatthew G Knepley 
4389a42bb27SBarry Smith /*@C
4399a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4406757b960SDave May    DM options in the database.
4419a42bb27SBarry Smith 
442d083f849SBarry Smith    Logically Collective on dm
4439a42bb27SBarry Smith 
4449a42bb27SBarry Smith    Input Parameter:
4458353ddbbSDave May +  da - the DM context
4469a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4479a42bb27SBarry Smith 
4489a42bb27SBarry Smith    Notes:
4499a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4509a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith    Level: advanced
4539a42bb27SBarry Smith 
4549a42bb27SBarry Smith .seealso: DMSetFromOptions()
4559a42bb27SBarry Smith @*/
4567087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4579a42bb27SBarry Smith {
4589a42bb27SBarry Smith   PetscErrorCode ierr;
4599a42bb27SBarry Smith 
4609a42bb27SBarry Smith   PetscFunctionBegin;
4619a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4629a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
463691be533SLawrence Mitchell   if (dm->sf) {
464691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
465691be533SLawrence Mitchell   }
4661bb6d2a8SBarry Smith   if (dm->sectionSF) {
4671bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
468691be533SLawrence Mitchell   }
4699a42bb27SBarry Smith   PetscFunctionReturn(0);
4709a42bb27SBarry Smith }
4719a42bb27SBarry Smith 
47231697293SDave May /*@C
47331697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
47431697293SDave May    DM options in the database.
47531697293SDave May 
476d083f849SBarry Smith    Logically Collective on dm
47731697293SDave May 
47831697293SDave May    Input Parameters:
47931697293SDave May +  dm - the DM context
48031697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48131697293SDave May 
48231697293SDave May    Notes:
48331697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
48431697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
48531697293SDave May 
48631697293SDave May    Level: advanced
48731697293SDave May 
48831697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
48931697293SDave May @*/
49031697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49131697293SDave May {
49231697293SDave May   PetscErrorCode ierr;
49331697293SDave May 
49431697293SDave May   PetscFunctionBegin;
49531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
49631697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
49731697293SDave May   PetscFunctionReturn(0);
49831697293SDave May }
49931697293SDave May 
50031697293SDave May /*@C
50131697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
50231697293SDave May    DM options in the database.
50331697293SDave May 
50431697293SDave May    Not Collective
50531697293SDave May 
50631697293SDave May    Input Parameters:
50731697293SDave May .  dm - the DM context
50831697293SDave May 
50931697293SDave May    Output Parameters:
51031697293SDave May .  prefix - pointer to the prefix string used is returned
51131697293SDave May 
51295452b02SPatrick Sanan    Notes:
51395452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
51431697293SDave May    sufficient length to hold the prefix.
51531697293SDave May 
51631697293SDave May    Level: advanced
51731697293SDave May 
51831697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
51931697293SDave May @*/
52031697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52131697293SDave May {
52231697293SDave May   PetscErrorCode ierr;
52331697293SDave May 
52431697293SDave May   PetscFunctionBegin;
52531697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
52631697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
52731697293SDave May   PetscFunctionReturn(0);
52831697293SDave May }
52931697293SDave May 
53088bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53188bdff64SToby Isaac {
53288bdff64SToby Isaac   PetscInt i, refct = ((PetscObject) dm)->refct;
53388bdff64SToby Isaac   DMNamedVecLink nlink;
53488bdff64SToby Isaac   PetscErrorCode ierr;
53588bdff64SToby Isaac 
53688bdff64SToby Isaac   PetscFunctionBegin;
537aab5bcd8SJed Brown   *ncrefct = 0;
53888bdff64SToby Isaac   /* count all the circular references of DM and its contained Vecs */
53988bdff64SToby Isaac   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
54088bdff64SToby Isaac     if (dm->localin[i])  refct--;
54188bdff64SToby Isaac     if (dm->globalin[i]) refct--;
54288bdff64SToby Isaac   }
54388bdff64SToby Isaac   for (nlink=dm->namedglobal; nlink; nlink=nlink->next) refct--;
54488bdff64SToby Isaac   for (nlink=dm->namedlocal; nlink; nlink=nlink->next) refct--;
54588bdff64SToby Isaac   if (dm->x) {
54688bdff64SToby Isaac     DM obj;
54788bdff64SToby Isaac     ierr = VecGetDM(dm->x, &obj);CHKERRQ(ierr);
54888bdff64SToby Isaac     if (obj == dm) refct--;
54988bdff64SToby Isaac   }
55088bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
55188bdff64SToby Isaac     refct--;
55288bdff64SToby Isaac     if (recurseCoarse) {
55388bdff64SToby Isaac       PetscInt coarseCount;
55488bdff64SToby Isaac 
55588bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55688bdff64SToby Isaac       refct += coarseCount;
55788bdff64SToby Isaac     }
55888bdff64SToby Isaac   }
55988bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
56088bdff64SToby Isaac     refct--;
56188bdff64SToby Isaac     if (recurseFine) {
56288bdff64SToby Isaac       PetscInt fineCount;
56388bdff64SToby Isaac 
56488bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56588bdff64SToby Isaac       refct += fineCount;
56688bdff64SToby Isaac     }
56788bdff64SToby Isaac   }
56888bdff64SToby Isaac   *ncrefct = refct;
56988bdff64SToby Isaac   PetscFunctionReturn(0);
57088bdff64SToby Isaac }
57188bdff64SToby Isaac 
572f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
573354557abSToby Isaac {
574*5d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
575354557abSToby Isaac   PetscErrorCode ierr;
576354557abSToby Isaac 
577354557abSToby Isaac   PetscFunctionBegin;
578354557abSToby Isaac   /* destroy the labels */
579354557abSToby Isaac   while (next) {
580354557abSToby Isaac     DMLabelLink tmp = next->next;
581354557abSToby Isaac 
582*5d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel) dm->depthLabel = NULL;
583354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
584354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
585354557abSToby Isaac     next = tmp;
586354557abSToby Isaac   }
587*5d80c0bfSVaclav Hapla   dm->labels = NULL;
588354557abSToby Isaac   PetscFunctionReturn(0);
589354557abSToby Isaac }
590354557abSToby Isaac 
59147c6ae99SBarry Smith /*@
5928472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59347c6ae99SBarry Smith 
594d083f849SBarry Smith     Collective on dm
59547c6ae99SBarry Smith 
59647c6ae99SBarry Smith     Input Parameter:
59747c6ae99SBarry Smith .   dm - the DM object to destroy
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith     Level: developer
60047c6ae99SBarry Smith 
601e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
60247c6ae99SBarry Smith 
60347c6ae99SBarry Smith @*/
604fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60547c6ae99SBarry Smith {
60688bdff64SToby Isaac   PetscInt       i, cnt;
607dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60847c6ae99SBarry Smith   PetscErrorCode ierr;
60947c6ae99SBarry Smith 
61047c6ae99SBarry Smith   PetscFunctionBegin;
6116bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6126bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61387e657c6SBarry Smith 
61488bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61588bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61688bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
61788bdff64SToby Isaac   if (--cnt > 0) {*dm = 0; PetscFunctionReturn(0);}
618732e2eb9SMatthew G Knepley   /*
619732e2eb9SMatthew G Knepley      Need this test because the dm references the vectors that
620732e2eb9SMatthew G Knepley      reference the dm, so destroying the dm calls destroy on the
621732e2eb9SMatthew G Knepley      vectors that cause another destroy on the dm
622732e2eb9SMatthew G Knepley   */
6236bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6246bf464f9SBarry Smith   ((PetscObject) (*dm))->refct = 0;
625732e2eb9SMatthew G Knepley   for (i=0; i<DM_MAX_WORK_VECTORS; i++) {
6266bf464f9SBarry Smith     if ((*dm)->localout[i]) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Destroying a DM that has a local vector obtained with DMGetLocalVector()");
6276bf464f9SBarry Smith     ierr = VecDestroy(&(*dm)->localin[i]);CHKERRQ(ierr);
628732e2eb9SMatthew G Knepley   }
629f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6300298fd71SBarry Smith   (*dm)->namedglobal = NULL;
631f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6322348bcf4SPeter Brune     nnext = nlink->next;
6332348bcf4SPeter 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);
6342348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6352348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6362348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6372348bcf4SPeter Brune   }
638f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6390298fd71SBarry Smith   (*dm)->namedlocal = NULL;
640f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
641f490541aSPeter Brune     nnext = nlink->next;
642f490541aSPeter 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);
643f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
644f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
645f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
646f490541aSPeter Brune   }
6472348bcf4SPeter Brune 
648b17ce1afSJed Brown   /* Destroy the list of hooks */
649c833c3b5SJed Brown   {
650c833c3b5SJed Brown     DMCoarsenHookLink link,next;
651b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
652b17ce1afSJed Brown       next = link->next;
653b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
654b17ce1afSJed Brown     }
6550298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
656c833c3b5SJed Brown   }
657c833c3b5SJed Brown   {
658c833c3b5SJed Brown     DMRefineHookLink link,next;
659c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
660c833c3b5SJed Brown       next = link->next;
661c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
662c833c3b5SJed Brown     }
6630298fd71SBarry Smith     (*dm)->refinehook = NULL;
664c833c3b5SJed Brown   }
665be081cd6SPeter Brune   {
666be081cd6SPeter Brune     DMSubDomainHookLink link,next;
667be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
668be081cd6SPeter Brune       next = link->next;
669be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
670be081cd6SPeter Brune     }
6710298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
672be081cd6SPeter Brune   }
673baf369e7SPeter Brune   {
674baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
675baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
676baf369e7SPeter Brune       next = link->next;
677baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
678baf369e7SPeter Brune     }
6790298fd71SBarry Smith     (*dm)->gtolhook = NULL;
680baf369e7SPeter Brune   }
681d4d07f1eSToby Isaac   {
682d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
683d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
684d4d07f1eSToby Isaac       next = link->next;
685d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
686d4d07f1eSToby Isaac     }
687d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
688d4d07f1eSToby Isaac   }
689aa1993deSMatthew G Knepley   /* Destroy the work arrays */
690aa1993deSMatthew G Knepley   {
691aa1993deSMatthew G Knepley     DMWorkLink link,next;
692aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
693aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
694aa1993deSMatthew G Knepley       next = link->next;
695aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
696aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
697aa1993deSMatthew G Knepley     }
6980298fd71SBarry Smith     (*dm)->workin = NULL;
699aa1993deSMatthew G Knepley   }
700c58f1c22SToby Isaac   /* destroy the labels */
701f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
702f4cdcedcSVaclav Hapla   /* destroy the fields */
703e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
704f4cdcedcSVaclav Hapla   /* destroy the boundaries */
705e6f8dbb6SToby Isaac   {
706e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
707e6f8dbb6SToby Isaac     while (next) {
708e6f8dbb6SToby Isaac       DMBoundary b = next;
709e6f8dbb6SToby Isaac 
710e6f8dbb6SToby Isaac       next = b->next;
711e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
712e6f8dbb6SToby Isaac     }
713e6f8dbb6SToby Isaac   }
714b17ce1afSJed Brown 
71552536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
71652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
71752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
71852536dc3SBarry Smith 
7191a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7201a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7211a266240SBarry Smith   }
72287e657c6SBarry Smith   ierr = VecDestroy(&(*dm)->x);CHKERRQ(ierr);
72371cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7244dcab191SBarry Smith   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
7256bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7266bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
727073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
72888ed4aceSMatthew G Knepley 
7291bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7301bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7318b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
732fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
733fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
73488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7351bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
736736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
737736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7388e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
739736995cdSBlaise Bourdin     }
740736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
741736995cdSBlaise Bourdin   }
74288bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
74388bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
74488bdff64SToby Isaac   }
745a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
74688bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
74788bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
74888bdff64SToby Isaac   }
74988bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
750f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7516636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7526636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7536636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
7545dc8c3f7SMatthew G. Knepley   ierr = PetscFree3((*dm)->L,(*dm)->maxCell,(*dm)->bdtype);CHKERRQ(ierr);
755ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
756ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
757ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7586636e97aSMatthew G Knepley 
759e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
76014f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
761e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
762e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
763732e2eb9SMatthew G Knepley 
764ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7656bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
766ed3c66a1SDave May   }
767435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
768732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
76947c6ae99SBarry Smith   PetscFunctionReturn(0);
77047c6ae99SBarry Smith }
77147c6ae99SBarry Smith 
772d7bf68aeSBarry Smith /*@
773d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
774d7bf68aeSBarry Smith 
775d083f849SBarry Smith     Collective on dm
776d7bf68aeSBarry Smith 
777d7bf68aeSBarry Smith     Input Parameter:
778d7bf68aeSBarry Smith .   dm - the DM object to setup
779d7bf68aeSBarry Smith 
780d7bf68aeSBarry Smith     Level: developer
781d7bf68aeSBarry Smith 
782e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
783d7bf68aeSBarry Smith 
784d7bf68aeSBarry Smith @*/
7857087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
786d7bf68aeSBarry Smith {
787d7bf68aeSBarry Smith   PetscErrorCode ierr;
788d7bf68aeSBarry Smith 
789d7bf68aeSBarry Smith   PetscFunctionBegin;
790171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7918387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
792d7bf68aeSBarry Smith   if (dm->ops->setup) {
793d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
794d7bf68aeSBarry Smith   }
7958387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
796d7bf68aeSBarry Smith   PetscFunctionReturn(0);
797d7bf68aeSBarry Smith }
798d7bf68aeSBarry Smith 
799d7bf68aeSBarry Smith /*@
800d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
801d7bf68aeSBarry Smith 
802d083f849SBarry Smith     Collective on dm
803d7bf68aeSBarry Smith 
804d7bf68aeSBarry Smith     Input Parameter:
805d7bf68aeSBarry Smith .   dm - the DM object to set options for
806d7bf68aeSBarry Smith 
807732e2eb9SMatthew G Knepley     Options Database:
8084164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8094164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8104164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8118f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
812732e2eb9SMatthew G Knepley 
813d7bf68aeSBarry Smith     Level: developer
814d7bf68aeSBarry Smith 
815e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
816d7bf68aeSBarry Smith 
817d7bf68aeSBarry Smith @*/
8187087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
819d7bf68aeSBarry Smith {
8207781c08eSBarry Smith   char           typeName[256];
821ca266f36SBarry Smith   PetscBool      flg;
822d7bf68aeSBarry Smith   PetscErrorCode ierr;
823d7bf68aeSBarry Smith 
824d7bf68aeSBarry Smith   PetscFunctionBegin;
825171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82649be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
82749be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8281bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8293194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8300298fd71SBarry 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);
831a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
832f9ba7244SBarry Smith   if (flg) {
833f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
834f9ba7244SBarry Smith   }
835a264d7a6SBarry 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);
836073dac72SJed Brown   if (flg) {
837521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
838073dac72SJed Brown   }
8398f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
840f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
841e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
842f9ba7244SBarry Smith   }
843f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8440633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84582fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
846d7bf68aeSBarry Smith   PetscFunctionReturn(0);
847d7bf68aeSBarry Smith }
848d7bf68aeSBarry Smith 
849fc9bc008SSatish Balay /*@C
850224748a4SBarry Smith     DMView - Views a DM
85147c6ae99SBarry Smith 
852d083f849SBarry Smith     Collective on dm
85347c6ae99SBarry Smith 
85447c6ae99SBarry Smith     Input Parameter:
85547c6ae99SBarry Smith +   dm - the DM object to view
85647c6ae99SBarry Smith -   v - the viewer
85747c6ae99SBarry Smith 
858224748a4SBarry Smith     Level: beginner
85947c6ae99SBarry Smith 
860e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
86147c6ae99SBarry Smith 
86247c6ae99SBarry Smith @*/
8637087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
86447c6ae99SBarry Smith {
86547c6ae99SBarry Smith   PetscErrorCode    ierr;
86632c0f0efSBarry Smith   PetscBool         isbinary;
86776a8abe0SBarry Smith   PetscMPIInt       size;
86876a8abe0SBarry Smith   PetscViewerFormat format;
86947c6ae99SBarry Smith 
87047c6ae99SBarry Smith   PetscFunctionBegin;
871171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8723014e516SBarry Smith   if (!v) {
873ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8743014e516SBarry Smith   }
875b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
87674903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
87774903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
87874903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
87974903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
88074903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
88174903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
88274903a4fSStefano Zampini      in an error here */
88374903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
8848bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
885b1b135c8SBarry Smith 
88676a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
88776a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
88876a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
88998c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
89032c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
89132c0f0efSBarry Smith   if (isbinary) {
89255849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
89332c0f0efSBarry Smith     char     type[256];
89432c0f0efSBarry Smith 
89532c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
89632c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
89732c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
89832c0f0efSBarry Smith   }
8990c010503SBarry Smith   if (dm->ops->view) {
9000c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
90147c6ae99SBarry Smith   }
90247c6ae99SBarry Smith   PetscFunctionReturn(0);
90347c6ae99SBarry Smith }
90447c6ae99SBarry Smith 
90547c6ae99SBarry Smith /*@
9068472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
90747c6ae99SBarry Smith 
908d083f849SBarry Smith     Collective on dm
90947c6ae99SBarry Smith 
91047c6ae99SBarry Smith     Input Parameter:
91147c6ae99SBarry Smith .   dm - the DM object
91247c6ae99SBarry Smith 
91347c6ae99SBarry Smith     Output Parameter:
91447c6ae99SBarry Smith .   vec - the global vector
91547c6ae99SBarry Smith 
916073dac72SJed Brown     Level: beginner
91747c6ae99SBarry Smith 
918e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
91947c6ae99SBarry Smith 
92047c6ae99SBarry Smith @*/
9217087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
92247c6ae99SBarry Smith {
92347c6ae99SBarry Smith   PetscErrorCode ierr;
92447c6ae99SBarry Smith 
92547c6ae99SBarry Smith   PetscFunctionBegin;
926171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
927b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
928b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
92947c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
93047c6ae99SBarry Smith   PetscFunctionReturn(0);
93147c6ae99SBarry Smith }
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith /*@
9348472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith     Not Collective
93747c6ae99SBarry Smith 
93847c6ae99SBarry Smith     Input Parameter:
93947c6ae99SBarry Smith .   dm - the DM object
94047c6ae99SBarry Smith 
94147c6ae99SBarry Smith     Output Parameter:
94247c6ae99SBarry Smith .   vec - the local vector
94347c6ae99SBarry Smith 
944073dac72SJed Brown     Level: beginner
94547c6ae99SBarry Smith 
946e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith @*/
9497087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
95047c6ae99SBarry Smith {
95147c6ae99SBarry Smith   PetscErrorCode ierr;
95247c6ae99SBarry Smith 
95347c6ae99SBarry Smith   PetscFunctionBegin;
954171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
955b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
956b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
95747c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
95847c6ae99SBarry Smith   PetscFunctionReturn(0);
95947c6ae99SBarry Smith }
96047c6ae99SBarry Smith 
9611411c6eeSJed Brown /*@
9621411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9631411c6eeSJed Brown 
964d083f849SBarry Smith    Collective on dm
9651411c6eeSJed Brown 
9661411c6eeSJed Brown    Input Parameter:
9671411c6eeSJed Brown .  dm - the DM that provides the mapping
9681411c6eeSJed Brown 
9691411c6eeSJed Brown    Output Parameter:
9701411c6eeSJed Brown .  ltog - the mapping
9711411c6eeSJed Brown 
9721411c6eeSJed Brown    Level: intermediate
9731411c6eeSJed Brown 
9741411c6eeSJed Brown    Notes:
9751411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9761411c6eeSJed Brown    MatSetLocalToGlobalMapping().
9771411c6eeSJed Brown 
978fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
9791411c6eeSJed Brown @*/
9807087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
9811411c6eeSJed Brown {
9820be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
9831411c6eeSJed Brown   PetscErrorCode ierr;
9841411c6eeSJed Brown 
9851411c6eeSJed Brown   PetscFunctionBegin;
9861411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
9871411c6eeSJed Brown   PetscValidPointer(ltog,2);
9881411c6eeSJed Brown   if (!dm->ltogmap) {
98937d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
99037d0c07bSMatthew G Knepley 
99192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
99237d0c07bSMatthew G Knepley     if (section) {
993a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
99437d0c07bSMatthew G Knepley       PetscInt       *ltog;
995ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
99637d0c07bSMatthew G Knepley 
997e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
99837d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
999ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1000ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
100137d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1002a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
100337d0c07bSMatthew G Knepley 
100437d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
100537d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1006a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1007a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
100837d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10091a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10101a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10111a7dc684SMatthew G. Knepley         if (dof) {
1012b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1013b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10141a7dc684SMatthew G. Knepley         }
101537d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1016a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1017a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
101837d0c07bSMatthew G Knepley         }
101937d0c07bSMatthew G Knepley       }
1020bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10210be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10220be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10230be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10240be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10257591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10267591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1027ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1028ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1029ccf3bd66SMatthew G. Knepley         n /= bs;
1030ccf3bd66SMatthew G. Knepley       }
1031ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10323bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
103337d0c07bSMatthew G Knepley     } else {
1034b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1035184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10361411c6eeSJed Brown     }
103737d0c07bSMatthew G Knepley   }
10381411c6eeSJed Brown   *ltog = dm->ltogmap;
10391411c6eeSJed Brown   PetscFunctionReturn(0);
10401411c6eeSJed Brown }
10411411c6eeSJed Brown 
10421411c6eeSJed Brown /*@
10431411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10441411c6eeSJed Brown 
10451411c6eeSJed Brown    Not Collective
10461411c6eeSJed Brown 
10471411c6eeSJed Brown    Input Parameter:
10481411c6eeSJed Brown .  dm - the DM with block structure
10491411c6eeSJed Brown 
10501411c6eeSJed Brown    Output Parameter:
10511411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10521411c6eeSJed Brown 
10531411c6eeSJed Brown    Level: intermediate
10541411c6eeSJed Brown 
1055fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10561411c6eeSJed Brown @*/
10577087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10581411c6eeSJed Brown {
10591411c6eeSJed Brown   PetscFunctionBegin;
10601411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1061534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10621411c6eeSJed Brown   if (dm->bs < 1) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"DM does not have enough information to provide a block size yet");
10631411c6eeSJed Brown   *bs = dm->bs;
10641411c6eeSJed Brown   PetscFunctionReturn(0);
10651411c6eeSJed Brown }
10661411c6eeSJed Brown 
106747c6ae99SBarry Smith /*@
10688472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
106947c6ae99SBarry Smith 
1070d083f849SBarry Smith     Collective on dm1
107147c6ae99SBarry Smith 
107247c6ae99SBarry Smith     Input Parameter:
107347c6ae99SBarry Smith +   dm1 - the DM object
107447c6ae99SBarry Smith -   dm2 - the second, finer DM object
107547c6ae99SBarry Smith 
107647c6ae99SBarry Smith     Output Parameter:
107747c6ae99SBarry Smith +  mat - the interpolation
107847c6ae99SBarry Smith -  vec - the scaling (optional)
107947c6ae99SBarry Smith 
108047c6ae99SBarry Smith     Level: developer
108147c6ae99SBarry Smith 
108295452b02SPatrick Sanan     Notes:
108395452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
108485afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1085d52bd9f3SBarry Smith 
10861f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1087d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
108885afcc9aSBarry Smith 
108985afcc9aSBarry Smith 
10903ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
109147c6ae99SBarry Smith 
109247c6ae99SBarry Smith @*/
1093e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
109447c6ae99SBarry Smith {
109547c6ae99SBarry Smith   PetscErrorCode ierr;
109647c6ae99SBarry Smith 
109747c6ae99SBarry Smith   PetscFunctionBegin;
1098171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1099171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1100c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1101b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dm1)->type_name);
1102cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110325296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1104cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
110547c6ae99SBarry Smith   PetscFunctionReturn(0);
110647c6ae99SBarry Smith }
110747c6ae99SBarry Smith 
11083ad4599aSBarry Smith /*@
11093ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11103ad4599aSBarry Smith 
1111d083f849SBarry Smith     Collective on dm1
11123ad4599aSBarry Smith 
11133ad4599aSBarry Smith     Input Parameter:
11143ad4599aSBarry Smith +   dm1 - the DM object
11153ad4599aSBarry Smith -   dm2 - the second, finer DM object
11163ad4599aSBarry Smith 
11173ad4599aSBarry Smith     Output Parameter:
11183ad4599aSBarry Smith .  mat - the restriction
11193ad4599aSBarry Smith 
11203ad4599aSBarry Smith 
11213ad4599aSBarry Smith     Level: developer
11223ad4599aSBarry Smith 
112395452b02SPatrick Sanan     Notes:
112495452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
11253ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11263ad4599aSBarry Smith 
11273ad4599aSBarry Smith 
11283ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11293ad4599aSBarry Smith 
11303ad4599aSBarry Smith @*/
11313ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11323ad4599aSBarry Smith {
11333ad4599aSBarry Smith   PetscErrorCode ierr;
11343ad4599aSBarry Smith 
11353ad4599aSBarry Smith   PetscFunctionBegin;
11363ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11373ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11385a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1139b9d85ea2SLisandro Dalcin   if (!dm1->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dm1)->type_name);
11403ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11413ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11423ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11433ad4599aSBarry Smith   PetscFunctionReturn(0);
11443ad4599aSBarry Smith }
11453ad4599aSBarry Smith 
114647c6ae99SBarry Smith /*@
11478472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
114847c6ae99SBarry Smith 
1149d083f849SBarry Smith     Collective on dm1
115047c6ae99SBarry Smith 
115147c6ae99SBarry Smith     Input Parameter:
115247c6ae99SBarry Smith +   dm1 - the DM object
115347c6ae99SBarry Smith -   dm2 - the second, finer DM object
115447c6ae99SBarry Smith 
115547c6ae99SBarry Smith     Output Parameter:
11566dbf9973SLawrence Mitchell .   mat - the injection
115747c6ae99SBarry Smith 
115847c6ae99SBarry Smith     Level: developer
115947c6ae99SBarry Smith 
116095452b02SPatrick Sanan    Notes:
116195452b02SPatrick Sanan     For DMDA objects this only works for "uniform refinement", that is the refined mesh was obtained DMRefine() or the coarse mesh was obtained by
116285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
116385afcc9aSBarry Smith 
1164e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
116547c6ae99SBarry Smith 
116647c6ae99SBarry Smith @*/
11676dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
116847c6ae99SBarry Smith {
116947c6ae99SBarry Smith   PetscErrorCode ierr;
117047c6ae99SBarry Smith 
117147c6ae99SBarry Smith   PetscFunctionBegin;
1172171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1173171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11745a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1175b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dm1)->type_name);
11765a84ad33SLisandro Dalcin   ierr = PetscLogEventBegin(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
11775a84ad33SLisandro Dalcin   ierr = (*dm1->ops->createinjection)(dm1,dm2,mat);CHKERRQ(ierr);
11785a84ad33SLisandro Dalcin   ierr = PetscLogEventEnd(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
117947c6ae99SBarry Smith   PetscFunctionReturn(0);
118047c6ae99SBarry Smith }
118147c6ae99SBarry Smith 
1182b412c318SBarry Smith /*@
1183bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1184bd041c0cSMatthew G. Knepley 
1185d083f849SBarry Smith   Collective on dm1
1186bd041c0cSMatthew G. Knepley 
1187bd041c0cSMatthew G. Knepley   Input Parameter:
1188bd041c0cSMatthew G. Knepley + dm1 - the DM object
1189bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1190bd041c0cSMatthew G. Knepley 
1191bd041c0cSMatthew G. Knepley   Output Parameter:
1192bd041c0cSMatthew G. Knepley . mat - the interpolation
1193bd041c0cSMatthew G. Knepley 
1194bd041c0cSMatthew G. Knepley   Level: developer
1195bd041c0cSMatthew G. Knepley 
1196bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1197bd041c0cSMatthew G. Knepley @*/
1198bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1199bd041c0cSMatthew G. Knepley {
1200bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1201bd041c0cSMatthew G. Knepley 
1202bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1203bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1204bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
12055a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1206b9d85ea2SLisandro Dalcin   if (!dm1->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dm1)->type_name);
1207bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1208bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1209bd041c0cSMatthew G. Knepley }
1210bd041c0cSMatthew G. Knepley 
1211bd041c0cSMatthew G. Knepley /*@
1212b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
121347c6ae99SBarry Smith 
1214d083f849SBarry Smith     Collective on dm
121547c6ae99SBarry Smith 
121647c6ae99SBarry Smith     Input Parameter:
121747c6ae99SBarry Smith +   dm - the DM object
12185bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
121947c6ae99SBarry Smith 
122047c6ae99SBarry Smith     Output Parameter:
122147c6ae99SBarry Smith .   coloring - the coloring
122247c6ae99SBarry Smith 
122347c6ae99SBarry Smith     Level: developer
122447c6ae99SBarry Smith 
1225b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
122647c6ae99SBarry Smith 
1227aab9d709SJed Brown @*/
1228b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
122947c6ae99SBarry Smith {
123047c6ae99SBarry Smith   PetscErrorCode ierr;
123147c6ae99SBarry Smith 
123247c6ae99SBarry Smith   PetscFunctionBegin;
1233171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12345a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1235b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1236b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
123747c6ae99SBarry Smith   PetscFunctionReturn(0);
123847c6ae99SBarry Smith }
123947c6ae99SBarry Smith 
1240b412c318SBarry Smith /*@
12418472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
124247c6ae99SBarry Smith 
1243d083f849SBarry Smith     Collective on dm
124447c6ae99SBarry Smith 
124547c6ae99SBarry Smith     Input Parameter:
1246b412c318SBarry Smith .   dm - the DM object
124747c6ae99SBarry Smith 
124847c6ae99SBarry Smith     Output Parameter:
124947c6ae99SBarry Smith .   mat - the empty Jacobian
125047c6ae99SBarry Smith 
1251073dac72SJed Brown     Level: beginner
125247c6ae99SBarry Smith 
125395452b02SPatrick Sanan     Notes:
125495452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
125594013140SBarry Smith        do not need to do it yourself.
125694013140SBarry Smith 
125794013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12587889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
125994013140SBarry Smith 
126094013140SBarry 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
126194013140SBarry Smith        internally by PETSc.
126294013140SBarry Smith 
126394013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1264aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
126594013140SBarry Smith 
1266b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
126747c6ae99SBarry Smith 
1268aab9d709SJed Brown @*/
1269b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
127047c6ae99SBarry Smith {
127147c6ae99SBarry Smith   PetscErrorCode ierr;
127247c6ae99SBarry Smith 
127347c6ae99SBarry Smith   PetscFunctionBegin;
1274171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1275c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1276b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
12775a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1278fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1279b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1280e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1281e5e52638SMatthew G. Knepley   if (dm->Nf) {
1282e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1283e571a35bSMatthew G. Knepley     PetscInt     Nf;
1284e571a35bSMatthew G. Knepley 
1285e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1286e571a35bSMatthew G. Knepley     if (Nf == 1) {
1287e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1288e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1289e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1290e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1291e571a35bSMatthew G. Knepley       }
1292e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1293e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1294e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1295e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1296e571a35bSMatthew G. Knepley       }
1297e571a35bSMatthew G. Knepley     }
1298e571a35bSMatthew G. Knepley   }
1299fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
130047c6ae99SBarry Smith   PetscFunctionReturn(0);
130147c6ae99SBarry Smith }
130247c6ae99SBarry Smith 
1303732e2eb9SMatthew G Knepley /*@
1304950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1305732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1306732e2eb9SMatthew G Knepley 
1307d083f849SBarry Smith   Logically Collective on dm
1308732e2eb9SMatthew G Knepley 
1309732e2eb9SMatthew G Knepley   Input Parameter:
1310732e2eb9SMatthew G Knepley + dm - the DM
1311732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1312732e2eb9SMatthew G Knepley 
1313732e2eb9SMatthew G Knepley   Level: developer
1314b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1315732e2eb9SMatthew G Knepley @*/
1316732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1317732e2eb9SMatthew G Knepley {
1318732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1319732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1320732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1321732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1322732e2eb9SMatthew G Knepley }
1323732e2eb9SMatthew G Knepley 
1324b06ff27eSHong Zhang /*@
1325b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1326b06ff27eSHong Zhang     but the array for values will not be allocated.
1327b06ff27eSHong Zhang 
1328d083f849SBarry Smith   Logically Collective on dm
1329b06ff27eSHong Zhang 
1330b06ff27eSHong Zhang   Input Parameter:
1331b06ff27eSHong Zhang + dm - the DM
1332b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1333b06ff27eSHong Zhang 
1334b06ff27eSHong Zhang   Level: developer
1335b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1336b06ff27eSHong Zhang @*/
1337b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1338b06ff27eSHong Zhang {
1339b06ff27eSHong Zhang   PetscFunctionBegin;
1340b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1341b06ff27eSHong Zhang   dm->structure_only = only;
1342b06ff27eSHong Zhang   PetscFunctionReturn(0);
1343b06ff27eSHong Zhang }
1344b06ff27eSHong Zhang 
1345a89ea682SMatthew G Knepley /*@C
1346aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1347a89ea682SMatthew G Knepley 
1348a89ea682SMatthew G Knepley   Not Collective
1349a89ea682SMatthew G Knepley 
1350a89ea682SMatthew G Knepley   Input Parameters:
1351a89ea682SMatthew G Knepley + dm - the DM object
1352aa1993deSMatthew G Knepley . count - The minium size
135369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1354a89ea682SMatthew G Knepley 
1355a89ea682SMatthew G Knepley   Output Parameter:
1356a89ea682SMatthew G Knepley . array - the work array
1357a89ea682SMatthew G Knepley 
1358a89ea682SMatthew G Knepley   Level: developer
1359a89ea682SMatthew G Knepley 
1360a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1361a89ea682SMatthew G Knepley @*/
136269291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1363a89ea682SMatthew G Knepley {
1364a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1365aa1993deSMatthew G Knepley   DMWorkLink     link;
136669291d52SBarry Smith   PetscMPIInt    dsize;
1367a89ea682SMatthew G Knepley 
1368a89ea682SMatthew G Knepley   PetscFunctionBegin;
1369a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1370aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1371aa1993deSMatthew G Knepley   if (dm->workin) {
1372aa1993deSMatthew G Knepley     link       = dm->workin;
1373aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1374aa1993deSMatthew G Knepley   } else {
1375b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1376a89ea682SMatthew G Knepley   }
137769291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
13785056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1379aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1380854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1381854ce69bSBarry Smith     link->bytes = dsize*count;
1382aa1993deSMatthew G Knepley   }
1383aa1993deSMatthew G Knepley   link->next   = dm->workout;
1384aa1993deSMatthew G Knepley   dm->workout  = link;
1385aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1386a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1387a89ea682SMatthew G Knepley }
1388a89ea682SMatthew G Knepley 
1389aa1993deSMatthew G Knepley /*@C
1390aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1391aa1993deSMatthew G Knepley 
1392aa1993deSMatthew G Knepley   Not Collective
1393aa1993deSMatthew G Knepley 
1394aa1993deSMatthew G Knepley   Input Parameters:
1395aa1993deSMatthew G Knepley + dm - the DM object
1396aa1993deSMatthew G Knepley . count - The minium size
139769291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1398aa1993deSMatthew G Knepley 
1399aa1993deSMatthew G Knepley   Output Parameter:
1400aa1993deSMatthew G Knepley . array - the work array
1401aa1993deSMatthew G Knepley 
1402aa1993deSMatthew G Knepley   Level: developer
1403aa1993deSMatthew G Knepley 
140495452b02SPatrick Sanan   Developer Notes:
140595452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1406aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1407aa1993deSMatthew G Knepley @*/
140869291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1409aa1993deSMatthew G Knepley {
1410aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1411aa1993deSMatthew G Knepley 
1412aa1993deSMatthew G Knepley   PetscFunctionBegin;
1413aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1414aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1415aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1416aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1417aa1993deSMatthew G Knepley       *p           = link->next;
1418aa1993deSMatthew G Knepley       link->next   = dm->workin;
1419aa1993deSMatthew G Knepley       dm->workin   = link;
14200298fd71SBarry Smith       *(void**)mem = NULL;
1421aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1422aa1993deSMatthew G Knepley     }
1423aa1993deSMatthew G Knepley   }
1424aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1425aa1993deSMatthew G Knepley }
1426e7c4fc90SDmitry Karpeev 
1427435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1428435a35e8SMatthew G Knepley {
1429435a35e8SMatthew G Knepley   PetscFunctionBegin;
1430435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
143182f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1432435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1433435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1434435a35e8SMatthew G Knepley }
1435435a35e8SMatthew G Knepley 
14360a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14370a50eb56SMatthew G. Knepley {
14380a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14390a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1440f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14410a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14420a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14430a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14440a50eb56SMatthew G. Knepley }
14450a50eb56SMatthew G. Knepley 
1446f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1447f9d4088aSMatthew G. Knepley {
1448f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1449f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1450f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1451f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1452f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1453f9d4088aSMatthew G. Knepley }
1454f9d4088aSMatthew G. Knepley 
1455f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1456f9d4088aSMatthew G. Knepley {
1457f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1458f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1459f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1460f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1461f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1462f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1463f9d4088aSMatthew G. Knepley }
1464f9d4088aSMatthew G. Knepley 
14654f3b5142SJed Brown /*@C
14664d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14674d343eeaSMatthew G Knepley 
14684d343eeaSMatthew G Knepley   Not collective
14694d343eeaSMatthew G Knepley 
14704d343eeaSMatthew G Knepley   Input Parameter:
14714d343eeaSMatthew G Knepley . dm - the DM object
14724d343eeaSMatthew G Knepley 
14734d343eeaSMatthew G Knepley   Output Parameters:
14740298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14750298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14760298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
14774d343eeaSMatthew G Knepley 
14784d343eeaSMatthew G Knepley   Level: intermediate
14794d343eeaSMatthew G Knepley 
148021c9b008SJed Brown   Notes:
148121c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
148221c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
148321c9b008SJed Brown   PetscFree().
148421c9b008SJed Brown 
14854d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
14864d343eeaSMatthew G Knepley @*/
148737d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
14884d343eeaSMatthew G Knepley {
148937d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
14904d343eeaSMatthew G Knepley   PetscErrorCode ierr;
14914d343eeaSMatthew G Knepley 
14924d343eeaSMatthew G Knepley   PetscFunctionBegin;
14934d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
149469ca1f37SDmitry Karpeev   if (numFields) {
1495534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
149669ca1f37SDmitry Karpeev     *numFields = 0;
149769ca1f37SDmitry Karpeev   }
149837d0c07bSMatthew G Knepley   if (fieldNames) {
149937d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15000298fd71SBarry Smith     *fieldNames = NULL;
150169ca1f37SDmitry Karpeev   }
150269ca1f37SDmitry Karpeev   if (fields) {
150369ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15040298fd71SBarry Smith     *fields = NULL;
150569ca1f37SDmitry Karpeev   }
150692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
150737d0c07bSMatthew G Knepley   if (section) {
15083a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
150937d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
151037d0c07bSMatthew G Knepley 
1511e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
151237d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15133a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
151437d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
151537d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
151637d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15173a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
151837d0c07bSMatthew G Knepley     }
151937d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
152037d0c07bSMatthew G Knepley       PetscInt gdof;
152137d0c07bSMatthew G Knepley 
152237d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
152337d0c07bSMatthew G Knepley       if (gdof > 0) {
152437d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15253a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
152637d0c07bSMatthew G Knepley 
152737d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
152837d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15293a544194SStefano Zampini           fpdof = fdof-fcdof;
15303a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15313a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15323a544194SStefano Zampini             fieldNc[f] = 1;
15333a544194SStefano Zampini           }
15343a544194SStefano Zampini           fieldSizes[f] += fpdof;
153537d0c07bSMatthew G Knepley         }
153637d0c07bSMatthew G Knepley       }
153737d0c07bSMatthew G Knepley     }
153837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1539785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
154037d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
154137d0c07bSMatthew G Knepley     }
154237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
154337d0c07bSMatthew G Knepley       PetscInt gdof, goff;
154437d0c07bSMatthew G Knepley 
154537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
154637d0c07bSMatthew G Knepley       if (gdof > 0) {
154737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
154837d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
154937d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
155037d0c07bSMatthew G Knepley 
155137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
155237d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
155337d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
155437d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
155537d0c07bSMatthew G Knepley           }
155637d0c07bSMatthew G Knepley         }
155737d0c07bSMatthew G Knepley       }
155837d0c07bSMatthew G Knepley     }
15598865f1eaSKarl Rupp     if (numFields) *numFields = nF;
156037d0c07bSMatthew G Knepley     if (fieldNames) {
1561785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
156237d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
156337d0c07bSMatthew G Knepley         const char *fieldName;
156437d0c07bSMatthew G Knepley 
156537d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
156637d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
156737d0c07bSMatthew G Knepley       }
156837d0c07bSMatthew G Knepley     }
156937d0c07bSMatthew G Knepley     if (fields) {
1570785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
157137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15723a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15733a544194SStefano Zampini 
157482f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15753a544194SStefano Zampini         in[0] = -fieldNc[f];
15763a544194SStefano Zampini         in[1] = fieldNc[f];
15773a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
15783a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
15793a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
158037d0c07bSMatthew G Knepley       }
158137d0c07bSMatthew G Knepley     }
15823a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
15838865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
15848865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
158569ca1f37SDmitry Karpeev   }
15864d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
15874d343eeaSMatthew G Knepley }
15884d343eeaSMatthew G Knepley 
158916621825SDmitry Karpeev 
159016621825SDmitry Karpeev /*@C
159116621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
159216621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
159316621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1594e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1595e7c4fc90SDmitry Karpeev 
1596e7c4fc90SDmitry Karpeev   Not collective
1597e7c4fc90SDmitry Karpeev 
1598e7c4fc90SDmitry Karpeev   Input Parameter:
1599e7c4fc90SDmitry Karpeev . dm - the DM object
1600e7c4fc90SDmitry Karpeev 
1601e7c4fc90SDmitry Karpeev   Output Parameters:
16020298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16030298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16040298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16050298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1606e7c4fc90SDmitry Karpeev 
1607e7c4fc90SDmitry Karpeev   Level: intermediate
1608e7c4fc90SDmitry Karpeev 
1609e7c4fc90SDmitry Karpeev   Notes:
1610e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1611e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1612e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1613e7c4fc90SDmitry Karpeev 
1614e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1615e7c4fc90SDmitry Karpeev @*/
161616621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1617e7c4fc90SDmitry Karpeev {
1618e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1619e7c4fc90SDmitry Karpeev 
1620e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1621e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16228865f1eaSKarl Rupp   if (len) {
1623534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
16248865f1eaSKarl Rupp     *len = 0;
16258865f1eaSKarl Rupp   }
16268865f1eaSKarl Rupp   if (namelist) {
16278865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16288865f1eaSKarl Rupp     *namelist = 0;
16298865f1eaSKarl Rupp   }
16308865f1eaSKarl Rupp   if (islist) {
16318865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16328865f1eaSKarl Rupp     *islist = 0;
16338865f1eaSKarl Rupp   }
16348865f1eaSKarl Rupp   if (dmlist) {
16358865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16368865f1eaSKarl Rupp     *dmlist = 0;
16378865f1eaSKarl Rupp   }
1638f3f0edfdSDmitry Karpeev   /*
1639f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1640f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1641f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1642f3f0edfdSDmitry Karpeev    */
1643ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
164416621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1645435a35e8SMatthew G Knepley     PetscSection section;
1646435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1647435a35e8SMatthew G Knepley 
164892fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1649435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1650435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1651f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
165203dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
165303dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
165403dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1655435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1656435a35e8SMatthew G Knepley         const char *fieldName;
1657435a35e8SMatthew G Knepley 
165803dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
165903dc3394SMatthew G. Knepley         if (namelist) {
1660435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1661435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1662435a35e8SMatthew G Knepley         }
166303dc3394SMatthew G. Knepley       }
1664435a35e8SMatthew G Knepley     } else {
166569ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1666e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16670298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1668e7c4fc90SDmitry Karpeev     }
16698865f1eaSKarl Rupp   } else {
167016621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
167116621825SDmitry Karpeev   }
167216621825SDmitry Karpeev   PetscFunctionReturn(0);
167316621825SDmitry Karpeev }
167416621825SDmitry Karpeev 
1675564cec59SMatthew G. Knepley /*@
1676435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1677435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1678435a35e8SMatthew G Knepley 
1679435a35e8SMatthew G Knepley   Not collective
1680435a35e8SMatthew G Knepley 
1681435a35e8SMatthew G Knepley   Input Parameters:
16822adcc780SMatthew G. Knepley + dm        - The DM object
16832adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
16842adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1685435a35e8SMatthew G Knepley 
1686435a35e8SMatthew G Knepley   Output Parameters:
16872adcc780SMatthew G. Knepley + is - The global indices for the subproblem
16882adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1689435a35e8SMatthew G Knepley 
16905d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
16915d3b26e6SMatthew G. Knepley 
1692435a35e8SMatthew G Knepley   Level: intermediate
1693435a35e8SMatthew G Knepley 
16945d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1695435a35e8SMatthew G Knepley @*/
169637bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1697435a35e8SMatthew G Knepley {
1698435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1699435a35e8SMatthew G Knepley 
1700435a35e8SMatthew G Knepley   PetscFunctionBegin;
1701435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1702435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17038865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17048865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1705b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1706435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1707435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1708435a35e8SMatthew G Knepley }
1709435a35e8SMatthew G Knepley 
17102adcc780SMatthew G. Knepley /*@C
17112adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17122adcc780SMatthew G. Knepley 
17132adcc780SMatthew G. Knepley   Not collective
17142adcc780SMatthew G. Knepley 
17152adcc780SMatthew G. Knepley   Input Parameter:
17162adcc780SMatthew G. Knepley + dms - The DM objects
17172adcc780SMatthew G. Knepley - len - The number of DMs
17182adcc780SMatthew G. Knepley 
17192adcc780SMatthew G. Knepley   Output Parameters:
1720a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17212adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17222adcc780SMatthew G. Knepley 
17235d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17245d3b26e6SMatthew G. Knepley 
17252adcc780SMatthew G. Knepley   Level: intermediate
17262adcc780SMatthew G. Knepley 
17275d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17282adcc780SMatthew G. Knepley @*/
17292adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17302adcc780SMatthew G. Knepley {
17312adcc780SMatthew G. Knepley   PetscInt       i;
17322adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17332adcc780SMatthew G. Knepley 
17342adcc780SMatthew G. Knepley   PetscFunctionBegin;
17352adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17362adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17372adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1738a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17392adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17402adcc780SMatthew G. Knepley   if (len) {
1741b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1742b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1743b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
17442adcc780SMatthew G. Knepley   }
17452adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17462adcc780SMatthew G. Knepley }
17472adcc780SMatthew G. Knepley 
174816621825SDmitry Karpeev 
174916621825SDmitry Karpeev /*@C
17508d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17518d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17528d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17538d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17548d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
175516621825SDmitry Karpeev 
175616621825SDmitry Karpeev   Not collective
175716621825SDmitry Karpeev 
175816621825SDmitry Karpeev   Input Parameter:
175916621825SDmitry Karpeev . dm - the DM object
176016621825SDmitry Karpeev 
176116621825SDmitry Karpeev   Output Parameters:
17620298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17630298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17640298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17650298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17660298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
176716621825SDmitry Karpeev 
176816621825SDmitry Karpeev   Level: intermediate
176916621825SDmitry Karpeev 
177016621825SDmitry Karpeev   Notes:
177116621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
177216621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
177316621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
177416621825SDmitry Karpeev 
17758d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
177616621825SDmitry Karpeev @*/
17778d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
177816621825SDmitry Karpeev {
177916621825SDmitry Karpeev   PetscErrorCode      ierr;
1780be081cd6SPeter Brune   DMSubDomainHookLink link;
1781be081cd6SPeter Brune   PetscInt            i,l;
178216621825SDmitry Karpeev 
178316621825SDmitry Karpeev   PetscFunctionBegin;
178416621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
178514a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
17860298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
17870298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
17880298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
17890298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1790f3f0edfdSDmitry Karpeev   /*
1791f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1792f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1793f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1794f3f0edfdSDmitry Karpeev    */
1795ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
179616621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1797be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
179814a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1799f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1800be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1801be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1802be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1803be081cd6SPeter Brune         }
1804648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1805e7c4fc90SDmitry Karpeev       }
180614a18fd3SPeter Brune     }
180714a18fd3SPeter Brune     if (len) *len = l;
180814a18fd3SPeter Brune   }
1809e30e807fSPeter Brune   PetscFunctionReturn(0);
1810e30e807fSPeter Brune }
1811e30e807fSPeter Brune 
1812e30e807fSPeter Brune 
1813e30e807fSPeter Brune /*@C
1814e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1815e30e807fSPeter Brune 
1816e30e807fSPeter Brune   Not collective
1817e30e807fSPeter Brune 
1818e30e807fSPeter Brune   Input Parameters:
1819e30e807fSPeter Brune + dm - the DM object
1820e30e807fSPeter Brune . n  - the number of subdomain scatters
1821e30e807fSPeter Brune - subdms - the local subdomains
1822e30e807fSPeter Brune 
1823e30e807fSPeter Brune   Output Parameters:
1824e30e807fSPeter Brune + n     - the number of scatters returned
1825e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1826e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1827e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1828e30e807fSPeter Brune 
182995452b02SPatrick Sanan   Notes:
183095452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1831e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1832e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1833e30e807fSPeter Brune   solution and residual data.
1834e30e807fSPeter Brune 
1835e30e807fSPeter Brune   Level: developer
1836e30e807fSPeter Brune 
1837e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1838e30e807fSPeter Brune @*/
1839e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1840e30e807fSPeter Brune {
1841e30e807fSPeter Brune   PetscErrorCode ierr;
1842e30e807fSPeter Brune 
1843e30e807fSPeter Brune   PetscFunctionBegin;
1844e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1845e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1846b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1847e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1848e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1849e7c4fc90SDmitry Karpeev }
1850e7c4fc90SDmitry Karpeev 
185147c6ae99SBarry Smith /*@
185247c6ae99SBarry Smith   DMRefine - Refines a DM object
185347c6ae99SBarry Smith 
1854d083f849SBarry Smith   Collective on dm
185547c6ae99SBarry Smith 
185647c6ae99SBarry Smith   Input Parameter:
185747c6ae99SBarry Smith + dm   - the DM object
185891d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
185947c6ae99SBarry Smith 
186047c6ae99SBarry Smith   Output Parameter:
18610298fd71SBarry Smith . dmf - the refined DM, or NULL
1862ae0a1c52SMatthew G Knepley 
18630298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
186447c6ae99SBarry Smith 
186547c6ae99SBarry Smith   Level: developer
186647c6ae99SBarry Smith 
1867e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
186847c6ae99SBarry Smith @*/
18697087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
187047c6ae99SBarry Smith {
187147c6ae99SBarry Smith   PetscErrorCode   ierr;
1872c833c3b5SJed Brown   DMRefineHookLink link;
187347c6ae99SBarry Smith 
187447c6ae99SBarry Smith   PetscFunctionBegin;
1875732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1876b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
18771ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
187847c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
18794057135bSMatthew G Knepley   if (*dmf) {
188043842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
18818865f1eaSKarl Rupp 
18828cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
18838865f1eaSKarl Rupp 
1884644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
18850598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1886656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
18878865f1eaSKarl Rupp 
1888e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1889c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
18908865f1eaSKarl Rupp       if (link->refinehook) {
18918865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
18928865f1eaSKarl Rupp       }
1893c833c3b5SJed Brown     }
1894c833c3b5SJed Brown   }
18951ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1896c833c3b5SJed Brown   PetscFunctionReturn(0);
1897c833c3b5SJed Brown }
1898c833c3b5SJed Brown 
1899bb9467b5SJed Brown /*@C
1900c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1901c833c3b5SJed Brown 
1902c833c3b5SJed Brown    Logically Collective
1903c833c3b5SJed Brown 
1904c833c3b5SJed Brown    Input Arguments:
1905c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1906c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1907c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19080298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1909c833c3b5SJed Brown 
1910c833c3b5SJed Brown    Calling sequence of refinehook:
1911c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1912c833c3b5SJed Brown 
1913c833c3b5SJed Brown +  coarse - coarse level DM
1914c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1915c833c3b5SJed Brown -  ctx - optional user-defined function context
1916c833c3b5SJed Brown 
1917c833c3b5SJed Brown    Calling sequence for interphook:
1918c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1919c833c3b5SJed Brown 
1920c833c3b5SJed Brown +  coarse - coarse level DM
1921c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1922c833c3b5SJed Brown .  fine - fine level DM to update
1923c833c3b5SJed Brown -  ctx - optional user-defined function context
1924c833c3b5SJed Brown 
1925c833c3b5SJed Brown    Level: advanced
1926c833c3b5SJed Brown 
1927c833c3b5SJed Brown    Notes:
1928c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1929c833c3b5SJed Brown 
1930c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1931c833c3b5SJed Brown 
1932bb9467b5SJed Brown    This function is currently not available from Fortran.
1933bb9467b5SJed Brown 
1934c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1935c833c3b5SJed Brown @*/
1936c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1937c833c3b5SJed Brown {
1938c833c3b5SJed Brown   PetscErrorCode   ierr;
1939c833c3b5SJed Brown   DMRefineHookLink link,*p;
1940c833c3b5SJed Brown 
1941c833c3b5SJed Brown   PetscFunctionBegin;
1942c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19433d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19443d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19453d8e3701SJed Brown   }
194695dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1947c833c3b5SJed Brown   link->refinehook = refinehook;
1948c833c3b5SJed Brown   link->interphook = interphook;
1949c833c3b5SJed Brown   link->ctx        = ctx;
19500298fd71SBarry Smith   link->next       = NULL;
1951c833c3b5SJed Brown   *p               = link;
1952c833c3b5SJed Brown   PetscFunctionReturn(0);
1953c833c3b5SJed Brown }
1954c833c3b5SJed Brown 
19553d8e3701SJed Brown /*@C
19563d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19573d8e3701SJed Brown 
19583d8e3701SJed Brown    Logically Collective
19593d8e3701SJed Brown 
19603d8e3701SJed Brown    Input Arguments:
19613d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19623d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19633d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19643d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19653d8e3701SJed Brown 
19663d8e3701SJed Brown    Level: advanced
19673d8e3701SJed Brown 
19683d8e3701SJed Brown    Notes:
19693d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19703d8e3701SJed Brown 
19713d8e3701SJed Brown    This function is currently not available from Fortran.
19723d8e3701SJed Brown 
19733d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19743d8e3701SJed Brown @*/
19753d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19763d8e3701SJed Brown {
19773d8e3701SJed Brown   PetscErrorCode   ierr;
19783d8e3701SJed Brown   DMRefineHookLink link,*p;
19793d8e3701SJed Brown 
19803d8e3701SJed Brown   PetscFunctionBegin;
19813d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19823d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
19833d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
19843d8e3701SJed Brown       link = *p;
19853d8e3701SJed Brown       *p = link->next;
19863d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
19873d8e3701SJed Brown       break;
19883d8e3701SJed Brown     }
19893d8e3701SJed Brown   }
19903d8e3701SJed Brown   PetscFunctionReturn(0);
19913d8e3701SJed Brown }
19923d8e3701SJed Brown 
1993c833c3b5SJed Brown /*@
1994c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
1995c833c3b5SJed Brown 
1996c833c3b5SJed Brown    Collective if any hooks are
1997c833c3b5SJed Brown 
1998c833c3b5SJed Brown    Input Arguments:
1999c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2000e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2001c833c3b5SJed Brown -  fine - finer DM to update
2002c833c3b5SJed Brown 
2003c833c3b5SJed Brown    Level: developer
2004c833c3b5SJed Brown 
2005c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2006c833c3b5SJed Brown @*/
2007c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2008c833c3b5SJed Brown {
2009c833c3b5SJed Brown   PetscErrorCode   ierr;
2010c833c3b5SJed Brown   DMRefineHookLink link;
2011c833c3b5SJed Brown 
2012c833c3b5SJed Brown   PetscFunctionBegin;
2013c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20148865f1eaSKarl Rupp     if (link->interphook) {
20158865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20168865f1eaSKarl Rupp     }
20174057135bSMatthew G Knepley   }
201847c6ae99SBarry Smith   PetscFunctionReturn(0);
201947c6ae99SBarry Smith }
202047c6ae99SBarry Smith 
2021eb3f98d2SBarry Smith /*@
2022eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2023eb3f98d2SBarry Smith 
2024eb3f98d2SBarry Smith     Not Collective
2025eb3f98d2SBarry Smith 
2026eb3f98d2SBarry Smith     Input Parameter:
2027eb3f98d2SBarry Smith .   dm - the DM object
2028eb3f98d2SBarry Smith 
2029eb3f98d2SBarry Smith     Output Parameter:
2030eb3f98d2SBarry Smith .   level - number of refinements
2031eb3f98d2SBarry Smith 
2032eb3f98d2SBarry Smith     Level: developer
2033eb3f98d2SBarry Smith 
20346a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2035eb3f98d2SBarry Smith 
2036eb3f98d2SBarry Smith @*/
2037eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2038eb3f98d2SBarry Smith {
2039eb3f98d2SBarry Smith   PetscFunctionBegin;
2040eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2041eb3f98d2SBarry Smith   *level = dm->levelup;
2042eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2043eb3f98d2SBarry Smith }
2044eb3f98d2SBarry Smith 
2045fef3a512SBarry Smith /*@
2046fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2047fef3a512SBarry Smith 
2048fef3a512SBarry Smith     Not Collective
2049fef3a512SBarry Smith 
2050fef3a512SBarry Smith     Input Parameter:
2051fef3a512SBarry Smith +   dm - the DM object
2052fef3a512SBarry Smith -   level - number of refinements
2053fef3a512SBarry Smith 
2054fef3a512SBarry Smith     Level: advanced
2055fef3a512SBarry Smith 
205695452b02SPatrick Sanan     Notes:
205795452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2058fef3a512SBarry Smith 
2059fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2060fef3a512SBarry Smith 
2061fef3a512SBarry Smith @*/
2062fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2063fef3a512SBarry Smith {
2064fef3a512SBarry Smith   PetscFunctionBegin;
2065fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2066fef3a512SBarry Smith   dm->levelup = level;
2067fef3a512SBarry Smith   PetscFunctionReturn(0);
2068fef3a512SBarry Smith }
2069fef3a512SBarry Smith 
2070ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2071ca3d3a14SMatthew G. Knepley {
2072ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2073ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2074ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2075ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2076ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2077ca3d3a14SMatthew G. Knepley }
2078ca3d3a14SMatthew G. Knepley 
2079ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2080ca3d3a14SMatthew G. Knepley {
2081ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2082ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2083ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2084ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2085ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2086ca3d3a14SMatthew G. Knepley }
2087ca3d3a14SMatthew G. Knepley 
2088ca3d3a14SMatthew G. Knepley /*@
2089c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2090ca3d3a14SMatthew G. Knepley 
2091ca3d3a14SMatthew G. Knepley   Input Parameter:
2092ca3d3a14SMatthew G. Knepley . dm - The DM
2093ca3d3a14SMatthew G. Knepley 
2094ca3d3a14SMatthew G. Knepley   Output Parameter:
2095ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2096ca3d3a14SMatthew G. Knepley 
2097ca3d3a14SMatthew G. Knepley   Level: developer
2098ca3d3a14SMatthew G. Knepley 
2099ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2100ca3d3a14SMatthew G. Knepley @*/
2101ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2102ca3d3a14SMatthew G. Knepley {
2103ca3d3a14SMatthew G. Knepley   Vec            tv;
2104ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2105ca3d3a14SMatthew G. Knepley 
2106ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2107ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2108534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2109ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2110ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2111ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2112ca3d3a14SMatthew G. Knepley }
2113ca3d3a14SMatthew G. Knepley 
2114ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2115ca3d3a14SMatthew G. Knepley {
2116ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2117ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2118ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2119ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2120ca3d3a14SMatthew G. Knepley 
2121ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2122ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
212392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2124ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2125ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2126ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
212792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2128ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2129ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2130ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2131ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2132ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2133ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2134ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2135ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2136ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2137ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2138ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2139ca3d3a14SMatthew G. Knepley     }
2140ca3d3a14SMatthew G. Knepley   }
2141ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2142ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2143ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2144ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2145ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2146ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2147ca3d3a14SMatthew G. Knepley       if (dof) {
2148ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2149ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2150ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2151ca3d3a14SMatthew G. Knepley 
2152ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2153ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2154ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2155580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2156ca3d3a14SMatthew G. Knepley       }
2157ca3d3a14SMatthew G. Knepley     }
2158ca3d3a14SMatthew G. Knepley   }
2159ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2160ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2161ca3d3a14SMatthew G. Knepley }
2162ca3d3a14SMatthew G. Knepley 
2163ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2164ca3d3a14SMatthew G. Knepley {
2165ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2166ca3d3a14SMatthew G. Knepley 
2167ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2168ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2169ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2170ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2171ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2172ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2173ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2174ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2175ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2176ca3d3a14SMatthew G. Knepley }
2177ca3d3a14SMatthew G. Knepley 
2178bb9467b5SJed Brown /*@C
2179baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2180baf369e7SPeter Brune 
2181baf369e7SPeter Brune    Logically Collective
2182baf369e7SPeter Brune 
2183baf369e7SPeter Brune    Input Arguments:
2184baf369e7SPeter Brune +  dm - the DM
2185baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2186baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
21870298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2188baf369e7SPeter Brune 
2189baf369e7SPeter Brune    Calling sequence for beginhook:
2190baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2191baf369e7SPeter Brune 
2192baf369e7SPeter Brune +  dm - global DM
2193baf369e7SPeter Brune .  g - global vector
2194baf369e7SPeter Brune .  mode - mode
2195baf369e7SPeter Brune .  l - local vector
2196baf369e7SPeter Brune -  ctx - optional user-defined function context
2197baf369e7SPeter Brune 
2198baf369e7SPeter Brune 
2199baf369e7SPeter Brune    Calling sequence for endhook:
2200ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2201baf369e7SPeter Brune 
2202baf369e7SPeter Brune +  global - global DM
2203baf369e7SPeter Brune -  ctx - optional user-defined function context
2204baf369e7SPeter Brune 
2205baf369e7SPeter Brune    Level: advanced
2206baf369e7SPeter Brune 
2207baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2208baf369e7SPeter Brune @*/
2209baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2210baf369e7SPeter Brune {
2211baf369e7SPeter Brune   PetscErrorCode          ierr;
2212baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2213baf369e7SPeter Brune 
2214baf369e7SPeter Brune   PetscFunctionBegin;
2215baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2216baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
221795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2218baf369e7SPeter Brune   link->beginhook = beginhook;
2219baf369e7SPeter Brune   link->endhook   = endhook;
2220baf369e7SPeter Brune   link->ctx       = ctx;
22210298fd71SBarry Smith   link->next      = NULL;
2222baf369e7SPeter Brune   *p              = link;
2223baf369e7SPeter Brune   PetscFunctionReturn(0);
2224baf369e7SPeter Brune }
2225baf369e7SPeter Brune 
22264c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22274c274da1SToby Isaac {
22284c274da1SToby Isaac   Mat cMat;
22294c274da1SToby Isaac   Vec cVec;
22304c274da1SToby Isaac   PetscSection section, cSec;
22314c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22324c274da1SToby Isaac   PetscErrorCode ierr;
22334c274da1SToby Isaac 
22344c274da1SToby Isaac   PetscFunctionBegin;
22354c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22364c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22374c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22385db9a05bSToby Isaac     PetscInt nRows;
22395db9a05bSToby Isaac 
22405db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22415db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
224292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
22437711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22444c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
22454c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22464c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22474c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22484c274da1SToby Isaac       if (dof) {
22494c274da1SToby Isaac         PetscScalar *vals;
22504c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
22514c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
22524c274da1SToby Isaac       }
22534c274da1SToby Isaac     }
22544c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22554c274da1SToby Isaac   }
22564c274da1SToby Isaac   PetscFunctionReturn(0);
22574c274da1SToby Isaac }
22584c274da1SToby Isaac 
225947c6ae99SBarry Smith /*@
226001729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
226101729b5cSPatrick Sanan 
2262d083f849SBarry Smith     Neighbor-wise Collective on dm
226301729b5cSPatrick Sanan 
226401729b5cSPatrick Sanan     Input Parameters:
226501729b5cSPatrick Sanan +   dm - the DM object
226601729b5cSPatrick Sanan .   g - the global vector
226701729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
226801729b5cSPatrick Sanan -   l - the local vector
226901729b5cSPatrick Sanan 
227001729b5cSPatrick Sanan     Notes:
227101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
227201729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
227301729b5cSPatrick Sanan 
227401729b5cSPatrick Sanan     Level: beginner
227501729b5cSPatrick Sanan 
227601729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
227701729b5cSPatrick Sanan 
227801729b5cSPatrick Sanan @*/
227901729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
228001729b5cSPatrick Sanan {
228101729b5cSPatrick Sanan   PetscErrorCode ierr;
228201729b5cSPatrick Sanan 
228301729b5cSPatrick Sanan   PetscFunctionBegin;
228401729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
228501729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
228601729b5cSPatrick Sanan   PetscFunctionReturn(0);
228701729b5cSPatrick Sanan }
228801729b5cSPatrick Sanan 
228901729b5cSPatrick Sanan /*@
229047c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
229147c6ae99SBarry Smith 
2292d083f849SBarry Smith     Neighbor-wise Collective on dm
229347c6ae99SBarry Smith 
229447c6ae99SBarry Smith     Input Parameters:
229547c6ae99SBarry Smith +   dm - the DM object
229647c6ae99SBarry Smith .   g - the global vector
229747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
229847c6ae99SBarry Smith -   l - the local vector
229947c6ae99SBarry Smith 
230001729b5cSPatrick Sanan     Level: intermediate
230147c6ae99SBarry Smith 
230201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
230347c6ae99SBarry Smith 
230447c6ae99SBarry Smith @*/
23057087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
230647c6ae99SBarry Smith {
23077128ae9fSMatthew G Knepley   PetscSF                 sf;
230847c6ae99SBarry Smith   PetscErrorCode          ierr;
2309baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
231047c6ae99SBarry Smith 
231147c6ae99SBarry Smith   PetscFunctionBegin;
2312171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2313baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23148865f1eaSKarl Rupp     if (link->beginhook) {
23158865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23168865f1eaSKarl Rupp     }
2317baf369e7SPeter Brune   }
23181bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
23197128ae9fSMatthew G Knepley   if (sf) {
2320ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2321ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23227128ae9fSMatthew G Knepley 
232382f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23247128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2325ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23267128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23277128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2328ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23297128ae9fSMatthew G Knepley   } else {
233033907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2331843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23327128ae9fSMatthew G Knepley   }
233347c6ae99SBarry Smith   PetscFunctionReturn(0);
233447c6ae99SBarry Smith }
233547c6ae99SBarry Smith 
233647c6ae99SBarry Smith /*@
233747c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
233847c6ae99SBarry Smith 
2339d083f849SBarry Smith     Neighbor-wise Collective on dm
234047c6ae99SBarry Smith 
234147c6ae99SBarry Smith     Input Parameters:
234247c6ae99SBarry Smith +   dm - the DM object
234347c6ae99SBarry Smith .   g - the global vector
234447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
234547c6ae99SBarry Smith -   l - the local vector
234647c6ae99SBarry Smith 
234701729b5cSPatrick Sanan     Level: intermediate
234847c6ae99SBarry Smith 
234901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
235047c6ae99SBarry Smith 
235147c6ae99SBarry Smith @*/
23527087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
235347c6ae99SBarry Smith {
23547128ae9fSMatthew G Knepley   PetscSF                 sf;
235547c6ae99SBarry Smith   PetscErrorCode          ierr;
2356ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2357ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2358ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2359baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
236047c6ae99SBarry Smith 
236147c6ae99SBarry Smith   PetscFunctionBegin;
2362171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23631bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2364ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
23657128ae9fSMatthew G Knepley   if (sf) {
236682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23677128ae9fSMatthew G Knepley 
23687128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2369ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23707128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23717128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2372ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2373ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
23747128ae9fSMatthew G Knepley   } else {
237533907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2376843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23777128ae9fSMatthew G Knepley   }
23784c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2379baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2380baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2381baf369e7SPeter Brune   }
238247c6ae99SBarry Smith   PetscFunctionReturn(0);
238347c6ae99SBarry Smith }
238447c6ae99SBarry Smith 
2385d4d07f1eSToby Isaac /*@C
2386d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2387d4d07f1eSToby Isaac 
2388d4d07f1eSToby Isaac    Logically Collective
2389d4d07f1eSToby Isaac 
2390d4d07f1eSToby Isaac    Input Arguments:
2391d4d07f1eSToby Isaac +  dm - the DM
2392d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2393d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2394d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2395d4d07f1eSToby Isaac 
2396d4d07f1eSToby Isaac    Calling sequence for beginhook:
2397d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2398d4d07f1eSToby Isaac 
2399d4d07f1eSToby Isaac +  dm - global DM
2400d4d07f1eSToby Isaac .  l - local vector
2401d4d07f1eSToby Isaac .  mode - mode
2402d4d07f1eSToby Isaac .  g - global vector
2403d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2404d4d07f1eSToby Isaac 
2405d4d07f1eSToby Isaac 
2406d4d07f1eSToby Isaac    Calling sequence for endhook:
2407d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2408d4d07f1eSToby Isaac 
2409d4d07f1eSToby Isaac +  global - global DM
2410d4d07f1eSToby Isaac .  l - local vector
2411d4d07f1eSToby Isaac .  mode - mode
2412d4d07f1eSToby Isaac .  g - global vector
2413d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2414d4d07f1eSToby Isaac 
2415d4d07f1eSToby Isaac    Level: advanced
2416d4d07f1eSToby Isaac 
2417d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2418d4d07f1eSToby Isaac @*/
2419d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2420d4d07f1eSToby Isaac {
2421d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2422d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2423d4d07f1eSToby Isaac 
2424d4d07f1eSToby Isaac   PetscFunctionBegin;
2425d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2426d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
242795dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2428d4d07f1eSToby Isaac   link->beginhook = beginhook;
2429d4d07f1eSToby Isaac   link->endhook   = endhook;
2430d4d07f1eSToby Isaac   link->ctx       = ctx;
2431d4d07f1eSToby Isaac   link->next      = NULL;
2432d4d07f1eSToby Isaac   *p              = link;
2433d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2434d4d07f1eSToby Isaac }
2435d4d07f1eSToby Isaac 
24364c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24374c274da1SToby Isaac {
24384c274da1SToby Isaac   Mat cMat;
24394c274da1SToby Isaac   Vec cVec;
24404c274da1SToby Isaac   PetscSection section, cSec;
24414c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24424c274da1SToby Isaac   PetscErrorCode ierr;
24434c274da1SToby Isaac 
24444c274da1SToby Isaac   PetscFunctionBegin;
24454c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24464c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24474c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
24485db9a05bSToby Isaac     PetscInt nRows;
24495db9a05bSToby Isaac 
24505db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24515db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
245292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24537711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24544c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24554c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24564c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24574c274da1SToby Isaac       if (dof) {
24584c274da1SToby Isaac         PetscInt d;
24594c274da1SToby Isaac         PetscScalar *vals;
24604c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
24614c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
24624c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
24634c274da1SToby Isaac          * we just extracted */
24644c274da1SToby Isaac         for (d = 0; d < dof; d++) {
24654c274da1SToby Isaac           vals[d] = 0.;
24664c274da1SToby Isaac         }
24674c274da1SToby Isaac       }
24684c274da1SToby Isaac     }
24694c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
24704c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24714c274da1SToby Isaac   }
24724c274da1SToby Isaac   PetscFunctionReturn(0);
24734c274da1SToby Isaac }
247401729b5cSPatrick Sanan /*@
247501729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
247601729b5cSPatrick Sanan 
2477d083f849SBarry Smith     Neighbor-wise Collective on dm
247801729b5cSPatrick Sanan 
247901729b5cSPatrick Sanan     Input Parameters:
248001729b5cSPatrick Sanan +   dm - the DM object
248101729b5cSPatrick Sanan .   l - the local vector
248201729b5cSPatrick Sanan .   mode - if INSERT_VALUES then no parallel communication is used, if ADD_VALUES then all ghost points from the same base point accumulate into that base point.
248301729b5cSPatrick Sanan -   g - the global vector
248401729b5cSPatrick Sanan 
248501729b5cSPatrick Sanan     Notes:
248601729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
248701729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
248801729b5cSPatrick Sanan 
248901729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
249001729b5cSPatrick Sanan            INSERT_VALUES is not supported for DMDA; in that case simply compute the values directly into a global vector instead of a local one.
249101729b5cSPatrick Sanan 
249201729b5cSPatrick Sanan     Level: beginner
249301729b5cSPatrick Sanan 
249401729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
249501729b5cSPatrick Sanan 
249601729b5cSPatrick Sanan @*/
249701729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
249801729b5cSPatrick Sanan {
249901729b5cSPatrick Sanan   PetscErrorCode ierr;
250001729b5cSPatrick Sanan 
250101729b5cSPatrick Sanan   PetscFunctionBegin;
250201729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
250301729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
250401729b5cSPatrick Sanan   PetscFunctionReturn(0);
250501729b5cSPatrick Sanan }
25064c274da1SToby Isaac 
250747c6ae99SBarry Smith /*@
250801729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25099a42bb27SBarry Smith 
2510d083f849SBarry Smith     Neighbor-wise Collective on dm
25119a42bb27SBarry Smith 
25129a42bb27SBarry Smith     Input Parameters:
25139a42bb27SBarry Smith +   dm - the DM object
2514f6813fd5SJed Brown .   l - the local vector
25151eb28f2eSBarry 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.
25161eb28f2eSBarry Smith -   g - the global vector
25179a42bb27SBarry Smith 
251895452b02SPatrick Sanan     Notes:
251995452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
252084330215SMatthew 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.
25219a42bb27SBarry Smith 
252201729b5cSPatrick Sanan     Level: intermediate
25239a42bb27SBarry Smith 
252401729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25259a42bb27SBarry Smith 
25269a42bb27SBarry Smith @*/
25277087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25289a42bb27SBarry Smith {
25297128ae9fSMatthew G Knepley   PetscSF                 sf;
253084330215SMatthew G. Knepley   PetscSection            s, gs;
2531d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2532ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2533ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2534ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2535ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
253684330215SMatthew G. Knepley   PetscErrorCode          ierr;
25379a42bb27SBarry Smith 
25389a42bb27SBarry Smith   PetscFunctionBegin;
2539171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2540d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2541d4d07f1eSToby Isaac     if (link->beginhook) {
2542d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2543d4d07f1eSToby Isaac     }
2544d4d07f1eSToby Isaac   }
25454c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
25461bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
254792fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
25487128ae9fSMatthew G Knepley   switch (mode) {
25497128ae9fSMatthew G Knepley   case INSERT_VALUES:
25507128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2551304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
255284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25537128ae9fSMatthew G Knepley   case ADD_VALUES:
25547128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2555304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
255684330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25577128ae9fSMatthew G Knepley   default:
255882f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25597128ae9fSMatthew G Knepley   }
2560ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2561ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2562ca3d3a14SMatthew G. Knepley     if (transform) {
2563ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2564ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2565ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2566ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2567ca3d3a14SMatthew G. Knepley     } else {
2568ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2569ca3d3a14SMatthew G. Knepley     }
25707128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2571ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2572a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
257384330215SMatthew G. Knepley     } else if (s && isInsert) {
257484330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
257584330215SMatthew G. Knepley 
2576e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
257784330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
257884330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
257984330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2580b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
258184330215SMatthew G. Knepley 
258284330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
258303442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
258484330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2585b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
258684330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
258784330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2588b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
258903442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2590b3b16f48SMatthew 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);
2591b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2592b3b16f48SMatthew G. Knepley         if (!gcdof) {
259384330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2594b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2595b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
259684330215SMatthew G. Knepley           const PetscInt *cdofs;
259784330215SMatthew G. Knepley           PetscInt        cind = 0;
259884330215SMatthew G. Knepley 
259984330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2600b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
260184330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2602b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
260384330215SMatthew G. Knepley           }
2604b3b16f48SMatthew 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);
260584330215SMatthew G. Knepley       }
2606ca3d3a14SMatthew G. Knepley     }
26077128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2608ca3d3a14SMatthew G. Knepley     if (transform) {
2609ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2610ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2611ca3d3a14SMatthew G. Knepley     } else {
2612ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2613ca3d3a14SMatthew G. Knepley     }
26147128ae9fSMatthew G Knepley   } else {
2615b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2616843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26177128ae9fSMatthew G Knepley   }
26189a42bb27SBarry Smith   PetscFunctionReturn(0);
26199a42bb27SBarry Smith }
26209a42bb27SBarry Smith 
26219a42bb27SBarry Smith /*@
26229a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
262347c6ae99SBarry Smith 
2624d083f849SBarry Smith     Neighbor-wise Collective on dm
262547c6ae99SBarry Smith 
262647c6ae99SBarry Smith     Input Parameters:
262747c6ae99SBarry Smith +   dm - the DM object
2628f6813fd5SJed Brown .   l - the local vector
262947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2630f6813fd5SJed Brown -   g - the global vector
263147c6ae99SBarry Smith 
263201729b5cSPatrick Sanan     Level: intermediate
263347c6ae99SBarry Smith 
2634e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
263547c6ae99SBarry Smith 
263647c6ae99SBarry Smith @*/
26377087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
263847c6ae99SBarry Smith {
26397128ae9fSMatthew G Knepley   PetscSF                 sf;
264084330215SMatthew G. Knepley   PetscSection            s;
2641d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2642ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
264384330215SMatthew G. Knepley   PetscErrorCode          ierr;
264447c6ae99SBarry Smith 
264547c6ae99SBarry Smith   PetscFunctionBegin;
2646171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26471bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
264892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26497128ae9fSMatthew G Knepley   switch (mode) {
26507128ae9fSMatthew G Knepley   case INSERT_VALUES:
26517128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
265284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26537128ae9fSMatthew G Knepley   case ADD_VALUES:
26547128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
265584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26567128ae9fSMatthew G Knepley   default:
265782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26587128ae9fSMatthew G Knepley   }
265984330215SMatthew G. Knepley   if (sf && !isInsert) {
2660ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2661ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2662ca3d3a14SMatthew G. Knepley     Vec                tmpl;
266384330215SMatthew G. Knepley 
2664ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2665ca3d3a14SMatthew G. Knepley     if (transform) {
2666ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2667ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2668ca3d3a14SMatthew G. Knepley     } else {
2669ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2670ca3d3a14SMatthew G. Knepley     }
26717128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2672a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2673ca3d3a14SMatthew G. Knepley     if (transform) {
2674ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2675ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2676ca3d3a14SMatthew G. Knepley     } else {
2677ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2678ca3d3a14SMatthew G. Knepley     }
26797128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
268084330215SMatthew G. Knepley   } else if (s && isInsert) {
26817128ae9fSMatthew G Knepley   } else {
2682b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2683843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26847128ae9fSMatthew G Knepley   }
2685d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2686d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2687d4d07f1eSToby Isaac   }
268847c6ae99SBarry Smith   PetscFunctionReturn(0);
268947c6ae99SBarry Smith }
269047c6ae99SBarry Smith 
2691f089877aSRichard Tran Mills /*@
2692bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2693bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2694d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2695f089877aSRichard Tran Mills 
2696d083f849SBarry Smith    Neighbor-wise Collective on dm
2697f089877aSRichard Tran Mills 
2698f089877aSRichard Tran Mills    Input Parameters:
2699f089877aSRichard Tran Mills +  dm - the DM object
2700bc0a1609SRichard Tran Mills .  g - the original local vector
2701bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2702f089877aSRichard Tran Mills 
2703bc0a1609SRichard Tran Mills    Output Parameter:
2704bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2705f089877aSRichard Tran Mills 
2706f089877aSRichard Tran Mills    Level: intermediate
2707f089877aSRichard Tran Mills 
2708bc0a1609SRichard Tran Mills    Notes:
2709bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2710bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2711bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2712bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2713bc0a1609SRichard Tran Mills 
2714bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2715f089877aSRichard Tran Mills 
2716f089877aSRichard Tran Mills @*/
2717f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2718f089877aSRichard Tran Mills {
2719f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2720f089877aSRichard Tran Mills 
2721f089877aSRichard Tran Mills   PetscFunctionBegin;
2722f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2723bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2724f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2725f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2726f089877aSRichard Tran Mills }
2727f089877aSRichard Tran Mills 
2728f089877aSRichard Tran Mills /*@
2729bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2730bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2731d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2732f089877aSRichard Tran Mills 
2733d083f849SBarry Smith    Neighbor-wise Collective on dm
2734f089877aSRichard Tran Mills 
2735f089877aSRichard Tran Mills    Input Parameters:
2736bc0a1609SRichard Tran Mills +  da - the DM object
2737bc0a1609SRichard Tran Mills .  g - the original local vector
2738bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2739f089877aSRichard Tran Mills 
2740bc0a1609SRichard Tran Mills    Output Parameter:
2741bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2742f089877aSRichard Tran Mills 
2743f089877aSRichard Tran Mills    Level: intermediate
2744f089877aSRichard Tran Mills 
2745bc0a1609SRichard Tran Mills    Notes:
2746bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2747bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2748bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2749bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2750bc0a1609SRichard Tran Mills 
2751bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2752f089877aSRichard Tran Mills 
2753f089877aSRichard Tran Mills @*/
2754f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2755f089877aSRichard Tran Mills {
2756f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2757f089877aSRichard Tran Mills 
2758f089877aSRichard Tran Mills   PetscFunctionBegin;
2759f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2760bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2761f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2762f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2763f089877aSRichard Tran Mills }
2764f089877aSRichard Tran Mills 
2765f089877aSRichard Tran Mills 
276647c6ae99SBarry Smith /*@
276747c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
276847c6ae99SBarry Smith 
2769d083f849SBarry Smith     Collective on dm
277047c6ae99SBarry Smith 
277147c6ae99SBarry Smith     Input Parameter:
277247c6ae99SBarry Smith +   dm - the DM object
277391d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
277447c6ae99SBarry Smith 
277547c6ae99SBarry Smith     Output Parameter:
277647c6ae99SBarry Smith .   dmc - the coarsened DM
277747c6ae99SBarry Smith 
277847c6ae99SBarry Smith     Level: developer
277947c6ae99SBarry Smith 
2780e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
278147c6ae99SBarry Smith 
278247c6ae99SBarry Smith @*/
27837087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
278447c6ae99SBarry Smith {
278547c6ae99SBarry Smith   PetscErrorCode    ierr;
2786b17ce1afSJed Brown   DMCoarsenHookLink link;
278747c6ae99SBarry Smith 
278847c6ae99SBarry Smith   PetscFunctionBegin;
2789171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2790b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
279147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
279247c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2793b9d85ea2SLisandro Dalcin   if (*dmc) {
2794a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
279543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
27968cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2797644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
27980598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2799656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2800e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2801b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2802b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2803b17ce1afSJed Brown     }
2804b9d85ea2SLisandro Dalcin   }
280547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2806b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2807b17ce1afSJed Brown   PetscFunctionReturn(0);
2808b17ce1afSJed Brown }
2809b17ce1afSJed Brown 
2810bb9467b5SJed Brown /*@C
2811b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2812b17ce1afSJed Brown 
2813b17ce1afSJed Brown    Logically Collective
2814b17ce1afSJed Brown 
2815b17ce1afSJed Brown    Input Arguments:
2816b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2817b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2818b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28190298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2820b17ce1afSJed Brown 
2821b17ce1afSJed Brown    Calling sequence of coarsenhook:
2822b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2823b17ce1afSJed Brown 
2824b17ce1afSJed Brown +  fine - fine level DM
2825b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2826b17ce1afSJed Brown -  ctx - optional user-defined function context
2827b17ce1afSJed Brown 
2828b17ce1afSJed Brown    Calling sequence for restricthook:
2829c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2830b17ce1afSJed Brown 
2831b17ce1afSJed Brown +  fine - fine level DM
2832b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2833c833c3b5SJed Brown .  rscale - scaling vector for restriction
2834c833c3b5SJed Brown .  inject - matrix restricting by injection
2835b17ce1afSJed Brown .  coarse - coarse level DM to update
2836b17ce1afSJed Brown -  ctx - optional user-defined function context
2837b17ce1afSJed Brown 
2838b17ce1afSJed Brown    Level: advanced
2839b17ce1afSJed Brown 
2840b17ce1afSJed Brown    Notes:
2841b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2842b17ce1afSJed Brown 
2843b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2844b17ce1afSJed Brown 
2845b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2846b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2847b17ce1afSJed Brown 
2848bb9467b5SJed Brown    This function is currently not available from Fortran.
2849bb9467b5SJed Brown 
2850dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2851b17ce1afSJed Brown @*/
2852b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2853b17ce1afSJed Brown {
2854b17ce1afSJed Brown   PetscErrorCode    ierr;
2855b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2856b17ce1afSJed Brown 
2857b17ce1afSJed Brown   PetscFunctionBegin;
2858b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
28591e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
28601e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
28611e3d8eccSJed Brown   }
286295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2863b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2864b17ce1afSJed Brown   link->restricthook = restricthook;
2865b17ce1afSJed Brown   link->ctx          = ctx;
28660298fd71SBarry Smith   link->next         = NULL;
2867b17ce1afSJed Brown   *p                 = link;
2868b17ce1afSJed Brown   PetscFunctionReturn(0);
2869b17ce1afSJed Brown }
2870b17ce1afSJed Brown 
2871dc822a44SJed Brown /*@C
2872dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2873dc822a44SJed Brown 
2874dc822a44SJed Brown    Logically Collective
2875dc822a44SJed Brown 
2876dc822a44SJed Brown    Input Arguments:
2877dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2878dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2879dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2880dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2881dc822a44SJed Brown 
2882dc822a44SJed Brown    Level: advanced
2883dc822a44SJed Brown 
2884dc822a44SJed Brown    Notes:
2885dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2886dc822a44SJed Brown 
2887dc822a44SJed Brown    This function is currently not available from Fortran.
2888dc822a44SJed Brown 
2889dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2890dc822a44SJed Brown @*/
2891dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2892dc822a44SJed Brown {
2893dc822a44SJed Brown   PetscErrorCode    ierr;
2894dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2895dc822a44SJed Brown 
2896dc822a44SJed Brown   PetscFunctionBegin;
2897dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2898dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2899dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2900dc822a44SJed Brown       link = *p;
2901dc822a44SJed Brown       *p = link->next;
2902dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2903dc822a44SJed Brown       break;
2904dc822a44SJed Brown     }
2905dc822a44SJed Brown   }
2906dc822a44SJed Brown   PetscFunctionReturn(0);
2907dc822a44SJed Brown }
2908dc822a44SJed Brown 
2909dc822a44SJed Brown 
2910b17ce1afSJed Brown /*@
2911b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2912b17ce1afSJed Brown 
2913b17ce1afSJed Brown    Collective if any hooks are
2914b17ce1afSJed Brown 
2915b17ce1afSJed Brown    Input Arguments:
2916b17ce1afSJed Brown +  fine - finer DM to use as a base
2917b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2918e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2919b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2920e91eccc2SStefano Zampini -  coarse - coarser DM to update
2921b17ce1afSJed Brown 
2922b17ce1afSJed Brown    Level: developer
2923b17ce1afSJed Brown 
2924b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2925b17ce1afSJed Brown @*/
2926b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2927b17ce1afSJed Brown {
2928b17ce1afSJed Brown   PetscErrorCode    ierr;
2929b17ce1afSJed Brown   DMCoarsenHookLink link;
2930b17ce1afSJed Brown 
2931b17ce1afSJed Brown   PetscFunctionBegin;
2932b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29338865f1eaSKarl Rupp     if (link->restricthook) {
29348865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29358865f1eaSKarl Rupp     }
2936b17ce1afSJed Brown   }
293747c6ae99SBarry Smith   PetscFunctionReturn(0);
293847c6ae99SBarry Smith }
293947c6ae99SBarry Smith 
2940bb9467b5SJed Brown /*@C
2941be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
29425dbd56e3SPeter Brune 
2943d083f849SBarry Smith    Logically Collective on global
29445dbd56e3SPeter Brune 
29455dbd56e3SPeter Brune    Input Arguments:
29465dbd56e3SPeter Brune +  global - global DM
2947ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
29485dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
29490298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
29505dbd56e3SPeter Brune 
2951ec4806b8SPeter Brune 
2952ec4806b8SPeter Brune    Calling sequence for ddhook:
2953ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2954ec4806b8SPeter Brune 
2955ec4806b8SPeter Brune +  global - global DM
2956ec4806b8SPeter Brune .  block  - block DM
2957ec4806b8SPeter Brune -  ctx - optional user-defined function context
2958ec4806b8SPeter Brune 
29595dbd56e3SPeter Brune    Calling sequence for restricthook:
2960ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
29615dbd56e3SPeter Brune 
29625dbd56e3SPeter Brune +  global - global DM
29635dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
29645dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2965ec4806b8SPeter Brune .  block  - block DM
29665dbd56e3SPeter Brune -  ctx - optional user-defined function context
29675dbd56e3SPeter Brune 
29685dbd56e3SPeter Brune    Level: advanced
29695dbd56e3SPeter Brune 
29705dbd56e3SPeter Brune    Notes:
2971ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
29725dbd56e3SPeter Brune 
29735dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
29745dbd56e3SPeter Brune 
29755dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2976ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
29775dbd56e3SPeter Brune 
2978bb9467b5SJed Brown    This function is currently not available from Fortran.
2979bb9467b5SJed Brown 
29805dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
29815dbd56e3SPeter Brune @*/
2982be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
29835dbd56e3SPeter Brune {
29845dbd56e3SPeter Brune   PetscErrorCode      ierr;
2985be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
29865dbd56e3SPeter Brune 
29875dbd56e3SPeter Brune   PetscFunctionBegin;
29885dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
2989b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
2990b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
2991b3a6b972SJed Brown   }
299295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
29935dbd56e3SPeter Brune   link->restricthook = restricthook;
2994be081cd6SPeter Brune   link->ddhook       = ddhook;
29955dbd56e3SPeter Brune   link->ctx          = ctx;
29960298fd71SBarry Smith   link->next         = NULL;
29975dbd56e3SPeter Brune   *p                 = link;
29985dbd56e3SPeter Brune   PetscFunctionReturn(0);
29995dbd56e3SPeter Brune }
30005dbd56e3SPeter Brune 
3001b3a6b972SJed Brown /*@C
3002b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3003b3a6b972SJed Brown 
3004b3a6b972SJed Brown    Logically Collective
3005b3a6b972SJed Brown 
3006b3a6b972SJed Brown    Input Arguments:
3007b3a6b972SJed Brown +  global - global DM
3008b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3009b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3010b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3011b3a6b972SJed Brown 
3012b3a6b972SJed Brown    Level: advanced
3013b3a6b972SJed Brown 
3014b3a6b972SJed Brown    Notes:
3015b3a6b972SJed Brown 
3016b3a6b972SJed Brown    This function is currently not available from Fortran.
3017b3a6b972SJed Brown 
3018b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3019b3a6b972SJed Brown @*/
3020b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3021b3a6b972SJed Brown {
3022b3a6b972SJed Brown   PetscErrorCode      ierr;
3023b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3024b3a6b972SJed Brown 
3025b3a6b972SJed Brown   PetscFunctionBegin;
3026b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3027b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3028b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3029b3a6b972SJed Brown       link = *p;
3030b3a6b972SJed Brown       *p = link->next;
3031b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3032b3a6b972SJed Brown       break;
3033b3a6b972SJed Brown     }
3034b3a6b972SJed Brown   }
3035b3a6b972SJed Brown   PetscFunctionReturn(0);
3036b3a6b972SJed Brown }
3037b3a6b972SJed Brown 
30385dbd56e3SPeter Brune /*@
3039be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30405dbd56e3SPeter Brune 
30415dbd56e3SPeter Brune    Collective if any hooks are
30425dbd56e3SPeter Brune 
30435dbd56e3SPeter Brune    Input Arguments:
30445dbd56e3SPeter Brune +  fine - finer DM to use as a base
3045be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3046be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
30475dbd56e3SPeter Brune -  coarse - coarer DM to update
30485dbd56e3SPeter Brune 
30495dbd56e3SPeter Brune    Level: developer
30505dbd56e3SPeter Brune 
30515dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
30525dbd56e3SPeter Brune @*/
3053be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
30545dbd56e3SPeter Brune {
30555dbd56e3SPeter Brune   PetscErrorCode      ierr;
3056be081cd6SPeter Brune   DMSubDomainHookLink link;
30575dbd56e3SPeter Brune 
30585dbd56e3SPeter Brune   PetscFunctionBegin;
3059be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
30608865f1eaSKarl Rupp     if (link->restricthook) {
30618865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
30628865f1eaSKarl Rupp     }
30635dbd56e3SPeter Brune   }
30645dbd56e3SPeter Brune   PetscFunctionReturn(0);
30655dbd56e3SPeter Brune }
30665dbd56e3SPeter Brune 
30675fe1f584SPeter Brune /*@
30686a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
30695fe1f584SPeter Brune 
30705fe1f584SPeter Brune     Not Collective
30715fe1f584SPeter Brune 
30725fe1f584SPeter Brune     Input Parameter:
30735fe1f584SPeter Brune .   dm - the DM object
30745fe1f584SPeter Brune 
30755fe1f584SPeter Brune     Output Parameter:
30766a7d9d85SPeter Brune .   level - number of coarsenings
30775fe1f584SPeter Brune 
30785fe1f584SPeter Brune     Level: developer
30795fe1f584SPeter Brune 
30806a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
30815fe1f584SPeter Brune 
30825fe1f584SPeter Brune @*/
30835fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
30845fe1f584SPeter Brune {
30855fe1f584SPeter Brune   PetscFunctionBegin;
30865fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3087b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
30885fe1f584SPeter Brune   *level = dm->leveldown;
30895fe1f584SPeter Brune   PetscFunctionReturn(0);
30905fe1f584SPeter Brune }
30915fe1f584SPeter Brune 
30929a64c4a8SMatthew G. Knepley /*@
30939a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
30949a64c4a8SMatthew G. Knepley 
30959a64c4a8SMatthew G. Knepley     Not Collective
30969a64c4a8SMatthew G. Knepley 
30979a64c4a8SMatthew G. Knepley     Input Parameters:
30989a64c4a8SMatthew G. Knepley +   dm - the DM object
30999a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31009a64c4a8SMatthew G. Knepley 
31019a64c4a8SMatthew G. Knepley     Level: developer
31029a64c4a8SMatthew G. Knepley 
31039a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31049a64c4a8SMatthew G. Knepley @*/
31059a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31069a64c4a8SMatthew G. Knepley {
31079a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31089a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31099a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31109a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31119a64c4a8SMatthew G. Knepley }
31129a64c4a8SMatthew G. Knepley 
31135fe1f584SPeter Brune 
31145fe1f584SPeter Brune 
311547c6ae99SBarry Smith /*@C
311647c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
311747c6ae99SBarry Smith 
3118d083f849SBarry Smith     Collective on dm
311947c6ae99SBarry Smith 
312047c6ae99SBarry Smith     Input Parameter:
312147c6ae99SBarry Smith +   dm - the DM object
312247c6ae99SBarry Smith -   nlevels - the number of levels of refinement
312347c6ae99SBarry Smith 
312447c6ae99SBarry Smith     Output Parameter:
312547c6ae99SBarry Smith .   dmf - the refined DM hierarchy
312647c6ae99SBarry Smith 
312747c6ae99SBarry Smith     Level: developer
312847c6ae99SBarry Smith 
3129e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
313047c6ae99SBarry Smith 
313147c6ae99SBarry Smith @*/
31327087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
313347c6ae99SBarry Smith {
313447c6ae99SBarry Smith   PetscErrorCode ierr;
313547c6ae99SBarry Smith 
313647c6ae99SBarry Smith   PetscFunctionBegin;
3137171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3138ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
313947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3140b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
314147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
314247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
314347c6ae99SBarry Smith   } else if (dm->ops->refine) {
314447c6ae99SBarry Smith     PetscInt i;
314547c6ae99SBarry Smith 
3146ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
314747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3148ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
314947c6ae99SBarry Smith     }
3150ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
315147c6ae99SBarry Smith   PetscFunctionReturn(0);
315247c6ae99SBarry Smith }
315347c6ae99SBarry Smith 
315447c6ae99SBarry Smith /*@C
315547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
315647c6ae99SBarry Smith 
3157d083f849SBarry Smith     Collective on dm
315847c6ae99SBarry Smith 
315947c6ae99SBarry Smith     Input Parameter:
316047c6ae99SBarry Smith +   dm - the DM object
316147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
316247c6ae99SBarry Smith 
316347c6ae99SBarry Smith     Output Parameter:
316447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
316547c6ae99SBarry Smith 
316647c6ae99SBarry Smith     Level: developer
316747c6ae99SBarry Smith 
3168e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
316947c6ae99SBarry Smith 
317047c6ae99SBarry Smith @*/
31717087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
317247c6ae99SBarry Smith {
317347c6ae99SBarry Smith   PetscErrorCode ierr;
317447c6ae99SBarry Smith 
317547c6ae99SBarry Smith   PetscFunctionBegin;
3176171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3177ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
317847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
317947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
318047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
318147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
318247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
318347c6ae99SBarry Smith     PetscInt i;
318447c6ae99SBarry Smith 
3185ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
318647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3187ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
318847c6ae99SBarry Smith     }
3189ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
319047c6ae99SBarry Smith   PetscFunctionReturn(0);
319147c6ae99SBarry Smith }
319247c6ae99SBarry Smith 
31931a266240SBarry Smith /*@C
31941a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
31951a266240SBarry Smith 
31961a266240SBarry Smith     Not Collective
31971a266240SBarry Smith 
31981a266240SBarry Smith     Input Parameters:
31991a266240SBarry Smith +   dm - the DM object
32001a266240SBarry Smith -   destroy - the destroy function
32011a266240SBarry Smith 
32021a266240SBarry Smith     Level: intermediate
32031a266240SBarry Smith 
3204e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32051a266240SBarry Smith 
3206f07f9ceaSJed Brown @*/
32071a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32081a266240SBarry Smith {
32091a266240SBarry Smith   PetscFunctionBegin;
3210171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32111a266240SBarry Smith   dm->ctxdestroy = destroy;
32121a266240SBarry Smith   PetscFunctionReturn(0);
32131a266240SBarry Smith }
32141a266240SBarry Smith 
3215b07ff414SBarry Smith /*@
32161b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
321747c6ae99SBarry Smith 
321847c6ae99SBarry Smith     Not Collective
321947c6ae99SBarry Smith 
322047c6ae99SBarry Smith     Input Parameters:
322147c6ae99SBarry Smith +   dm - the DM object
322247c6ae99SBarry Smith -   ctx - the user context
322347c6ae99SBarry Smith 
322447c6ae99SBarry Smith     Level: intermediate
322547c6ae99SBarry Smith 
3226e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
322747c6ae99SBarry Smith 
322847c6ae99SBarry Smith @*/
32291b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
323047c6ae99SBarry Smith {
323147c6ae99SBarry Smith   PetscFunctionBegin;
3232171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
323347c6ae99SBarry Smith   dm->ctx = ctx;
323447c6ae99SBarry Smith   PetscFunctionReturn(0);
323547c6ae99SBarry Smith }
323647c6ae99SBarry Smith 
323747c6ae99SBarry Smith /*@
32381b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
323947c6ae99SBarry Smith 
324047c6ae99SBarry Smith     Not Collective
324147c6ae99SBarry Smith 
324247c6ae99SBarry Smith     Input Parameter:
324347c6ae99SBarry Smith .   dm - the DM object
324447c6ae99SBarry Smith 
324547c6ae99SBarry Smith     Output Parameter:
324647c6ae99SBarry Smith .   ctx - the user context
324747c6ae99SBarry Smith 
324847c6ae99SBarry Smith     Level: intermediate
324947c6ae99SBarry Smith 
3250e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
325147c6ae99SBarry Smith 
325247c6ae99SBarry Smith @*/
32531b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
325447c6ae99SBarry Smith {
325547c6ae99SBarry Smith   PetscFunctionBegin;
3256171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32571b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
325847c6ae99SBarry Smith   PetscFunctionReturn(0);
325947c6ae99SBarry Smith }
326047c6ae99SBarry Smith 
326108da532bSDmitry Karpeev /*@C
3262df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
326308da532bSDmitry Karpeev 
3264d083f849SBarry Smith     Logically Collective on dm
326508da532bSDmitry Karpeev 
326608da532bSDmitry Karpeev     Input Parameter:
326708da532bSDmitry Karpeev +   dm - the DM object
32680298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
326908da532bSDmitry Karpeev 
327008da532bSDmitry Karpeev     Level: intermediate
327108da532bSDmitry Karpeev 
3272835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
327308da532bSDmitry Karpeev          DMSetJacobian()
327408da532bSDmitry Karpeev 
327508da532bSDmitry Karpeev @*/
327608da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
327708da532bSDmitry Karpeev {
327808da532bSDmitry Karpeev   PetscFunctionBegin;
32795a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
328008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
328108da532bSDmitry Karpeev   PetscFunctionReturn(0);
328208da532bSDmitry Karpeev }
328308da532bSDmitry Karpeev 
328408da532bSDmitry Karpeev /*@
328508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
328608da532bSDmitry Karpeev 
328708da532bSDmitry Karpeev     Not Collective
328808da532bSDmitry Karpeev 
328908da532bSDmitry Karpeev     Input Parameter:
329008da532bSDmitry Karpeev .   dm - the DM object to destroy
329108da532bSDmitry Karpeev 
329208da532bSDmitry Karpeev     Output Parameter:
329308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
329408da532bSDmitry Karpeev 
329508da532bSDmitry Karpeev     Level: developer
329608da532bSDmitry Karpeev 
329774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
329808da532bSDmitry Karpeev 
329908da532bSDmitry Karpeev @*/
330008da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
330108da532bSDmitry Karpeev {
330208da532bSDmitry Karpeev   PetscFunctionBegin;
33035a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3304534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
330508da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
330608da532bSDmitry Karpeev   PetscFunctionReturn(0);
330708da532bSDmitry Karpeev }
330808da532bSDmitry Karpeev 
330908da532bSDmitry Karpeev /*@C
331008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
331108da532bSDmitry Karpeev 
3312d083f849SBarry Smith     Logically Collective on dm
331308da532bSDmitry Karpeev 
331408da532bSDmitry Karpeev     Input Parameters:
3315907376e6SBarry Smith .   dm - the DM object
331608da532bSDmitry Karpeev 
331708da532bSDmitry Karpeev     Output parameters:
331808da532bSDmitry Karpeev +   xl - lower bound
331908da532bSDmitry Karpeev -   xu - upper bound
332008da532bSDmitry Karpeev 
3321907376e6SBarry Smith     Level: advanced
3322907376e6SBarry Smith 
332395452b02SPatrick Sanan     Notes:
332495452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
332508da532bSDmitry Karpeev 
332674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
332708da532bSDmitry Karpeev 
332808da532bSDmitry Karpeev @*/
332908da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
333008da532bSDmitry Karpeev {
333108da532bSDmitry Karpeev   PetscErrorCode ierr;
33325fd66863SKarl Rupp 
333308da532bSDmitry Karpeev   PetscFunctionBegin;
33345a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
333508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
33365a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3337b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
333808da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
333908da532bSDmitry Karpeev   PetscFunctionReturn(0);
334008da532bSDmitry Karpeev }
334108da532bSDmitry Karpeev 
3342b0ae01b7SPeter Brune /*@
3343b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3344b0ae01b7SPeter Brune 
3345b0ae01b7SPeter Brune     Not Collective
3346b0ae01b7SPeter Brune 
3347b0ae01b7SPeter Brune     Input Parameter:
3348b0ae01b7SPeter Brune .   dm - the DM object
3349b0ae01b7SPeter Brune 
3350b0ae01b7SPeter Brune     Output Parameter:
3351b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3352b0ae01b7SPeter Brune 
3353b0ae01b7SPeter Brune     Level: developer
3354b0ae01b7SPeter Brune 
33551565f0a7SPatrick Sanan .seealso DMCreateColoring()
3356b0ae01b7SPeter Brune 
3357b0ae01b7SPeter Brune @*/
3358b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3359b0ae01b7SPeter Brune {
3360b0ae01b7SPeter Brune   PetscFunctionBegin;
33615a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3362534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3363b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3364b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3365b0ae01b7SPeter Brune }
3366b0ae01b7SPeter Brune 
33673ad4599aSBarry Smith /*@
33683ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
33693ad4599aSBarry Smith 
33703ad4599aSBarry Smith     Not Collective
33713ad4599aSBarry Smith 
33723ad4599aSBarry Smith     Input Parameter:
33733ad4599aSBarry Smith .   dm - the DM object
33743ad4599aSBarry Smith 
33753ad4599aSBarry Smith     Output Parameter:
33763ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
33773ad4599aSBarry Smith 
33783ad4599aSBarry Smith     Level: developer
33793ad4599aSBarry Smith 
33801565f0a7SPatrick Sanan .seealso DMCreateRestriction()
33813ad4599aSBarry Smith 
33823ad4599aSBarry Smith @*/
33833ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
33843ad4599aSBarry Smith {
33853ad4599aSBarry Smith   PetscFunctionBegin;
33865a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3387534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
33883ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
33893ad4599aSBarry Smith   PetscFunctionReturn(0);
33903ad4599aSBarry Smith }
33913ad4599aSBarry Smith 
3392a7058e45SLawrence Mitchell 
3393a7058e45SLawrence Mitchell /*@
3394a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3395a7058e45SLawrence Mitchell 
3396a7058e45SLawrence Mitchell     Not Collective
3397a7058e45SLawrence Mitchell 
3398a7058e45SLawrence Mitchell     Input Parameter:
3399a7058e45SLawrence Mitchell .   dm - the DM object
3400a7058e45SLawrence Mitchell 
3401a7058e45SLawrence Mitchell     Output Parameter:
3402a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3403a7058e45SLawrence Mitchell 
3404a7058e45SLawrence Mitchell     Level: developer
3405a7058e45SLawrence Mitchell 
34061565f0a7SPatrick Sanan .seealso DMCreateInjection()
3407a7058e45SLawrence Mitchell 
3408a7058e45SLawrence Mitchell @*/
3409a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3410a7058e45SLawrence Mitchell {
34114a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
34125a84ad33SLisandro Dalcin 
3413a7058e45SLawrence Mitchell   PetscFunctionBegin;
34145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3415534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34165a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
34174a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
34185a84ad33SLisandro Dalcin   } else {
34195a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
34205a84ad33SLisandro Dalcin   }
3421a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3422a7058e45SLawrence Mitchell }
3423a7058e45SLawrence Mitchell 
34245a84ad33SLisandro Dalcin 
3425748fac09SDmitry Karpeev /*@C
342608da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
342708da532bSDmitry Karpeev 
3428d083f849SBarry Smith     Collective on dm
342908da532bSDmitry Karpeev 
343008da532bSDmitry Karpeev     Input Parameter:
343108da532bSDmitry Karpeev +   dm - the DM object
34320298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
343308da532bSDmitry Karpeev 
343408da532bSDmitry Karpeev     Level: developer
343508da532bSDmitry Karpeev 
343674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
343708da532bSDmitry Karpeev 
343808da532bSDmitry Karpeev @*/
343908da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
344008da532bSDmitry Karpeev {
344108da532bSDmitry Karpeev   PetscErrorCode ierr;
34425fd66863SKarl Rupp 
344308da532bSDmitry Karpeev   PetscFunctionBegin;
3444b9d85ea2SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
344508da532bSDmitry Karpeev   if (x) {
344608da532bSDmitry Karpeev     if (!dm->x) {
344708da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
344808da532bSDmitry Karpeev     }
344908da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
34508865f1eaSKarl Rupp   } else if (dm->x) {
345108da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
345208da532bSDmitry Karpeev   }
345308da532bSDmitry Karpeev   PetscFunctionReturn(0);
345408da532bSDmitry Karpeev }
345508da532bSDmitry Karpeev 
34560298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3457264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3458264ace61SBarry Smith 
3459264ace61SBarry Smith /*@C
3460264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3461264ace61SBarry Smith 
3462d083f849SBarry Smith   Collective on dm
3463264ace61SBarry Smith 
3464264ace61SBarry Smith   Input Parameters:
3465264ace61SBarry Smith + dm     - The DM object
3466264ace61SBarry Smith - method - The name of the DM type
3467264ace61SBarry Smith 
3468264ace61SBarry Smith   Options Database Key:
3469264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3470264ace61SBarry Smith 
3471264ace61SBarry Smith   Notes:
3472e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3473264ace61SBarry Smith 
3474264ace61SBarry Smith   Level: intermediate
3475264ace61SBarry Smith 
3476264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3477264ace61SBarry Smith @*/
347819fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3479264ace61SBarry Smith {
3480264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3481264ace61SBarry Smith   PetscBool      match;
3482264ace61SBarry Smith   PetscErrorCode ierr;
3483264ace61SBarry Smith 
3484264ace61SBarry Smith   PetscFunctionBegin;
3485264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3486251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3487264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3488264ace61SBarry Smith 
34890f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
34901c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3491ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3492264ace61SBarry Smith 
3493264ace61SBarry Smith   if (dm->ops->destroy) {
3494264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3495264ace61SBarry Smith   }
3496d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3497264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3498d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3499264ace61SBarry Smith   PetscFunctionReturn(0);
3500264ace61SBarry Smith }
3501264ace61SBarry Smith 
3502264ace61SBarry Smith /*@C
3503264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3504264ace61SBarry Smith 
3505264ace61SBarry Smith   Not Collective
3506264ace61SBarry Smith 
3507264ace61SBarry Smith   Input Parameter:
3508264ace61SBarry Smith . dm  - The DM
3509264ace61SBarry Smith 
3510264ace61SBarry Smith   Output Parameter:
3511264ace61SBarry Smith . type - The DM type name
3512264ace61SBarry Smith 
3513264ace61SBarry Smith   Level: intermediate
3514264ace61SBarry Smith 
3515264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3516264ace61SBarry Smith @*/
351719fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3518264ace61SBarry Smith {
3519264ace61SBarry Smith   PetscErrorCode ierr;
3520264ace61SBarry Smith 
3521264ace61SBarry Smith   PetscFunctionBegin;
3522264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3523c959eef4SJed Brown   PetscValidPointer(type,2);
3524607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3525264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3526264ace61SBarry Smith   PetscFunctionReturn(0);
3527264ace61SBarry Smith }
3528264ace61SBarry Smith 
352967a56275SMatthew G Knepley /*@C
353067a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
353167a56275SMatthew G Knepley 
3532d083f849SBarry Smith   Collective on dm
353367a56275SMatthew G Knepley 
353467a56275SMatthew G Knepley   Input Parameters:
353567a56275SMatthew G Knepley + dm - the DM
353667a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
353767a56275SMatthew G Knepley 
353867a56275SMatthew G Knepley   Output Parameter:
353967a56275SMatthew G Knepley . M - pointer to new DM
354067a56275SMatthew G Knepley 
354167a56275SMatthew G Knepley   Notes:
354267a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
354367a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
354467a56275SMatthew G Knepley   of the input DM.
354567a56275SMatthew G Knepley 
354667a56275SMatthew G Knepley   Level: intermediate
354767a56275SMatthew G Knepley 
354867a56275SMatthew G Knepley .seealso: DMCreate()
354967a56275SMatthew G Knepley @*/
355019fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
355167a56275SMatthew G Knepley {
355267a56275SMatthew G Knepley   DM             B;
355367a56275SMatthew G Knepley   char           convname[256];
3554c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
355567a56275SMatthew G Knepley   PetscErrorCode ierr;
355667a56275SMatthew G Knepley 
355767a56275SMatthew G Knepley   PetscFunctionBegin;
355867a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
355967a56275SMatthew G Knepley   PetscValidType(dm,1);
356067a56275SMatthew G Knepley   PetscValidPointer(M,3);
3561251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3562c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3563c067b6caSMatthew G. Knepley   if (sametype) {
3564c067b6caSMatthew G. Knepley     *M   = dm;
3565c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3566c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3567c067b6caSMatthew G. Knepley   } else {
35680298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
356967a56275SMatthew G Knepley 
357067a56275SMatthew G Knepley     /*
357167a56275SMatthew G Knepley        Order of precedence:
357267a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
357367a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
357467a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
357567a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
357667a56275SMatthew G Knepley        5) Use a really basic converter.
357767a56275SMatthew G Knepley     */
357867a56275SMatthew G Knepley 
357967a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3580a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3581a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3582a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3583a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3584a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
35850005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
358667a56275SMatthew G Knepley     if (conv) goto foundconv;
358767a56275SMatthew G Knepley 
358867a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
358982f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
359067a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3591a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3592a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3593a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3594a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3595a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
35960005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
359767a56275SMatthew G Knepley     if (conv) {
3598fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
359967a56275SMatthew G Knepley       goto foundconv;
360067a56275SMatthew G Knepley     }
360167a56275SMatthew G Knepley 
360267a56275SMatthew G Knepley #if 0
360367a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
360467a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3605fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
360667a56275SMatthew G Knepley     if (conv) goto foundconv;
360767a56275SMatthew G Knepley 
360867a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
360967a56275SMatthew G Knepley     if (dm->ops->convert) {
361067a56275SMatthew G Knepley       conv = dm->ops->convert;
361167a56275SMatthew G Knepley     }
361267a56275SMatthew G Knepley     if (conv) goto foundconv;
361367a56275SMatthew G Knepley #endif
361467a56275SMatthew G Knepley 
361567a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
361682f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
361767a56275SMatthew G Knepley 
361867a56275SMatthew G Knepley foundconv:
361967a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
362067a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
362112fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
362290b157c4SStefano Zampini     {
362390b157c4SStefano Zampini       PetscBool             isper;
362412fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
362512fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
362690b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
362790b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
362812fa691eSMatthew G. Knepley     }
362967a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
363067a56275SMatthew G Knepley   }
363167a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
363267a56275SMatthew G Knepley   PetscFunctionReturn(0);
363367a56275SMatthew G Knepley }
3634264ace61SBarry Smith 
3635264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3636264ace61SBarry Smith 
3637264ace61SBarry Smith /*@C
36381c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36391c84c290SBarry Smith 
36401c84c290SBarry Smith   Not Collective
36411c84c290SBarry Smith 
36421c84c290SBarry Smith   Input Parameters:
36431c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36441c84c290SBarry Smith - create_func - The creation routine itself
36451c84c290SBarry Smith 
36461c84c290SBarry Smith   Notes:
36471c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36481c84c290SBarry Smith 
36491c84c290SBarry Smith 
36501c84c290SBarry Smith   Sample usage:
36511c84c290SBarry Smith .vb
3652bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36531c84c290SBarry Smith .ve
36541c84c290SBarry Smith 
36551c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36561c84c290SBarry Smith .vb
36571c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
36581c84c290SBarry Smith     DMSetType(DM,"my_da");
36591c84c290SBarry Smith .ve
36601c84c290SBarry Smith    or at runtime via the option
36611c84c290SBarry Smith .vb
36621c84c290SBarry Smith     -da_type my_da
36631c84c290SBarry Smith .ve
3664264ace61SBarry Smith 
3665264ace61SBarry Smith   Level: advanced
36661c84c290SBarry Smith 
3667bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
36681c84c290SBarry Smith 
3669264ace61SBarry Smith @*/
3670bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3671264ace61SBarry Smith {
3672264ace61SBarry Smith   PetscErrorCode ierr;
3673264ace61SBarry Smith 
3674264ace61SBarry Smith   PetscFunctionBegin;
36751d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3676a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3677264ace61SBarry Smith   PetscFunctionReturn(0);
3678264ace61SBarry Smith }
3679264ace61SBarry Smith 
3680b859378eSBarry Smith /*@C
368155849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3682b859378eSBarry Smith 
3683d083f849SBarry Smith   Collective on viewer
3684b859378eSBarry Smith 
3685b859378eSBarry Smith   Input Parameters:
3686b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3687b859378eSBarry Smith            some related function before a call to DMLoad().
3688b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3689b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3690b859378eSBarry Smith 
3691b859378eSBarry Smith    Level: intermediate
3692b859378eSBarry Smith 
3693b859378eSBarry Smith   Notes:
369455849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3695b859378eSBarry Smith 
3696b859378eSBarry Smith   Notes for advanced users:
3697b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3698b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3699b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3700b859378eSBarry Smith   format is
3701b859378eSBarry Smith .vb
3702b859378eSBarry Smith      has not yet been determined
3703b859378eSBarry Smith .ve
3704b859378eSBarry Smith 
3705b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3706b859378eSBarry Smith @*/
3707b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3708b859378eSBarry Smith {
37099331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3710b859378eSBarry Smith   PetscErrorCode ierr;
3711b859378eSBarry Smith 
3712b859378eSBarry Smith   PetscFunctionBegin;
3713b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3714b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3715fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
371632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37179331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
37189331c7a4SMatthew G. Knepley   if (isbinary) {
37199331c7a4SMatthew G. Knepley     PetscInt classid;
37209331c7a4SMatthew G. Knepley     char     type[256];
3721b859378eSBarry Smith 
3722060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37239200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3724060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
372532c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37269331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37279331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37289331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37299331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3730b859378eSBarry Smith   PetscFunctionReturn(0);
3731b859378eSBarry Smith }
3732b859378eSBarry Smith 
3733b2e4378dSMatthew G. Knepley /*@
3734b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3735b2e4378dSMatthew G. Knepley 
3736b2e4378dSMatthew G. Knepley   Not collective
3737b2e4378dSMatthew G. Knepley 
3738b2e4378dSMatthew G. Knepley   Input Parameter:
3739b2e4378dSMatthew G. Knepley . dm - the DM
3740b2e4378dSMatthew G. Knepley 
3741b2e4378dSMatthew G. Knepley   Output Parameters:
3742b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3743b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3744b2e4378dSMatthew G. Knepley 
3745b2e4378dSMatthew G. Knepley   Level: beginner
3746b2e4378dSMatthew G. Knepley 
3747b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3748b2e4378dSMatthew G. Knepley 
3749b2e4378dSMatthew G. Knepley 
3750b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3751b2e4378dSMatthew G. Knepley @*/
3752b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3753b2e4378dSMatthew G. Knepley {
3754b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3755b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3756b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3757b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3758b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3759b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3760b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3761b2e4378dSMatthew G. Knepley 
3762b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3763b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3764b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3765b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3766b2e4378dSMatthew G. Knepley   if (coords) {
3767b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3768b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3769b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3770b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3771b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3772b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3773b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3774b2e4378dSMatthew G. Knepley       }
3775b2e4378dSMatthew G. Knepley     }
3776b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3777b2e4378dSMatthew G. Knepley   } else {
3778b2e4378dSMatthew G. Knepley     PetscBool isda;
3779b2e4378dSMatthew G. Knepley 
3780b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3781b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3782b2e4378dSMatthew G. Knepley   }
3783b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3784b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3785b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3786b2e4378dSMatthew G. Knepley }
3787b2e4378dSMatthew G. Knepley 
3788b2e4378dSMatthew G. Knepley /*@
3789b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3790b2e4378dSMatthew G. Knepley 
3791b2e4378dSMatthew G. Knepley   Collective
3792b2e4378dSMatthew G. Knepley 
3793b2e4378dSMatthew G. Knepley   Input Parameter:
3794b2e4378dSMatthew G. Knepley . dm - the DM
3795b2e4378dSMatthew G. Knepley 
3796b2e4378dSMatthew G. Knepley   Output Parameters:
3797b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3798b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3799b2e4378dSMatthew G. Knepley 
3800b2e4378dSMatthew G. Knepley   Level: beginner
3801b2e4378dSMatthew G. Knepley 
3802b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3803b2e4378dSMatthew G. Knepley @*/
3804b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3805b2e4378dSMatthew G. Knepley {
3806b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38076ce308c4SMatthew G. Knepley   PetscInt       cdim;
38086ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3809b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3810b2e4378dSMatthew G. Knepley 
3811b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3812b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3813b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3814b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3815b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3816b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3817b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3818b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3819b2e4378dSMatthew G. Knepley }
3820b2e4378dSMatthew G. Knepley 
38217da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38227da65231SMatthew G Knepley 
3823a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3824a6dfd86eSKarl Rupp {
38251d47ebbbSSatish Balay   PetscInt       f;
38261b30c384SMatthew G Knepley   PetscErrorCode ierr;
38271b30c384SMatthew G Knepley 
38287da65231SMatthew G Knepley   PetscFunctionBegin;
382974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38301d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
383157622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
38327da65231SMatthew G Knepley   }
38337da65231SMatthew G Knepley   PetscFunctionReturn(0);
38347da65231SMatthew G Knepley }
38357da65231SMatthew G Knepley 
3836a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3837a6dfd86eSKarl Rupp {
38381b30c384SMatthew G Knepley   PetscInt       f, g;
38397da65231SMatthew G Knepley   PetscErrorCode ierr;
38407da65231SMatthew G Knepley 
38417da65231SMatthew G Knepley   PetscFunctionBegin;
384274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38431d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
384474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
38451d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3846e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
38477da65231SMatthew G Knepley     }
384874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
38497da65231SMatthew G Knepley   }
38507da65231SMatthew G Knepley   PetscFunctionReturn(0);
38517da65231SMatthew G Knepley }
3852e7c4fc90SDmitry Karpeev 
38536113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3854e759306cSMatthew G. Knepley {
38550c5b8624SToby Isaac   PetscInt          localSize, bs;
38560c5b8624SToby Isaac   PetscMPIInt       size;
38570c5b8624SToby Isaac   Vec               x, xglob;
38580c5b8624SToby Isaac   const PetscScalar *xarray;
3859e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3860e759306cSMatthew G. Knepley 
3861e759306cSMatthew G. Knepley   PetscFunctionBegin;
38629852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3863e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3864e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
38656113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
38660c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
38670c5b8624SToby Isaac   if (size > 1) {
38680c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
38690c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
38700c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
38710c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
38720c5b8624SToby Isaac   } else {
38730c5b8624SToby Isaac     xglob = x;
38740c5b8624SToby Isaac   }
38750c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
38760c5b8624SToby Isaac   if (size > 1) {
38770c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
38780c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
38790c5b8624SToby Isaac   }
3880e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3881e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3882e759306cSMatthew G. Knepley }
3883e759306cSMatthew G. Knepley 
388488ed4aceSMatthew G Knepley /*@
38851bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3886061576a5SJed Brown 
3887061576a5SJed Brown   Input Parameter:
3888061576a5SJed Brown . dm - The DM
3889061576a5SJed Brown 
3890061576a5SJed Brown   Output Parameter:
3891061576a5SJed Brown . section - The PetscSection
3892061576a5SJed Brown 
3893061576a5SJed Brown   Options Database Keys:
3894061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3895061576a5SJed Brown 
3896061576a5SJed Brown   Level: advanced
3897061576a5SJed Brown 
3898061576a5SJed Brown   Notes:
3899061576a5SJed Brown   Use DMGetLocalSection() in new code.
3900061576a5SJed Brown 
3901061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3902061576a5SJed Brown 
3903061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3904061576a5SJed Brown @*/
3905061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3906061576a5SJed Brown {
3907061576a5SJed Brown   PetscErrorCode ierr;
3908061576a5SJed Brown 
3909061576a5SJed Brown   PetscFunctionBegin;
3910061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3911061576a5SJed Brown   PetscFunctionReturn(0);
3912061576a5SJed Brown }
3913061576a5SJed Brown 
3914061576a5SJed Brown /*@
3915061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
391688ed4aceSMatthew G Knepley 
391788ed4aceSMatthew G Knepley   Input Parameter:
391888ed4aceSMatthew G Knepley . dm - The DM
391988ed4aceSMatthew G Knepley 
392088ed4aceSMatthew G Knepley   Output Parameter:
392188ed4aceSMatthew G Knepley . section - The PetscSection
392288ed4aceSMatthew G Knepley 
3923e5893cccSMatthew G. Knepley   Options Database Keys:
3924e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3925e5893cccSMatthew G. Knepley 
392688ed4aceSMatthew G Knepley   Level: intermediate
392788ed4aceSMatthew G Knepley 
392888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
392988ed4aceSMatthew G Knepley 
3930061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
393188ed4aceSMatthew G Knepley @*/
3932061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
39330adebc6cSBarry Smith {
3934fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3935fd59a867SMatthew G. Knepley 
393688ed4aceSMatthew G Knepley   PetscFunctionBegin;
393788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
393888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
39391bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
3940e5e52638SMatthew G. Knepley     PetscInt d;
3941e5e52638SMatthew G. Knepley 
3942e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
39431bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
39441bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
39452f0f8703SMatthew G. Knepley   }
39461bb6d2a8SBarry Smith   *section = dm->localSection;
394788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
394888ed4aceSMatthew G Knepley }
394988ed4aceSMatthew G Knepley 
395088ed4aceSMatthew G Knepley /*@
39511bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
3952061576a5SJed Brown 
3953061576a5SJed Brown   Input Parameters:
3954061576a5SJed Brown + dm - The DM
3955061576a5SJed Brown - section - The PetscSection
3956061576a5SJed Brown 
3957061576a5SJed Brown   Level: advanced
3958061576a5SJed Brown 
3959061576a5SJed Brown   Notes:
3960061576a5SJed Brown   Use DMSetLocalSection() in new code.
3961061576a5SJed Brown 
3962061576a5SJed Brown   Any existing Section will be destroyed
3963061576a5SJed Brown 
3964061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
3965061576a5SJed Brown @*/
3966061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
3967061576a5SJed Brown {
3968061576a5SJed Brown   PetscErrorCode ierr;
3969061576a5SJed Brown 
3970061576a5SJed Brown   PetscFunctionBegin;
3971061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
3972061576a5SJed Brown   PetscFunctionReturn(0);
3973061576a5SJed Brown }
3974061576a5SJed Brown 
3975061576a5SJed Brown /*@
3976061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
397788ed4aceSMatthew G Knepley 
397888ed4aceSMatthew G Knepley   Input Parameters:
397988ed4aceSMatthew G Knepley + dm - The DM
398088ed4aceSMatthew G Knepley - section - The PetscSection
398188ed4aceSMatthew G Knepley 
398288ed4aceSMatthew G Knepley   Level: intermediate
398388ed4aceSMatthew G Knepley 
398488ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
398588ed4aceSMatthew G Knepley 
3986061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
398788ed4aceSMatthew G Knepley @*/
3988061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
39890adebc6cSBarry Smith {
3990c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
3991af122d2aSMatthew G Knepley   PetscInt       f;
399288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
399388ed4aceSMatthew G Knepley 
399488ed4aceSMatthew G Knepley   PetscFunctionBegin;
399588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3996b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
39971d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
39981bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
39991bb6d2a8SBarry Smith   dm->localSection = section;
40001bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4001af122d2aSMatthew G Knepley   if (numFields) {
4002af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4003af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40040f21e855SMatthew G. Knepley       PetscObject disc;
4005af122d2aSMatthew G Knepley       const char *name;
4006af122d2aSMatthew G Knepley 
40071bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
400844a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40090f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4010af122d2aSMatthew G Knepley     }
4011af122d2aSMatthew G Knepley   }
4012e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40131bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
401488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
401588ed4aceSMatthew G Knepley }
401688ed4aceSMatthew G Knepley 
40179435951eSToby Isaac /*@
4018b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40199435951eSToby Isaac 
4020e228b242SToby Isaac   not collective
4021e228b242SToby Isaac 
40229435951eSToby Isaac   Input Parameter:
40239435951eSToby Isaac . dm - The DM
40249435951eSToby Isaac 
40259435951eSToby Isaac   Output Parameter:
40269435951eSToby 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.
40279435951eSToby 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.
40289435951eSToby Isaac 
40299435951eSToby Isaac   Level: advanced
40309435951eSToby Isaac 
40319435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
40329435951eSToby Isaac 
40339435951eSToby Isaac .seealso: DMSetDefaultConstraints()
40349435951eSToby Isaac @*/
40359435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
40369435951eSToby Isaac {
40379435951eSToby Isaac   PetscErrorCode ierr;
40389435951eSToby Isaac 
40399435951eSToby Isaac   PetscFunctionBegin;
40409435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40419435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
404245a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
404345a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
40449435951eSToby Isaac   PetscFunctionReturn(0);
40459435951eSToby Isaac }
40469435951eSToby Isaac 
40479435951eSToby Isaac /*@
4048b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
40499435951eSToby Isaac 
40509435951eSToby 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().
40519435951eSToby Isaac 
40529435951eSToby 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.
40539435951eSToby Isaac 
4054e228b242SToby Isaac   collective on dm
4055e228b242SToby Isaac 
40569435951eSToby Isaac   Input Parameters:
40579435951eSToby Isaac + dm - The DM
4058e228b242SToby 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).
4059e228b242SToby 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).
40609435951eSToby Isaac 
40619435951eSToby Isaac   Level: advanced
40629435951eSToby Isaac 
40639435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
40649435951eSToby Isaac 
40659435951eSToby Isaac .seealso: DMGetDefaultConstraints()
40669435951eSToby Isaac @*/
40679435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
40689435951eSToby Isaac {
4069e228b242SToby Isaac   PetscMPIInt result;
40709435951eSToby Isaac   PetscErrorCode ierr;
40719435951eSToby Isaac 
40729435951eSToby Isaac   PetscFunctionBegin;
40739435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4074e228b242SToby Isaac   if (section) {
4075e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4076e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4077f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4078e228b242SToby Isaac   }
4079e228b242SToby Isaac   if (mat) {
4080e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4081e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4082f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4083e228b242SToby Isaac   }
40849435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40859435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
40869435951eSToby Isaac   dm->defaultConstraintSection = section;
40879435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
40889435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
40899435951eSToby Isaac   dm->defaultConstraintMat = mat;
40909435951eSToby Isaac   PetscFunctionReturn(0);
40919435951eSToby Isaac }
40929435951eSToby Isaac 
4093497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4094507e4973SMatthew G. Knepley /*
4095507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4096507e4973SMatthew G. Knepley 
4097507e4973SMatthew G. Knepley   Input Parameters:
4098507e4973SMatthew G. Knepley + dm - The DM
4099507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4100507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4101507e4973SMatthew G. Knepley 
4102507e4973SMatthew G. Knepley   Level: intermediate
4103507e4973SMatthew G. Knepley 
41041bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4105507e4973SMatthew G. Knepley */
4106f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4107507e4973SMatthew G. Knepley {
4108507e4973SMatthew G. Knepley   MPI_Comm        comm;
4109507e4973SMatthew G. Knepley   PetscLayout     layout;
4110507e4973SMatthew G. Knepley   const PetscInt *ranges;
4111507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4112507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4113507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4114507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4115507e4973SMatthew G. Knepley 
4116507e4973SMatthew G. Knepley   PetscFunctionBegin;
4117507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4118507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4119507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4120507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4121507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4122507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4123507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4124507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4125507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4126507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4127507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4128507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4129f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4130507e4973SMatthew G. Knepley 
4131507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4132507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4133507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4134507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4135507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4136507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4137507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4138507e4973SMatthew 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;}
4139507e4973SMatthew 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;}
4140507e4973SMatthew G. Knepley     if (gdof < 0) {
4141507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4142507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4143507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4144507e4973SMatthew G. Knepley 
4145507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4146507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4147507e4973SMatthew 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;}
4148507e4973SMatthew G. Knepley       }
4149507e4973SMatthew G. Knepley     }
4150507e4973SMatthew G. Knepley   }
4151507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4152507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4153b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4154507e4973SMatthew G. Knepley   if (!gvalid) {
4155507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4156507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4157507e4973SMatthew G. Knepley   }
4158507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4159507e4973SMatthew G. Knepley }
4160f741bcd2SMatthew G. Knepley #endif
4161507e4973SMatthew G. Knepley 
416288ed4aceSMatthew G Knepley /*@
4163e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
416488ed4aceSMatthew G Knepley 
4165d083f849SBarry Smith   Collective on dm
41668b1ab98fSJed Brown 
416788ed4aceSMatthew G Knepley   Input Parameter:
416888ed4aceSMatthew G Knepley . dm - The DM
416988ed4aceSMatthew G Knepley 
417088ed4aceSMatthew G Knepley   Output Parameter:
417188ed4aceSMatthew G Knepley . section - The PetscSection
417288ed4aceSMatthew G Knepley 
417388ed4aceSMatthew G Knepley   Level: intermediate
417488ed4aceSMatthew G Knepley 
417588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
417688ed4aceSMatthew G Knepley 
417792fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
417888ed4aceSMatthew G Knepley @*/
4179e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
41800adebc6cSBarry Smith {
418188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
418288ed4aceSMatthew G Knepley 
418388ed4aceSMatthew G Knepley   PetscFunctionBegin;
418488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
418588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41861bb6d2a8SBarry Smith   if (!dm->globalSection) {
4187fd59a867SMatthew G. Knepley     PetscSection s;
4188fd59a867SMatthew G. Knepley 
418992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4190fd59a867SMatthew 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");
419133907cc2SStefano Zampini     if (!dm->sf) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM must have a point PetscSF in order to create a global PetscSection");
41921bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4193cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
41941bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
41951bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
419688ed4aceSMatthew G Knepley   }
41971bb6d2a8SBarry Smith   *section = dm->globalSection;
419888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
419988ed4aceSMatthew G Knepley }
420088ed4aceSMatthew G Knepley 
4201b21d0597SMatthew G Knepley /*@
4202e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4203b21d0597SMatthew G Knepley 
4204b21d0597SMatthew G Knepley   Input Parameters:
4205b21d0597SMatthew G Knepley + dm - The DM
42065080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4207b21d0597SMatthew G Knepley 
4208b21d0597SMatthew G Knepley   Level: intermediate
4209b21d0597SMatthew G Knepley 
4210b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4211b21d0597SMatthew G Knepley 
421292fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4213b21d0597SMatthew G Knepley @*/
4214e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42150adebc6cSBarry Smith {
4216b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4217b21d0597SMatthew G Knepley 
4218b21d0597SMatthew G Knepley   PetscFunctionBegin;
4219b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42205080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42211d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42221bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42231bb6d2a8SBarry Smith   dm->globalSection = section;
4224497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42251bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4226507e4973SMatthew G. Knepley #endif
4227b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4228b21d0597SMatthew G Knepley }
4229b21d0597SMatthew G Knepley 
423088ed4aceSMatthew G Knepley /*@
42311bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
423288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
423388ed4aceSMatthew G Knepley 
423488ed4aceSMatthew G Knepley   Input Parameter:
423588ed4aceSMatthew G Knepley . dm - The DM
423688ed4aceSMatthew G Knepley 
423788ed4aceSMatthew G Knepley   Output Parameter:
423888ed4aceSMatthew G Knepley . sf - The PetscSF
423988ed4aceSMatthew G Knepley 
424088ed4aceSMatthew G Knepley   Level: intermediate
424188ed4aceSMatthew G Knepley 
424288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
424388ed4aceSMatthew G Knepley 
42441bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
424588ed4aceSMatthew G Knepley @*/
42461bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
42470adebc6cSBarry Smith {
424888ed4aceSMatthew G Knepley   PetscInt       nroots;
424988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
425088ed4aceSMatthew G Knepley 
425188ed4aceSMatthew G Knepley   PetscFunctionBegin;
425288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
425388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
42541bb6d2a8SBarry Smith   if (!dm->sectionSF) {
42551bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
425633907cc2SStefano Zampini   }
42571bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
425888ed4aceSMatthew G Knepley   if (nroots < 0) {
425988ed4aceSMatthew G Knepley     PetscSection section, gSection;
426088ed4aceSMatthew G Knepley 
426192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
426231ea6d37SMatthew G Knepley     if (section) {
4263e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
42641bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
426531ea6d37SMatthew G Knepley     } else {
42660298fd71SBarry Smith       *sf = NULL;
426731ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
426831ea6d37SMatthew G Knepley     }
426988ed4aceSMatthew G Knepley   }
42701bb6d2a8SBarry Smith   *sf = dm->sectionSF;
427188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
427288ed4aceSMatthew G Knepley }
427388ed4aceSMatthew G Knepley 
427488ed4aceSMatthew G Knepley /*@
42751bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
427688ed4aceSMatthew G Knepley 
427788ed4aceSMatthew G Knepley   Input Parameters:
427888ed4aceSMatthew G Knepley + dm - The DM
427988ed4aceSMatthew G Knepley - sf - The PetscSF
428088ed4aceSMatthew G Knepley 
428188ed4aceSMatthew G Knepley   Level: intermediate
428288ed4aceSMatthew G Knepley 
428388ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
428488ed4aceSMatthew G Knepley 
42851bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
428688ed4aceSMatthew G Knepley @*/
42871bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
42880adebc6cSBarry Smith {
428988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
429088ed4aceSMatthew G Knepley 
429188ed4aceSMatthew G Knepley   PetscFunctionBegin;
429288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4293b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
429433907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
42951bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
42961bb6d2a8SBarry Smith   dm->sectionSF = sf;
429788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
429888ed4aceSMatthew G Knepley }
429988ed4aceSMatthew G Knepley 
430088ed4aceSMatthew G Knepley /*@C
43011bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
430288ed4aceSMatthew G Knepley   describing the data layout.
430388ed4aceSMatthew G Knepley 
430488ed4aceSMatthew G Knepley   Input Parameters:
430588ed4aceSMatthew G Knepley + dm - The DM
430688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
430788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
430888ed4aceSMatthew G Knepley 
43091bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
431088ed4aceSMatthew G Knepley 
43111bb6d2a8SBarry Smith   Level: developer
43121bb6d2a8SBarry Smith 
43131bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43141bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43151bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43161bb6d2a8SBarry Smith 
43171bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
431888ed4aceSMatthew G Knepley @*/
43191bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
432088ed4aceSMatthew G Knepley {
432182f516ccSBarry Smith   MPI_Comm       comm;
432288ed4aceSMatthew G Knepley   PetscLayout    layout;
432388ed4aceSMatthew G Knepley   const PetscInt *ranges;
432488ed4aceSMatthew G Knepley   PetscInt       *local;
432588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4326ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
432788ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
432888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
432988ed4aceSMatthew G Knepley 
433088ed4aceSMatthew G Knepley   PetscFunctionBegin;
433188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4332367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
433388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
433488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
433588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
433688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
433788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
433888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
433988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
434088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
434188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4342ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
43436636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
434488ed4aceSMatthew G Knepley 
43456636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43466636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4347235fbf56SMatthew 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));
43486636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
434988ed4aceSMatthew G Knepley   }
4350785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4351785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
435288ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
43531f588964SMatthew G Knepley     const PetscInt *cind;
43546636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
435588ed4aceSMatthew G Knepley 
435688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
435788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
435888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
435988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
436088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43616636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
436288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
43636636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
43646636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
43656636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4366057b4bcdSMatthew 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);
43676636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
43686636e97aSMatthew G Knepley     }
436988ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
437088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
437188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
437288ed4aceSMatthew G Knepley     }
437388ed4aceSMatthew G Knepley     if (gdof < 0) {
43746636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
437588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
437688ed4aceSMatthew G Knepley 
437705376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
437831d3f06eSJed Brown         if (r < 0) r = -(r+2);
437905376888SMatthew 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);
438088ed4aceSMatthew G Knepley         remote[l].rank  = r;
438188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
438288ed4aceSMatthew G Knepley       }
438388ed4aceSMatthew G Knepley     } else {
43846636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
438588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
438688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
438788ed4aceSMatthew G Knepley       }
438888ed4aceSMatthew G Knepley     }
438988ed4aceSMatthew G Knepley   }
43906636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
439188ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
43921bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
439388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
439488ed4aceSMatthew G Knepley }
4395af122d2aSMatthew G Knepley 
4396b21d0597SMatthew G Knepley /*@
4397b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4398b21d0597SMatthew G Knepley 
4399b21d0597SMatthew G Knepley   Input Parameter:
4400b21d0597SMatthew G Knepley . dm - The DM
4401b21d0597SMatthew G Knepley 
4402b21d0597SMatthew G Knepley   Output Parameter:
4403b21d0597SMatthew G Knepley . sf - The PetscSF
4404b21d0597SMatthew G Knepley 
4405b21d0597SMatthew G Knepley   Level: intermediate
4406b21d0597SMatthew G Knepley 
4407b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4408b21d0597SMatthew G Knepley 
44091bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4410b21d0597SMatthew G Knepley @*/
44110adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44120adebc6cSBarry Smith {
4413b21d0597SMatthew G Knepley   PetscFunctionBegin;
4414b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4415b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4416b21d0597SMatthew G Knepley   *sf = dm->sf;
4417b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4418b21d0597SMatthew G Knepley }
4419b21d0597SMatthew G Knepley 
4420057b4bcdSMatthew G Knepley /*@
4421057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4422057b4bcdSMatthew G Knepley 
4423057b4bcdSMatthew G Knepley   Input Parameters:
4424057b4bcdSMatthew G Knepley + dm - The DM
4425057b4bcdSMatthew G Knepley - sf - The PetscSF
4426057b4bcdSMatthew G Knepley 
4427057b4bcdSMatthew G Knepley   Level: intermediate
4428057b4bcdSMatthew G Knepley 
44291bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4430057b4bcdSMatthew G Knepley @*/
44310adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
44320adebc6cSBarry Smith {
4433057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4434057b4bcdSMatthew G Knepley 
4435057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4436057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4437b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4438057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
443933907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4440057b4bcdSMatthew G Knepley   dm->sf = sf;
4441057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4442057b4bcdSMatthew G Knepley }
4443057b4bcdSMatthew G Knepley 
444434aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
444534aa8a36SMatthew G. Knepley {
444634aa8a36SMatthew G. Knepley   PetscClassId   id;
444734aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
444834aa8a36SMatthew G. Knepley 
444934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
445034aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
445134aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
445234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
445334aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
445434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
445517c1d62eSMatthew G. Knepley   } else {
445617c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
445734aa8a36SMatthew G. Knepley   }
445834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
445934aa8a36SMatthew G. Knepley }
446034aa8a36SMatthew G. Knepley 
446144a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
446244a7f3ddSMatthew G. Knepley {
446344a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
446444a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
446544a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
446644a7f3ddSMatthew G. Knepley 
446744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
446844a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
446944a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
447044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
447144a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
447244a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
447344a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
447444a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
447544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
447644a7f3ddSMatthew G. Knepley }
447744a7f3ddSMatthew G. Knepley 
447844a7f3ddSMatthew G. Knepley /*@
447944a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
448044a7f3ddSMatthew G. Knepley 
4481d083f849SBarry Smith   Logically collective on dm
448244a7f3ddSMatthew G. Knepley 
448344a7f3ddSMatthew G. Knepley   Input Parameter:
448444a7f3ddSMatthew G. Knepley . dm - The DM
448544a7f3ddSMatthew G. Knepley 
448644a7f3ddSMatthew G. Knepley   Level: intermediate
448744a7f3ddSMatthew G. Knepley 
448844a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
448944a7f3ddSMatthew G. Knepley @*/
449044a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
449144a7f3ddSMatthew G. Knepley {
449244a7f3ddSMatthew G. Knepley   PetscInt       f;
449344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
449444a7f3ddSMatthew G. Knepley 
449544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
449644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
449744a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
449844a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
449944a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
450044a7f3ddSMatthew G. Knepley   }
450144a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
450244a7f3ddSMatthew G. Knepley   dm->fields = NULL;
450344a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
450444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
450544a7f3ddSMatthew G. Knepley }
450644a7f3ddSMatthew G. Knepley 
4507689b5837SMatthew G. Knepley /*@
4508689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4509689b5837SMatthew G. Knepley 
4510689b5837SMatthew G. Knepley   Not collective
4511689b5837SMatthew G. Knepley 
4512689b5837SMatthew G. Knepley   Input Parameter:
4513689b5837SMatthew G. Knepley . dm - The DM
4514689b5837SMatthew G. Knepley 
4515689b5837SMatthew G. Knepley   Output Parameter:
4516689b5837SMatthew G. Knepley . Nf - The number of fields
4517689b5837SMatthew G. Knepley 
4518689b5837SMatthew G. Knepley   Level: intermediate
4519689b5837SMatthew G. Knepley 
4520689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4521689b5837SMatthew G. Knepley @*/
45220f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45230f21e855SMatthew G. Knepley {
45240f21e855SMatthew G. Knepley   PetscFunctionBegin;
45250f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4526534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
452744a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4528af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4529af122d2aSMatthew G Knepley }
4530af122d2aSMatthew G Knepley 
4531689b5837SMatthew G. Knepley /*@
4532689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4533689b5837SMatthew G. Knepley 
4534d083f849SBarry Smith   Logically collective on dm
4535689b5837SMatthew G. Knepley 
4536689b5837SMatthew G. Knepley   Input Parameters:
4537689b5837SMatthew G. Knepley + dm - The DM
4538689b5837SMatthew G. Knepley - Nf - The number of fields
4539689b5837SMatthew G. Knepley 
4540689b5837SMatthew G. Knepley   Level: intermediate
4541689b5837SMatthew G. Knepley 
4542689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4543689b5837SMatthew G. Knepley @*/
4544af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4545af122d2aSMatthew G Knepley {
45460f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4547af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4548af122d2aSMatthew G Knepley 
4549af122d2aSMatthew G Knepley   PetscFunctionBegin;
4550af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
455144a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
45520f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
45530f21e855SMatthew G. Knepley     PetscContainer obj;
45540f21e855SMatthew G. Knepley 
45550f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
455644a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
45570f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4558af122d2aSMatthew G Knepley   }
4559af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4560af122d2aSMatthew G Knepley }
4561af122d2aSMatthew G Knepley 
4562c1929be8SMatthew G. Knepley /*@
4563c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4564c1929be8SMatthew G. Knepley 
4565c1929be8SMatthew G. Knepley   Not collective
4566c1929be8SMatthew G. Knepley 
4567c1929be8SMatthew G. Knepley   Input Parameters:
4568c1929be8SMatthew G. Knepley + dm - The DM
4569c1929be8SMatthew G. Knepley - f  - The field number
4570c1929be8SMatthew G. Knepley 
457144a7f3ddSMatthew G. Knepley   Output Parameters:
457244a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
457344a7f3ddSMatthew G. Knepley - field - The discretization object
4574c1929be8SMatthew G. Knepley 
457544a7f3ddSMatthew G. Knepley   Level: intermediate
4576c1929be8SMatthew G. Knepley 
457744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4578c1929be8SMatthew G. Knepley @*/
457944a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4580af122d2aSMatthew G Knepley {
4581af122d2aSMatthew G Knepley   PetscFunctionBegin;
4582af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
458344a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
458444a7f3ddSMatthew G. Knepley   if ((f < 0) || (f >= dm->Nf)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, dm->Nf);
458544a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
458644a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4587decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4588decb47aaSMatthew G. Knepley }
4589decb47aaSMatthew G. Knepley 
4590c1929be8SMatthew G. Knepley /*@
4591c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4592c1929be8SMatthew G. Knepley 
4593d083f849SBarry Smith   Logically collective on dm
4594c1929be8SMatthew G. Knepley 
4595c1929be8SMatthew G. Knepley   Input Parameters:
4596c1929be8SMatthew G. Knepley + dm    - The DM
4597c1929be8SMatthew G. Knepley . f     - The field number
459844a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4599c1929be8SMatthew G. Knepley - field - The discretization object
4600c1929be8SMatthew G. Knepley 
460144a7f3ddSMatthew G. Knepley   Level: intermediate
4602c1929be8SMatthew G. Knepley 
460344a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4604c1929be8SMatthew G. Knepley @*/
460544a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4606decb47aaSMatthew G. Knepley {
4607decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4608decb47aaSMatthew G. Knepley 
4609decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4610decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4611e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
461244a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4613e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
461444a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
461544a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
461644a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
461744a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
461844a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
461944a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
462044a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
462134aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46222df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
462344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
462444a7f3ddSMatthew G. Knepley }
462544a7f3ddSMatthew G. Knepley 
462644a7f3ddSMatthew G. Knepley /*@
462744a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
462844a7f3ddSMatthew G. Knepley 
4629d083f849SBarry Smith   Logically collective on dm
463044a7f3ddSMatthew G. Knepley 
463144a7f3ddSMatthew G. Knepley   Input Parameters:
463244a7f3ddSMatthew G. Knepley + dm    - The DM
463344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
463444a7f3ddSMatthew G. Knepley - field - The discretization object
463544a7f3ddSMatthew G. Knepley 
463644a7f3ddSMatthew G. Knepley   Level: intermediate
463744a7f3ddSMatthew G. Knepley 
463844a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
463944a7f3ddSMatthew G. Knepley @*/
464044a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
464144a7f3ddSMatthew G. Knepley {
464244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
464344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
464444a7f3ddSMatthew G. Knepley 
464544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
464644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
464744a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
464844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
464944a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
465044a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
465144a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
465244a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
465344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
465434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
46552df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4656af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4657af122d2aSMatthew G Knepley }
46586636e97aSMatthew G Knepley 
4659e5e52638SMatthew G. Knepley /*@
4660e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4661e5e52638SMatthew G. Knepley 
4662d083f849SBarry Smith   Collective on dm
4663e5e52638SMatthew G. Knepley 
4664e5e52638SMatthew G. Knepley   Input Parameter:
4665e5e52638SMatthew G. Knepley . dm - The DM
4666e5e52638SMatthew G. Knepley 
4667e5e52638SMatthew G. Knepley   Output Parameter:
4668e5e52638SMatthew G. Knepley . newdm - The DM
4669e5e52638SMatthew G. Knepley 
4670e5e52638SMatthew G. Knepley   Level: advanced
4671e5e52638SMatthew G. Knepley 
4672e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4673e5e52638SMatthew G. Knepley @*/
4674e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4675e5e52638SMatthew G. Knepley {
4676e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4677e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4678e5e52638SMatthew G. Knepley 
4679e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4680e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4681e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4682e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4683e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4684e5e52638SMatthew G. Knepley     DMLabel     label;
4685e5e52638SMatthew G. Knepley     PetscObject field;
468634aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4687e5e52638SMatthew G. Knepley 
4688e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4689e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
469034aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
469134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
469234aa8a36SMatthew G. Knepley   }
469334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
469434aa8a36SMatthew G. Knepley }
469534aa8a36SMatthew G. Knepley 
469634aa8a36SMatthew G. Knepley /*@
469734aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
469834aa8a36SMatthew G. Knepley 
469934aa8a36SMatthew G. Knepley   Not collective
470034aa8a36SMatthew G. Knepley 
470134aa8a36SMatthew G. Knepley   Input Parameters:
470234aa8a36SMatthew G. Knepley + dm - The DM object
470334aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
470434aa8a36SMatthew G. Knepley 
470534aa8a36SMatthew G. Knepley   Output Parameter:
470634aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
470734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
470834aa8a36SMatthew G. Knepley 
470934aa8a36SMatthew G. Knepley   Notes:
471034aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
471134aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
471234aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4713979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
471434aa8a36SMatthew G. Knepley 
471534aa8a36SMatthew G. Knepley   Level: developer
471634aa8a36SMatthew G. Knepley 
471734aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
471834aa8a36SMatthew G. Knepley @*/
471934aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
472034aa8a36SMatthew G. Knepley {
472134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
472234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4723534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4724534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
472534aa8a36SMatthew G. Knepley   if (f < 0) {
472634aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
472734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
472834aa8a36SMatthew G. Knepley   } else {
472934aa8a36SMatthew G. Knepley     PetscInt       Nf;
473034aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
473134aa8a36SMatthew G. Knepley 
473234aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
473334aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
473434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
473534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
473634aa8a36SMatthew G. Knepley   }
473734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
473834aa8a36SMatthew G. Knepley }
473934aa8a36SMatthew G. Knepley 
474034aa8a36SMatthew G. Knepley /*@
474134aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
474234aa8a36SMatthew G. Knepley 
474334aa8a36SMatthew G. Knepley   Not collective
474434aa8a36SMatthew G. Knepley 
474534aa8a36SMatthew G. Knepley   Input Parameters:
474634aa8a36SMatthew G. Knepley + dm         - The DM object
474734aa8a36SMatthew G. Knepley . f          - The field number
474834aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
474934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
475034aa8a36SMatthew G. Knepley 
475134aa8a36SMatthew G. Knepley   Notes:
475234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
475334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
475434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4755979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
475634aa8a36SMatthew G. Knepley 
475734aa8a36SMatthew G. Knepley   Level: developer
475834aa8a36SMatthew G. Knepley 
475934aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
476034aa8a36SMatthew G. Knepley @*/
476134aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
476234aa8a36SMatthew G. Knepley {
476334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
476434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
476534aa8a36SMatthew G. Knepley   if (f < 0) {
476634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
476734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
476834aa8a36SMatthew G. Knepley   } else {
476934aa8a36SMatthew G. Knepley     PetscInt       Nf;
477034aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
477134aa8a36SMatthew G. Knepley 
477234aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
477334aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
477434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
477534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4776e5e52638SMatthew G. Knepley   }
4777e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4778e5e52638SMatthew G. Knepley }
4779e5e52638SMatthew G. Knepley 
4780b0441da4SMatthew G. Knepley /*@
4781b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4782b0441da4SMatthew G. Knepley 
4783b0441da4SMatthew G. Knepley   Not collective
4784b0441da4SMatthew G. Knepley 
4785b0441da4SMatthew G. Knepley   Input Parameters:
4786b0441da4SMatthew G. Knepley . dm - The DM object
4787b0441da4SMatthew G. Knepley 
4788b0441da4SMatthew G. Knepley   Output Parameter:
4789b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4790b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4791b0441da4SMatthew G. Knepley 
4792b0441da4SMatthew G. Knepley   Notes:
4793b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4794b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4795b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4796b0441da4SMatthew G. Knepley 
4797b0441da4SMatthew G. Knepley   Level: developer
4798b0441da4SMatthew G. Knepley 
4799b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4800b0441da4SMatthew G. Knepley @*/
4801b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4802b0441da4SMatthew G. Knepley {
4803b0441da4SMatthew G. Knepley   PetscInt       Nf;
4804b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4805b0441da4SMatthew G. Knepley 
4806b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4807b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4808534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4809534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4810b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4811b0441da4SMatthew G. Knepley   if (!Nf) {
4812b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4813b0441da4SMatthew G. Knepley   } else {
4814b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4815b0441da4SMatthew G. Knepley   }
4816b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4817b0441da4SMatthew G. Knepley }
4818b0441da4SMatthew G. Knepley 
4819b0441da4SMatthew G. Knepley /*@
4820b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4821b0441da4SMatthew G. Knepley 
4822b0441da4SMatthew G. Knepley   Not collective
4823b0441da4SMatthew G. Knepley 
4824b0441da4SMatthew G. Knepley   Input Parameters:
4825b0441da4SMatthew G. Knepley + dm         - The DM object
4826b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4827b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4828b0441da4SMatthew G. Knepley 
4829b0441da4SMatthew G. Knepley   Notes:
4830b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4831b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4832b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4833b0441da4SMatthew G. Knepley 
4834b0441da4SMatthew G. Knepley   Level: developer
4835b0441da4SMatthew G. Knepley 
4836b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4837b0441da4SMatthew G. Knepley @*/
4838b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4839b0441da4SMatthew G. Knepley {
4840b0441da4SMatthew G. Knepley   PetscInt       Nf;
4841b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4842b0441da4SMatthew G. Knepley 
4843b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4844b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4845b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4846b0441da4SMatthew G. Knepley   if (!Nf) {
4847b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4848b0441da4SMatthew G. Knepley   } else {
4849b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4850e5e52638SMatthew G. Knepley   }
4851e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4852e5e52638SMatthew G. Knepley }
4853e5e52638SMatthew G. Knepley 
4854e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4855e5e52638SMatthew G. Knepley {
4856e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4857e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4858e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4859e5e52638SMatthew G. Knepley 
4860e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4861e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4862e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4863e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4864b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4865e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4866e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4867e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4868e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4869e5e52638SMatthew G. Knepley }
4870e5e52638SMatthew G. Knepley 
4871e5e52638SMatthew G. Knepley /*@
4872e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4873e5e52638SMatthew G. Knepley 
4874e5e52638SMatthew G. Knepley   Not collective
4875e5e52638SMatthew G. Knepley 
4876e5e52638SMatthew G. Knepley   Input Parameter:
4877e5e52638SMatthew G. Knepley . dm - The DM
4878e5e52638SMatthew G. Knepley 
4879e5e52638SMatthew G. Knepley   Output Parameter:
4880e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4881e5e52638SMatthew G. Knepley 
4882e5e52638SMatthew G. Knepley   Level: intermediate
4883e5e52638SMatthew G. Knepley 
4884e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4885e5e52638SMatthew G. Knepley @*/
4886e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4887e5e52638SMatthew G. Knepley {
4888e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4889e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4890534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4891e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4892e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4893e5e52638SMatthew G. Knepley }
4894e5e52638SMatthew G. Knepley 
4895e5e52638SMatthew G. Knepley /*@
4896e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4897e5e52638SMatthew G. Knepley 
4898d083f849SBarry Smith   Logically collective on dm
4899e5e52638SMatthew G. Knepley 
4900e5e52638SMatthew G. Knepley   Input Parameter:
4901e5e52638SMatthew G. Knepley . dm - The DM
4902e5e52638SMatthew G. Knepley 
4903e5e52638SMatthew G. Knepley   Level: intermediate
4904e5e52638SMatthew G. Knepley 
4905e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4906e5e52638SMatthew G. Knepley @*/
4907e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4908e5e52638SMatthew G. Knepley {
4909e5e52638SMatthew G. Knepley   PetscInt       s;
4910e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4911e5e52638SMatthew G. Knepley 
4912e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4913e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4914e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4915e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4916e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4917b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4918e5e52638SMatthew G. Knepley   }
4919e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4920e5e52638SMatthew G. Knepley   dm->probs = NULL;
4921e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4922e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4923e5e52638SMatthew G. Knepley }
4924e5e52638SMatthew G. Knepley 
4925e5e52638SMatthew G. Knepley /*@
4926e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4927e5e52638SMatthew G. Knepley 
4928e5e52638SMatthew G. Knepley   Not collective
4929e5e52638SMatthew G. Knepley 
4930e5e52638SMatthew G. Knepley   Input Parameter:
4931e5e52638SMatthew G. Knepley . dm    - The DM
4932e5e52638SMatthew G. Knepley 
4933e5e52638SMatthew G. Knepley   Output Parameter:
4934e5e52638SMatthew G. Knepley . prob - The default PetscDS
4935e5e52638SMatthew G. Knepley 
4936e5e52638SMatthew G. Knepley   Level: intermediate
4937e5e52638SMatthew G. Knepley 
4938e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4939e5e52638SMatthew G. Knepley @*/
4940e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4941e5e52638SMatthew G. Knepley {
4942b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4943b0143b4dSMatthew G. Knepley 
4944e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4945e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4946e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4947b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4948b0143b4dSMatthew G. Knepley     PetscDS ds;
4949b0143b4dSMatthew G. Knepley 
4950b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4951b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4952b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4953b0143b4dSMatthew G. Knepley   }
4954b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4955e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4956e5e52638SMatthew G. Knepley }
4957e5e52638SMatthew G. Knepley 
4958e5e52638SMatthew G. Knepley /*@
4959e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4960e5e52638SMatthew G. Knepley 
4961e5e52638SMatthew G. Knepley   Not collective
4962e5e52638SMatthew G. Knepley 
4963e5e52638SMatthew G. Knepley   Input Parameters:
4964e5e52638SMatthew G. Knepley + dm    - The DM
4965e5e52638SMatthew G. Knepley - point - Cell for the DS
4966e5e52638SMatthew G. Knepley 
4967e5e52638SMatthew G. Knepley   Output Parameter:
4968e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4969e5e52638SMatthew G. Knepley 
4970e5e52638SMatthew G. Knepley   Level: developer
4971e5e52638SMatthew G. Knepley 
4972b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4973e5e52638SMatthew G. Knepley @*/
4974e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4975e5e52638SMatthew G. Knepley {
4976e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
4977e5e52638SMatthew G. Knepley   PetscInt       s;
4978e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4979e5e52638SMatthew G. Knepley 
4980e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4981e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4982e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
4983e5e52638SMatthew G. Knepley   *prob = NULL;
4984e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4985e5e52638SMatthew G. Knepley     PetscInt val;
4986e5e52638SMatthew G. Knepley 
4987e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
4988e5e52638SMatthew G. Knepley     else {
4989e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
4990e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
4991e5e52638SMatthew G. Knepley     }
4992e5e52638SMatthew G. Knepley   }
4993e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
4994e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4995e5e52638SMatthew G. Knepley }
4996e5e52638SMatthew G. Knepley 
4997e5e52638SMatthew G. Knepley /*@
4998e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
4999e5e52638SMatthew G. Knepley 
5000e5e52638SMatthew G. Knepley   Not collective
5001e5e52638SMatthew G. Knepley 
5002e5e52638SMatthew G. Knepley   Input Parameters:
5003e5e52638SMatthew G. Knepley + dm    - The DM
5004e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5005e5e52638SMatthew G. Knepley 
5006b3cf3223SMatthew G. Knepley   Output Parameters:
5007b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5008b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5009e5e52638SMatthew G. Knepley 
5010e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5011e5e52638SMatthew G. Knepley 
5012e5e52638SMatthew G. Knepley   Level: advanced
5013e5e52638SMatthew G. Knepley 
5014e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5015e5e52638SMatthew G. Knepley @*/
5016b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5017e5e52638SMatthew G. Knepley {
5018e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5019e5e52638SMatthew G. Knepley 
5020e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5021e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5022e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5023b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5024b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5025e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5026b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5027b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5028b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5029b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5030b3cf3223SMatthew G. Knepley     }
5031e5e52638SMatthew G. Knepley   }
50322df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5033e5e52638SMatthew G. Knepley }
5034e5e52638SMatthew G. Knepley 
5035e5e52638SMatthew G. Knepley /*@
5036e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5037e5e52638SMatthew G. Knepley 
5038e5e52638SMatthew G. Knepley   Not collective
5039e5e52638SMatthew G. Knepley 
5040e5e52638SMatthew G. Knepley   Input Parameters:
5041e5e52638SMatthew G. Knepley + dm  - The DM
5042e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5043e5e52638SMatthew G. Knepley 
5044e5e52638SMatthew G. Knepley   Output Parameters:
5045b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5046b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5047b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
5048e5e52638SMatthew G. Knepley 
5049e5e52638SMatthew G. Knepley   Level: advanced
5050e5e52638SMatthew G. Knepley 
5051e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5052e5e52638SMatthew G. Knepley @*/
5053b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5054e5e52638SMatthew G. Knepley {
5055e5e52638SMatthew G. Knepley   PetscInt       Nds;
5056e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5059e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5060e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5061e5e52638SMatthew G. Knepley   if ((num < 0) || (num >= Nds)) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Region number %D is not in [0, %D)", num, Nds);
5062e5e52638SMatthew G. Knepley   if (label) {
5063e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5064e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5065e5e52638SMatthew G. Knepley   }
5066b3cf3223SMatthew G. Knepley   if (fields) {
5067b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5068b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5069b3cf3223SMatthew G. Knepley   }
5070e5e52638SMatthew G. Knepley   if (ds) {
5071b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5072e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5073e5e52638SMatthew G. Knepley   }
5074e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5075e5e52638SMatthew G. Knepley }
5076e5e52638SMatthew G. Knepley 
5077e5e52638SMatthew G. Knepley /*@
5078e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5079e5e52638SMatthew G. Knepley 
5080d083f849SBarry Smith   Collective on dm
5081e5e52638SMatthew G. Knepley 
5082e5e52638SMatthew G. Knepley   Input Parameters:
5083e5e52638SMatthew G. Knepley + dm     - The DM
5084e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5085b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5086e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5087e5e52638SMatthew G. Knepley 
5088b3cf3223SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM. If DS is replaced,
5089b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5090e5e52638SMatthew G. Knepley 
5091e5e52638SMatthew G. Knepley   Level: advanced
5092e5e52638SMatthew G. Knepley 
5093e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5094e5e52638SMatthew G. Knepley @*/
5095b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5096e5e52638SMatthew G. Knepley {
5097e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5098e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5099e5e52638SMatthew G. Knepley 
5100e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5101e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5102e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5103e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5104e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5105e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5106b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5107e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5108e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5109e5e52638SMatthew G. Knepley     }
5110e5e52638SMatthew G. Knepley   }
51111b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5112e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5113b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5114e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5115e5e52638SMatthew G. Knepley   if (!label) {
5116e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5117e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5118e5e52638SMatthew G. Knepley     Nds = 0;
5119e5e52638SMatthew G. Knepley   }
5120e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5121b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5122e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5123e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5124e5e52638SMatthew G. Knepley }
5125e5e52638SMatthew G. Knepley 
5126e5e52638SMatthew G. Knepley /*@
5127e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5128e5e52638SMatthew G. Knepley 
5129d083f849SBarry Smith   Collective on dm
5130e5e52638SMatthew G. Knepley 
5131e5e52638SMatthew G. Knepley   Input Parameter:
5132e5e52638SMatthew G. Knepley . dm - The DM
5133e5e52638SMatthew G. Knepley 
5134e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5135e5e52638SMatthew G. Knepley 
5136e5e52638SMatthew G. Knepley   Level: intermediate
5137e5e52638SMatthew G. Knepley 
5138e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5139e5e52638SMatthew G. Knepley @*/
5140e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5141e5e52638SMatthew G. Knepley {
5142e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5143e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5144b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5145e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5146e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5147e5e52638SMatthew G. Knepley 
5148e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5149e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5150e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5151e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5152e5e52638SMatthew G. Knepley    1) NULL
5153e5e52638SMatthew G. Knepley    2) Hybrid cells
5154e5e52638SMatthew G. Knepley   */
5155e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5156e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5157e5e52638SMatthew G. Knepley   /* Create default DS */
5158b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
51592df9ee95SMatthew G. Knepley   if (!prob) {
5160b3cf3223SMatthew G. Knepley     IS        fields;
5161b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5162b3cf3223SMatthew G. Knepley 
5163b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5164b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5165b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
516688f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
516788f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
516888f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
516988f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
517088f0c812SMatthew G. Knepley 
51712df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5172b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
51732df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5174b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5175b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
51762df9ee95SMatthew G. Knepley   }
5177e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5178e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5179b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5180e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5181e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5182e5e52638SMatthew G. Knepley 
5183e5e52638SMatthew G. Knepley     if (label) {
51840122748bSMatthew G. Knepley       DM        plex;
5185a2d47024SMatthew G. Knepley       IS        fields;
5186a2d47024SMatthew G. Knepley       PetscInt *fld;
51870122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
51880122748bSMatthew G. Knepley 
51890122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
51900122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
51910122748bSMatthew G. Knepley       ierr = DMPlexGetHybridBounds(plex, depth >= 0 ? &pMax[depth] : NULL, depth>1 ? &pMax[depth-1] : NULL, depth>2 ? &pMax[1] : NULL, &pMax[0]);CHKERRQ(ierr);
51920122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
51930122748bSMatthew G. Knepley 
5194e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5195e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5196e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5197a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5198a2d47024SMatthew G. Knepley       fld[0] = f;
5199a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5200a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5201a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5202a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5203a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5204a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5205e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5206e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5207e5e52638SMatthew G. Knepley       break;
5208e5e52638SMatthew G. Knepley     }
5209e5e52638SMatthew G. Knepley   }
5210e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5211b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5212e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5213e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5214e5e52638SMatthew G. Knepley 
5215e5e52638SMatthew G. Knepley     if (!label) {
5216e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5217e5e52638SMatthew G. Knepley       if (probh) {
5218e5e52638SMatthew G. Knepley         PetscFE subfe;
5219e5e52638SMatthew G. Knepley 
5220e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5221e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5222e5e52638SMatthew G. Knepley       }
5223e5e52638SMatthew G. Knepley     } else {
5224e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5225e5e52638SMatthew G. Knepley     }
5226e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5227e5e52638SMatthew G. Knepley     {
5228e5e52638SMatthew G. Knepley       PetscClassId id;
5229e5e52638SMatthew G. Knepley 
5230e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5231e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5232e5e52638SMatthew G. Knepley     }
5233e5e52638SMatthew G. Knepley   }
5234e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5235e5e52638SMatthew G. Knepley   /* Setup DSes */
5236e5e52638SMatthew G. Knepley   if (doSetup) {
5237e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5238e5e52638SMatthew G. Knepley   }
5239e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5240e5e52638SMatthew G. Knepley }
5241e5e52638SMatthew G. Knepley 
5242e5e52638SMatthew G. Knepley /*@
5243e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5244e5e52638SMatthew G. Knepley 
5245d083f849SBarry Smith   Collective on dm
5246e5e52638SMatthew G. Knepley 
5247e5e52638SMatthew G. Knepley   Input Parameter:
5248e5e52638SMatthew G. Knepley . dm - The DM
5249e5e52638SMatthew G. Knepley 
5250e5e52638SMatthew G. Knepley   Output Parameter:
5251e5e52638SMatthew G. Knepley . newdm - The DM
5252e5e52638SMatthew G. Knepley 
5253e5e52638SMatthew G. Knepley   Level: advanced
5254e5e52638SMatthew G. Knepley 
5255e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5256e5e52638SMatthew G. Knepley @*/
5257e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5258e5e52638SMatthew G. Knepley {
5259e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5260e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5261e5e52638SMatthew G. Knepley 
5262e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5263e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5264e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5265e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5266e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5267e5e52638SMatthew G. Knepley     DMLabel label;
5268b3cf3223SMatthew G. Knepley     IS      fields;
5269e5e52638SMatthew G. Knepley     PetscDS ds;
5270e5e52638SMatthew G. Knepley 
5271b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5272b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5273e5e52638SMatthew G. Knepley   }
5274e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5275e5e52638SMatthew G. Knepley }
5276e5e52638SMatthew G. Knepley 
5277e5e52638SMatthew G. Knepley /*@
5278e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5279e5e52638SMatthew G. Knepley 
5280d083f849SBarry Smith   Collective on dm
5281e5e52638SMatthew G. Knepley 
5282e5e52638SMatthew G. Knepley   Input Parameter:
5283e5e52638SMatthew G. Knepley . dm - The DM
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley   Output Parameter:
5286e5e52638SMatthew G. Knepley . newdm - The DM
5287e5e52638SMatthew G. Knepley 
5288e5e52638SMatthew G. Knepley   Level: advanced
5289e5e52638SMatthew G. Knepley 
5290e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5291e5e52638SMatthew G. Knepley @*/
5292e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5293e5e52638SMatthew G. Knepley {
5294e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5295e5e52638SMatthew G. Knepley 
5296e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5297e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5298e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5299e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5300e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5301e5e52638SMatthew G. Knepley }
5302e5e52638SMatthew G. Knepley 
5303b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5304b64e0483SPeter Brune {
5305b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5306b64e0483SPeter Brune   PetscErrorCode ierr;
5307b64e0483SPeter Brune   Vec coords,ccoords;
53086dbf9973SLawrence Mitchell   Mat inject;
5309b64e0483SPeter Brune   PetscFunctionBegin;
5310b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5311b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5312b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5313b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5314b64e0483SPeter Brune   if (coords && !ccoords) {
5315b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
53166668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
53176dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
53182adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
53196dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5320b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5321b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5322b64e0483SPeter Brune   }
5323b64e0483SPeter Brune   PetscFunctionReturn(0);
5324b64e0483SPeter Brune }
5325b64e0483SPeter Brune 
532603dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
532703dadc2fSPeter Brune {
532803dadc2fSPeter Brune   DM dm_coord,subdm_coord;
532903dadc2fSPeter Brune   PetscErrorCode ierr;
533003dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
533103dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
533203dadc2fSPeter Brune   PetscFunctionBegin;
533303dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
533403dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
533503dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
533603dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
533703dadc2fSPeter Brune   if (coords && !ccoords) {
533803dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
53396668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
534003dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
534124640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
534203dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
534303dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
534403dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
53451ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
534603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
534703dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
534803dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
534903dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
535003dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
535103dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
535203dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
535303dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
535403dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
535503dadc2fSPeter Brune   }
535603dadc2fSPeter Brune   PetscFunctionReturn(0);
535703dadc2fSPeter Brune }
535803dadc2fSPeter Brune 
5359c73cfb54SMatthew G. Knepley /*@
5360c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5361c73cfb54SMatthew G. Knepley 
5362c73cfb54SMatthew G. Knepley   Not collective
5363c73cfb54SMatthew G. Knepley 
5364c73cfb54SMatthew G. Knepley   Input Parameter:
5365c73cfb54SMatthew G. Knepley . dm - The DM
5366c73cfb54SMatthew G. Knepley 
5367c73cfb54SMatthew G. Knepley   Output Parameter:
5368c73cfb54SMatthew G. Knepley . dim - The topological dimension
5369c73cfb54SMatthew G. Knepley 
5370c73cfb54SMatthew G. Knepley   Level: beginner
5371c73cfb54SMatthew G. Knepley 
5372c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5373c73cfb54SMatthew G. Knepley @*/
5374c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5375c73cfb54SMatthew G. Knepley {
5376c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5377c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5378534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5379c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5380c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5381c73cfb54SMatthew G. Knepley }
5382c73cfb54SMatthew G. Knepley 
5383c73cfb54SMatthew G. Knepley /*@
5384c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5385c73cfb54SMatthew G. Knepley 
5386c73cfb54SMatthew G. Knepley   Collective on dm
5387c73cfb54SMatthew G. Knepley 
5388c73cfb54SMatthew G. Knepley   Input Parameters:
5389c73cfb54SMatthew G. Knepley + dm - The DM
5390c73cfb54SMatthew G. Knepley - dim - The topological dimension
5391c73cfb54SMatthew G. Knepley 
5392c73cfb54SMatthew G. Knepley   Level: beginner
5393c73cfb54SMatthew G. Knepley 
5394c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5395c73cfb54SMatthew G. Knepley @*/
5396c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5397c73cfb54SMatthew G. Knepley {
5398e5e52638SMatthew G. Knepley   PetscDS        ds;
5399f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5400f17e8794SMatthew G. Knepley 
5401c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5402c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5403c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5404c73cfb54SMatthew G. Knepley   dm->dim = dim;
5405e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5406e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5407c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5408c73cfb54SMatthew G. Knepley }
5409c73cfb54SMatthew G. Knepley 
5410793f3fe5SMatthew G. Knepley /*@
5411793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5412793f3fe5SMatthew G. Knepley 
5413d083f849SBarry Smith   Collective on dm
5414793f3fe5SMatthew G. Knepley 
5415793f3fe5SMatthew G. Knepley   Input Parameters:
5416793f3fe5SMatthew G. Knepley + dm - the DM
5417793f3fe5SMatthew G. Knepley - dim - the dimension
5418793f3fe5SMatthew G. Knepley 
5419793f3fe5SMatthew G. Knepley   Output Parameters:
5420793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5421aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5422793f3fe5SMatthew G. Knepley 
5423793f3fe5SMatthew G. Knepley   Note:
5424793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5425a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5426793f3fe5SMatthew G. Knepley   then the interval is empty.
5427793f3fe5SMatthew G. Knepley 
5428793f3fe5SMatthew G. Knepley   Level: intermediate
5429793f3fe5SMatthew G. Knepley 
5430793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5431793f3fe5SMatthew G. Knepley @*/
5432793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5433793f3fe5SMatthew G. Knepley {
5434793f3fe5SMatthew G. Knepley   PetscInt       d;
5435793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5436793f3fe5SMatthew G. Knepley 
5437793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5438793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5439793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5440793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5441b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5442793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5443793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5444793f3fe5SMatthew G. Knepley }
5445793f3fe5SMatthew G. Knepley 
54466636e97aSMatthew G Knepley /*@
54476636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
54486636e97aSMatthew G Knepley 
5449d083f849SBarry Smith   Collective on dm
54506636e97aSMatthew G Knepley 
54516636e97aSMatthew G Knepley   Input Parameters:
54526636e97aSMatthew G Knepley + dm - the DM
54536636e97aSMatthew G Knepley - c - coordinate vector
54546636e97aSMatthew G Knepley 
54552dd40e9bSPatrick Sanan   Notes:
54562dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
54572dd40e9bSPatrick Sanan 
54582dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
54596636e97aSMatthew G Knepley 
54606636e97aSMatthew G Knepley   Level: intermediate
54616636e97aSMatthew G Knepley 
54622dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
54636636e97aSMatthew G Knepley @*/
54646636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
54656636e97aSMatthew G Knepley {
54666636e97aSMatthew G Knepley   PetscErrorCode ierr;
54676636e97aSMatthew G Knepley 
54686636e97aSMatthew G Knepley   PetscFunctionBegin;
54696636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54706636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
54716636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
54726636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
54736636e97aSMatthew G Knepley   dm->coordinates = c;
54746636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5475b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
547603dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
54776636e97aSMatthew G Knepley   PetscFunctionReturn(0);
54786636e97aSMatthew G Knepley }
54796636e97aSMatthew G Knepley 
54806636e97aSMatthew G Knepley /*@
54816636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
54826636e97aSMatthew G Knepley 
54837058e716SVaclav Hapla   Not collective
54846636e97aSMatthew G Knepley 
54856636e97aSMatthew G Knepley    Input Parameters:
54866636e97aSMatthew G Knepley +  dm - the DM
54876636e97aSMatthew G Knepley -  c - coordinate vector
54886636e97aSMatthew G Knepley 
54892dd40e9bSPatrick Sanan   Notes:
54906636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
54916636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
54926636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
54936636e97aSMatthew G Knepley 
54942dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
54952dd40e9bSPatrick Sanan 
54966636e97aSMatthew G Knepley   Level: intermediate
54976636e97aSMatthew G Knepley 
54986636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
54996636e97aSMatthew G Knepley @*/
55006636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
55016636e97aSMatthew G Knepley {
55026636e97aSMatthew G Knepley   PetscErrorCode ierr;
55036636e97aSMatthew G Knepley 
55046636e97aSMatthew G Knepley   PetscFunctionBegin;
55056636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55066636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55076636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55086636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
55098865f1eaSKarl Rupp 
55106636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
55118865f1eaSKarl Rupp 
55126636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55146636e97aSMatthew G Knepley }
55156636e97aSMatthew G Knepley 
55166636e97aSMatthew G Knepley /*@
55176636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
55186636e97aSMatthew G Knepley 
5519d083f849SBarry Smith   Collective on dm
55206636e97aSMatthew G Knepley 
55216636e97aSMatthew G Knepley   Input Parameter:
55226636e97aSMatthew G Knepley . dm - the DM
55236636e97aSMatthew G Knepley 
55246636e97aSMatthew G Knepley   Output Parameter:
55256636e97aSMatthew G Knepley . c - global coordinate vector
55266636e97aSMatthew G Knepley 
55276636e97aSMatthew G Knepley   Note:
55286636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
55296636e97aSMatthew G Knepley 
55306636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
55316636e97aSMatthew G Knepley 
55326636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55336636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55346636e97aSMatthew G Knepley 
55356636e97aSMatthew G Knepley   Level: intermediate
55366636e97aSMatthew G Knepley 
55376636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55386636e97aSMatthew G Knepley @*/
55396636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
55406636e97aSMatthew G Knepley {
55416636e97aSMatthew G Knepley   PetscErrorCode ierr;
55426636e97aSMatthew G Knepley 
55436636e97aSMatthew G Knepley   PetscFunctionBegin;
55446636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55456636e97aSMatthew G Knepley   PetscValidPointer(c,2);
55461f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
55470298fd71SBarry Smith     DM        cdm = NULL;
55481970a576SMatthew G. Knepley     PetscBool localized;
55496636e97aSMatthew G Knepley 
55506636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
55516636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
55521970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
55531970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
55541970a576SMatthew G. Knepley     if (localized) {
55551970a576SMatthew G. Knepley       PetscInt cdim;
55561970a576SMatthew G. Knepley 
55571970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
55581970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
55591970a576SMatthew G. Knepley     }
55606636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
55616636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55626636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55636636e97aSMatthew G Knepley   }
55646636e97aSMatthew G Knepley   *c = dm->coordinates;
55656636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55666636e97aSMatthew G Knepley }
55676636e97aSMatthew G Knepley 
55686636e97aSMatthew G Knepley /*@
556981e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
557081e9a530SVaclav Hapla 
5571d083f849SBarry Smith   Collective on dm
557281e9a530SVaclav Hapla 
557381e9a530SVaclav Hapla   Input Parameter:
557481e9a530SVaclav Hapla . dm - the DM
557581e9a530SVaclav Hapla 
557681e9a530SVaclav Hapla   Level: advanced
557781e9a530SVaclav Hapla 
557881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
557981e9a530SVaclav Hapla @*/
558081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
558181e9a530SVaclav Hapla {
558281e9a530SVaclav Hapla   PetscErrorCode ierr;
558381e9a530SVaclav Hapla 
558481e9a530SVaclav Hapla   PetscFunctionBegin;
558581e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
558681e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
55871970a576SMatthew G. Knepley     DM        cdm = NULL;
55881970a576SMatthew G. Knepley     PetscBool localized;
55891970a576SMatthew G. Knepley 
559081e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
559181e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
55921970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
55931970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
55941970a576SMatthew G. Knepley     if (localized) {
55951970a576SMatthew G. Knepley       PetscInt cdim;
55961970a576SMatthew G. Knepley 
55971970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
55981970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
55991970a576SMatthew G. Knepley     }
560081e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
560181e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
560281e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
560381e9a530SVaclav Hapla   }
560481e9a530SVaclav Hapla   PetscFunctionReturn(0);
560581e9a530SVaclav Hapla }
560681e9a530SVaclav Hapla 
560781e9a530SVaclav Hapla /*@
56086636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
56096636e97aSMatthew G Knepley 
5610d083f849SBarry Smith   Collective on dm
56116636e97aSMatthew G Knepley 
56126636e97aSMatthew G Knepley   Input Parameter:
56136636e97aSMatthew G Knepley . dm - the DM
56146636e97aSMatthew G Knepley 
56156636e97aSMatthew G Knepley   Output Parameter:
56166636e97aSMatthew G Knepley . c - coordinate vector
56176636e97aSMatthew G Knepley 
56186636e97aSMatthew G Knepley   Note:
56196636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56206636e97aSMatthew G Knepley 
56216636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
56226636e97aSMatthew G Knepley 
56236636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56246636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56256636e97aSMatthew G Knepley 
56266636e97aSMatthew G Knepley   Level: intermediate
56276636e97aSMatthew G Knepley 
562881e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
56296636e97aSMatthew G Knepley @*/
56306636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
56316636e97aSMatthew G Knepley {
56326636e97aSMatthew G Knepley   PetscErrorCode ierr;
56336636e97aSMatthew G Knepley 
56346636e97aSMatthew G Knepley   PetscFunctionBegin;
56356636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56366636e97aSMatthew G Knepley   PetscValidPointer(c,2);
563781e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
56386636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56396636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56406636e97aSMatthew G Knepley }
56416636e97aSMatthew G Knepley 
564281e9a530SVaclav Hapla /*@
564381e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
564481e9a530SVaclav Hapla 
564581e9a530SVaclav Hapla   Not collective
564681e9a530SVaclav Hapla 
564781e9a530SVaclav Hapla   Input Parameter:
564881e9a530SVaclav Hapla . dm - the DM
564981e9a530SVaclav Hapla 
565081e9a530SVaclav Hapla   Output Parameter:
565181e9a530SVaclav Hapla . c - coordinate vector
565281e9a530SVaclav Hapla 
565381e9a530SVaclav Hapla   Level: advanced
565481e9a530SVaclav Hapla 
565581e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
565681e9a530SVaclav Hapla @*/
565781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
565881e9a530SVaclav Hapla {
565981e9a530SVaclav Hapla   PetscFunctionBegin;
566081e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
566181e9a530SVaclav Hapla   PetscValidPointer(c,2);
566281e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
56636636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56646636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56656636e97aSMatthew G Knepley }
56666636e97aSMatthew G Knepley 
56672db98f8dSVaclav Hapla /*@
56682db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
56692db98f8dSVaclav Hapla 
56702db98f8dSVaclav Hapla   Not collective
56712db98f8dSVaclav Hapla 
56722db98f8dSVaclav Hapla   Input Parameter:
56732db98f8dSVaclav Hapla + dm - the DM
56742db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
56752db98f8dSVaclav Hapla 
56762db98f8dSVaclav Hapla   Output Parameter:
56772db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
56782db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
56792db98f8dSVaclav Hapla 
56802db98f8dSVaclav Hapla   Note:
56812db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
56822db98f8dSVaclav Hapla 
56832db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
56842db98f8dSVaclav Hapla 
56852db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
56862db98f8dSVaclav Hapla 
56872db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56882db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56892db98f8dSVaclav Hapla 
56902db98f8dSVaclav Hapla   Level: advanced
56912db98f8dSVaclav Hapla 
56922db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
56932db98f8dSVaclav Hapla @*/
56942db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
56952db98f8dSVaclav Hapla {
56962db98f8dSVaclav Hapla   PetscSection        cs, newcs;
56972db98f8dSVaclav Hapla   Vec                 coords;
56982db98f8dSVaclav Hapla   const PetscScalar   *arr;
56992db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
57002db98f8dSVaclav Hapla   PetscInt            n;
57012db98f8dSVaclav Hapla   PetscErrorCode      ierr;
57022db98f8dSVaclav Hapla 
57032db98f8dSVaclav Hapla   PetscFunctionBegin;
57042db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57052db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
57062db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
57072db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
57082db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
57091bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
57101bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
57112db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
57122db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
57132db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
57142db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
57152db98f8dSVaclav Hapla   if (pCoord) {
57162db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
57172db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
57182db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
57192db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5720ad9ac99dSVaclav Hapla   } else {
5721ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
57222db98f8dSVaclav Hapla   }
5723ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5724ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
57252db98f8dSVaclav Hapla   PetscFunctionReturn(0);
57262db98f8dSVaclav Hapla }
57272db98f8dSVaclav Hapla 
5728f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5729f19dbd58SToby Isaac {
5730f19dbd58SToby Isaac   PetscErrorCode ierr;
5731f19dbd58SToby Isaac 
5732f19dbd58SToby Isaac   PetscFunctionBegin;
5733f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5734f19dbd58SToby Isaac   PetscValidPointer(field,2);
5735f19dbd58SToby Isaac   if (!dm->coordinateField) {
5736f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5737f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5738f19dbd58SToby Isaac     }
5739f19dbd58SToby Isaac   }
5740f19dbd58SToby Isaac   *field = dm->coordinateField;
5741f19dbd58SToby Isaac   PetscFunctionReturn(0);
5742f19dbd58SToby Isaac }
5743f19dbd58SToby Isaac 
5744f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5745f19dbd58SToby Isaac {
5746f19dbd58SToby Isaac   PetscErrorCode ierr;
5747f19dbd58SToby Isaac 
5748f19dbd58SToby Isaac   PetscFunctionBegin;
5749f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5750f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5751c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5752f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5753f19dbd58SToby Isaac   dm->coordinateField = field;
5754f19dbd58SToby Isaac   PetscFunctionReturn(0);
5755f19dbd58SToby Isaac }
5756f19dbd58SToby Isaac 
57576636e97aSMatthew G Knepley /*@
57581cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
57596636e97aSMatthew G Knepley 
5760d083f849SBarry Smith   Collective on dm
57616636e97aSMatthew G Knepley 
57626636e97aSMatthew G Knepley   Input Parameter:
57636636e97aSMatthew G Knepley . dm - the DM
57646636e97aSMatthew G Knepley 
57656636e97aSMatthew G Knepley   Output Parameter:
57666636e97aSMatthew G Knepley . cdm - coordinate DM
57676636e97aSMatthew G Knepley 
57686636e97aSMatthew G Knepley   Level: intermediate
57696636e97aSMatthew G Knepley 
57701cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
57716636e97aSMatthew G Knepley @*/
57726636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
57736636e97aSMatthew G Knepley {
57746636e97aSMatthew G Knepley   PetscErrorCode ierr;
57756636e97aSMatthew G Knepley 
57766636e97aSMatthew G Knepley   PetscFunctionBegin;
57776636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
57786636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
57796636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5780308f8a94SToby Isaac     DM cdm;
5781308f8a94SToby Isaac 
578282f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5783308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5784308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5785308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5786308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5787308f8a94SToby Isaac     dm->coordinateDM = cdm;
57886636e97aSMatthew G Knepley   }
57896636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
57906636e97aSMatthew G Knepley   PetscFunctionReturn(0);
57916636e97aSMatthew G Knepley }
5792e87bb0d3SMatthew G Knepley 
57931cfe2091SMatthew G. Knepley /*@
57941cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
57951cfe2091SMatthew G. Knepley 
5796d083f849SBarry Smith   Logically Collective on dm
57971cfe2091SMatthew G. Knepley 
57981cfe2091SMatthew G. Knepley   Input Parameters:
57991cfe2091SMatthew G. Knepley + dm - the DM
58001cfe2091SMatthew G. Knepley - cdm - coordinate DM
58011cfe2091SMatthew G. Knepley 
58021cfe2091SMatthew G. Knepley   Level: intermediate
58031cfe2091SMatthew G. Knepley 
58041cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58051cfe2091SMatthew G. Knepley @*/
58061cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
58071cfe2091SMatthew G. Knepley {
58081cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
58091cfe2091SMatthew G. Knepley 
58101cfe2091SMatthew G. Knepley   PetscFunctionBegin;
58111cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58121cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5813f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
58141cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
58151cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
58161cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
58171cfe2091SMatthew G. Knepley }
58181cfe2091SMatthew G. Knepley 
581946e270d4SMatthew G. Knepley /*@
582046e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
582146e270d4SMatthew G. Knepley 
582246e270d4SMatthew G. Knepley   Not Collective
582346e270d4SMatthew G. Knepley 
582446e270d4SMatthew G. Knepley   Input Parameter:
582546e270d4SMatthew G. Knepley . dm - The DM object
582646e270d4SMatthew G. Knepley 
582746e270d4SMatthew G. Knepley   Output Parameter:
582846e270d4SMatthew G. Knepley . dim - The embedding dimension
582946e270d4SMatthew G. Knepley 
583046e270d4SMatthew G. Knepley   Level: intermediate
583146e270d4SMatthew G. Knepley 
583292fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
583346e270d4SMatthew G. Knepley @*/
583446e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
583546e270d4SMatthew G. Knepley {
583646e270d4SMatthew G. Knepley   PetscFunctionBegin;
583746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5838534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
58399a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
58409a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
58419a9a41abSToby Isaac   }
584246e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
584346e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
584446e270d4SMatthew G. Knepley }
584546e270d4SMatthew G. Knepley 
584646e270d4SMatthew G. Knepley /*@
584746e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
584846e270d4SMatthew G. Knepley 
584946e270d4SMatthew G. Knepley   Not Collective
585046e270d4SMatthew G. Knepley 
585146e270d4SMatthew G. Knepley   Input Parameters:
585246e270d4SMatthew G. Knepley + dm  - The DM object
585346e270d4SMatthew G. Knepley - dim - The embedding dimension
585446e270d4SMatthew G. Knepley 
585546e270d4SMatthew G. Knepley   Level: intermediate
585646e270d4SMatthew G. Knepley 
585792fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
585846e270d4SMatthew G. Knepley @*/
585946e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
586046e270d4SMatthew G. Knepley {
5861e5e52638SMatthew G. Knepley   PetscDS        ds;
5862f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5863f17e8794SMatthew G. Knepley 
586446e270d4SMatthew G. Knepley   PetscFunctionBegin;
586546e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
586646e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5867e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5868e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
586946e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
587046e270d4SMatthew G. Knepley }
587146e270d4SMatthew G. Knepley 
5872e8abe2deSMatthew G. Knepley /*@
5873e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5874e8abe2deSMatthew G. Knepley 
5875d083f849SBarry Smith   Collective on dm
5876e8abe2deSMatthew G. Knepley 
5877e8abe2deSMatthew G. Knepley   Input Parameter:
5878e8abe2deSMatthew G. Knepley . dm - The DM object
5879e8abe2deSMatthew G. Knepley 
5880e8abe2deSMatthew G. Knepley   Output Parameter:
5881e8abe2deSMatthew G. Knepley . section - The PetscSection object
5882e8abe2deSMatthew G. Knepley 
5883e8abe2deSMatthew G. Knepley   Level: intermediate
5884e8abe2deSMatthew G. Knepley 
588592fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5886e8abe2deSMatthew G. Knepley @*/
5887e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5888e8abe2deSMatthew G. Knepley {
5889e8abe2deSMatthew G. Knepley   DM             cdm;
5890e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5891e8abe2deSMatthew G. Knepley 
5892e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5893e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5894e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5895e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
589692fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5897e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5898e8abe2deSMatthew G. Knepley }
5899e8abe2deSMatthew G. Knepley 
5900e8abe2deSMatthew G. Knepley /*@
5901e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5902e8abe2deSMatthew G. Knepley 
5903e8abe2deSMatthew G. Knepley   Not Collective
5904e8abe2deSMatthew G. Knepley 
5905e8abe2deSMatthew G. Knepley   Input Parameters:
5906e8abe2deSMatthew G. Knepley + dm      - The DM object
590746e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5908e8abe2deSMatthew G. Knepley - section - The PetscSection object
5909e8abe2deSMatthew G. Knepley 
5910e8abe2deSMatthew G. Knepley   Level: intermediate
5911e8abe2deSMatthew G. Knepley 
591292fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5913e8abe2deSMatthew G. Knepley @*/
591446e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5915e8abe2deSMatthew G. Knepley {
5916e8abe2deSMatthew G. Knepley   DM             cdm;
5917e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5918e8abe2deSMatthew G. Knepley 
5919e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5920e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
592146e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5922e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
592392fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
592446e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
59254c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
592646e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
592746e270d4SMatthew G. Knepley 
592846e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
592946e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
593046e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
593146e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
593246e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
593346e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
593446e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
593546e270d4SMatthew G. Knepley     }
5936ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
593746e270d4SMatthew G. Knepley   }
5938e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5939e8abe2deSMatthew G. Knepley }
5940e8abe2deSMatthew G. Knepley 
59415dc8c3f7SMatthew G. Knepley /*@C
594290b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
59435dc8c3f7SMatthew G. Knepley 
59445dc8c3f7SMatthew G. Knepley   Input Parameters:
594590b157c4SStefano Zampini . dm      - The DM object
594690b157c4SStefano Zampini 
594790b157c4SStefano Zampini   Output Parameters:
594890b157c4SStefano Zampini + per     - Whether the DM is periodic or not
59495dc8c3f7SMatthew 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
59505dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59515dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
59525dc8c3f7SMatthew G. Knepley 
59535dc8c3f7SMatthew G. Knepley   Level: developer
59545dc8c3f7SMatthew G. Knepley 
59555dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
59565dc8c3f7SMatthew G. Knepley @*/
595790b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5958c6b900c6SMatthew G. Knepley {
5959c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5960c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
596190b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5962c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5963c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
59645dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5965c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5966c6b900c6SMatthew G. Knepley }
5967c6b900c6SMatthew G. Knepley 
59685dc8c3f7SMatthew G. Knepley /*@C
59695dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
59705dc8c3f7SMatthew G. Knepley 
59715dc8c3f7SMatthew G. Knepley   Input Parameters:
59725dc8c3f7SMatthew G. Knepley + dm      - The DM object
597390b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
59745dc8c3f7SMatthew 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
59755dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59765dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
59775dc8c3f7SMatthew G. Knepley 
59785dc8c3f7SMatthew G. Knepley   Level: developer
59795dc8c3f7SMatthew G. Knepley 
59805dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
59815dc8c3f7SMatthew G. Knepley @*/
598290b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
5983c6b900c6SMatthew G. Knepley {
5984c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
5985c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
5986c6b900c6SMatthew G. Knepley 
5987c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5988c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
598990b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
599090b157c4SStefano Zampini   if (maxCell) {
5991534a8f05SLisandro Dalcin     PetscValidRealPointer(maxCell,3);
5992534a8f05SLisandro Dalcin     PetscValidRealPointer(L,4);
599390b157c4SStefano Zampini     PetscValidPointer(bd,5);
599490b157c4SStefano Zampini   }
59955dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
59965dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
599790b157c4SStefano Zampini   if (maxCell) {
59985dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
59995dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
600090b157c4SStefano Zampini   }
6001072d7d67SStefano Zampini   dm->periodic = per;
6002c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6003c6b900c6SMatthew G. Knepley }
6004c6b900c6SMatthew G. Knepley 
60052e17dfb7SMatthew G. Knepley /*@
60062e17dfb7SMatthew 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.
60072e17dfb7SMatthew G. Knepley 
60082e17dfb7SMatthew G. Knepley   Input Parameters:
60092e17dfb7SMatthew G. Knepley + dm     - The DM
601065da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
601165da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
60122e17dfb7SMatthew G. Knepley 
60132e17dfb7SMatthew G. Knepley   Output Parameter:
60142e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60152e17dfb7SMatthew G. Knepley 
60162e17dfb7SMatthew G. Knepley   Level: developer
60172e17dfb7SMatthew G. Knepley 
60182e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60192e17dfb7SMatthew G. Knepley @*/
602065da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
60212e17dfb7SMatthew G. Knepley {
60222e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
60232e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
60242e17dfb7SMatthew G. Knepley 
60252e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60262e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
60272e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60282e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60292e17dfb7SMatthew G. Knepley   } else {
603065da65dcSMatthew G. Knepley     if (endpoint) {
603165da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6032da3333bfSMatthew 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)) {
6033da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
603465da65dcSMatthew G. Knepley         } else {
6035da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
603665da65dcSMatthew G. Knepley         }
603765da65dcSMatthew G. Knepley       }
603865da65dcSMatthew G. Knepley     } else {
60392e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
60401118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
60412e17dfb7SMatthew G. Knepley       }
60422e17dfb7SMatthew G. Knepley     }
604365da65dcSMatthew G. Knepley   }
60442e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60452e17dfb7SMatthew G. Knepley }
60462e17dfb7SMatthew G. Knepley 
60472e17dfb7SMatthew G. Knepley /*
60482e17dfb7SMatthew 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.
60492e17dfb7SMatthew G. Knepley 
60502e17dfb7SMatthew G. Knepley   Input Parameters:
60512e17dfb7SMatthew G. Knepley + dm     - The DM
60522e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
60532e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
60542e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
60552e17dfb7SMatthew G. Knepley 
60562e17dfb7SMatthew G. Knepley   Output Parameter:
60572e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60582e17dfb7SMatthew G. Knepley 
60592e17dfb7SMatthew G. Knepley   Level: developer
60602e17dfb7SMatthew G. Knepley 
60612e17dfb7SMatthew 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
60622e17dfb7SMatthew G. Knepley 
60632e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60642e17dfb7SMatthew G. Knepley */
60652e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
60662e17dfb7SMatthew G. Knepley {
60672e17dfb7SMatthew G. Knepley   PetscInt d;
60682e17dfb7SMatthew G. Knepley 
60692e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60702e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60712e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60722e17dfb7SMatthew G. Knepley   } else {
60732e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6074908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
60752e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
60762e17dfb7SMatthew G. Knepley       } else {
60772e17dfb7SMatthew G. Knepley         out[d] = in[d];
60782e17dfb7SMatthew G. Knepley       }
60792e17dfb7SMatthew G. Knepley     }
60802e17dfb7SMatthew G. Knepley   }
60812e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60822e17dfb7SMatthew G. Knepley }
60832e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
60842e17dfb7SMatthew G. Knepley {
60852e17dfb7SMatthew G. Knepley   PetscInt d;
60862e17dfb7SMatthew G. Knepley 
60872e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60882e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60892e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60902e17dfb7SMatthew G. Knepley   } else {
60912e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6092908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
60932e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
60942e17dfb7SMatthew G. Knepley       } else {
60952e17dfb7SMatthew G. Knepley         out[d] = in[d];
60962e17dfb7SMatthew G. Knepley       }
60972e17dfb7SMatthew G. Knepley     }
60982e17dfb7SMatthew G. Knepley   }
60992e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61002e17dfb7SMatthew G. Knepley }
61012e17dfb7SMatthew G. Knepley 
61022e17dfb7SMatthew G. Knepley /*
61032e17dfb7SMatthew 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.
61042e17dfb7SMatthew G. Knepley 
61052e17dfb7SMatthew G. Knepley   Input Parameters:
61062e17dfb7SMatthew G. Knepley + dm     - The DM
61072e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61082e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61092e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
61102e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
61112e17dfb7SMatthew G. Knepley 
61122e17dfb7SMatthew G. Knepley   Output Parameter:
61132e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
61142e17dfb7SMatthew G. Knepley 
61152e17dfb7SMatthew G. Knepley   Level: developer
61162e17dfb7SMatthew G. Knepley 
61172e17dfb7SMatthew 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
61182e17dfb7SMatthew G. Knepley 
61192e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
61202e17dfb7SMatthew G. Knepley */
61212e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61222e17dfb7SMatthew G. Knepley {
61232e17dfb7SMatthew G. Knepley   PetscInt d;
61242e17dfb7SMatthew G. Knepley 
61252e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61262e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61272e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
61282e17dfb7SMatthew G. Knepley   } else {
61292e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6130908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61312e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61322e17dfb7SMatthew G. Knepley       } else {
61332e17dfb7SMatthew G. Knepley         out[d] += in[d];
61342e17dfb7SMatthew G. Knepley       }
61352e17dfb7SMatthew G. Knepley     }
61362e17dfb7SMatthew G. Knepley   }
61372e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61382e17dfb7SMatthew G. Knepley }
61392e17dfb7SMatthew G. Knepley 
614036447a5eSToby Isaac /*@
61418f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
61428f700142SStefano Zampini 
61438f700142SStefano Zampini   Not collective
614436447a5eSToby Isaac 
614536447a5eSToby Isaac   Input Parameter:
614636447a5eSToby Isaac . dm - The DM
614736447a5eSToby Isaac 
614836447a5eSToby Isaac   Output Parameter:
614936447a5eSToby Isaac   areLocalized - True if localized
615036447a5eSToby Isaac 
615136447a5eSToby Isaac   Level: developer
615236447a5eSToby Isaac 
61538f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
615436447a5eSToby Isaac @*/
61558f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
615636447a5eSToby Isaac {
615736447a5eSToby Isaac   DM             cdm;
615836447a5eSToby Isaac   PetscSection   coordSection;
615946a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
616046a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
616136447a5eSToby Isaac   PetscErrorCode ierr;
616236447a5eSToby Isaac 
616336447a5eSToby Isaac   PetscFunctionBegin;
616436447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6165534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
61668b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
616746a3a80fSLisandro Dalcin 
616836447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
616936447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
617046a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
61719f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
617246a3a80fSLisandro Dalcin 
61739f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
617446a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
617536447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
617636447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
617746a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
617846a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
617936447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
618046a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
618136447a5eSToby Isaac   }
61828f700142SStefano Zampini   *areLocalized = alreadyLocalized;
618336447a5eSToby Isaac   PetscFunctionReturn(0);
618436447a5eSToby Isaac }
618536447a5eSToby Isaac 
61868f700142SStefano Zampini /*@
61878f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
61888f700142SStefano Zampini 
61898f700142SStefano Zampini   Collective on dm
61908f700142SStefano Zampini 
61918f700142SStefano Zampini   Input Parameter:
61928f700142SStefano Zampini . dm - The DM
61938f700142SStefano Zampini 
61948f700142SStefano Zampini   Output Parameter:
61958f700142SStefano Zampini   areLocalized - True if localized
61968f700142SStefano Zampini 
61978f700142SStefano Zampini   Level: developer
61988f700142SStefano Zampini 
61998f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
62008f700142SStefano Zampini @*/
62018f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
62028f700142SStefano Zampini {
62038f700142SStefano Zampini   PetscBool      localized;
62048f700142SStefano Zampini   PetscErrorCode ierr;
62058f700142SStefano Zampini 
62068f700142SStefano Zampini   PetscFunctionBegin;
62078f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6208534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62098f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
62108f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
62118f700142SStefano Zampini   PetscFunctionReturn(0);
62128f700142SStefano Zampini }
621336447a5eSToby Isaac 
62142e17dfb7SMatthew G. Knepley /*@
6215492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
62162e17dfb7SMatthew G. Knepley 
62178f700142SStefano Zampini   Collective on dm
62188f700142SStefano Zampini 
62192e17dfb7SMatthew G. Knepley   Input Parameter:
62202e17dfb7SMatthew G. Knepley . dm - The DM
62212e17dfb7SMatthew G. Knepley 
62222e17dfb7SMatthew G. Knepley   Level: developer
62232e17dfb7SMatthew G. Knepley 
62248f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
62252e17dfb7SMatthew G. Knepley @*/
62262e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
62272e17dfb7SMatthew G. Knepley {
62282e17dfb7SMatthew G. Knepley   DM             cdm;
62292e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
62302e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
62313e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
62323e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6233e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
62343e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
62353e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
62362e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
62372e17dfb7SMatthew G. Knepley 
62382e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62392e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
624092c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6241f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6242f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6243f7cbd40bSStefano Zampini 
62442e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
62452e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62462e17dfb7SMatthew G. Knepley   {
62472e17dfb7SMatthew G. Knepley     PetscBool isplex;
62482e17dfb7SMatthew G. Knepley 
62492e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
62502e17dfb7SMatthew G. Knepley     if (isplex) {
62512e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
62523e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
625369291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
62543e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
62553e922f36SToby Isaac       newStart = vStart;
62563e922f36SToby Isaac       newEnd   = vEnd;
62573e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
62583e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
62593e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
62603e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
62613e922f36SToby Isaac       }
62622e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
62632e17dfb7SMatthew G. Knepley   }
62642e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
626543eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
62662e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
62673e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6268e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
62693e922f36SToby Isaac 
62702e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
62712e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
62722e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
62732e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
62743e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
62753e922f36SToby Isaac 
627669291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
62773e922f36SToby Isaac   localized = &anchor[bs];
62783e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
62793e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
62803e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
62813e922f36SToby Isaac 
62823e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
62833e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
62843e922f36SToby Isaac       PetscInt     b;
62853e922f36SToby Isaac 
62863e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
62873e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
62883e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
62893e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
62903e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
62913e922f36SToby Isaac         for (b = 0; b < bs; b++) {
62923e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
62933e922f36SToby Isaac         }
62943e922f36SToby Isaac         if (b < bs) break;
62953e922f36SToby Isaac       }
62963e922f36SToby Isaac       if (d < dof/bs) {
62973e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
62983e922f36SToby Isaac           PetscInt cdof;
62993e922f36SToby Isaac 
63003e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
63013e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
63023e922f36SToby Isaac         }
63033e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
63043e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
63053e922f36SToby Isaac       }
63063e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63073e922f36SToby Isaac     }
63083e922f36SToby Isaac   }
63093e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63103e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
631169291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63123e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
631369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63143e922f36SToby Isaac     PetscFunctionReturn(0);
63153e922f36SToby Isaac   }
63162e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63172e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63182e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
63192e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
63202e17dfb7SMatthew G. Knepley   }
63212e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
63222e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6323c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
63242e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
63252e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
63262e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
63272e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6328c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63292e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
63302e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63312e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63322e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
63332e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
63342e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
63352e17dfb7SMatthew G. Knepley   }
63363e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63373e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63383e922f36SToby Isaac 
63392e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
63402e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
63413e922f36SToby Isaac       PetscInt     b, cdof;
63422e17dfb7SMatthew G. Knepley 
63433e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
63443e922f36SToby Isaac       if (!cdof) continue;
63452e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63462e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
63472e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63482e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
63492e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63502e17dfb7SMatthew G. Knepley     }
63513e922f36SToby Isaac   }
635269291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
635369291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6354c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63552e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
63562e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
63572e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
63582e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
63592e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
63602e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63612e17dfb7SMatthew G. Knepley }
63622e17dfb7SMatthew G. Knepley 
6363e87bb0d3SMatthew G Knepley /*@
63643a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6365e87bb0d3SMatthew G Knepley 
6366d083f849SBarry Smith   Collective on v (see explanation below)
6367e87bb0d3SMatthew G Knepley 
6368e87bb0d3SMatthew G Knepley   Input Parameters:
6369e87bb0d3SMatthew G Knepley + dm - The DM
63703a93e3b7SToby Isaac . v - The Vec of points
637162a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
63723a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6373e87bb0d3SMatthew G Knepley 
637461e3bb9bSMatthew G Knepley   Output Parameter:
637562a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
637662a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
63773a93e3b7SToby Isaac 
6378e87bb0d3SMatthew G Knepley 
6379e87bb0d3SMatthew G Knepley   Level: developer
638061e3bb9bSMatthew G Knepley 
638162a38674SMatthew G. Knepley   Notes:
63823a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
638362a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
63843a93e3b7SToby Isaac 
63853a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
638662a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
63873a93e3b7SToby Isaac 
63883a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
63893a93e3b7SToby Isaac 
639062a38674SMatthew G. Knepley $    const PetscSFNode *cells;
639162a38674SMatthew G. Knepley $    PetscInt           nFound;
6392a6216909SToby Isaac $    const PetscInt    *found;
639362a38674SMatthew G. Knepley $
6394a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
63953a93e3b7SToby Isaac 
63963a93e3b7SToby 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
63973a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
63983a93e3b7SToby Isaac 
639962a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
640061e3bb9bSMatthew G Knepley @*/
640162a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6402e87bb0d3SMatthew G Knepley {
6403735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6404735aa83eSMatthew G Knepley 
6405e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6406e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6407e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6408e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
64093a93e3b7SToby Isaac   if (*cellSF) {
64103a93e3b7SToby Isaac     PetscMPIInt result;
64113a93e3b7SToby Isaac 
6412e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6413a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
64143a93e3b7SToby 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");
6415e0fc9d1bSMatthew G. Knepley   } else {
64163a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
64173a93e3b7SToby Isaac   }
6418b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
641947a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
642062a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
642147a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6422e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6423e87bb0d3SMatthew G Knepley }
642414f150ffSMatthew G. Knepley 
6425f4d763aaSMatthew G. Knepley /*@
6426f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6427f4d763aaSMatthew G. Knepley 
64288f700142SStefano Zampini   Collective on dm
64298f700142SStefano Zampini 
6430f4d763aaSMatthew G. Knepley   Input Parameter:
6431f4d763aaSMatthew G. Knepley . dm - The original DM
6432f4d763aaSMatthew G. Knepley 
6433f4d763aaSMatthew G. Knepley   Output Parameter:
6434f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6435f4d763aaSMatthew G. Knepley 
6436f4d763aaSMatthew G. Knepley   Level: intermediate
6437f4d763aaSMatthew G. Knepley 
6438e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6439f4d763aaSMatthew G. Knepley @*/
644014f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
644114f150ffSMatthew G. Knepley {
6442c26acbdeSMatthew G. Knepley   PetscSection   section;
64432d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
644414f150ffSMatthew G. Knepley   PetscErrorCode ierr;
644514f150ffSMatthew G. Knepley 
644614f150ffSMatthew G. Knepley   PetscFunctionBegin;
644714f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
644814f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
644992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6450c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6451127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
64522d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6453c26acbdeSMatthew G. Knepley     *odm = dm;
6454c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6455c26acbdeSMatthew G. Knepley   }
645614f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6457c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
645814f150ffSMatthew G. Knepley     PetscSF      sf;
645914f150ffSMatthew G. Knepley 
646014f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6461e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
646214f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
646392fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
646414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
646514f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
646615b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6467e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
646814f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
646914f150ffSMatthew G. Knepley   }
647014f150ffSMatthew G. Knepley   *odm = dm->dmBC;
647114f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
647214f150ffSMatthew G. Knepley }
6473f4d763aaSMatthew G. Knepley 
6474f4d763aaSMatthew G. Knepley /*@
6475cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6476f4d763aaSMatthew G. Knepley 
6477f4d763aaSMatthew G. Knepley   Input Parameter:
6478f4d763aaSMatthew G. Knepley . dm - The original DM
6479f4d763aaSMatthew G. Knepley 
6480cdb7a50dSMatthew G. Knepley   Output Parameters:
6481cdb7a50dSMatthew G. Knepley + num - The output sequence number
6482cdb7a50dSMatthew G. Knepley - val - The output sequence value
6483f4d763aaSMatthew G. Knepley 
6484f4d763aaSMatthew G. Knepley   Level: intermediate
6485f4d763aaSMatthew G. Knepley 
6486f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6487f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6488f4d763aaSMatthew G. Knepley 
6489f4d763aaSMatthew G. Knepley .seealso: VecView()
6490f4d763aaSMatthew G. Knepley @*/
6491cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6492f4d763aaSMatthew G. Knepley {
6493f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6494f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6495534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6496534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6497f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6498f4d763aaSMatthew G. Knepley }
6499f4d763aaSMatthew G. Knepley 
6500f4d763aaSMatthew G. Knepley /*@
6501cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6502f4d763aaSMatthew G. Knepley 
6503f4d763aaSMatthew G. Knepley   Input Parameters:
6504f4d763aaSMatthew G. Knepley + dm - The original DM
6505cdb7a50dSMatthew G. Knepley . num - The output sequence number
6506cdb7a50dSMatthew G. Knepley - val - The output sequence value
6507f4d763aaSMatthew G. Knepley 
6508f4d763aaSMatthew G. Knepley   Level: intermediate
6509f4d763aaSMatthew G. Knepley 
6510f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6511f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6512f4d763aaSMatthew G. Knepley 
6513f4d763aaSMatthew G. Knepley .seealso: VecView()
6514f4d763aaSMatthew G. Knepley @*/
6515cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6516f4d763aaSMatthew G. Knepley {
6517f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6518f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6519f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6520cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6521cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6522cdb7a50dSMatthew G. Knepley }
6523cdb7a50dSMatthew G. Knepley 
6524cdb7a50dSMatthew G. Knepley /*@C
6525cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6526cdb7a50dSMatthew G. Knepley 
6527cdb7a50dSMatthew G. Knepley   Input Parameters:
6528cdb7a50dSMatthew G. Knepley + dm   - The original DM
6529cdb7a50dSMatthew G. Knepley . name - The sequence name
6530cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6531cdb7a50dSMatthew G. Knepley 
6532cdb7a50dSMatthew G. Knepley   Output Parameter:
6533cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6534cdb7a50dSMatthew G. Knepley 
6535cdb7a50dSMatthew G. Knepley   Level: intermediate
6536cdb7a50dSMatthew G. Knepley 
6537cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6538cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6539cdb7a50dSMatthew G. Knepley 
6540cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6541cdb7a50dSMatthew G. Knepley @*/
6542cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6543cdb7a50dSMatthew G. Knepley {
6544cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6545cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6546cdb7a50dSMatthew G. Knepley 
6547cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6548cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6549cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6550534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6551cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6552cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6553cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6554cdb7a50dSMatthew G. Knepley     PetscScalar value;
6555cdb7a50dSMatthew G. Knepley 
655639d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
65574aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6558cdb7a50dSMatthew G. Knepley #endif
6559cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6560f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6561f4d763aaSMatthew G. Knepley }
65628e4ac7eaSMatthew G. Knepley 
65638e4ac7eaSMatthew G. Knepley /*@
65648e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
65658e4ac7eaSMatthew G. Knepley 
65668e4ac7eaSMatthew G. Knepley   Not collective
65678e4ac7eaSMatthew G. Knepley 
65688e4ac7eaSMatthew G. Knepley   Input Parameter:
65698e4ac7eaSMatthew G. Knepley . dm - The DM
65708e4ac7eaSMatthew G. Knepley 
65718e4ac7eaSMatthew G. Knepley   Output Parameter:
65728e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
65738e4ac7eaSMatthew G. Knepley 
65748e4ac7eaSMatthew G. Knepley   Level: beginner
65758e4ac7eaSMatthew G. Knepley 
65768e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
65778e4ac7eaSMatthew G. Knepley @*/
65788e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
65798e4ac7eaSMatthew G. Knepley {
65808e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
65818e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6582534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
65838e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
65848e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
65858e4ac7eaSMatthew G. Knepley }
65868e4ac7eaSMatthew G. Knepley 
65878e4ac7eaSMatthew G. Knepley /*@
65885d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
65898e4ac7eaSMatthew G. Knepley 
65908e4ac7eaSMatthew G. Knepley   Collective on dm
65918e4ac7eaSMatthew G. Knepley 
65928e4ac7eaSMatthew G. Knepley   Input Parameters:
65938e4ac7eaSMatthew G. Knepley + dm - The DM
65948e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
65958e4ac7eaSMatthew G. Knepley 
65965d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
65975d3b26e6SMatthew G. Knepley 
65988e4ac7eaSMatthew G. Knepley   Level: beginner
65998e4ac7eaSMatthew G. Knepley 
66005d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
66018e4ac7eaSMatthew G. Knepley @*/
66028e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
66038e4ac7eaSMatthew G. Knepley {
66048e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66058e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66068833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66078e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66088e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66098e4ac7eaSMatthew G. Knepley }
6610c58f1c22SToby Isaac 
6611c58f1c22SToby Isaac 
6612c58f1c22SToby Isaac /*@C
6613c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6614c58f1c22SToby Isaac 
6615c58f1c22SToby Isaac   Not Collective
6616c58f1c22SToby Isaac 
6617c58f1c22SToby Isaac   Input Parameters:
6618c58f1c22SToby Isaac + dm   - The DM object
6619c58f1c22SToby Isaac - name - The label name
6620c58f1c22SToby Isaac 
6621c58f1c22SToby Isaac   Level: intermediate
6622c58f1c22SToby Isaac 
6623c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6624c58f1c22SToby Isaac @*/
6625c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6626c58f1c22SToby Isaac {
6627*5d80c0bfSVaclav Hapla   PetscBool      flg;
6628*5d80c0bfSVaclav Hapla   DMLabel        label;
6629c58f1c22SToby Isaac   PetscErrorCode ierr;
6630c58f1c22SToby Isaac 
6631c58f1c22SToby Isaac   PetscFunctionBegin;
6632c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6633c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6634*5d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6635c58f1c22SToby Isaac   if (!flg) {
6636*5d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
6637*5d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
6638*5d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6639c58f1c22SToby Isaac   }
6640c58f1c22SToby Isaac   PetscFunctionReturn(0);
6641c58f1c22SToby Isaac }
6642c58f1c22SToby Isaac 
6643c58f1c22SToby Isaac /*@C
6644c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6645c58f1c22SToby Isaac 
6646c58f1c22SToby Isaac   Not Collective
6647c58f1c22SToby Isaac 
6648c58f1c22SToby Isaac   Input Parameters:
6649c58f1c22SToby Isaac + dm   - The DM object
6650c58f1c22SToby Isaac . name - The label name
6651c58f1c22SToby Isaac - point - The mesh point
6652c58f1c22SToby Isaac 
6653c58f1c22SToby Isaac   Output Parameter:
6654c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6655c58f1c22SToby Isaac 
6656c58f1c22SToby Isaac   Level: beginner
6657c58f1c22SToby Isaac 
6658c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6659c58f1c22SToby Isaac @*/
6660c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6661c58f1c22SToby Isaac {
6662c58f1c22SToby Isaac   DMLabel        label;
6663c58f1c22SToby Isaac   PetscErrorCode ierr;
6664c58f1c22SToby Isaac 
6665c58f1c22SToby Isaac   PetscFunctionBegin;
6666c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6667c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6668c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
666913903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6670c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6671c58f1c22SToby Isaac   PetscFunctionReturn(0);
6672c58f1c22SToby Isaac }
6673c58f1c22SToby Isaac 
6674c58f1c22SToby Isaac /*@C
6675c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6676c58f1c22SToby Isaac 
6677c58f1c22SToby Isaac   Not Collective
6678c58f1c22SToby Isaac 
6679c58f1c22SToby Isaac   Input Parameters:
6680c58f1c22SToby Isaac + dm   - The DM object
6681c58f1c22SToby Isaac . name - The label name
6682c58f1c22SToby Isaac . point - The mesh point
6683c58f1c22SToby Isaac - value - The label value for this point
6684c58f1c22SToby Isaac 
6685c58f1c22SToby Isaac   Output Parameter:
6686c58f1c22SToby Isaac 
6687c58f1c22SToby Isaac   Level: beginner
6688c58f1c22SToby Isaac 
6689c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6690c58f1c22SToby Isaac @*/
6691c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6692c58f1c22SToby Isaac {
6693c58f1c22SToby Isaac   DMLabel        label;
6694c58f1c22SToby Isaac   PetscErrorCode ierr;
6695c58f1c22SToby Isaac 
6696c58f1c22SToby Isaac   PetscFunctionBegin;
6697c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6698c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6699c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6700c58f1c22SToby Isaac   if (!label) {
6701c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6702c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6703c58f1c22SToby Isaac   }
6704c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6705c58f1c22SToby Isaac   PetscFunctionReturn(0);
6706c58f1c22SToby Isaac }
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac /*@C
6709c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6710c58f1c22SToby Isaac 
6711c58f1c22SToby Isaac   Not Collective
6712c58f1c22SToby Isaac 
6713c58f1c22SToby Isaac   Input Parameters:
6714c58f1c22SToby Isaac + dm   - The DM object
6715c58f1c22SToby Isaac . name - The label name
6716c58f1c22SToby Isaac . point - The mesh point
6717c58f1c22SToby Isaac - value - The label value for this point
6718c58f1c22SToby Isaac 
6719c58f1c22SToby Isaac   Output Parameter:
6720c58f1c22SToby Isaac 
6721c58f1c22SToby Isaac   Level: beginner
6722c58f1c22SToby Isaac 
6723c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6724c58f1c22SToby Isaac @*/
6725c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6726c58f1c22SToby Isaac {
6727c58f1c22SToby Isaac   DMLabel        label;
6728c58f1c22SToby Isaac   PetscErrorCode ierr;
6729c58f1c22SToby Isaac 
6730c58f1c22SToby Isaac   PetscFunctionBegin;
6731c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6732c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6733c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6734c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6735c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6736c58f1c22SToby Isaac   PetscFunctionReturn(0);
6737c58f1c22SToby Isaac }
6738c58f1c22SToby Isaac 
6739c58f1c22SToby Isaac /*@C
6740c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac   Not Collective
6743c58f1c22SToby Isaac 
6744c58f1c22SToby Isaac   Input Parameters:
6745c58f1c22SToby Isaac + dm   - The DM object
6746c58f1c22SToby Isaac - name - The label name
6747c58f1c22SToby Isaac 
6748c58f1c22SToby Isaac   Output Parameter:
6749c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6750c58f1c22SToby Isaac 
6751c58f1c22SToby Isaac   Level: beginner
6752c58f1c22SToby Isaac 
6753df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6754c58f1c22SToby Isaac @*/
6755c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6756c58f1c22SToby Isaac {
6757c58f1c22SToby Isaac   DMLabel        label;
6758c58f1c22SToby Isaac   PetscErrorCode ierr;
6759c58f1c22SToby Isaac 
6760c58f1c22SToby Isaac   PetscFunctionBegin;
6761c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6762c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6763534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6764c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6765c58f1c22SToby Isaac   *size = 0;
6766c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6767c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6768c58f1c22SToby Isaac   PetscFunctionReturn(0);
6769c58f1c22SToby Isaac }
6770c58f1c22SToby Isaac 
6771c58f1c22SToby Isaac /*@C
6772c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6773c58f1c22SToby Isaac 
6774c58f1c22SToby Isaac   Not Collective
6775c58f1c22SToby Isaac 
6776c58f1c22SToby Isaac   Input Parameters:
6777c58f1c22SToby Isaac + mesh - The DM object
6778c58f1c22SToby Isaac - name - The label name
6779c58f1c22SToby Isaac 
6780c58f1c22SToby Isaac   Output Parameter:
6781c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac   Level: beginner
6784c58f1c22SToby Isaac 
6785c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6786c58f1c22SToby Isaac @*/
6787c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6788c58f1c22SToby Isaac {
6789c58f1c22SToby Isaac   DMLabel        label;
6790c58f1c22SToby Isaac   PetscErrorCode ierr;
6791c58f1c22SToby Isaac 
6792c58f1c22SToby Isaac   PetscFunctionBegin;
6793c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6794c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6795c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6796c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6797c58f1c22SToby Isaac   *ids = NULL;
6798dab2e251SBlaise Bourdin  if (label) {
6799c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6800dab2e251SBlaise Bourdin   } else {
6801dab2e251SBlaise Bourdin     /* returning an empty IS */
6802dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6803dab2e251SBlaise Bourdin   }
6804c58f1c22SToby Isaac   PetscFunctionReturn(0);
6805c58f1c22SToby Isaac }
6806c58f1c22SToby Isaac 
6807c58f1c22SToby Isaac /*@C
6808c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6809c58f1c22SToby Isaac 
6810c58f1c22SToby Isaac   Not Collective
6811c58f1c22SToby Isaac 
6812c58f1c22SToby Isaac   Input Parameters:
6813c58f1c22SToby Isaac + dm - The DM object
6814c58f1c22SToby Isaac . name - The label name
6815c58f1c22SToby Isaac - value - The stratum value
6816c58f1c22SToby Isaac 
6817c58f1c22SToby Isaac   Output Parameter:
6818c58f1c22SToby Isaac . size - The stratum size
6819c58f1c22SToby Isaac 
6820c58f1c22SToby Isaac   Level: beginner
6821c58f1c22SToby Isaac 
6822c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6823c58f1c22SToby Isaac @*/
6824c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6825c58f1c22SToby Isaac {
6826c58f1c22SToby Isaac   DMLabel        label;
6827c58f1c22SToby Isaac   PetscErrorCode ierr;
6828c58f1c22SToby Isaac 
6829c58f1c22SToby Isaac   PetscFunctionBegin;
6830c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6831c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6832534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6833c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6834c58f1c22SToby Isaac   *size = 0;
6835c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6836c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6837c58f1c22SToby Isaac   PetscFunctionReturn(0);
6838c58f1c22SToby Isaac }
6839c58f1c22SToby Isaac 
6840c58f1c22SToby Isaac /*@C
6841c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   Not Collective
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac   Input Parameters:
6846c58f1c22SToby Isaac + dm - The DM object
6847c58f1c22SToby Isaac . name - The label name
6848c58f1c22SToby Isaac - value - The stratum value
6849c58f1c22SToby Isaac 
6850c58f1c22SToby Isaac   Output Parameter:
6851c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6852c58f1c22SToby Isaac 
6853c58f1c22SToby Isaac   Level: beginner
6854c58f1c22SToby Isaac 
6855c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6856c58f1c22SToby Isaac @*/
6857c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6858c58f1c22SToby Isaac {
6859c58f1c22SToby Isaac   DMLabel        label;
6860c58f1c22SToby Isaac   PetscErrorCode ierr;
6861c58f1c22SToby Isaac 
6862c58f1c22SToby Isaac   PetscFunctionBegin;
6863c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6864c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6865c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6866c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6867c58f1c22SToby Isaac   *points = NULL;
6868c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6869c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6870c58f1c22SToby Isaac   PetscFunctionReturn(0);
6871c58f1c22SToby Isaac }
6872c58f1c22SToby Isaac 
68734de306b1SToby Isaac /*@C
68749044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68754de306b1SToby Isaac 
68764de306b1SToby Isaac   Not Collective
68774de306b1SToby Isaac 
68784de306b1SToby Isaac   Input Parameters:
68794de306b1SToby Isaac + dm - The DM object
68804de306b1SToby Isaac . name - The label name
68814de306b1SToby Isaac . value - The stratum value
68824de306b1SToby Isaac - points - The stratum points
68834de306b1SToby Isaac 
68844de306b1SToby Isaac   Level: beginner
68854de306b1SToby Isaac 
68864de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
68874de306b1SToby Isaac @*/
68884de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
68894de306b1SToby Isaac {
68904de306b1SToby Isaac   DMLabel        label;
68914de306b1SToby Isaac   PetscErrorCode ierr;
68924de306b1SToby Isaac 
68934de306b1SToby Isaac   PetscFunctionBegin;
68944de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
68954de306b1SToby Isaac   PetscValidCharPointer(name, 2);
68964de306b1SToby Isaac   PetscValidPointer(points, 4);
68974de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
68984de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
68994de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
69004de306b1SToby Isaac   PetscFunctionReturn(0);
69014de306b1SToby Isaac }
69024de306b1SToby Isaac 
6903c58f1c22SToby Isaac /*@C
6904c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6905c58f1c22SToby Isaac 
6906c58f1c22SToby Isaac   Not Collective
6907c58f1c22SToby Isaac 
6908c58f1c22SToby Isaac   Input Parameters:
6909c58f1c22SToby Isaac + dm   - The DM object
6910c58f1c22SToby Isaac . name - The label name
6911c58f1c22SToby Isaac - value - The label value for this point
6912c58f1c22SToby Isaac 
6913c58f1c22SToby Isaac   Output Parameter:
6914c58f1c22SToby Isaac 
6915c58f1c22SToby Isaac   Level: beginner
6916c58f1c22SToby Isaac 
6917c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6918c58f1c22SToby Isaac @*/
6919c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6920c58f1c22SToby Isaac {
6921c58f1c22SToby Isaac   DMLabel        label;
6922c58f1c22SToby Isaac   PetscErrorCode ierr;
6923c58f1c22SToby Isaac 
6924c58f1c22SToby Isaac   PetscFunctionBegin;
6925c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6926c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6927c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6928c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6929c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6930c58f1c22SToby Isaac   PetscFunctionReturn(0);
6931c58f1c22SToby Isaac }
6932c58f1c22SToby Isaac 
6933c58f1c22SToby Isaac /*@
6934c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6935c58f1c22SToby Isaac 
6936c58f1c22SToby Isaac   Not Collective
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac   Input Parameter:
6939c58f1c22SToby Isaac . dm   - The DM object
6940c58f1c22SToby Isaac 
6941c58f1c22SToby Isaac   Output Parameter:
6942c58f1c22SToby Isaac . numLabels - the number of Labels
6943c58f1c22SToby Isaac 
6944c58f1c22SToby Isaac   Level: intermediate
6945c58f1c22SToby Isaac 
6946c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6947c58f1c22SToby Isaac @*/
6948c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6949c58f1c22SToby Isaac {
6950*5d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6951c58f1c22SToby Isaac   PetscInt  n    = 0;
6952c58f1c22SToby Isaac 
6953c58f1c22SToby Isaac   PetscFunctionBegin;
6954c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6955534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6956c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6957c58f1c22SToby Isaac   *numLabels = n;
6958c58f1c22SToby Isaac   PetscFunctionReturn(0);
6959c58f1c22SToby Isaac }
6960c58f1c22SToby Isaac 
6961c58f1c22SToby Isaac /*@C
6962c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   Not Collective
6965c58f1c22SToby Isaac 
6966c58f1c22SToby Isaac   Input Parameters:
6967c58f1c22SToby Isaac + dm - The DM object
6968c58f1c22SToby Isaac - n  - the label number
6969c58f1c22SToby Isaac 
6970c58f1c22SToby Isaac   Output Parameter:
6971c58f1c22SToby Isaac . name - the label name
6972c58f1c22SToby Isaac 
6973c58f1c22SToby Isaac   Level: intermediate
6974c58f1c22SToby Isaac 
6975c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6976c58f1c22SToby Isaac @*/
6977c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
6978c58f1c22SToby Isaac {
6979*5d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
6980c58f1c22SToby Isaac   PetscInt       l    = 0;
6981d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
6982c58f1c22SToby Isaac 
6983c58f1c22SToby Isaac   PetscFunctionBegin;
6984c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6985c58f1c22SToby Isaac   PetscValidPointer(name, 3);
6986c58f1c22SToby Isaac   while (next) {
6987c58f1c22SToby Isaac     if (l == n) {
6988d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
6989c58f1c22SToby Isaac       PetscFunctionReturn(0);
6990c58f1c22SToby Isaac     }
6991c58f1c22SToby Isaac     ++l;
6992c58f1c22SToby Isaac     next = next->next;
6993c58f1c22SToby Isaac   }
6994c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
6995c58f1c22SToby Isaac }
6996c58f1c22SToby Isaac 
6997c58f1c22SToby Isaac /*@C
6998c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
6999c58f1c22SToby Isaac 
7000c58f1c22SToby Isaac   Not Collective
7001c58f1c22SToby Isaac 
7002c58f1c22SToby Isaac   Input Parameters:
7003c58f1c22SToby Isaac + dm   - The DM object
7004c58f1c22SToby Isaac - name - The label name
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   Output Parameter:
7007c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7008c58f1c22SToby Isaac 
7009c58f1c22SToby Isaac   Level: intermediate
7010c58f1c22SToby Isaac 
7011c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7012c58f1c22SToby Isaac @*/
7013c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7014c58f1c22SToby Isaac {
7015*5d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7016d67d17b1SMatthew G. Knepley   const char    *lname;
7017c58f1c22SToby Isaac   PetscErrorCode ierr;
7018c58f1c22SToby Isaac 
7019c58f1c22SToby Isaac   PetscFunctionBegin;
7020c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7021c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7022534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7023c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7024c58f1c22SToby Isaac   while (next) {
7025d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7026d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7027c58f1c22SToby Isaac     if (*hasLabel) break;
7028c58f1c22SToby Isaac     next = next->next;
7029c58f1c22SToby Isaac   }
7030c58f1c22SToby Isaac   PetscFunctionReturn(0);
7031c58f1c22SToby Isaac }
7032c58f1c22SToby Isaac 
7033c58f1c22SToby Isaac /*@C
7034c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7035c58f1c22SToby Isaac 
7036c58f1c22SToby Isaac   Not Collective
7037c58f1c22SToby Isaac 
7038c58f1c22SToby Isaac   Input Parameters:
7039c58f1c22SToby Isaac + dm   - The DM object
7040c58f1c22SToby Isaac - name - The label name
7041c58f1c22SToby Isaac 
7042c58f1c22SToby Isaac   Output Parameter:
7043c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7044c58f1c22SToby Isaac 
7045c58f1c22SToby Isaac   Level: intermediate
7046c58f1c22SToby Isaac 
7047c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7048c58f1c22SToby Isaac @*/
7049c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7050c58f1c22SToby Isaac {
7051*5d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7052c58f1c22SToby Isaac   PetscBool      hasLabel;
7053d67d17b1SMatthew G. Knepley   const char    *lname;
7054c58f1c22SToby Isaac   PetscErrorCode ierr;
7055c58f1c22SToby Isaac 
7056c58f1c22SToby Isaac   PetscFunctionBegin;
7057c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7058c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7059c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7060c58f1c22SToby Isaac   *label = NULL;
7061c58f1c22SToby Isaac   while (next) {
7062d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7063d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7064c58f1c22SToby Isaac     if (hasLabel) {
7065c58f1c22SToby Isaac       *label = next->label;
7066c58f1c22SToby Isaac       break;
7067c58f1c22SToby Isaac     }
7068c58f1c22SToby Isaac     next = next->next;
7069c58f1c22SToby Isaac   }
7070c58f1c22SToby Isaac   PetscFunctionReturn(0);
7071c58f1c22SToby Isaac }
7072c58f1c22SToby Isaac 
7073c58f1c22SToby Isaac /*@C
7074c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7075c58f1c22SToby Isaac 
7076c58f1c22SToby Isaac   Not Collective
7077c58f1c22SToby Isaac 
7078c58f1c22SToby Isaac   Input Parameters:
7079c58f1c22SToby Isaac + dm - The DM object
7080c58f1c22SToby Isaac - n  - the label number
7081c58f1c22SToby Isaac 
7082c58f1c22SToby Isaac   Output Parameter:
7083c58f1c22SToby Isaac . label - the label
7084c58f1c22SToby Isaac 
7085c58f1c22SToby Isaac   Level: intermediate
7086c58f1c22SToby Isaac 
7087c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7088c58f1c22SToby Isaac @*/
7089c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7090c58f1c22SToby Isaac {
7091*5d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7092c58f1c22SToby Isaac   PetscInt    l    = 0;
7093c58f1c22SToby Isaac 
7094c58f1c22SToby Isaac   PetscFunctionBegin;
7095c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7096c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7097c58f1c22SToby Isaac   while (next) {
7098c58f1c22SToby Isaac     if (l == n) {
7099c58f1c22SToby Isaac       *label = next->label;
7100c58f1c22SToby Isaac       PetscFunctionReturn(0);
7101c58f1c22SToby Isaac     }
7102c58f1c22SToby Isaac     ++l;
7103c58f1c22SToby Isaac     next = next->next;
7104c58f1c22SToby Isaac   }
7105c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7106c58f1c22SToby Isaac }
7107c58f1c22SToby Isaac 
7108c58f1c22SToby Isaac /*@C
7109c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7110c58f1c22SToby Isaac 
7111c58f1c22SToby Isaac   Not Collective
7112c58f1c22SToby Isaac 
7113c58f1c22SToby Isaac   Input Parameters:
7114c58f1c22SToby Isaac + dm   - The DM object
7115c58f1c22SToby Isaac - label - The DMLabel
7116c58f1c22SToby Isaac 
7117c58f1c22SToby Isaac   Level: developer
7118c58f1c22SToby Isaac 
7119c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7120c58f1c22SToby Isaac @*/
7121c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7122c58f1c22SToby Isaac {
7123*5d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7124c58f1c22SToby Isaac   PetscBool      hasLabel;
7125d67d17b1SMatthew G. Knepley   const char    *lname;
7126*5d80c0bfSVaclav Hapla   PetscBool      flg;
7127c58f1c22SToby Isaac   PetscErrorCode ierr;
7128c58f1c22SToby Isaac 
7129c58f1c22SToby Isaac   PetscFunctionBegin;
7130c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7131d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7132d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7133d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7134c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7135c58f1c22SToby Isaac   tmpLabel->label  = label;
7136c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
7137*5d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
7138*5d80c0bfSVaclav Hapla   *p = tmpLabel;
713908f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
7140*5d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
7141*5d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7142c58f1c22SToby Isaac   PetscFunctionReturn(0);
7143c58f1c22SToby Isaac }
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac /*@C
7146e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7147c58f1c22SToby Isaac 
7148c58f1c22SToby Isaac   Not Collective
7149c58f1c22SToby Isaac 
7150c58f1c22SToby Isaac   Input Parameters:
7151c58f1c22SToby Isaac + dm   - The DM object
7152c58f1c22SToby Isaac - name - The label name
7153c58f1c22SToby Isaac 
7154c58f1c22SToby Isaac   Output Parameter:
7155c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7156c58f1c22SToby Isaac 
7157c58f1c22SToby Isaac   Level: developer
7158c58f1c22SToby Isaac 
7159e5472504SVaclav Hapla   Notes:
7160e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7161e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7162e5472504SVaclav Hapla 
7163e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7164e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7165e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7166e5472504SVaclav Hapla 
7167e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7168c58f1c22SToby Isaac @*/
7169c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7170c58f1c22SToby Isaac {
717195d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7172c58f1c22SToby Isaac   PetscBool      hasLabel;
7173d67d17b1SMatthew G. Knepley   const char    *lname;
7174c58f1c22SToby Isaac   PetscErrorCode ierr;
7175c58f1c22SToby Isaac 
7176c58f1c22SToby Isaac   PetscFunctionBegin;
7177c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7178e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7179e5472504SVaclav Hapla   if (label) {
7180e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7181c58f1c22SToby Isaac     *label = NULL;
7182e5472504SVaclav Hapla   }
7183*5d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
718495d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7185d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7186c58f1c22SToby Isaac     if (hasLabel) {
718795d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7188c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
718995d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
719095d578d6SVaclav Hapla       if (label) *label = link->label;
719195d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
719295d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7193c58f1c22SToby Isaac       break;
7194c58f1c22SToby Isaac     }
7195c58f1c22SToby Isaac   }
7196c58f1c22SToby Isaac   PetscFunctionReturn(0);
7197c58f1c22SToby Isaac }
7198c58f1c22SToby Isaac 
7199306894acSVaclav Hapla /*@
7200306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7201306894acSVaclav Hapla 
7202306894acSVaclav Hapla   Not Collective
7203306894acSVaclav Hapla 
7204306894acSVaclav Hapla   Input Parameters:
7205306894acSVaclav Hapla + dm   - The DM object
7206306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7207306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7208306894acSVaclav Hapla 
7209306894acSVaclav Hapla   Level: developer
7210306894acSVaclav Hapla 
7211306894acSVaclav Hapla   Notes:
7212306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7213306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7214306894acSVaclav Hapla   *label nullified.
7215306894acSVaclav Hapla 
7216306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7217306894acSVaclav Hapla @*/
7218306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7219306894acSVaclav Hapla {
722043e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7221306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7222306894acSVaclav Hapla   PetscErrorCode ierr;
7223306894acSVaclav Hapla 
7224306894acSVaclav Hapla   PetscFunctionBegin;
7225306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7226306894acSVaclav Hapla   PetscValidPointer(label, 2);
7227f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7228306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7229306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
7230*5d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
723143e45a93SVaclav Hapla     if (*label == link->label) {
7232306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
723343e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7234306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
723543e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
723643e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
723743e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7238306894acSVaclav Hapla       break;
7239306894acSVaclav Hapla     }
7240306894acSVaclav Hapla   }
7241306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7242306894acSVaclav Hapla   PetscFunctionReturn(0);
7243306894acSVaclav Hapla }
7244306894acSVaclav Hapla 
7245c58f1c22SToby Isaac /*@C
7246c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7247c58f1c22SToby Isaac 
7248c58f1c22SToby Isaac   Not Collective
7249c58f1c22SToby Isaac 
7250c58f1c22SToby Isaac   Input Parameters:
7251c58f1c22SToby Isaac + dm   - The DM object
7252c58f1c22SToby Isaac - name - The label name
7253c58f1c22SToby Isaac 
7254c58f1c22SToby Isaac   Output Parameter:
7255c58f1c22SToby Isaac . output - The flag for output
7256c58f1c22SToby Isaac 
7257c58f1c22SToby Isaac   Level: developer
7258c58f1c22SToby Isaac 
7259c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7260c58f1c22SToby Isaac @*/
7261c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7262c58f1c22SToby Isaac {
7263*5d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7264d67d17b1SMatthew G. Knepley   const char    *lname;
7265c58f1c22SToby Isaac   PetscErrorCode ierr;
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac   PetscFunctionBegin;
7268c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7269c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7270c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7271c58f1c22SToby Isaac   while (next) {
7272c58f1c22SToby Isaac     PetscBool flg;
7273c58f1c22SToby Isaac 
7274d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7275d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7276c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7277c58f1c22SToby Isaac     next = next->next;
7278c58f1c22SToby Isaac   }
7279c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7280c58f1c22SToby Isaac }
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac /*@C
7283c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac   Not Collective
7286c58f1c22SToby Isaac 
7287c58f1c22SToby Isaac   Input Parameters:
7288c58f1c22SToby Isaac + dm     - The DM object
7289c58f1c22SToby Isaac . name   - The label name
7290c58f1c22SToby Isaac - output - The flag for output
7291c58f1c22SToby Isaac 
7292c58f1c22SToby Isaac   Level: developer
7293c58f1c22SToby Isaac 
7294c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7295c58f1c22SToby Isaac @*/
7296c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7297c58f1c22SToby Isaac {
7298*5d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7299d67d17b1SMatthew G. Knepley   const char    *lname;
7300c58f1c22SToby Isaac   PetscErrorCode ierr;
7301c58f1c22SToby Isaac 
7302c58f1c22SToby Isaac   PetscFunctionBegin;
7303c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7304534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7305c58f1c22SToby Isaac   while (next) {
7306c58f1c22SToby Isaac     PetscBool flg;
7307c58f1c22SToby Isaac 
7308d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7309d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7310c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7311c58f1c22SToby Isaac     next = next->next;
7312c58f1c22SToby Isaac   }
7313c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7314c58f1c22SToby Isaac }
7315c58f1c22SToby Isaac 
7316c58f1c22SToby Isaac /*@
7317c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7318c58f1c22SToby Isaac 
7319d083f849SBarry Smith   Collective on dmA
7320c58f1c22SToby Isaac 
7321c58f1c22SToby Isaac   Input Parameter:
7322*5d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
73232e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
7324*5d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7325*5d80c0bfSVaclav Hapla - all  - Copy all labels including "depth" and "dim" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7326c58f1c22SToby Isaac 
7327c58f1c22SToby Isaac   Level: intermediate
7328c58f1c22SToby Isaac 
7329c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7330c58f1c22SToby Isaac 
7331*5d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7332c58f1c22SToby Isaac @*/
7333*5d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7334c58f1c22SToby Isaac {
7335c58f1c22SToby Isaac   DMLabel        label, labelNew;
7336c58f1c22SToby Isaac   const char    *name;
7337c58f1c22SToby Isaac   PetscBool      flg;
7338*5d80c0bfSVaclav Hapla   DMLabelLink    link;
7339*5d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7340c58f1c22SToby Isaac 
7341*5d80c0bfSVaclav Hapla   PetscFunctionBegin;
7342*5d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
7343*5d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
7344*5d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
7345*5d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
7346*5d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
7347*5d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
7348*5d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
7349*5d80c0bfSVaclav Hapla     label=link->label;
7350*5d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
7351*5d80c0bfSVaclav Hapla     if (!all) {
7352c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7353c58f1c22SToby Isaac       if (flg) continue;
73547d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
73557d5acc75SStefano Zampini       if (flg) continue;
7356*5d80c0bfSVaclav Hapla     } else {
7357*5d80c0bfSVaclav Hapla       dmB->depthLabel = dmA->depthLabel;
7358*5d80c0bfSVaclav Hapla     }
7359*5d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7360c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
7361*5d80c0bfSVaclav Hapla     } else {
7362*5d80c0bfSVaclav Hapla       labelNew = label;
7363*5d80c0bfSVaclav Hapla     }
7364c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
7365*5d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7366c58f1c22SToby Isaac   }
7367c58f1c22SToby Isaac   PetscFunctionReturn(0);
7368c58f1c22SToby Isaac }
7369a8fb8f29SToby Isaac 
7370a8fb8f29SToby Isaac /*@
7371a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7372a8fb8f29SToby Isaac 
7373a8fb8f29SToby Isaac   Input Parameter:
7374a8fb8f29SToby Isaac . dm - The DM object
7375a8fb8f29SToby Isaac 
7376a8fb8f29SToby Isaac   Output Parameter:
7377a8fb8f29SToby Isaac . cdm - The coarse DM
7378a8fb8f29SToby Isaac 
7379a8fb8f29SToby Isaac   Level: intermediate
7380a8fb8f29SToby Isaac 
7381a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7382a8fb8f29SToby Isaac @*/
7383a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7384a8fb8f29SToby Isaac {
7385a8fb8f29SToby Isaac   PetscFunctionBegin;
7386a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7387a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7388a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7389a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7390a8fb8f29SToby Isaac }
7391a8fb8f29SToby Isaac 
7392a8fb8f29SToby Isaac /*@
7393a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7394a8fb8f29SToby Isaac 
7395a8fb8f29SToby Isaac   Input Parameters:
7396a8fb8f29SToby Isaac + dm - The DM object
7397a8fb8f29SToby Isaac - cdm - The coarse DM
7398a8fb8f29SToby Isaac 
7399a8fb8f29SToby Isaac   Level: intermediate
7400a8fb8f29SToby Isaac 
7401a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7402a8fb8f29SToby Isaac @*/
7403a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7404a8fb8f29SToby Isaac {
7405a8fb8f29SToby Isaac   PetscErrorCode ierr;
7406a8fb8f29SToby Isaac 
7407a8fb8f29SToby Isaac   PetscFunctionBegin;
7408a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7409a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7410a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7411a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7412a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7413a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7414a8fb8f29SToby Isaac }
7415a8fb8f29SToby Isaac 
741688bdff64SToby Isaac /*@
741788bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
741888bdff64SToby Isaac 
741988bdff64SToby Isaac   Input Parameter:
742088bdff64SToby Isaac . dm - The DM object
742188bdff64SToby Isaac 
742288bdff64SToby Isaac   Output Parameter:
742388bdff64SToby Isaac . fdm - The fine DM
742488bdff64SToby Isaac 
742588bdff64SToby Isaac   Level: intermediate
742688bdff64SToby Isaac 
742788bdff64SToby Isaac .seealso: DMSetFineDM()
742888bdff64SToby Isaac @*/
742988bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
743088bdff64SToby Isaac {
743188bdff64SToby Isaac   PetscFunctionBegin;
743288bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
743388bdff64SToby Isaac   PetscValidPointer(fdm, 2);
743488bdff64SToby Isaac   *fdm = dm->fineMesh;
743588bdff64SToby Isaac   PetscFunctionReturn(0);
743688bdff64SToby Isaac }
743788bdff64SToby Isaac 
743888bdff64SToby Isaac /*@
743988bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
744088bdff64SToby Isaac 
744188bdff64SToby Isaac   Input Parameters:
744288bdff64SToby Isaac + dm - The DM object
744388bdff64SToby Isaac - fdm - The fine DM
744488bdff64SToby Isaac 
744588bdff64SToby Isaac   Level: intermediate
744688bdff64SToby Isaac 
744788bdff64SToby Isaac .seealso: DMGetFineDM()
744888bdff64SToby Isaac @*/
744988bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
745088bdff64SToby Isaac {
745188bdff64SToby Isaac   PetscErrorCode ierr;
745288bdff64SToby Isaac 
745388bdff64SToby Isaac   PetscFunctionBegin;
745488bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
745588bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
745688bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
745788bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
745888bdff64SToby Isaac   dm->fineMesh = fdm;
745988bdff64SToby Isaac   PetscFunctionReturn(0);
746088bdff64SToby Isaac }
746188bdff64SToby Isaac 
7462a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7463a6ba4734SToby Isaac 
7464a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7465a6ba4734SToby Isaac {
7466e5e52638SMatthew G. Knepley   PetscInt       d;
7467a6ba4734SToby Isaac   PetscErrorCode ierr;
7468a6ba4734SToby Isaac 
7469a6ba4734SToby Isaac   PetscFunctionBegin;
7470e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7471e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7472e5e52638SMatthew G. Knepley   }
7473a6ba4734SToby Isaac   PetscFunctionReturn(0);
7474a6ba4734SToby Isaac }
7475a6ba4734SToby Isaac 
7476a6ba4734SToby Isaac /*@C
7477a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7478a6ba4734SToby Isaac 
7479a6ba4734SToby Isaac   Input Parameters:
74804c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7481f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7482a6ba4734SToby Isaac . name        - The BC name
7483a6ba4734SToby Isaac . labelname   - The label defining constrained points
7484a6ba4734SToby Isaac . field       - The field to constrain
7485e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7486a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7487a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7488a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7489a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7490a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7491a6ba4734SToby Isaac 
7492a6ba4734SToby Isaac   Options Database Keys:
7493a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7494a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7495a6ba4734SToby Isaac 
7496a6ba4734SToby Isaac   Level: developer
7497a6ba4734SToby Isaac 
7498a6ba4734SToby Isaac .seealso: DMGetBoundary()
7499a6ba4734SToby Isaac @*/
7500a30ec4eaSSatish 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)
7501a6ba4734SToby Isaac {
7502e5e52638SMatthew G. Knepley   PetscDS        ds;
7503a6ba4734SToby Isaac   PetscErrorCode ierr;
7504a6ba4734SToby Isaac 
7505a6ba4734SToby Isaac   PetscFunctionBegin;
7506a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7507e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7508e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7509a6ba4734SToby Isaac   PetscFunctionReturn(0);
7510a6ba4734SToby Isaac }
7511a6ba4734SToby Isaac 
7512a6ba4734SToby Isaac /*@
7513a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7514a6ba4734SToby Isaac 
7515a6ba4734SToby Isaac   Input Parameters:
7516a6ba4734SToby Isaac . dm - The mesh object
7517a6ba4734SToby Isaac 
7518a6ba4734SToby Isaac   Output Parameters:
7519a6ba4734SToby Isaac . numBd - The number of BC
7520a6ba4734SToby Isaac 
7521a6ba4734SToby Isaac   Level: intermediate
7522a6ba4734SToby Isaac 
7523a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7524a6ba4734SToby Isaac @*/
7525a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7526a6ba4734SToby Isaac {
7527e5e52638SMatthew G. Knepley   PetscDS        ds;
752858ebd649SToby Isaac   PetscErrorCode ierr;
7529a6ba4734SToby Isaac 
7530a6ba4734SToby Isaac   PetscFunctionBegin;
7531a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7532e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7533e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7534a6ba4734SToby Isaac   PetscFunctionReturn(0);
7535a6ba4734SToby Isaac }
7536a6ba4734SToby Isaac 
7537a6ba4734SToby Isaac /*@C
75381c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7539a6ba4734SToby Isaac 
7540a6ba4734SToby Isaac   Input Parameters:
7541a6ba4734SToby Isaac + dm          - The mesh object
7542a6ba4734SToby Isaac - bd          - The BC number
7543a6ba4734SToby Isaac 
7544a6ba4734SToby Isaac   Output Parameters:
7545f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7546a6ba4734SToby Isaac . name        - The BC name
7547a6ba4734SToby Isaac . labelname   - The label defining constrained points
7548a6ba4734SToby Isaac . field       - The field to constrain
7549a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7550a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7551a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7552a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7553a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7554a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7555a6ba4734SToby Isaac 
7556a6ba4734SToby Isaac   Options Database Keys:
7557a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7558a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7559a6ba4734SToby Isaac 
7560a6ba4734SToby Isaac   Level: developer
7561a6ba4734SToby Isaac 
7562a6ba4734SToby Isaac .seealso: DMAddBoundary()
7563a6ba4734SToby Isaac @*/
7564a30ec4eaSSatish 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)
7565a6ba4734SToby Isaac {
7566e5e52638SMatthew G. Knepley   PetscDS        ds;
756758ebd649SToby Isaac   PetscErrorCode ierr;
7568a6ba4734SToby Isaac 
7569a6ba4734SToby Isaac   PetscFunctionBegin;
7570a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7571e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7572e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7573a6ba4734SToby Isaac   PetscFunctionReturn(0);
7574a6ba4734SToby Isaac }
7575a6ba4734SToby Isaac 
7576e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7577e6f8dbb6SToby Isaac {
7578e5e52638SMatthew G. Knepley   PetscDS        ds;
7579dff059c6SToby Isaac   DMBoundary    *lastnext;
7580e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7581e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7582e6f8dbb6SToby Isaac 
7583e6f8dbb6SToby Isaac   PetscFunctionBegin;
7584e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7585e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
758647a1f5adSToby Isaac   if (dm->boundary) {
758747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
758847a1f5adSToby Isaac 
758947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
759047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
759147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
759247a1f5adSToby Isaac     while (next) {
759347a1f5adSToby Isaac       DMBoundary b = next;
759447a1f5adSToby Isaac 
759547a1f5adSToby Isaac       next = b->next;
759647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7597a6ba4734SToby Isaac     }
759847a1f5adSToby Isaac     dm->boundary = NULL;
7599a6ba4734SToby Isaac   }
760047a1f5adSToby Isaac 
7601dff059c6SToby Isaac   lastnext = &(dm->boundary);
7602e6f8dbb6SToby Isaac   while (dsbound) {
7603e6f8dbb6SToby Isaac     DMBoundary dmbound;
7604e6f8dbb6SToby Isaac 
7605e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7606e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7607e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7608994fe344SLisandro Dalcin     if (!dmbound->label) {ierr = PetscInfo2(dm, "DSBoundary %s wants label %s, which is not in this dm.\n",dsbound->name,dsbound->labelname);CHKERRQ(ierr);}
760947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7610dff059c6SToby Isaac     *lastnext = dmbound;
7611dff059c6SToby Isaac     lastnext = &(dmbound->next);
7612dff059c6SToby Isaac     dsbound = dsbound->next;
7613a6ba4734SToby Isaac   }
7614a6ba4734SToby Isaac   PetscFunctionReturn(0);
7615a6ba4734SToby Isaac }
7616a6ba4734SToby Isaac 
7617a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7618a6ba4734SToby Isaac {
7619b95f2879SToby Isaac   DMBoundary     b;
7620a6ba4734SToby Isaac   PetscErrorCode ierr;
7621a6ba4734SToby Isaac 
7622a6ba4734SToby Isaac   PetscFunctionBegin;
7623a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7624534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7625a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7626e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7627b95f2879SToby Isaac   b = dm->boundary;
7628a6ba4734SToby Isaac   while (b && !(*isBd)) {
7629e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7630e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
76313424c85cSToby Isaac 
76323424c85cSToby Isaac     if (label) {
7633a6ba4734SToby Isaac       PetscInt i;
7634a6ba4734SToby Isaac 
7635e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7636e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7637a6ba4734SToby Isaac       }
7638a6ba4734SToby Isaac     }
7639a6ba4734SToby Isaac     b = b->next;
7640a6ba4734SToby Isaac   }
7641a6ba4734SToby Isaac   PetscFunctionReturn(0);
7642a6ba4734SToby Isaac }
76434d6f44ffSToby Isaac 
76444d6f44ffSToby Isaac /*@C
76454d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
76464d6f44ffSToby Isaac 
76474d6f44ffSToby Isaac   Input Parameters:
76484d6f44ffSToby Isaac + dm      - The DM
76490709b2feSToby Isaac . time    - The time
76504d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76514d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76524d6f44ffSToby Isaac - mode    - The insertion mode for values
76534d6f44ffSToby Isaac 
76544d6f44ffSToby Isaac   Output Parameter:
76554d6f44ffSToby Isaac . X - vector
76564d6f44ffSToby Isaac 
76574d6f44ffSToby Isaac    Calling sequence of func:
76580709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
76594d6f44ffSToby Isaac 
76604d6f44ffSToby Isaac +  dim - The spatial dimension
76614d6f44ffSToby Isaac .  x   - The coordinates
76624d6f44ffSToby Isaac .  Nf  - The number of fields
76634d6f44ffSToby Isaac .  u   - The output field values
76644d6f44ffSToby Isaac -  ctx - optional user-defined function context
76654d6f44ffSToby Isaac 
76664d6f44ffSToby Isaac   Level: developer
76674d6f44ffSToby Isaac 
76682716604bSToby Isaac .seealso: DMComputeL2Diff()
76694d6f44ffSToby Isaac @*/
76700709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
76714d6f44ffSToby Isaac {
76724d6f44ffSToby Isaac   Vec            localX;
76734d6f44ffSToby Isaac   PetscErrorCode ierr;
76744d6f44ffSToby Isaac 
76754d6f44ffSToby Isaac   PetscFunctionBegin;
76764d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
76774d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
76780709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
76794d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
76804d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
76814d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
76824d6f44ffSToby Isaac   PetscFunctionReturn(0);
76834d6f44ffSToby Isaac }
76844d6f44ffSToby Isaac 
76850709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
76864d6f44ffSToby Isaac {
76874d6f44ffSToby Isaac   PetscErrorCode ierr;
76884d6f44ffSToby Isaac 
76894d6f44ffSToby Isaac   PetscFunctionBegin;
76904d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
76914d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
76920918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
76930709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
76944d6f44ffSToby Isaac   PetscFunctionReturn(0);
76954d6f44ffSToby Isaac }
76964d6f44ffSToby Isaac 
76972c53366bSMatthew 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)
76982c53366bSMatthew G. Knepley {
76992c53366bSMatthew G. Knepley   Vec            localX;
77002c53366bSMatthew G. Knepley   PetscErrorCode ierr;
77012c53366bSMatthew G. Knepley 
77022c53366bSMatthew G. Knepley   PetscFunctionBegin;
77032c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77042c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77052c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77062c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77072c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77082c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77092c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
77102c53366bSMatthew G. Knepley }
77112c53366bSMatthew G. Knepley 
77121c531cf8SMatthew 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)
77134d6f44ffSToby Isaac {
77144d6f44ffSToby Isaac   PetscErrorCode ierr;
77154d6f44ffSToby Isaac 
77164d6f44ffSToby Isaac   PetscFunctionBegin;
77174d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77184d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
77190918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
77201c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77214d6f44ffSToby Isaac   PetscFunctionReturn(0);
77224d6f44ffSToby Isaac }
77232716604bSToby Isaac 
77248c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
77258c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
77268c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
77278c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7728191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
77298c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
77308c6c5593SMatthew G. Knepley {
77318c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
77328c6c5593SMatthew G. Knepley 
77338c6c5593SMatthew G. Knepley   PetscFunctionBegin;
77348c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77358c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
77368c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
77370918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
77388c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
77398c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
77408c6c5593SMatthew G. Knepley }
77418c6c5593SMatthew G. Knepley 
77421c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
77438c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
77448c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
77458c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7746191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
77478c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
77488c6c5593SMatthew G. Knepley {
77498c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
77508c6c5593SMatthew G. Knepley 
77518c6c5593SMatthew G. Knepley   PetscFunctionBegin;
77528c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77538c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
77548c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
77550918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
77561c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
77578c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
77588c6c5593SMatthew G. Knepley }
77598c6c5593SMatthew G. Knepley 
77602716604bSToby Isaac /*@C
77612716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
77622716604bSToby Isaac 
77632716604bSToby Isaac   Input Parameters:
77642716604bSToby Isaac + dm    - The DM
77650709b2feSToby Isaac . time  - The time
77662716604bSToby Isaac . funcs - The functions to evaluate for each field component
77672716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7768574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
77692716604bSToby Isaac 
77702716604bSToby Isaac   Output Parameter:
77712716604bSToby Isaac . diff - The diff ||u - u_h||_2
77722716604bSToby Isaac 
77732716604bSToby Isaac   Level: developer
77742716604bSToby Isaac 
77751189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
77762716604bSToby Isaac @*/
77770709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
77782716604bSToby Isaac {
77792716604bSToby Isaac   PetscErrorCode ierr;
77802716604bSToby Isaac 
77812716604bSToby Isaac   PetscFunctionBegin;
77822716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7783b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
77840918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
77850709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
77862716604bSToby Isaac   PetscFunctionReturn(0);
77872716604bSToby Isaac }
7788b698f381SToby Isaac 
7789b698f381SToby Isaac /*@C
7790b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7791b698f381SToby Isaac 
7792d083f849SBarry Smith   Collective on dm
7793d083f849SBarry Smith 
7794b698f381SToby Isaac   Input Parameters:
7795b698f381SToby Isaac + dm    - The DM
7796b698f381SToby Isaac , time  - The time
7797b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7798b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7799574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7800b698f381SToby Isaac - n     - The vector to project along
7801b698f381SToby Isaac 
7802b698f381SToby Isaac   Output Parameter:
7803b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7804b698f381SToby Isaac 
7805b698f381SToby Isaac   Level: developer
7806b698f381SToby Isaac 
7807b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7808b698f381SToby Isaac @*/
7809b698f381SToby 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)
7810b698f381SToby Isaac {
7811b698f381SToby Isaac   PetscErrorCode ierr;
7812b698f381SToby Isaac 
7813b698f381SToby Isaac   PetscFunctionBegin;
7814b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7815b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7816b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7817b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7818b698f381SToby Isaac   PetscFunctionReturn(0);
7819b698f381SToby Isaac }
7820b698f381SToby Isaac 
78212a16baeaSToby Isaac /*@C
78222a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
78232a16baeaSToby Isaac 
7824d083f849SBarry Smith   Collective on dm
7825d083f849SBarry Smith 
78262a16baeaSToby Isaac   Input Parameters:
78272a16baeaSToby Isaac + dm    - The DM
78282a16baeaSToby Isaac . time  - The time
78292a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
78302a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7831574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
78322a16baeaSToby Isaac 
78332a16baeaSToby Isaac   Output Parameter:
78342a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
78352a16baeaSToby Isaac 
78362a16baeaSToby Isaac   Level: developer
78372a16baeaSToby Isaac 
78381189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
78392a16baeaSToby Isaac @*/
78401189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
78412a16baeaSToby Isaac {
78422a16baeaSToby Isaac   PetscErrorCode ierr;
78432a16baeaSToby Isaac 
78442a16baeaSToby Isaac   PetscFunctionBegin;
78452a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78462a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
78470918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
78482a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
78492a16baeaSToby Isaac   PetscFunctionReturn(0);
78502a16baeaSToby Isaac }
78512a16baeaSToby Isaac 
7852df0b854cSToby Isaac /*@C
7853df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7854cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7855df0b854cSToby Isaac 
7856df0b854cSToby Isaac   Collective on dm
7857df0b854cSToby Isaac 
7858df0b854cSToby Isaac   Input parameters:
7859df0b854cSToby Isaac + dm - the pre-adaptation DM object
7860a1b0c543SToby Isaac - label - label with the flags
7861df0b854cSToby Isaac 
7862df0b854cSToby Isaac   Output parameters:
78630d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7864df0b854cSToby Isaac 
7865df0b854cSToby Isaac   Level: intermediate
78660d1cd5e0SMatthew G. Knepley 
78670d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7868df0b854cSToby Isaac @*/
78690d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7870df0b854cSToby Isaac {
7871df0b854cSToby Isaac   PetscErrorCode ierr;
7872df0b854cSToby Isaac 
7873df0b854cSToby Isaac   PetscFunctionBegin;
7874df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7875a1b0c543SToby Isaac   PetscValidPointer(label,2);
78760d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
78770d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
78786f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
78790d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
78800d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
78810d1cd5e0SMatthew G. Knepley }
78820d1cd5e0SMatthew G. Knepley 
78830d1cd5e0SMatthew G. Knepley /*@C
78840d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
78850d1cd5e0SMatthew G. Knepley 
78860d1cd5e0SMatthew G. Knepley   Input Parameters:
78870d1cd5e0SMatthew G. Knepley + dm - The DM object
78880d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
78896f25b0d8SLisandro 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_".
78900d1cd5e0SMatthew G. Knepley 
78910d1cd5e0SMatthew G. Knepley   Output Parameter:
78920d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
78930d1cd5e0SMatthew G. Knepley 
78940d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
78950d1cd5e0SMatthew G. Knepley 
78960d1cd5e0SMatthew G. Knepley   Level: advanced
78970d1cd5e0SMatthew G. Knepley 
78980d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
78990d1cd5e0SMatthew G. Knepley @*/
79000d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
79010d1cd5e0SMatthew G. Knepley {
79020d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
79030d1cd5e0SMatthew G. Knepley 
79040d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
79050d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79060d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
79070d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
79080d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
79096f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
79106f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
79110d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7912df0b854cSToby Isaac   PetscFunctionReturn(0);
7913df0b854cSToby Isaac }
7914c4088d22SMatthew G. Knepley 
7915502a2867SDave May /*@C
7916502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7917502a2867SDave May 
7918502a2867SDave May  Not Collective
7919502a2867SDave May 
7920502a2867SDave May  Input Parameter:
7921502a2867SDave May  . dm    - The DM
7922502a2867SDave May 
7923502a2867SDave May  Output Parameter:
7924502a2867SDave May  . nranks - the number of neighbours
7925502a2867SDave May  . ranks - the neighbors ranks
7926502a2867SDave May 
7927502a2867SDave May  Notes:
7928502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7929502a2867SDave May 
7930502a2867SDave May  Level: beginner
7931502a2867SDave May 
7932dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
7933502a2867SDave May @*/
7934502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7935502a2867SDave May {
7936502a2867SDave May   PetscErrorCode ierr;
7937502a2867SDave May 
7938502a2867SDave May   PetscFunctionBegin;
7939502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79400918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7941502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7942502a2867SDave May   PetscFunctionReturn(0);
7943502a2867SDave May }
7944502a2867SDave May 
7945531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7946531c7667SBarry Smith 
7947531c7667SBarry Smith /*
7948531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7949531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7950531c7667SBarry Smith */
7951531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7952531c7667SBarry Smith {
7953531c7667SBarry Smith   PetscErrorCode ierr;
7954531c7667SBarry Smith 
7955531c7667SBarry Smith   PetscFunctionBegin;
7956531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7957531c7667SBarry Smith     Vec x1local;
7958531c7667SBarry Smith     DM  dm;
7959531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7960531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7961531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7962531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7963531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7964531c7667SBarry Smith     x1   = x1local;
7965531c7667SBarry Smith   }
7966531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7967531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7968531c7667SBarry Smith     DM  dm;
7969531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7970531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7971531c7667SBarry Smith   }
7972531c7667SBarry Smith   PetscFunctionReturn(0);
7973531c7667SBarry Smith }
7974531c7667SBarry Smith 
7975531c7667SBarry Smith /*@
7976531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
7977531c7667SBarry Smith 
7978531c7667SBarry Smith     Input Parameter:
7979531c7667SBarry Smith .    coloring - the MatFDColoring object
7980531c7667SBarry Smith 
798195452b02SPatrick Sanan     Developer Notes:
798295452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
7983531c7667SBarry Smith 
79841b266c99SBarry Smith     Level: advanced
79851b266c99SBarry Smith 
7986531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
7987531c7667SBarry Smith @*/
7988531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
7989531c7667SBarry Smith {
7990531c7667SBarry Smith   PetscFunctionBegin;
7991531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
7992531c7667SBarry Smith   PetscFunctionReturn(0);
7993531c7667SBarry Smith }
79948320bc6fSPatrick Sanan 
79958320bc6fSPatrick Sanan /*@
79968320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
79978320bc6fSPatrick Sanan 
79988320bc6fSPatrick Sanan     Collective
79998320bc6fSPatrick Sanan 
80008320bc6fSPatrick Sanan     Input Parameters:
80018320bc6fSPatrick Sanan +    dm - the first DM
80028320bc6fSPatrick Sanan -    dm2 - the second DM
80038320bc6fSPatrick Sanan 
80048320bc6fSPatrick Sanan     Output Parameters:
80058320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
80068320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
80078320bc6fSPatrick Sanan 
80088320bc6fSPatrick Sanan     Notes:
80098320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
80103d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
80118320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
80123d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
80133d862458SPatrick Sanan     decomposition, but hold different data.
80148320bc6fSPatrick Sanan 
80158320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
80168320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
80178320bc6fSPatrick Sanan 
80188320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
80198320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
80208320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
80218320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
80228320bc6fSPatrick Sanan 
80238320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
80248320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
80258320bc6fSPatrick Sanan .vb
80268320bc6fSPatrick Sanan   ...
80278320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
80288320bc6fSPatrick Sanan   if (set && compatible)  {
80298320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
80308320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
80313d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
80328320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
80338320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
80348320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
80358320bc6fSPatrick Sanan       }
80368320bc6fSPatrick Sanan     }
80378320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
80388320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
80398320bc6fSPatrick Sanan   } else {
80408320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
80418320bc6fSPatrick Sanan   }
80428320bc6fSPatrick Sanan   ...
80438320bc6fSPatrick Sanan .ve
80448320bc6fSPatrick Sanan 
80458320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
80468320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
80478320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
80488320bc6fSPatrick Sanan     always check the "set" output parameter.
80498320bc6fSPatrick Sanan 
80508320bc6fSPatrick Sanan     A DM is always compatible with itself.
80518320bc6fSPatrick Sanan 
80528320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
80538320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
80548320bc6fSPatrick Sanan     incompatible.
80558320bc6fSPatrick Sanan 
80568320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
80578320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
80588320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
80598320bc6fSPatrick Sanan 
80608320bc6fSPatrick Sanan     Developer Notes:
80613d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
80623d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
80638320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
80648320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
80658320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
80663d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
80673d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
80688320bc6fSPatrick Sanan 
80698320bc6fSPatrick Sanan     Level: advanced
80708320bc6fSPatrick Sanan 
80713d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
80728320bc6fSPatrick Sanan @*/
80738320bc6fSPatrick Sanan 
80748320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
80758320bc6fSPatrick Sanan {
80768320bc6fSPatrick Sanan   PetscErrorCode ierr;
80778320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
80788320bc6fSPatrick Sanan   DMType         type,type2;
80798320bc6fSPatrick Sanan   PetscBool      sameType;
80808320bc6fSPatrick Sanan 
80818320bc6fSPatrick Sanan   PetscFunctionBegin;
80828320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
80838320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
80848320bc6fSPatrick Sanan 
80858320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
80868320bc6fSPatrick Sanan   if (dm == dm2) {
80878320bc6fSPatrick Sanan     *set = PETSC_TRUE;
80888320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
80898320bc6fSPatrick Sanan     PetscFunctionReturn(0);
80908320bc6fSPatrick Sanan   }
80918320bc6fSPatrick Sanan 
80928320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
80938320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
80948320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
80958320bc6fSPatrick Sanan      determined by the implementation-specific logic */
80968320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
80978320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
80988320bc6fSPatrick Sanan     *set = PETSC_TRUE;
80998320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
81008320bc6fSPatrick Sanan     PetscFunctionReturn(0);
81018320bc6fSPatrick Sanan   }
81028320bc6fSPatrick Sanan 
81038320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
81048320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
81058320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
8106b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
81078320bc6fSPatrick Sanan   }
81088320bc6fSPatrick Sanan 
81098320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
81108320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
81118320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
81128320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
81138320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
81148320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
81158320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
81168320bc6fSPatrick Sanan   } else {
81178320bc6fSPatrick Sanan     *set = PETSC_FALSE;
81188320bc6fSPatrick Sanan   }
81198320bc6fSPatrick Sanan   PetscFunctionReturn(0);
81208320bc6fSPatrick Sanan }
8121