xref: /petsc/src/dm/interface/dm.c (revision fe2efc57b7777594bce7568e90822861480cbdc8)
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);
1205d80c0bfSVaclav 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 {
5745d80c0bfSVaclav 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 
5825d80c0bfSVaclav 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   }
5875d80c0bfSVaclav 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
850*fe2efc57SMark    DMViewFromOptions - View from Options
851*fe2efc57SMark 
852*fe2efc57SMark    Collective on DM
853*fe2efc57SMark 
854*fe2efc57SMark    Input Parameters:
855*fe2efc57SMark +  dm - the DM object
856*fe2efc57SMark -  obj - Optional object
857*fe2efc57SMark .  name - command line option
858*fe2efc57SMark 
859*fe2efc57SMark    Level: intermediate
860*fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
861*fe2efc57SMark @*/
862*fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
863*fe2efc57SMark {
864*fe2efc57SMark   PetscErrorCode ierr;
865*fe2efc57SMark 
866*fe2efc57SMark   PetscFunctionBegin;
867*fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
868*fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
869*fe2efc57SMark   PetscFunctionReturn(0);
870*fe2efc57SMark }
871*fe2efc57SMark 
872*fe2efc57SMark /*@C
873224748a4SBarry Smith     DMView - Views a DM
87447c6ae99SBarry Smith 
875d083f849SBarry Smith     Collective on dm
87647c6ae99SBarry Smith 
87747c6ae99SBarry Smith     Input Parameter:
87847c6ae99SBarry Smith +   dm - the DM object to view
87947c6ae99SBarry Smith -   v - the viewer
88047c6ae99SBarry Smith 
881224748a4SBarry Smith     Level: beginner
88247c6ae99SBarry Smith 
883e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88447c6ae99SBarry Smith 
88547c6ae99SBarry Smith @*/
8867087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
88747c6ae99SBarry Smith {
88847c6ae99SBarry Smith   PetscErrorCode    ierr;
88932c0f0efSBarry Smith   PetscBool         isbinary;
89076a8abe0SBarry Smith   PetscMPIInt       size;
89176a8abe0SBarry Smith   PetscViewerFormat format;
89247c6ae99SBarry Smith 
89347c6ae99SBarry Smith   PetscFunctionBegin;
894171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8953014e516SBarry Smith   if (!v) {
896ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
8973014e516SBarry Smith   }
898b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
89974903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
90074903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90174903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90274903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90374903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90474903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90574903a4fSStefano Zampini      in an error here */
90674903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9078bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
908b1b135c8SBarry Smith 
90976a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
91076a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91176a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91298c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91332c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91432c0f0efSBarry Smith   if (isbinary) {
91555849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91632c0f0efSBarry Smith     char     type[256];
91732c0f0efSBarry Smith 
91832c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT,PETSC_FALSE);CHKERRQ(ierr);
91932c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
92032c0f0efSBarry Smith     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR,PETSC_FALSE);CHKERRQ(ierr);
92132c0f0efSBarry Smith   }
9220c010503SBarry Smith   if (dm->ops->view) {
9230c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92447c6ae99SBarry Smith   }
92547c6ae99SBarry Smith   PetscFunctionReturn(0);
92647c6ae99SBarry Smith }
92747c6ae99SBarry Smith 
92847c6ae99SBarry Smith /*@
9298472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
93047c6ae99SBarry Smith 
931d083f849SBarry Smith     Collective on dm
93247c6ae99SBarry Smith 
93347c6ae99SBarry Smith     Input Parameter:
93447c6ae99SBarry Smith .   dm - the DM object
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith     Output Parameter:
93747c6ae99SBarry Smith .   vec - the global vector
93847c6ae99SBarry Smith 
939073dac72SJed Brown     Level: beginner
94047c6ae99SBarry Smith 
941e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94247c6ae99SBarry Smith 
94347c6ae99SBarry Smith @*/
9447087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94547c6ae99SBarry Smith {
94647c6ae99SBarry Smith   PetscErrorCode ierr;
94747c6ae99SBarry Smith 
94847c6ae99SBarry Smith   PetscFunctionBegin;
949171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
950b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
951b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95247c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95347c6ae99SBarry Smith   PetscFunctionReturn(0);
95447c6ae99SBarry Smith }
95547c6ae99SBarry Smith 
95647c6ae99SBarry Smith /*@
9578472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
95847c6ae99SBarry Smith 
95947c6ae99SBarry Smith     Not Collective
96047c6ae99SBarry Smith 
96147c6ae99SBarry Smith     Input Parameter:
96247c6ae99SBarry Smith .   dm - the DM object
96347c6ae99SBarry Smith 
96447c6ae99SBarry Smith     Output Parameter:
96547c6ae99SBarry Smith .   vec - the local vector
96647c6ae99SBarry Smith 
967073dac72SJed Brown     Level: beginner
96847c6ae99SBarry Smith 
969e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith @*/
9727087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
97347c6ae99SBarry Smith {
97447c6ae99SBarry Smith   PetscErrorCode ierr;
97547c6ae99SBarry Smith 
97647c6ae99SBarry Smith   PetscFunctionBegin;
977171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
978b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
979b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
98047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
98147c6ae99SBarry Smith   PetscFunctionReturn(0);
98247c6ae99SBarry Smith }
98347c6ae99SBarry Smith 
9841411c6eeSJed Brown /*@
9851411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
9861411c6eeSJed Brown 
987d083f849SBarry Smith    Collective on dm
9881411c6eeSJed Brown 
9891411c6eeSJed Brown    Input Parameter:
9901411c6eeSJed Brown .  dm - the DM that provides the mapping
9911411c6eeSJed Brown 
9921411c6eeSJed Brown    Output Parameter:
9931411c6eeSJed Brown .  ltog - the mapping
9941411c6eeSJed Brown 
9951411c6eeSJed Brown    Level: intermediate
9961411c6eeSJed Brown 
9971411c6eeSJed Brown    Notes:
9981411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
9991411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10001411c6eeSJed Brown 
1001fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10021411c6eeSJed Brown @*/
10037087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10041411c6eeSJed Brown {
10050be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10061411c6eeSJed Brown   PetscErrorCode ierr;
10071411c6eeSJed Brown 
10081411c6eeSJed Brown   PetscFunctionBegin;
10091411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10101411c6eeSJed Brown   PetscValidPointer(ltog,2);
10111411c6eeSJed Brown   if (!dm->ltogmap) {
101237d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
101337d0c07bSMatthew G Knepley 
101492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
101537d0c07bSMatthew G Knepley     if (section) {
1016a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
101737d0c07bSMatthew G Knepley       PetscInt       *ltog;
1018ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
101937d0c07bSMatthew G Knepley 
1020e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
102137d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1022ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1023ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
102437d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1025a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
102637d0c07bSMatthew G Knepley 
102737d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
102837d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1029a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1030a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
103137d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10321a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10331a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10341a7dc684SMatthew G. Knepley         if (dof) {
1035b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1036b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10371a7dc684SMatthew G. Knepley         }
103837d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1039a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1040a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
104137d0c07bSMatthew G Knepley         }
104237d0c07bSMatthew G Knepley       }
1043bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10440be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10450be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10460be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10470be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10487591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10497591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1050ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1051ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1052ccf3bd66SMatthew G. Knepley         n /= bs;
1053ccf3bd66SMatthew G. Knepley       }
1054ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10553bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
105637d0c07bSMatthew G Knepley     } else {
1057b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1058184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10591411c6eeSJed Brown     }
106037d0c07bSMatthew G Knepley   }
10611411c6eeSJed Brown   *ltog = dm->ltogmap;
10621411c6eeSJed Brown   PetscFunctionReturn(0);
10631411c6eeSJed Brown }
10641411c6eeSJed Brown 
10651411c6eeSJed Brown /*@
10661411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10671411c6eeSJed Brown 
10681411c6eeSJed Brown    Not Collective
10691411c6eeSJed Brown 
10701411c6eeSJed Brown    Input Parameter:
10711411c6eeSJed Brown .  dm - the DM with block structure
10721411c6eeSJed Brown 
10731411c6eeSJed Brown    Output Parameter:
10741411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10751411c6eeSJed Brown 
10761411c6eeSJed Brown    Level: intermediate
10771411c6eeSJed Brown 
1078fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10791411c6eeSJed Brown @*/
10807087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10811411c6eeSJed Brown {
10821411c6eeSJed Brown   PetscFunctionBegin;
10831411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1084534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
10851411c6eeSJed 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");
10861411c6eeSJed Brown   *bs = dm->bs;
10871411c6eeSJed Brown   PetscFunctionReturn(0);
10881411c6eeSJed Brown }
10891411c6eeSJed Brown 
109047c6ae99SBarry Smith /*@
10918472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
109247c6ae99SBarry Smith 
1093d083f849SBarry Smith     Collective on dm1
109447c6ae99SBarry Smith 
109547c6ae99SBarry Smith     Input Parameter:
109647c6ae99SBarry Smith +   dm1 - the DM object
109747c6ae99SBarry Smith -   dm2 - the second, finer DM object
109847c6ae99SBarry Smith 
109947c6ae99SBarry Smith     Output Parameter:
110047c6ae99SBarry Smith +  mat - the interpolation
110147c6ae99SBarry Smith -  vec - the scaling (optional)
110247c6ae99SBarry Smith 
110347c6ae99SBarry Smith     Level: developer
110447c6ae99SBarry Smith 
110595452b02SPatrick Sanan     Notes:
110695452b02SPatrick 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
110785afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1108d52bd9f3SBarry Smith 
11091f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1110d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
111185afcc9aSBarry Smith 
111285afcc9aSBarry Smith 
11133ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction()
111447c6ae99SBarry Smith 
111547c6ae99SBarry Smith @*/
1116e727c939SJed Brown PetscErrorCode  DMCreateInterpolation(DM dm1,DM dm2,Mat *mat,Vec *vec)
111747c6ae99SBarry Smith {
111847c6ae99SBarry Smith   PetscErrorCode ierr;
111947c6ae99SBarry Smith 
112047c6ae99SBarry Smith   PetscFunctionBegin;
1121171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1122171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
1123c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1124b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dm1)->type_name);
1125cea54e9dSPatrick Farrell   ierr = PetscLogEventBegin(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
112625296bd5SBarry Smith   ierr = (*dm1->ops->createinterpolation)(dm1,dm2,mat,vec);CHKERRQ(ierr);
1127cea54e9dSPatrick Farrell   ierr = PetscLogEventEnd(DM_CreateInterpolation,dm1,dm2,0,0);CHKERRQ(ierr);
112847c6ae99SBarry Smith   PetscFunctionReturn(0);
112947c6ae99SBarry Smith }
113047c6ae99SBarry Smith 
11313ad4599aSBarry Smith /*@
11323ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11333ad4599aSBarry Smith 
1134d083f849SBarry Smith     Collective on dm1
11353ad4599aSBarry Smith 
11363ad4599aSBarry Smith     Input Parameter:
11373ad4599aSBarry Smith +   dm1 - the DM object
11383ad4599aSBarry Smith -   dm2 - the second, finer DM object
11393ad4599aSBarry Smith 
11403ad4599aSBarry Smith     Output Parameter:
11413ad4599aSBarry Smith .  mat - the restriction
11423ad4599aSBarry Smith 
11433ad4599aSBarry Smith 
11443ad4599aSBarry Smith     Level: developer
11453ad4599aSBarry Smith 
114695452b02SPatrick Sanan     Notes:
114795452b02SPatrick 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
11483ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11493ad4599aSBarry Smith 
11503ad4599aSBarry Smith 
11513ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11523ad4599aSBarry Smith 
11533ad4599aSBarry Smith @*/
11543ad4599aSBarry Smith PetscErrorCode  DMCreateRestriction(DM dm1,DM dm2,Mat *mat)
11553ad4599aSBarry Smith {
11563ad4599aSBarry Smith   PetscErrorCode ierr;
11573ad4599aSBarry Smith 
11583ad4599aSBarry Smith   PetscFunctionBegin;
11593ad4599aSBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
11603ad4599aSBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11615a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1162b9d85ea2SLisandro Dalcin   if (!dm1->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dm1)->type_name);
11633ad4599aSBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11643ad4599aSBarry Smith   ierr = (*dm1->ops->createrestriction)(dm1,dm2,mat);CHKERRQ(ierr);
11653ad4599aSBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dm1,dm2,0,0);CHKERRQ(ierr);
11663ad4599aSBarry Smith   PetscFunctionReturn(0);
11673ad4599aSBarry Smith }
11683ad4599aSBarry Smith 
116947c6ae99SBarry Smith /*@
11708472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
117147c6ae99SBarry Smith 
1172d083f849SBarry Smith     Collective on dm1
117347c6ae99SBarry Smith 
117447c6ae99SBarry Smith     Input Parameter:
117547c6ae99SBarry Smith +   dm1 - the DM object
117647c6ae99SBarry Smith -   dm2 - the second, finer DM object
117747c6ae99SBarry Smith 
117847c6ae99SBarry Smith     Output Parameter:
11796dbf9973SLawrence Mitchell .   mat - the injection
118047c6ae99SBarry Smith 
118147c6ae99SBarry Smith     Level: developer
118247c6ae99SBarry Smith 
118395452b02SPatrick Sanan    Notes:
118495452b02SPatrick 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
118585afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
118685afcc9aSBarry Smith 
1187e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
118847c6ae99SBarry Smith 
118947c6ae99SBarry Smith @*/
11906dbf9973SLawrence Mitchell PetscErrorCode  DMCreateInjection(DM dm1,DM dm2,Mat *mat)
119147c6ae99SBarry Smith {
119247c6ae99SBarry Smith   PetscErrorCode ierr;
119347c6ae99SBarry Smith 
119447c6ae99SBarry Smith   PetscFunctionBegin;
1195171400e9SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
1196171400e9SBarry Smith   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
11975a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1198b9d85ea2SLisandro Dalcin   if (!dm1->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dm1)->type_name);
11995a84ad33SLisandro Dalcin   ierr = PetscLogEventBegin(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
12005a84ad33SLisandro Dalcin   ierr = (*dm1->ops->createinjection)(dm1,dm2,mat);CHKERRQ(ierr);
12015a84ad33SLisandro Dalcin   ierr = PetscLogEventEnd(DM_CreateInjection,dm1,dm2,0,0);CHKERRQ(ierr);
120247c6ae99SBarry Smith   PetscFunctionReturn(0);
120347c6ae99SBarry Smith }
120447c6ae99SBarry Smith 
1205b412c318SBarry Smith /*@
1206bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1207bd041c0cSMatthew G. Knepley 
1208d083f849SBarry Smith   Collective on dm1
1209bd041c0cSMatthew G. Knepley 
1210bd041c0cSMatthew G. Knepley   Input Parameter:
1211bd041c0cSMatthew G. Knepley + dm1 - the DM object
1212bd041c0cSMatthew G. Knepley - dm2 - the second, finer DM object
1213bd041c0cSMatthew G. Knepley 
1214bd041c0cSMatthew G. Knepley   Output Parameter:
1215bd041c0cSMatthew G. Knepley . mat - the interpolation
1216bd041c0cSMatthew G. Knepley 
1217bd041c0cSMatthew G. Knepley   Level: developer
1218bd041c0cSMatthew G. Knepley 
1219bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1220bd041c0cSMatthew G. Knepley @*/
1221bd041c0cSMatthew G. Knepley PetscErrorCode DMCreateMassMatrix(DM dm1, DM dm2, Mat *mat)
1222bd041c0cSMatthew G. Knepley {
1223bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1224bd041c0cSMatthew G. Knepley 
1225bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1226bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm1, DM_CLASSID, 1);
1227bd041c0cSMatthew G. Knepley   PetscValidHeaderSpecific(dm2, DM_CLASSID, 2);
12285a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1229b9d85ea2SLisandro Dalcin   if (!dm1->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dm1),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dm1)->type_name);
1230bd041c0cSMatthew G. Knepley   ierr = (*dm1->ops->createmassmatrix)(dm1, dm2, mat);CHKERRQ(ierr);
1231bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1232bd041c0cSMatthew G. Knepley }
1233bd041c0cSMatthew G. Knepley 
1234bd041c0cSMatthew G. Knepley /*@
1235b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
123647c6ae99SBarry Smith 
1237d083f849SBarry Smith     Collective on dm
123847c6ae99SBarry Smith 
123947c6ae99SBarry Smith     Input Parameter:
124047c6ae99SBarry Smith +   dm - the DM object
12415bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
124247c6ae99SBarry Smith 
124347c6ae99SBarry Smith     Output Parameter:
124447c6ae99SBarry Smith .   coloring - the coloring
124547c6ae99SBarry Smith 
124647c6ae99SBarry Smith     Level: developer
124747c6ae99SBarry Smith 
1248b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType()
124947c6ae99SBarry Smith 
1250aab9d709SJed Brown @*/
1251b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
125247c6ae99SBarry Smith {
125347c6ae99SBarry Smith   PetscErrorCode ierr;
125447c6ae99SBarry Smith 
125547c6ae99SBarry Smith   PetscFunctionBegin;
1256171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
12575a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1258b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1259b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
126047c6ae99SBarry Smith   PetscFunctionReturn(0);
126147c6ae99SBarry Smith }
126247c6ae99SBarry Smith 
1263b412c318SBarry Smith /*@
12648472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
126547c6ae99SBarry Smith 
1266d083f849SBarry Smith     Collective on dm
126747c6ae99SBarry Smith 
126847c6ae99SBarry Smith     Input Parameter:
1269b412c318SBarry Smith .   dm - the DM object
127047c6ae99SBarry Smith 
127147c6ae99SBarry Smith     Output Parameter:
127247c6ae99SBarry Smith .   mat - the empty Jacobian
127347c6ae99SBarry Smith 
1274073dac72SJed Brown     Level: beginner
127547c6ae99SBarry Smith 
127695452b02SPatrick Sanan     Notes:
127795452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
127894013140SBarry Smith        do not need to do it yourself.
127994013140SBarry Smith 
128094013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
12817889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
128294013140SBarry Smith 
128394013140SBarry 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
128494013140SBarry Smith        internally by PETSc.
128594013140SBarry Smith 
128694013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1287aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
128894013140SBarry Smith 
1289b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
129047c6ae99SBarry Smith 
1291aab9d709SJed Brown @*/
1292b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
129347c6ae99SBarry Smith {
129447c6ae99SBarry Smith   PetscErrorCode ierr;
129547c6ae99SBarry Smith 
129647c6ae99SBarry Smith   PetscFunctionBegin;
1297171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1298c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1299b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13005a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1301fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1302b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
1303e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1304e5e52638SMatthew G. Knepley   if (dm->Nf) {
1305e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1306e571a35bSMatthew G. Knepley     PetscInt     Nf;
1307e571a35bSMatthew G. Knepley 
1308e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1309e571a35bSMatthew G. Knepley     if (Nf == 1) {
1310e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
1311e571a35bSMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1312e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1313e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1314e571a35bSMatthew G. Knepley       }
1315e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
1316e571a35bSMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, &nullSpace);CHKERRQ(ierr);
1317e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1318e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1319e571a35bSMatthew G. Knepley       }
1320e571a35bSMatthew G. Knepley     }
1321e571a35bSMatthew G. Knepley   }
1322fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
132347c6ae99SBarry Smith   PetscFunctionReturn(0);
132447c6ae99SBarry Smith }
132547c6ae99SBarry Smith 
1326732e2eb9SMatthew G Knepley /*@
1327950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1328732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1329732e2eb9SMatthew G Knepley 
1330d083f849SBarry Smith   Logically Collective on dm
1331732e2eb9SMatthew G Knepley 
1332732e2eb9SMatthew G Knepley   Input Parameter:
1333732e2eb9SMatthew G Knepley + dm - the DM
1334732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1335732e2eb9SMatthew G Knepley 
1336732e2eb9SMatthew G Knepley   Level: developer
1337b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1338732e2eb9SMatthew G Knepley @*/
1339732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1340732e2eb9SMatthew G Knepley {
1341732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1342732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1343732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1344732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1345732e2eb9SMatthew G Knepley }
1346732e2eb9SMatthew G Knepley 
1347b06ff27eSHong Zhang /*@
1348b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1349b06ff27eSHong Zhang     but the array for values will not be allocated.
1350b06ff27eSHong Zhang 
1351d083f849SBarry Smith   Logically Collective on dm
1352b06ff27eSHong Zhang 
1353b06ff27eSHong Zhang   Input Parameter:
1354b06ff27eSHong Zhang + dm - the DM
1355b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1356b06ff27eSHong Zhang 
1357b06ff27eSHong Zhang   Level: developer
1358b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1359b06ff27eSHong Zhang @*/
1360b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1361b06ff27eSHong Zhang {
1362b06ff27eSHong Zhang   PetscFunctionBegin;
1363b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1364b06ff27eSHong Zhang   dm->structure_only = only;
1365b06ff27eSHong Zhang   PetscFunctionReturn(0);
1366b06ff27eSHong Zhang }
1367b06ff27eSHong Zhang 
1368a89ea682SMatthew G Knepley /*@C
1369aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1370a89ea682SMatthew G Knepley 
1371a89ea682SMatthew G Knepley   Not Collective
1372a89ea682SMatthew G Knepley 
1373a89ea682SMatthew G Knepley   Input Parameters:
1374a89ea682SMatthew G Knepley + dm - the DM object
1375aa1993deSMatthew G Knepley . count - The minium size
137669291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1377a89ea682SMatthew G Knepley 
1378a89ea682SMatthew G Knepley   Output Parameter:
1379a89ea682SMatthew G Knepley . array - the work array
1380a89ea682SMatthew G Knepley 
1381a89ea682SMatthew G Knepley   Level: developer
1382a89ea682SMatthew G Knepley 
1383a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1384a89ea682SMatthew G Knepley @*/
138569291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1386a89ea682SMatthew G Knepley {
1387a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1388aa1993deSMatthew G Knepley   DMWorkLink     link;
138969291d52SBarry Smith   PetscMPIInt    dsize;
1390a89ea682SMatthew G Knepley 
1391a89ea682SMatthew G Knepley   PetscFunctionBegin;
1392a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1393aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1394aa1993deSMatthew G Knepley   if (dm->workin) {
1395aa1993deSMatthew G Knepley     link       = dm->workin;
1396aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1397aa1993deSMatthew G Knepley   } else {
1398b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1399a89ea682SMatthew G Knepley   }
140069291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14015056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1402aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1403854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1404854ce69bSBarry Smith     link->bytes = dsize*count;
1405aa1993deSMatthew G Knepley   }
1406aa1993deSMatthew G Knepley   link->next   = dm->workout;
1407aa1993deSMatthew G Knepley   dm->workout  = link;
1408aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1409a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1410a89ea682SMatthew G Knepley }
1411a89ea682SMatthew G Knepley 
1412aa1993deSMatthew G Knepley /*@C
1413aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1414aa1993deSMatthew G Knepley 
1415aa1993deSMatthew G Knepley   Not Collective
1416aa1993deSMatthew G Knepley 
1417aa1993deSMatthew G Knepley   Input Parameters:
1418aa1993deSMatthew G Knepley + dm - the DM object
1419aa1993deSMatthew G Knepley . count - The minium size
142069291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1421aa1993deSMatthew G Knepley 
1422aa1993deSMatthew G Knepley   Output Parameter:
1423aa1993deSMatthew G Knepley . array - the work array
1424aa1993deSMatthew G Knepley 
1425aa1993deSMatthew G Knepley   Level: developer
1426aa1993deSMatthew G Knepley 
142795452b02SPatrick Sanan   Developer Notes:
142895452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1429aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1430aa1993deSMatthew G Knepley @*/
143169291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1432aa1993deSMatthew G Knepley {
1433aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1434aa1993deSMatthew G Knepley 
1435aa1993deSMatthew G Knepley   PetscFunctionBegin;
1436aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1437aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1438aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1439aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1440aa1993deSMatthew G Knepley       *p           = link->next;
1441aa1993deSMatthew G Knepley       link->next   = dm->workin;
1442aa1993deSMatthew G Knepley       dm->workin   = link;
14430298fd71SBarry Smith       *(void**)mem = NULL;
1444aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1445aa1993deSMatthew G Knepley     }
1446aa1993deSMatthew G Knepley   }
1447aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1448aa1993deSMatthew G Knepley }
1449e7c4fc90SDmitry Karpeev 
1450435a35e8SMatthew G Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1451435a35e8SMatthew G Knepley {
1452435a35e8SMatthew G Knepley   PetscFunctionBegin;
1453435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
145482f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1455435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1456435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1457435a35e8SMatthew G Knepley }
1458435a35e8SMatthew G Knepley 
14590a50eb56SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
14600a50eb56SMatthew G. Knepley {
14610a50eb56SMatthew G. Knepley   PetscFunctionBegin;
14620a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1463f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
14640a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
14650a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
14660a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
14670a50eb56SMatthew G. Knepley }
14680a50eb56SMatthew G. Knepley 
1469f9d4088aSMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1470f9d4088aSMatthew G. Knepley {
1471f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1472f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1473f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1474f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1475f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1476f9d4088aSMatthew G. Knepley }
1477f9d4088aSMatthew G. Knepley 
1478f9d4088aSMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt field, MatNullSpace *nullSpace))
1479f9d4088aSMatthew G. Knepley {
1480f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1481f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1482f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1483f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1484f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1485f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1486f9d4088aSMatthew G. Knepley }
1487f9d4088aSMatthew G. Knepley 
14884f3b5142SJed Brown /*@C
14894d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
14904d343eeaSMatthew G Knepley 
14914d343eeaSMatthew G Knepley   Not collective
14924d343eeaSMatthew G Knepley 
14934d343eeaSMatthew G Knepley   Input Parameter:
14944d343eeaSMatthew G Knepley . dm - the DM object
14954d343eeaSMatthew G Knepley 
14964d343eeaSMatthew G Knepley   Output Parameters:
14970298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
14980298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
14990298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
15004d343eeaSMatthew G Knepley 
15014d343eeaSMatthew G Knepley   Level: intermediate
15024d343eeaSMatthew G Knepley 
150321c9b008SJed Brown   Notes:
150421c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
150521c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
150621c9b008SJed Brown   PetscFree().
150721c9b008SJed Brown 
15084d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
15094d343eeaSMatthew G Knepley @*/
151037d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
15114d343eeaSMatthew G Knepley {
151237d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
15134d343eeaSMatthew G Knepley   PetscErrorCode ierr;
15144d343eeaSMatthew G Knepley 
15154d343eeaSMatthew G Knepley   PetscFunctionBegin;
15164d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
151769ca1f37SDmitry Karpeev   if (numFields) {
1518534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
151969ca1f37SDmitry Karpeev     *numFields = 0;
152069ca1f37SDmitry Karpeev   }
152137d0c07bSMatthew G Knepley   if (fieldNames) {
152237d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
15230298fd71SBarry Smith     *fieldNames = NULL;
152469ca1f37SDmitry Karpeev   }
152569ca1f37SDmitry Karpeev   if (fields) {
152669ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
15270298fd71SBarry Smith     *fields = NULL;
152869ca1f37SDmitry Karpeev   }
152992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
153037d0c07bSMatthew G Knepley   if (section) {
15313a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
153237d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
153337d0c07bSMatthew G Knepley 
1534e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
153537d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
15363a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
153737d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
153837d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
153937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
15403a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
154137d0c07bSMatthew G Knepley     }
154237d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
154337d0c07bSMatthew G Knepley       PetscInt gdof;
154437d0c07bSMatthew G Knepley 
154537d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
154637d0c07bSMatthew G Knepley       if (gdof > 0) {
154737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
15483a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
154937d0c07bSMatthew G Knepley 
155037d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
155137d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
15523a544194SStefano Zampini           fpdof = fdof-fcdof;
15533a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
15543a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
15553a544194SStefano Zampini             fieldNc[f] = 1;
15563a544194SStefano Zampini           }
15573a544194SStefano Zampini           fieldSizes[f] += fpdof;
155837d0c07bSMatthew G Knepley         }
155937d0c07bSMatthew G Knepley       }
156037d0c07bSMatthew G Knepley     }
156137d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1562785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
156337d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
156437d0c07bSMatthew G Knepley     }
156537d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
156637d0c07bSMatthew G Knepley       PetscInt gdof, goff;
156737d0c07bSMatthew G Knepley 
156837d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
156937d0c07bSMatthew G Knepley       if (gdof > 0) {
157037d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
157137d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
157237d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
157337d0c07bSMatthew G Knepley 
157437d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
157537d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
157637d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
157737d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
157837d0c07bSMatthew G Knepley           }
157937d0c07bSMatthew G Knepley         }
158037d0c07bSMatthew G Knepley       }
158137d0c07bSMatthew G Knepley     }
15828865f1eaSKarl Rupp     if (numFields) *numFields = nF;
158337d0c07bSMatthew G Knepley     if (fieldNames) {
1584785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
158537d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
158637d0c07bSMatthew G Knepley         const char *fieldName;
158737d0c07bSMatthew G Knepley 
158837d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
158937d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
159037d0c07bSMatthew G Knepley       }
159137d0c07bSMatthew G Knepley     }
159237d0c07bSMatthew G Knepley     if (fields) {
1593785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
159437d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
15953a544194SStefano Zampini         PetscInt bs, in[2], out[2];
15963a544194SStefano Zampini 
159782f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
15983a544194SStefano Zampini         in[0] = -fieldNc[f];
15993a544194SStefano Zampini         in[1] = fieldNc[f];
16003a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
16013a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
16023a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
160337d0c07bSMatthew G Knepley       }
160437d0c07bSMatthew G Knepley     }
16053a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
16068865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
16078865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
160869ca1f37SDmitry Karpeev   }
16094d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
16104d343eeaSMatthew G Knepley }
16114d343eeaSMatthew G Knepley 
161216621825SDmitry Karpeev 
161316621825SDmitry Karpeev /*@C
161416621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
161516621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
161616621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1617e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1618e7c4fc90SDmitry Karpeev 
1619e7c4fc90SDmitry Karpeev   Not collective
1620e7c4fc90SDmitry Karpeev 
1621e7c4fc90SDmitry Karpeev   Input Parameter:
1622e7c4fc90SDmitry Karpeev . dm - the DM object
1623e7c4fc90SDmitry Karpeev 
1624e7c4fc90SDmitry Karpeev   Output Parameters:
16250298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
16260298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
16270298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
16280298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1629e7c4fc90SDmitry Karpeev 
1630e7c4fc90SDmitry Karpeev   Level: intermediate
1631e7c4fc90SDmitry Karpeev 
1632e7c4fc90SDmitry Karpeev   Notes:
1633e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1634e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1635e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1636e7c4fc90SDmitry Karpeev 
1637e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1638e7c4fc90SDmitry Karpeev @*/
163916621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1640e7c4fc90SDmitry Karpeev {
1641e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1642e7c4fc90SDmitry Karpeev 
1643e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1644e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
16458865f1eaSKarl Rupp   if (len) {
1646534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
16478865f1eaSKarl Rupp     *len = 0;
16488865f1eaSKarl Rupp   }
16498865f1eaSKarl Rupp   if (namelist) {
16508865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
16518865f1eaSKarl Rupp     *namelist = 0;
16528865f1eaSKarl Rupp   }
16538865f1eaSKarl Rupp   if (islist) {
16548865f1eaSKarl Rupp     PetscValidPointer(islist,4);
16558865f1eaSKarl Rupp     *islist = 0;
16568865f1eaSKarl Rupp   }
16578865f1eaSKarl Rupp   if (dmlist) {
16588865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
16598865f1eaSKarl Rupp     *dmlist = 0;
16608865f1eaSKarl Rupp   }
1661f3f0edfdSDmitry Karpeev   /*
1662f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1663f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1664f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1665f3f0edfdSDmitry Karpeev    */
1666ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
166716621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1668435a35e8SMatthew G Knepley     PetscSection section;
1669435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1670435a35e8SMatthew G Knepley 
167192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1672435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1673435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1674f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
167503dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
167603dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
167703dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1678435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1679435a35e8SMatthew G Knepley         const char *fieldName;
1680435a35e8SMatthew G Knepley 
168103dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
168203dc3394SMatthew G. Knepley         if (namelist) {
1683435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1684435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1685435a35e8SMatthew G Knepley         }
168603dc3394SMatthew G. Knepley       }
1687435a35e8SMatthew G Knepley     } else {
168869ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1689e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
16900298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1691e7c4fc90SDmitry Karpeev     }
16928865f1eaSKarl Rupp   } else {
169316621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
169416621825SDmitry Karpeev   }
169516621825SDmitry Karpeev   PetscFunctionReturn(0);
169616621825SDmitry Karpeev }
169716621825SDmitry Karpeev 
1698564cec59SMatthew G. Knepley /*@
1699435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1700435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1701435a35e8SMatthew G Knepley 
1702435a35e8SMatthew G Knepley   Not collective
1703435a35e8SMatthew G Knepley 
1704435a35e8SMatthew G Knepley   Input Parameters:
17052adcc780SMatthew G. Knepley + dm        - The DM object
17062adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
17072adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1708435a35e8SMatthew G Knepley 
1709435a35e8SMatthew G Knepley   Output Parameters:
17102adcc780SMatthew G. Knepley + is - The global indices for the subproblem
17112adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1712435a35e8SMatthew G Knepley 
17135d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17145d3b26e6SMatthew G. Knepley 
1715435a35e8SMatthew G Knepley   Level: intermediate
1716435a35e8SMatthew G Knepley 
17175d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1718435a35e8SMatthew G Knepley @*/
171937bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1720435a35e8SMatthew G Knepley {
1721435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1722435a35e8SMatthew G Knepley 
1723435a35e8SMatthew G Knepley   PetscFunctionBegin;
1724435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1725435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
17268865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
17278865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1728b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1729435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1730435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1731435a35e8SMatthew G Knepley }
1732435a35e8SMatthew G Knepley 
17332adcc780SMatthew G. Knepley /*@C
17342adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
17352adcc780SMatthew G. Knepley 
17362adcc780SMatthew G. Knepley   Not collective
17372adcc780SMatthew G. Knepley 
17382adcc780SMatthew G. Knepley   Input Parameter:
17392adcc780SMatthew G. Knepley + dms - The DM objects
17402adcc780SMatthew G. Knepley - len - The number of DMs
17412adcc780SMatthew G. Knepley 
17422adcc780SMatthew G. Knepley   Output Parameters:
1743a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
17442adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
17452adcc780SMatthew G. Knepley 
17465d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
17475d3b26e6SMatthew G. Knepley 
17482adcc780SMatthew G. Knepley   Level: intermediate
17492adcc780SMatthew G. Knepley 
17505d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
17512adcc780SMatthew G. Knepley @*/
17522adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
17532adcc780SMatthew G. Knepley {
17542adcc780SMatthew G. Knepley   PetscInt       i;
17552adcc780SMatthew G. Knepley   PetscErrorCode ierr;
17562adcc780SMatthew G. Knepley 
17572adcc780SMatthew G. Knepley   PetscFunctionBegin;
17582adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
17592adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
17602adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1761a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
17622adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
17632adcc780SMatthew G. Knepley   if (len) {
1764b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1765b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1766b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
17672adcc780SMatthew G. Knepley   }
17682adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
17692adcc780SMatthew G. Knepley }
17702adcc780SMatthew G. Knepley 
177116621825SDmitry Karpeev 
177216621825SDmitry Karpeev /*@C
17738d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
17748d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
17758d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
17768d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
17778d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
177816621825SDmitry Karpeev 
177916621825SDmitry Karpeev   Not collective
178016621825SDmitry Karpeev 
178116621825SDmitry Karpeev   Input Parameter:
178216621825SDmitry Karpeev . dm - the DM object
178316621825SDmitry Karpeev 
178416621825SDmitry Karpeev   Output Parameters:
17850298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
17860298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
17870298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
17880298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
17890298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
179016621825SDmitry Karpeev 
179116621825SDmitry Karpeev   Level: intermediate
179216621825SDmitry Karpeev 
179316621825SDmitry Karpeev   Notes:
179416621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
179516621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
179616621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
179716621825SDmitry Karpeev 
17988d4ac253SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateDomainDecompositionDM(), DMCreateFieldDecomposition()
179916621825SDmitry Karpeev @*/
18008d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
180116621825SDmitry Karpeev {
180216621825SDmitry Karpeev   PetscErrorCode      ierr;
1803be081cd6SPeter Brune   DMSubDomainHookLink link;
1804be081cd6SPeter Brune   PetscInt            i,l;
180516621825SDmitry Karpeev 
180616621825SDmitry Karpeev   PetscFunctionBegin;
180716621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
180814a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
18090298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
18100298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
18110298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
18120298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1813f3f0edfdSDmitry Karpeev   /*
1814f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1815f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1816f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1817f3f0edfdSDmitry Karpeev    */
1818ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
181916621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1820be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
182114a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1822f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1823be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1824be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1825be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1826be081cd6SPeter Brune         }
1827648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1828e7c4fc90SDmitry Karpeev       }
182914a18fd3SPeter Brune     }
183014a18fd3SPeter Brune     if (len) *len = l;
183114a18fd3SPeter Brune   }
1832e30e807fSPeter Brune   PetscFunctionReturn(0);
1833e30e807fSPeter Brune }
1834e30e807fSPeter Brune 
1835e30e807fSPeter Brune 
1836e30e807fSPeter Brune /*@C
1837e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1838e30e807fSPeter Brune 
1839e30e807fSPeter Brune   Not collective
1840e30e807fSPeter Brune 
1841e30e807fSPeter Brune   Input Parameters:
1842e30e807fSPeter Brune + dm - the DM object
1843e30e807fSPeter Brune . n  - the number of subdomain scatters
1844e30e807fSPeter Brune - subdms - the local subdomains
1845e30e807fSPeter Brune 
1846e30e807fSPeter Brune   Output Parameters:
1847e30e807fSPeter Brune + n     - the number of scatters returned
1848e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
1849e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
1850e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
1851e30e807fSPeter Brune 
185295452b02SPatrick Sanan   Notes:
185395452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
1854e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
1855e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
1856e30e807fSPeter Brune   solution and residual data.
1857e30e807fSPeter Brune 
1858e30e807fSPeter Brune   Level: developer
1859e30e807fSPeter Brune 
1860e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1861e30e807fSPeter Brune @*/
1862e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
1863e30e807fSPeter Brune {
1864e30e807fSPeter Brune   PetscErrorCode ierr;
1865e30e807fSPeter Brune 
1866e30e807fSPeter Brune   PetscFunctionBegin;
1867e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1868e30e807fSPeter Brune   PetscValidPointer(subdms,3);
1869b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
1870e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
1871e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
1872e7c4fc90SDmitry Karpeev }
1873e7c4fc90SDmitry Karpeev 
187447c6ae99SBarry Smith /*@
187547c6ae99SBarry Smith   DMRefine - Refines a DM object
187647c6ae99SBarry Smith 
1877d083f849SBarry Smith   Collective on dm
187847c6ae99SBarry Smith 
187947c6ae99SBarry Smith   Input Parameter:
188047c6ae99SBarry Smith + dm   - the DM object
188191d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
188247c6ae99SBarry Smith 
188347c6ae99SBarry Smith   Output Parameter:
18840298fd71SBarry Smith . dmf - the refined DM, or NULL
1885ae0a1c52SMatthew G Knepley 
18860298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
188747c6ae99SBarry Smith 
188847c6ae99SBarry Smith   Level: developer
188947c6ae99SBarry Smith 
1890e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
189147c6ae99SBarry Smith @*/
18927087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
189347c6ae99SBarry Smith {
189447c6ae99SBarry Smith   PetscErrorCode   ierr;
1895c833c3b5SJed Brown   DMRefineHookLink link;
189647c6ae99SBarry Smith 
189747c6ae99SBarry Smith   PetscFunctionBegin;
1898732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1899b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
19001ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
190147c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
19024057135bSMatthew G Knepley   if (*dmf) {
190343842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
19048865f1eaSKarl Rupp 
19058cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
19068865f1eaSKarl Rupp 
1907644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
19080598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
1909656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
19108865f1eaSKarl Rupp 
1911e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
1912c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
19138865f1eaSKarl Rupp       if (link->refinehook) {
19148865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
19158865f1eaSKarl Rupp       }
1916c833c3b5SJed Brown     }
1917c833c3b5SJed Brown   }
19181ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
1919c833c3b5SJed Brown   PetscFunctionReturn(0);
1920c833c3b5SJed Brown }
1921c833c3b5SJed Brown 
1922bb9467b5SJed Brown /*@C
1923c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
1924c833c3b5SJed Brown 
1925c833c3b5SJed Brown    Logically Collective
1926c833c3b5SJed Brown 
1927c833c3b5SJed Brown    Input Arguments:
1928c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
1929c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
1930c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19310298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
1932c833c3b5SJed Brown 
1933c833c3b5SJed Brown    Calling sequence of refinehook:
1934c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
1935c833c3b5SJed Brown 
1936c833c3b5SJed Brown +  coarse - coarse level DM
1937c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
1938c833c3b5SJed Brown -  ctx - optional user-defined function context
1939c833c3b5SJed Brown 
1940c833c3b5SJed Brown    Calling sequence for interphook:
1941c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
1942c833c3b5SJed Brown 
1943c833c3b5SJed Brown +  coarse - coarse level DM
1944c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
1945c833c3b5SJed Brown .  fine - fine level DM to update
1946c833c3b5SJed Brown -  ctx - optional user-defined function context
1947c833c3b5SJed Brown 
1948c833c3b5SJed Brown    Level: advanced
1949c833c3b5SJed Brown 
1950c833c3b5SJed Brown    Notes:
1951c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
1952c833c3b5SJed Brown 
1953c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
1954c833c3b5SJed Brown 
1955bb9467b5SJed Brown    This function is currently not available from Fortran.
1956bb9467b5SJed Brown 
1957c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
1958c833c3b5SJed Brown @*/
1959c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
1960c833c3b5SJed Brown {
1961c833c3b5SJed Brown   PetscErrorCode   ierr;
1962c833c3b5SJed Brown   DMRefineHookLink link,*p;
1963c833c3b5SJed Brown 
1964c833c3b5SJed Brown   PetscFunctionBegin;
1965c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
19663d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
19673d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
19683d8e3701SJed Brown   }
196995dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
1970c833c3b5SJed Brown   link->refinehook = refinehook;
1971c833c3b5SJed Brown   link->interphook = interphook;
1972c833c3b5SJed Brown   link->ctx        = ctx;
19730298fd71SBarry Smith   link->next       = NULL;
1974c833c3b5SJed Brown   *p               = link;
1975c833c3b5SJed Brown   PetscFunctionReturn(0);
1976c833c3b5SJed Brown }
1977c833c3b5SJed Brown 
19783d8e3701SJed Brown /*@C
19793d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
19803d8e3701SJed Brown 
19813d8e3701SJed Brown    Logically Collective
19823d8e3701SJed Brown 
19833d8e3701SJed Brown    Input Arguments:
19843d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
19853d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
19863d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
19873d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
19883d8e3701SJed Brown 
19893d8e3701SJed Brown    Level: advanced
19903d8e3701SJed Brown 
19913d8e3701SJed Brown    Notes:
19923d8e3701SJed Brown    This function does nothing if the hook is not in the list.
19933d8e3701SJed Brown 
19943d8e3701SJed Brown    This function is currently not available from Fortran.
19953d8e3701SJed Brown 
19963d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
19973d8e3701SJed Brown @*/
19983d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
19993d8e3701SJed Brown {
20003d8e3701SJed Brown   PetscErrorCode   ierr;
20013d8e3701SJed Brown   DMRefineHookLink link,*p;
20023d8e3701SJed Brown 
20033d8e3701SJed Brown   PetscFunctionBegin;
20043d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
20053d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
20063d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
20073d8e3701SJed Brown       link = *p;
20083d8e3701SJed Brown       *p = link->next;
20093d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
20103d8e3701SJed Brown       break;
20113d8e3701SJed Brown     }
20123d8e3701SJed Brown   }
20133d8e3701SJed Brown   PetscFunctionReturn(0);
20143d8e3701SJed Brown }
20153d8e3701SJed Brown 
2016c833c3b5SJed Brown /*@
2017c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2018c833c3b5SJed Brown 
2019c833c3b5SJed Brown    Collective if any hooks are
2020c833c3b5SJed Brown 
2021c833c3b5SJed Brown    Input Arguments:
2022c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2023e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2024c833c3b5SJed Brown -  fine - finer DM to update
2025c833c3b5SJed Brown 
2026c833c3b5SJed Brown    Level: developer
2027c833c3b5SJed Brown 
2028c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2029c833c3b5SJed Brown @*/
2030c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2031c833c3b5SJed Brown {
2032c833c3b5SJed Brown   PetscErrorCode   ierr;
2033c833c3b5SJed Brown   DMRefineHookLink link;
2034c833c3b5SJed Brown 
2035c833c3b5SJed Brown   PetscFunctionBegin;
2036c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
20378865f1eaSKarl Rupp     if (link->interphook) {
20388865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
20398865f1eaSKarl Rupp     }
20404057135bSMatthew G Knepley   }
204147c6ae99SBarry Smith   PetscFunctionReturn(0);
204247c6ae99SBarry Smith }
204347c6ae99SBarry Smith 
2044eb3f98d2SBarry Smith /*@
2045eb3f98d2SBarry Smith     DMGetRefineLevel - Get's the number of refinements that have generated this DM.
2046eb3f98d2SBarry Smith 
2047eb3f98d2SBarry Smith     Not Collective
2048eb3f98d2SBarry Smith 
2049eb3f98d2SBarry Smith     Input Parameter:
2050eb3f98d2SBarry Smith .   dm - the DM object
2051eb3f98d2SBarry Smith 
2052eb3f98d2SBarry Smith     Output Parameter:
2053eb3f98d2SBarry Smith .   level - number of refinements
2054eb3f98d2SBarry Smith 
2055eb3f98d2SBarry Smith     Level: developer
2056eb3f98d2SBarry Smith 
20576a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2058eb3f98d2SBarry Smith 
2059eb3f98d2SBarry Smith @*/
2060eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2061eb3f98d2SBarry Smith {
2062eb3f98d2SBarry Smith   PetscFunctionBegin;
2063eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2064eb3f98d2SBarry Smith   *level = dm->levelup;
2065eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2066eb3f98d2SBarry Smith }
2067eb3f98d2SBarry Smith 
2068fef3a512SBarry Smith /*@
2069fef3a512SBarry Smith     DMSetRefineLevel - Set's the number of refinements that have generated this DM.
2070fef3a512SBarry Smith 
2071fef3a512SBarry Smith     Not Collective
2072fef3a512SBarry Smith 
2073fef3a512SBarry Smith     Input Parameter:
2074fef3a512SBarry Smith +   dm - the DM object
2075fef3a512SBarry Smith -   level - number of refinements
2076fef3a512SBarry Smith 
2077fef3a512SBarry Smith     Level: advanced
2078fef3a512SBarry Smith 
207995452b02SPatrick Sanan     Notes:
208095452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2081fef3a512SBarry Smith 
2082fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2083fef3a512SBarry Smith 
2084fef3a512SBarry Smith @*/
2085fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2086fef3a512SBarry Smith {
2087fef3a512SBarry Smith   PetscFunctionBegin;
2088fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2089fef3a512SBarry Smith   dm->levelup = level;
2090fef3a512SBarry Smith   PetscFunctionReturn(0);
2091fef3a512SBarry Smith }
2092fef3a512SBarry Smith 
2093ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2094ca3d3a14SMatthew G. Knepley {
2095ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2096ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2097ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2098ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2099ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2100ca3d3a14SMatthew G. Knepley }
2101ca3d3a14SMatthew G. Knepley 
2102ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2103ca3d3a14SMatthew G. Knepley {
2104ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2105ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2106ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2107ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2108ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2109ca3d3a14SMatthew G. Knepley }
2110ca3d3a14SMatthew G. Knepley 
2111ca3d3a14SMatthew G. Knepley /*@
2112c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2113ca3d3a14SMatthew G. Knepley 
2114ca3d3a14SMatthew G. Knepley   Input Parameter:
2115ca3d3a14SMatthew G. Knepley . dm - The DM
2116ca3d3a14SMatthew G. Knepley 
2117ca3d3a14SMatthew G. Knepley   Output Parameter:
2118ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2119ca3d3a14SMatthew G. Knepley 
2120ca3d3a14SMatthew G. Knepley   Level: developer
2121ca3d3a14SMatthew G. Knepley 
2122ca3d3a14SMatthew G. Knepley .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis()()
2123ca3d3a14SMatthew G. Knepley @*/
2124ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2125ca3d3a14SMatthew G. Knepley {
2126ca3d3a14SMatthew G. Knepley   Vec            tv;
2127ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2128ca3d3a14SMatthew G. Knepley 
2129ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2130ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2131534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2132ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2133ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2134ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2135ca3d3a14SMatthew G. Knepley }
2136ca3d3a14SMatthew G. Knepley 
2137ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2138ca3d3a14SMatthew G. Knepley {
2139ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2140ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2141ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2142ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2143ca3d3a14SMatthew G. Knepley 
2144ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2145ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
214692fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2147ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2148ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2149ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
215092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2151ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2152ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2153ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2154ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2155ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2156ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2157ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2158ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2159ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2160ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2161ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2162ca3d3a14SMatthew G. Knepley     }
2163ca3d3a14SMatthew G. Knepley   }
2164ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2165ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2166ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2167ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2168ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2169ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2170ca3d3a14SMatthew G. Knepley       if (dof) {
2171ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2172ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2173ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2174ca3d3a14SMatthew G. Knepley 
2175ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2176ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2177ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2178580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2179ca3d3a14SMatthew G. Knepley       }
2180ca3d3a14SMatthew G. Knepley     }
2181ca3d3a14SMatthew G. Knepley   }
2182ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2183ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2184ca3d3a14SMatthew G. Knepley }
2185ca3d3a14SMatthew G. Knepley 
2186ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2187ca3d3a14SMatthew G. Knepley {
2188ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2189ca3d3a14SMatthew G. Knepley 
2190ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2191ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2192ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2193ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2194ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2195ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2196ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2197ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2198ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2199ca3d3a14SMatthew G. Knepley }
2200ca3d3a14SMatthew G. Knepley 
2201bb9467b5SJed Brown /*@C
2202baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2203baf369e7SPeter Brune 
2204baf369e7SPeter Brune    Logically Collective
2205baf369e7SPeter Brune 
2206baf369e7SPeter Brune    Input Arguments:
2207baf369e7SPeter Brune +  dm - the DM
2208baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2209baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
22100298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2211baf369e7SPeter Brune 
2212baf369e7SPeter Brune    Calling sequence for beginhook:
2213baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2214baf369e7SPeter Brune 
2215baf369e7SPeter Brune +  dm - global DM
2216baf369e7SPeter Brune .  g - global vector
2217baf369e7SPeter Brune .  mode - mode
2218baf369e7SPeter Brune .  l - local vector
2219baf369e7SPeter Brune -  ctx - optional user-defined function context
2220baf369e7SPeter Brune 
2221baf369e7SPeter Brune 
2222baf369e7SPeter Brune    Calling sequence for endhook:
2223ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2224baf369e7SPeter Brune 
2225baf369e7SPeter Brune +  global - global DM
2226baf369e7SPeter Brune -  ctx - optional user-defined function context
2227baf369e7SPeter Brune 
2228baf369e7SPeter Brune    Level: advanced
2229baf369e7SPeter Brune 
2230baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2231baf369e7SPeter Brune @*/
2232baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2233baf369e7SPeter Brune {
2234baf369e7SPeter Brune   PetscErrorCode          ierr;
2235baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2236baf369e7SPeter Brune 
2237baf369e7SPeter Brune   PetscFunctionBegin;
2238baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2239baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
224095dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2241baf369e7SPeter Brune   link->beginhook = beginhook;
2242baf369e7SPeter Brune   link->endhook   = endhook;
2243baf369e7SPeter Brune   link->ctx       = ctx;
22440298fd71SBarry Smith   link->next      = NULL;
2245baf369e7SPeter Brune   *p              = link;
2246baf369e7SPeter Brune   PetscFunctionReturn(0);
2247baf369e7SPeter Brune }
2248baf369e7SPeter Brune 
22494c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
22504c274da1SToby Isaac {
22514c274da1SToby Isaac   Mat cMat;
22524c274da1SToby Isaac   Vec cVec;
22534c274da1SToby Isaac   PetscSection section, cSec;
22544c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
22554c274da1SToby Isaac   PetscErrorCode ierr;
22564c274da1SToby Isaac 
22574c274da1SToby Isaac   PetscFunctionBegin;
22584c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
22594c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
22604c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
22615db9a05bSToby Isaac     PetscInt nRows;
22625db9a05bSToby Isaac 
22635db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
22645db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
226592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
22667711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
22674c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
22684c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
22694c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
22704c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
22714c274da1SToby Isaac       if (dof) {
22724c274da1SToby Isaac         PetscScalar *vals;
22734c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
22744c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
22754c274da1SToby Isaac       }
22764c274da1SToby Isaac     }
22774c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
22784c274da1SToby Isaac   }
22794c274da1SToby Isaac   PetscFunctionReturn(0);
22804c274da1SToby Isaac }
22814c274da1SToby Isaac 
228247c6ae99SBarry Smith /*@
228301729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
228401729b5cSPatrick Sanan 
2285d083f849SBarry Smith     Neighbor-wise Collective on dm
228601729b5cSPatrick Sanan 
228701729b5cSPatrick Sanan     Input Parameters:
228801729b5cSPatrick Sanan +   dm - the DM object
228901729b5cSPatrick Sanan .   g - the global vector
229001729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
229101729b5cSPatrick Sanan -   l - the local vector
229201729b5cSPatrick Sanan 
229301729b5cSPatrick Sanan     Notes:
229401729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
229501729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
229601729b5cSPatrick Sanan 
229701729b5cSPatrick Sanan     Level: beginner
229801729b5cSPatrick Sanan 
229901729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
230001729b5cSPatrick Sanan 
230101729b5cSPatrick Sanan @*/
230201729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
230301729b5cSPatrick Sanan {
230401729b5cSPatrick Sanan   PetscErrorCode ierr;
230501729b5cSPatrick Sanan 
230601729b5cSPatrick Sanan   PetscFunctionBegin;
230701729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
230801729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
230901729b5cSPatrick Sanan   PetscFunctionReturn(0);
231001729b5cSPatrick Sanan }
231101729b5cSPatrick Sanan 
231201729b5cSPatrick Sanan /*@
231347c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
231447c6ae99SBarry Smith 
2315d083f849SBarry Smith     Neighbor-wise Collective on dm
231647c6ae99SBarry Smith 
231747c6ae99SBarry Smith     Input Parameters:
231847c6ae99SBarry Smith +   dm - the DM object
231947c6ae99SBarry Smith .   g - the global vector
232047c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
232147c6ae99SBarry Smith -   l - the local vector
232247c6ae99SBarry Smith 
232301729b5cSPatrick Sanan     Level: intermediate
232447c6ae99SBarry Smith 
232501729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
232647c6ae99SBarry Smith 
232747c6ae99SBarry Smith @*/
23287087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
232947c6ae99SBarry Smith {
23307128ae9fSMatthew G Knepley   PetscSF                 sf;
233147c6ae99SBarry Smith   PetscErrorCode          ierr;
2332baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
233347c6ae99SBarry Smith 
233447c6ae99SBarry Smith   PetscFunctionBegin;
2335171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2336baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
23378865f1eaSKarl Rupp     if (link->beginhook) {
23388865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
23398865f1eaSKarl Rupp     }
2340baf369e7SPeter Brune   }
23411bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
23427128ae9fSMatthew G Knepley   if (sf) {
2343ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2344ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
23457128ae9fSMatthew G Knepley 
234682f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23477128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2348ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23497128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23507128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2351ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
23527128ae9fSMatthew G Knepley   } else {
235333907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2354843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
23557128ae9fSMatthew G Knepley   }
235647c6ae99SBarry Smith   PetscFunctionReturn(0);
235747c6ae99SBarry Smith }
235847c6ae99SBarry Smith 
235947c6ae99SBarry Smith /*@
236047c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
236147c6ae99SBarry Smith 
2362d083f849SBarry Smith     Neighbor-wise Collective on dm
236347c6ae99SBarry Smith 
236447c6ae99SBarry Smith     Input Parameters:
236547c6ae99SBarry Smith +   dm - the DM object
236647c6ae99SBarry Smith .   g - the global vector
236747c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
236847c6ae99SBarry Smith -   l - the local vector
236947c6ae99SBarry Smith 
237001729b5cSPatrick Sanan     Level: intermediate
237147c6ae99SBarry Smith 
237201729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
237347c6ae99SBarry Smith 
237447c6ae99SBarry Smith @*/
23757087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
237647c6ae99SBarry Smith {
23777128ae9fSMatthew G Knepley   PetscSF                 sf;
237847c6ae99SBarry Smith   PetscErrorCode          ierr;
2379ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2380ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2381ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2382baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
238347c6ae99SBarry Smith 
238447c6ae99SBarry Smith   PetscFunctionBegin;
2385171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
23861bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2387ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
23887128ae9fSMatthew G Knepley   if (sf) {
238982f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
23907128ae9fSMatthew G Knepley 
23917128ae9fSMatthew G Knepley     ierr = VecGetArray(l, &lArray);CHKERRQ(ierr);
2392ae5cfb4aSMatthew G. Knepley     ierr = VecGetArrayRead(g, &gArray);CHKERRQ(ierr);
23937128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
23947128ae9fSMatthew G Knepley     ierr = VecRestoreArray(l, &lArray);CHKERRQ(ierr);
2395ae5cfb4aSMatthew G. Knepley     ierr = VecRestoreArrayRead(g, &gArray);CHKERRQ(ierr);
2396ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
23977128ae9fSMatthew G Knepley   } else {
239833907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2399843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
24007128ae9fSMatthew G Knepley   }
24014c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2402baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2403baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2404baf369e7SPeter Brune   }
240547c6ae99SBarry Smith   PetscFunctionReturn(0);
240647c6ae99SBarry Smith }
240747c6ae99SBarry Smith 
2408d4d07f1eSToby Isaac /*@C
2409d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2410d4d07f1eSToby Isaac 
2411d4d07f1eSToby Isaac    Logically Collective
2412d4d07f1eSToby Isaac 
2413d4d07f1eSToby Isaac    Input Arguments:
2414d4d07f1eSToby Isaac +  dm - the DM
2415d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2416d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2417d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2418d4d07f1eSToby Isaac 
2419d4d07f1eSToby Isaac    Calling sequence for beginhook:
2420d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2421d4d07f1eSToby Isaac 
2422d4d07f1eSToby Isaac +  dm - global DM
2423d4d07f1eSToby Isaac .  l - local vector
2424d4d07f1eSToby Isaac .  mode - mode
2425d4d07f1eSToby Isaac .  g - global vector
2426d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2427d4d07f1eSToby Isaac 
2428d4d07f1eSToby Isaac 
2429d4d07f1eSToby Isaac    Calling sequence for endhook:
2430d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2431d4d07f1eSToby Isaac 
2432d4d07f1eSToby Isaac +  global - global DM
2433d4d07f1eSToby Isaac .  l - local vector
2434d4d07f1eSToby Isaac .  mode - mode
2435d4d07f1eSToby Isaac .  g - global vector
2436d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2437d4d07f1eSToby Isaac 
2438d4d07f1eSToby Isaac    Level: advanced
2439d4d07f1eSToby Isaac 
2440d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2441d4d07f1eSToby Isaac @*/
2442d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2443d4d07f1eSToby Isaac {
2444d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2445d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2446d4d07f1eSToby Isaac 
2447d4d07f1eSToby Isaac   PetscFunctionBegin;
2448d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2449d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
245095dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2451d4d07f1eSToby Isaac   link->beginhook = beginhook;
2452d4d07f1eSToby Isaac   link->endhook   = endhook;
2453d4d07f1eSToby Isaac   link->ctx       = ctx;
2454d4d07f1eSToby Isaac   link->next      = NULL;
2455d4d07f1eSToby Isaac   *p              = link;
2456d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2457d4d07f1eSToby Isaac }
2458d4d07f1eSToby Isaac 
24594c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
24604c274da1SToby Isaac {
24614c274da1SToby Isaac   Mat cMat;
24624c274da1SToby Isaac   Vec cVec;
24634c274da1SToby Isaac   PetscSection section, cSec;
24644c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24654c274da1SToby Isaac   PetscErrorCode ierr;
24664c274da1SToby Isaac 
24674c274da1SToby Isaac   PetscFunctionBegin;
24684c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24694c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24704c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
24715db9a05bSToby Isaac     PetscInt nRows;
24725db9a05bSToby Isaac 
24735db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24745db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
247592fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24767711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24774c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24784c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24794c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24804c274da1SToby Isaac       if (dof) {
24814c274da1SToby Isaac         PetscInt d;
24824c274da1SToby Isaac         PetscScalar *vals;
24834c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
24844c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
24854c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
24864c274da1SToby Isaac          * we just extracted */
24874c274da1SToby Isaac         for (d = 0; d < dof; d++) {
24884c274da1SToby Isaac           vals[d] = 0.;
24894c274da1SToby Isaac         }
24904c274da1SToby Isaac       }
24914c274da1SToby Isaac     }
24924c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
24934c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24944c274da1SToby Isaac   }
24954c274da1SToby Isaac   PetscFunctionReturn(0);
24964c274da1SToby Isaac }
249701729b5cSPatrick Sanan /*@
249801729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
249901729b5cSPatrick Sanan 
2500d083f849SBarry Smith     Neighbor-wise Collective on dm
250101729b5cSPatrick Sanan 
250201729b5cSPatrick Sanan     Input Parameters:
250301729b5cSPatrick Sanan +   dm - the DM object
250401729b5cSPatrick Sanan .   l - the local vector
250501729b5cSPatrick 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.
250601729b5cSPatrick Sanan -   g - the global vector
250701729b5cSPatrick Sanan 
250801729b5cSPatrick Sanan     Notes:
250901729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
251001729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
251101729b5cSPatrick Sanan 
251201729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
251301729b5cSPatrick 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.
251401729b5cSPatrick Sanan 
251501729b5cSPatrick Sanan     Level: beginner
251601729b5cSPatrick Sanan 
251701729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
251801729b5cSPatrick Sanan 
251901729b5cSPatrick Sanan @*/
252001729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
252101729b5cSPatrick Sanan {
252201729b5cSPatrick Sanan   PetscErrorCode ierr;
252301729b5cSPatrick Sanan 
252401729b5cSPatrick Sanan   PetscFunctionBegin;
252501729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
252601729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
252701729b5cSPatrick Sanan   PetscFunctionReturn(0);
252801729b5cSPatrick Sanan }
25294c274da1SToby Isaac 
253047c6ae99SBarry Smith /*@
253101729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
25329a42bb27SBarry Smith 
2533d083f849SBarry Smith     Neighbor-wise Collective on dm
25349a42bb27SBarry Smith 
25359a42bb27SBarry Smith     Input Parameters:
25369a42bb27SBarry Smith +   dm - the DM object
2537f6813fd5SJed Brown .   l - the local vector
25381eb28f2eSBarry 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.
25391eb28f2eSBarry Smith -   g - the global vector
25409a42bb27SBarry Smith 
254195452b02SPatrick Sanan     Notes:
254295452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
254384330215SMatthew 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.
25449a42bb27SBarry Smith 
254501729b5cSPatrick Sanan     Level: intermediate
25469a42bb27SBarry Smith 
254701729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
25489a42bb27SBarry Smith 
25499a42bb27SBarry Smith @*/
25507087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
25519a42bb27SBarry Smith {
25527128ae9fSMatthew G Knepley   PetscSF                 sf;
255384330215SMatthew G. Knepley   PetscSection            s, gs;
2554d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2555ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2556ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2557ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2558ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
255984330215SMatthew G. Knepley   PetscErrorCode          ierr;
25609a42bb27SBarry Smith 
25619a42bb27SBarry Smith   PetscFunctionBegin;
2562171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2563d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2564d4d07f1eSToby Isaac     if (link->beginhook) {
2565d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2566d4d07f1eSToby Isaac     }
2567d4d07f1eSToby Isaac   }
25684c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
25691bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
257092fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
25717128ae9fSMatthew G Knepley   switch (mode) {
25727128ae9fSMatthew G Knepley   case INSERT_VALUES:
25737128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2574304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
257584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
25767128ae9fSMatthew G Knepley   case ADD_VALUES:
25777128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2578304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
257984330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
25807128ae9fSMatthew G Knepley   default:
258182f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25827128ae9fSMatthew G Knepley   }
2583ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2584ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2585ca3d3a14SMatthew G. Knepley     if (transform) {
2586ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2587ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2588ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2589ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2590ca3d3a14SMatthew G. Knepley     } else {
2591ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2592ca3d3a14SMatthew G. Knepley     }
25937128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2594ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2595a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
259684330215SMatthew G. Knepley     } else if (s && isInsert) {
259784330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
259884330215SMatthew G. Knepley 
2599e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
260084330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
260184330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
260284330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2603b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
260484330215SMatthew G. Knepley 
260584330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
260603442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
260784330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2608b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
260984330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
261084330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2611b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
261203442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2613b3b16f48SMatthew 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);
2614b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2615b3b16f48SMatthew G. Knepley         if (!gcdof) {
261684330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2617b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2618b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
261984330215SMatthew G. Knepley           const PetscInt *cdofs;
262084330215SMatthew G. Knepley           PetscInt        cind = 0;
262184330215SMatthew G. Knepley 
262284330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2623b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
262484330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2625b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
262684330215SMatthew G. Knepley           }
2627b3b16f48SMatthew 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);
262884330215SMatthew G. Knepley       }
2629ca3d3a14SMatthew G. Knepley     }
26307128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2631ca3d3a14SMatthew G. Knepley     if (transform) {
2632ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2633ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2634ca3d3a14SMatthew G. Knepley     } else {
2635ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2636ca3d3a14SMatthew G. Knepley     }
26377128ae9fSMatthew G Knepley   } else {
2638b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2639843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
26407128ae9fSMatthew G Knepley   }
26419a42bb27SBarry Smith   PetscFunctionReturn(0);
26429a42bb27SBarry Smith }
26439a42bb27SBarry Smith 
26449a42bb27SBarry Smith /*@
26459a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
264647c6ae99SBarry Smith 
2647d083f849SBarry Smith     Neighbor-wise Collective on dm
264847c6ae99SBarry Smith 
264947c6ae99SBarry Smith     Input Parameters:
265047c6ae99SBarry Smith +   dm - the DM object
2651f6813fd5SJed Brown .   l - the local vector
265247c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2653f6813fd5SJed Brown -   g - the global vector
265447c6ae99SBarry Smith 
265501729b5cSPatrick Sanan     Level: intermediate
265647c6ae99SBarry Smith 
2657e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
265847c6ae99SBarry Smith 
265947c6ae99SBarry Smith @*/
26607087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
266147c6ae99SBarry Smith {
26627128ae9fSMatthew G Knepley   PetscSF                 sf;
266384330215SMatthew G. Knepley   PetscSection            s;
2664d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2665ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
266684330215SMatthew G. Knepley   PetscErrorCode          ierr;
266747c6ae99SBarry Smith 
266847c6ae99SBarry Smith   PetscFunctionBegin;
2669171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
26701bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
267192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
26727128ae9fSMatthew G Knepley   switch (mode) {
26737128ae9fSMatthew G Knepley   case INSERT_VALUES:
26747128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
267584330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
26767128ae9fSMatthew G Knepley   case ADD_VALUES:
26777128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
267884330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
26797128ae9fSMatthew G Knepley   default:
268082f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
26817128ae9fSMatthew G Knepley   }
268284330215SMatthew G. Knepley   if (sf && !isInsert) {
2683ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2684ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2685ca3d3a14SMatthew G. Knepley     Vec                tmpl;
268684330215SMatthew G. Knepley 
2687ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2688ca3d3a14SMatthew G. Knepley     if (transform) {
2689ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2690ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2691ca3d3a14SMatthew G. Knepley     } else {
2692ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2693ca3d3a14SMatthew G. Knepley     }
26947128ae9fSMatthew G Knepley     ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2695a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2696ca3d3a14SMatthew G. Knepley     if (transform) {
2697ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2698ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2699ca3d3a14SMatthew G. Knepley     } else {
2700ae5cfb4aSMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2701ca3d3a14SMatthew G. Knepley     }
27027128ae9fSMatthew G Knepley     ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
270384330215SMatthew G. Knepley   } else if (s && isInsert) {
27047128ae9fSMatthew G Knepley   } else {
2705b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2706843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
27077128ae9fSMatthew G Knepley   }
2708d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2709d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2710d4d07f1eSToby Isaac   }
271147c6ae99SBarry Smith   PetscFunctionReturn(0);
271247c6ae99SBarry Smith }
271347c6ae99SBarry Smith 
2714f089877aSRichard Tran Mills /*@
2715bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2716bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2717d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2718f089877aSRichard Tran Mills 
2719d083f849SBarry Smith    Neighbor-wise Collective on dm
2720f089877aSRichard Tran Mills 
2721f089877aSRichard Tran Mills    Input Parameters:
2722f089877aSRichard Tran Mills +  dm - the DM object
2723bc0a1609SRichard Tran Mills .  g - the original local vector
2724bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2725f089877aSRichard Tran Mills 
2726bc0a1609SRichard Tran Mills    Output Parameter:
2727bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2728f089877aSRichard Tran Mills 
2729f089877aSRichard Tran Mills    Level: intermediate
2730f089877aSRichard Tran Mills 
2731bc0a1609SRichard Tran Mills    Notes:
2732bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2733bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2734bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2735bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2736bc0a1609SRichard Tran Mills 
2737bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2738f089877aSRichard Tran Mills 
2739f089877aSRichard Tran Mills @*/
2740f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2741f089877aSRichard Tran Mills {
2742f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2743f089877aSRichard Tran Mills 
2744f089877aSRichard Tran Mills   PetscFunctionBegin;
2745f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2746bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2747f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2748f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2749f089877aSRichard Tran Mills }
2750f089877aSRichard Tran Mills 
2751f089877aSRichard Tran Mills /*@
2752bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2753bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2754d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2755f089877aSRichard Tran Mills 
2756d083f849SBarry Smith    Neighbor-wise Collective on dm
2757f089877aSRichard Tran Mills 
2758f089877aSRichard Tran Mills    Input Parameters:
2759bc0a1609SRichard Tran Mills +  da - the DM object
2760bc0a1609SRichard Tran Mills .  g - the original local vector
2761bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2762f089877aSRichard Tran Mills 
2763bc0a1609SRichard Tran Mills    Output Parameter:
2764bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2765f089877aSRichard Tran Mills 
2766f089877aSRichard Tran Mills    Level: intermediate
2767f089877aSRichard Tran Mills 
2768bc0a1609SRichard Tran Mills    Notes:
2769bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2770bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2771bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2772bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2773bc0a1609SRichard Tran Mills 
2774bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2775f089877aSRichard Tran Mills 
2776f089877aSRichard Tran Mills @*/
2777f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2778f089877aSRichard Tran Mills {
2779f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2780f089877aSRichard Tran Mills 
2781f089877aSRichard Tran Mills   PetscFunctionBegin;
2782f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2783bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2784f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2785f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2786f089877aSRichard Tran Mills }
2787f089877aSRichard Tran Mills 
2788f089877aSRichard Tran Mills 
278947c6ae99SBarry Smith /*@
279047c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
279147c6ae99SBarry Smith 
2792d083f849SBarry Smith     Collective on dm
279347c6ae99SBarry Smith 
279447c6ae99SBarry Smith     Input Parameter:
279547c6ae99SBarry Smith +   dm - the DM object
279691d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
279747c6ae99SBarry Smith 
279847c6ae99SBarry Smith     Output Parameter:
279947c6ae99SBarry Smith .   dmc - the coarsened DM
280047c6ae99SBarry Smith 
280147c6ae99SBarry Smith     Level: developer
280247c6ae99SBarry Smith 
2803e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
280447c6ae99SBarry Smith 
280547c6ae99SBarry Smith @*/
28067087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
280747c6ae99SBarry Smith {
280847c6ae99SBarry Smith   PetscErrorCode    ierr;
2809b17ce1afSJed Brown   DMCoarsenHookLink link;
281047c6ae99SBarry Smith 
281147c6ae99SBarry Smith   PetscFunctionBegin;
2812171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2813b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
281447a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
281547c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2816b9d85ea2SLisandro Dalcin   if (*dmc) {
2817a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
281843842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
28198cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2820644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
28210598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2822656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2823e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2824b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2825b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2826b17ce1afSJed Brown     }
2827b9d85ea2SLisandro Dalcin   }
282847a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
2829b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
2830b17ce1afSJed Brown   PetscFunctionReturn(0);
2831b17ce1afSJed Brown }
2832b17ce1afSJed Brown 
2833bb9467b5SJed Brown /*@C
2834b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
2835b17ce1afSJed Brown 
2836b17ce1afSJed Brown    Logically Collective
2837b17ce1afSJed Brown 
2838b17ce1afSJed Brown    Input Arguments:
2839b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2840b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
2841b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
28420298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2843b17ce1afSJed Brown 
2844b17ce1afSJed Brown    Calling sequence of coarsenhook:
2845b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
2846b17ce1afSJed Brown 
2847b17ce1afSJed Brown +  fine - fine level DM
2848b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
2849b17ce1afSJed Brown -  ctx - optional user-defined function context
2850b17ce1afSJed Brown 
2851b17ce1afSJed Brown    Calling sequence for restricthook:
2852c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
2853b17ce1afSJed Brown 
2854b17ce1afSJed Brown +  fine - fine level DM
2855b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
2856c833c3b5SJed Brown .  rscale - scaling vector for restriction
2857c833c3b5SJed Brown .  inject - matrix restricting by injection
2858b17ce1afSJed Brown .  coarse - coarse level DM to update
2859b17ce1afSJed Brown -  ctx - optional user-defined function context
2860b17ce1afSJed Brown 
2861b17ce1afSJed Brown    Level: advanced
2862b17ce1afSJed Brown 
2863b17ce1afSJed Brown    Notes:
2864b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
2865b17ce1afSJed Brown 
2866b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2867b17ce1afSJed Brown 
2868b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2869b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
2870b17ce1afSJed Brown 
2871bb9467b5SJed Brown    This function is currently not available from Fortran.
2872bb9467b5SJed Brown 
2873dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2874b17ce1afSJed Brown @*/
2875b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2876b17ce1afSJed Brown {
2877b17ce1afSJed Brown   PetscErrorCode    ierr;
2878b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
2879b17ce1afSJed Brown 
2880b17ce1afSJed Brown   PetscFunctionBegin;
2881b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
28821e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
28831e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
28841e3d8eccSJed Brown   }
288595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
2886b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
2887b17ce1afSJed Brown   link->restricthook = restricthook;
2888b17ce1afSJed Brown   link->ctx          = ctx;
28890298fd71SBarry Smith   link->next         = NULL;
2890b17ce1afSJed Brown   *p                 = link;
2891b17ce1afSJed Brown   PetscFunctionReturn(0);
2892b17ce1afSJed Brown }
2893b17ce1afSJed Brown 
2894dc822a44SJed Brown /*@C
2895dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
2896dc822a44SJed Brown 
2897dc822a44SJed Brown    Logically Collective
2898dc822a44SJed Brown 
2899dc822a44SJed Brown    Input Arguments:
2900dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
2901dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
2902dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
2903dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2904dc822a44SJed Brown 
2905dc822a44SJed Brown    Level: advanced
2906dc822a44SJed Brown 
2907dc822a44SJed Brown    Notes:
2908dc822a44SJed Brown    This function does nothing if the hook is not in the list.
2909dc822a44SJed Brown 
2910dc822a44SJed Brown    This function is currently not available from Fortran.
2911dc822a44SJed Brown 
2912dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2913dc822a44SJed Brown @*/
2914dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
2915dc822a44SJed Brown {
2916dc822a44SJed Brown   PetscErrorCode    ierr;
2917dc822a44SJed Brown   DMCoarsenHookLink link,*p;
2918dc822a44SJed Brown 
2919dc822a44SJed Brown   PetscFunctionBegin;
2920dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
2921dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
2922dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
2923dc822a44SJed Brown       link = *p;
2924dc822a44SJed Brown       *p = link->next;
2925dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
2926dc822a44SJed Brown       break;
2927dc822a44SJed Brown     }
2928dc822a44SJed Brown   }
2929dc822a44SJed Brown   PetscFunctionReturn(0);
2930dc822a44SJed Brown }
2931dc822a44SJed Brown 
2932dc822a44SJed Brown 
2933b17ce1afSJed Brown /*@
2934b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
2935b17ce1afSJed Brown 
2936b17ce1afSJed Brown    Collective if any hooks are
2937b17ce1afSJed Brown 
2938b17ce1afSJed Brown    Input Arguments:
2939b17ce1afSJed Brown +  fine - finer DM to use as a base
2940b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
2941e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
2942b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
2943e91eccc2SStefano Zampini -  coarse - coarser DM to update
2944b17ce1afSJed Brown 
2945b17ce1afSJed Brown    Level: developer
2946b17ce1afSJed Brown 
2947b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
2948b17ce1afSJed Brown @*/
2949b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
2950b17ce1afSJed Brown {
2951b17ce1afSJed Brown   PetscErrorCode    ierr;
2952b17ce1afSJed Brown   DMCoarsenHookLink link;
2953b17ce1afSJed Brown 
2954b17ce1afSJed Brown   PetscFunctionBegin;
2955b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
29568865f1eaSKarl Rupp     if (link->restricthook) {
29578865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
29588865f1eaSKarl Rupp     }
2959b17ce1afSJed Brown   }
296047c6ae99SBarry Smith   PetscFunctionReturn(0);
296147c6ae99SBarry Smith }
296247c6ae99SBarry Smith 
2963bb9467b5SJed Brown /*@C
2964be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
29655dbd56e3SPeter Brune 
2966d083f849SBarry Smith    Logically Collective on global
29675dbd56e3SPeter Brune 
29685dbd56e3SPeter Brune    Input Arguments:
29695dbd56e3SPeter Brune +  global - global DM
2970ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
29715dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
29720298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
29735dbd56e3SPeter Brune 
2974ec4806b8SPeter Brune 
2975ec4806b8SPeter Brune    Calling sequence for ddhook:
2976ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
2977ec4806b8SPeter Brune 
2978ec4806b8SPeter Brune +  global - global DM
2979ec4806b8SPeter Brune .  block  - block DM
2980ec4806b8SPeter Brune -  ctx - optional user-defined function context
2981ec4806b8SPeter Brune 
29825dbd56e3SPeter Brune    Calling sequence for restricthook:
2983ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
29845dbd56e3SPeter Brune 
29855dbd56e3SPeter Brune +  global - global DM
29865dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
29875dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
2988ec4806b8SPeter Brune .  block  - block DM
29895dbd56e3SPeter Brune -  ctx - optional user-defined function context
29905dbd56e3SPeter Brune 
29915dbd56e3SPeter Brune    Level: advanced
29925dbd56e3SPeter Brune 
29935dbd56e3SPeter Brune    Notes:
2994ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
29955dbd56e3SPeter Brune 
29965dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
29975dbd56e3SPeter Brune 
29985dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
2999ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
30005dbd56e3SPeter Brune 
3001bb9467b5SJed Brown    This function is currently not available from Fortran.
3002bb9467b5SJed Brown 
30035dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
30045dbd56e3SPeter Brune @*/
3005be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
30065dbd56e3SPeter Brune {
30075dbd56e3SPeter Brune   PetscErrorCode      ierr;
3008be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
30095dbd56e3SPeter Brune 
30105dbd56e3SPeter Brune   PetscFunctionBegin;
30115dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3012b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3013b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3014b3a6b972SJed Brown   }
301595dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
30165dbd56e3SPeter Brune   link->restricthook = restricthook;
3017be081cd6SPeter Brune   link->ddhook       = ddhook;
30185dbd56e3SPeter Brune   link->ctx          = ctx;
30190298fd71SBarry Smith   link->next         = NULL;
30205dbd56e3SPeter Brune   *p                 = link;
30215dbd56e3SPeter Brune   PetscFunctionReturn(0);
30225dbd56e3SPeter Brune }
30235dbd56e3SPeter Brune 
3024b3a6b972SJed Brown /*@C
3025b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3026b3a6b972SJed Brown 
3027b3a6b972SJed Brown    Logically Collective
3028b3a6b972SJed Brown 
3029b3a6b972SJed Brown    Input Arguments:
3030b3a6b972SJed Brown +  global - global DM
3031b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3032b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3033b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3034b3a6b972SJed Brown 
3035b3a6b972SJed Brown    Level: advanced
3036b3a6b972SJed Brown 
3037b3a6b972SJed Brown    Notes:
3038b3a6b972SJed Brown 
3039b3a6b972SJed Brown    This function is currently not available from Fortran.
3040b3a6b972SJed Brown 
3041b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3042b3a6b972SJed Brown @*/
3043b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3044b3a6b972SJed Brown {
3045b3a6b972SJed Brown   PetscErrorCode      ierr;
3046b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3047b3a6b972SJed Brown 
3048b3a6b972SJed Brown   PetscFunctionBegin;
3049b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3050b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3051b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3052b3a6b972SJed Brown       link = *p;
3053b3a6b972SJed Brown       *p = link->next;
3054b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3055b3a6b972SJed Brown       break;
3056b3a6b972SJed Brown     }
3057b3a6b972SJed Brown   }
3058b3a6b972SJed Brown   PetscFunctionReturn(0);
3059b3a6b972SJed Brown }
3060b3a6b972SJed Brown 
30615dbd56e3SPeter Brune /*@
3062be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
30635dbd56e3SPeter Brune 
30645dbd56e3SPeter Brune    Collective if any hooks are
30655dbd56e3SPeter Brune 
30665dbd56e3SPeter Brune    Input Arguments:
30675dbd56e3SPeter Brune +  fine - finer DM to use as a base
3068be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3069be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
30705dbd56e3SPeter Brune -  coarse - coarer DM to update
30715dbd56e3SPeter Brune 
30725dbd56e3SPeter Brune    Level: developer
30735dbd56e3SPeter Brune 
30745dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
30755dbd56e3SPeter Brune @*/
3076be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
30775dbd56e3SPeter Brune {
30785dbd56e3SPeter Brune   PetscErrorCode      ierr;
3079be081cd6SPeter Brune   DMSubDomainHookLink link;
30805dbd56e3SPeter Brune 
30815dbd56e3SPeter Brune   PetscFunctionBegin;
3082be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
30838865f1eaSKarl Rupp     if (link->restricthook) {
30848865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
30858865f1eaSKarl Rupp     }
30865dbd56e3SPeter Brune   }
30875dbd56e3SPeter Brune   PetscFunctionReturn(0);
30885dbd56e3SPeter Brune }
30895dbd56e3SPeter Brune 
30905fe1f584SPeter Brune /*@
30916a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
30925fe1f584SPeter Brune 
30935fe1f584SPeter Brune     Not Collective
30945fe1f584SPeter Brune 
30955fe1f584SPeter Brune     Input Parameter:
30965fe1f584SPeter Brune .   dm - the DM object
30975fe1f584SPeter Brune 
30985fe1f584SPeter Brune     Output Parameter:
30996a7d9d85SPeter Brune .   level - number of coarsenings
31005fe1f584SPeter Brune 
31015fe1f584SPeter Brune     Level: developer
31025fe1f584SPeter Brune 
31036a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31045fe1f584SPeter Brune 
31055fe1f584SPeter Brune @*/
31065fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
31075fe1f584SPeter Brune {
31085fe1f584SPeter Brune   PetscFunctionBegin;
31095fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3110b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
31115fe1f584SPeter Brune   *level = dm->leveldown;
31125fe1f584SPeter Brune   PetscFunctionReturn(0);
31135fe1f584SPeter Brune }
31145fe1f584SPeter Brune 
31159a64c4a8SMatthew G. Knepley /*@
31169a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
31179a64c4a8SMatthew G. Knepley 
31189a64c4a8SMatthew G. Knepley     Not Collective
31199a64c4a8SMatthew G. Knepley 
31209a64c4a8SMatthew G. Knepley     Input Parameters:
31219a64c4a8SMatthew G. Knepley +   dm - the DM object
31229a64c4a8SMatthew G. Knepley -   level - number of coarsenings
31239a64c4a8SMatthew G. Knepley 
31249a64c4a8SMatthew G. Knepley     Level: developer
31259a64c4a8SMatthew G. Knepley 
31269a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
31279a64c4a8SMatthew G. Knepley @*/
31289a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
31299a64c4a8SMatthew G. Knepley {
31309a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
31319a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
31329a64c4a8SMatthew G. Knepley   dm->leveldown = level;
31339a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
31349a64c4a8SMatthew G. Knepley }
31359a64c4a8SMatthew G. Knepley 
31365fe1f584SPeter Brune 
31375fe1f584SPeter Brune 
313847c6ae99SBarry Smith /*@C
313947c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
314047c6ae99SBarry Smith 
3141d083f849SBarry Smith     Collective on dm
314247c6ae99SBarry Smith 
314347c6ae99SBarry Smith     Input Parameter:
314447c6ae99SBarry Smith +   dm - the DM object
314547c6ae99SBarry Smith -   nlevels - the number of levels of refinement
314647c6ae99SBarry Smith 
314747c6ae99SBarry Smith     Output Parameter:
314847c6ae99SBarry Smith .   dmf - the refined DM hierarchy
314947c6ae99SBarry Smith 
315047c6ae99SBarry Smith     Level: developer
315147c6ae99SBarry Smith 
3152e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
315347c6ae99SBarry Smith 
315447c6ae99SBarry Smith @*/
31557087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
315647c6ae99SBarry Smith {
315747c6ae99SBarry Smith   PetscErrorCode ierr;
315847c6ae99SBarry Smith 
315947c6ae99SBarry Smith   PetscFunctionBegin;
3160171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3161ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
316247c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3163b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
316447c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
316547c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
316647c6ae99SBarry Smith   } else if (dm->ops->refine) {
316747c6ae99SBarry Smith     PetscInt i;
316847c6ae99SBarry Smith 
3169ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
317047c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3171ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
317247c6ae99SBarry Smith     }
3173ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
317447c6ae99SBarry Smith   PetscFunctionReturn(0);
317547c6ae99SBarry Smith }
317647c6ae99SBarry Smith 
317747c6ae99SBarry Smith /*@C
317847c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
317947c6ae99SBarry Smith 
3180d083f849SBarry Smith     Collective on dm
318147c6ae99SBarry Smith 
318247c6ae99SBarry Smith     Input Parameter:
318347c6ae99SBarry Smith +   dm - the DM object
318447c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
318547c6ae99SBarry Smith 
318647c6ae99SBarry Smith     Output Parameter:
318747c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
318847c6ae99SBarry Smith 
318947c6ae99SBarry Smith     Level: developer
319047c6ae99SBarry Smith 
3191e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
319247c6ae99SBarry Smith 
319347c6ae99SBarry Smith @*/
31947087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
319547c6ae99SBarry Smith {
319647c6ae99SBarry Smith   PetscErrorCode ierr;
319747c6ae99SBarry Smith 
319847c6ae99SBarry Smith   PetscFunctionBegin;
3199171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3200ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
320147c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
320247c6ae99SBarry Smith   PetscValidPointer(dmc,3);
320347c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
320447c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
320547c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
320647c6ae99SBarry Smith     PetscInt i;
320747c6ae99SBarry Smith 
3208ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
320947c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3210ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
321147c6ae99SBarry Smith     }
3212ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
321347c6ae99SBarry Smith   PetscFunctionReturn(0);
321447c6ae99SBarry Smith }
321547c6ae99SBarry Smith 
32161a266240SBarry Smith /*@C
32171a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
32181a266240SBarry Smith 
32191a266240SBarry Smith     Not Collective
32201a266240SBarry Smith 
32211a266240SBarry Smith     Input Parameters:
32221a266240SBarry Smith +   dm - the DM object
32231a266240SBarry Smith -   destroy - the destroy function
32241a266240SBarry Smith 
32251a266240SBarry Smith     Level: intermediate
32261a266240SBarry Smith 
3227e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
32281a266240SBarry Smith 
3229f07f9ceaSJed Brown @*/
32301a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
32311a266240SBarry Smith {
32321a266240SBarry Smith   PetscFunctionBegin;
3233171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32341a266240SBarry Smith   dm->ctxdestroy = destroy;
32351a266240SBarry Smith   PetscFunctionReturn(0);
32361a266240SBarry Smith }
32371a266240SBarry Smith 
3238b07ff414SBarry Smith /*@
32391b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
324047c6ae99SBarry Smith 
324147c6ae99SBarry Smith     Not Collective
324247c6ae99SBarry Smith 
324347c6ae99SBarry Smith     Input Parameters:
324447c6ae99SBarry Smith +   dm - the DM object
324547c6ae99SBarry Smith -   ctx - the user context
324647c6ae99SBarry Smith 
324747c6ae99SBarry Smith     Level: intermediate
324847c6ae99SBarry Smith 
3249e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
325047c6ae99SBarry Smith 
325147c6ae99SBarry Smith @*/
32521b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
325347c6ae99SBarry Smith {
325447c6ae99SBarry Smith   PetscFunctionBegin;
3255171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
325647c6ae99SBarry Smith   dm->ctx = ctx;
325747c6ae99SBarry Smith   PetscFunctionReturn(0);
325847c6ae99SBarry Smith }
325947c6ae99SBarry Smith 
326047c6ae99SBarry Smith /*@
32611b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
326247c6ae99SBarry Smith 
326347c6ae99SBarry Smith     Not Collective
326447c6ae99SBarry Smith 
326547c6ae99SBarry Smith     Input Parameter:
326647c6ae99SBarry Smith .   dm - the DM object
326747c6ae99SBarry Smith 
326847c6ae99SBarry Smith     Output Parameter:
326947c6ae99SBarry Smith .   ctx - the user context
327047c6ae99SBarry Smith 
327147c6ae99SBarry Smith     Level: intermediate
327247c6ae99SBarry Smith 
3273e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
327447c6ae99SBarry Smith 
327547c6ae99SBarry Smith @*/
32761b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
327747c6ae99SBarry Smith {
327847c6ae99SBarry Smith   PetscFunctionBegin;
3279171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
32801b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
328147c6ae99SBarry Smith   PetscFunctionReturn(0);
328247c6ae99SBarry Smith }
328347c6ae99SBarry Smith 
328408da532bSDmitry Karpeev /*@C
3285df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
328608da532bSDmitry Karpeev 
3287d083f849SBarry Smith     Logically Collective on dm
328808da532bSDmitry Karpeev 
328908da532bSDmitry Karpeev     Input Parameter:
329008da532bSDmitry Karpeev +   dm - the DM object
32910298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
329208da532bSDmitry Karpeev 
329308da532bSDmitry Karpeev     Level: intermediate
329408da532bSDmitry Karpeev 
3295835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
329608da532bSDmitry Karpeev          DMSetJacobian()
329708da532bSDmitry Karpeev 
329808da532bSDmitry Karpeev @*/
329908da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
330008da532bSDmitry Karpeev {
330108da532bSDmitry Karpeev   PetscFunctionBegin;
33025a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
330308da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
330408da532bSDmitry Karpeev   PetscFunctionReturn(0);
330508da532bSDmitry Karpeev }
330608da532bSDmitry Karpeev 
330708da532bSDmitry Karpeev /*@
330808da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
330908da532bSDmitry Karpeev 
331008da532bSDmitry Karpeev     Not Collective
331108da532bSDmitry Karpeev 
331208da532bSDmitry Karpeev     Input Parameter:
331308da532bSDmitry Karpeev .   dm - the DM object to destroy
331408da532bSDmitry Karpeev 
331508da532bSDmitry Karpeev     Output Parameter:
331608da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
331708da532bSDmitry Karpeev 
331808da532bSDmitry Karpeev     Level: developer
331908da532bSDmitry Karpeev 
332074e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
332108da532bSDmitry Karpeev 
332208da532bSDmitry Karpeev @*/
332308da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
332408da532bSDmitry Karpeev {
332508da532bSDmitry Karpeev   PetscFunctionBegin;
33265a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3327534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
332808da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
332908da532bSDmitry Karpeev   PetscFunctionReturn(0);
333008da532bSDmitry Karpeev }
333108da532bSDmitry Karpeev 
333208da532bSDmitry Karpeev /*@C
333308da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
333408da532bSDmitry Karpeev 
3335d083f849SBarry Smith     Logically Collective on dm
333608da532bSDmitry Karpeev 
333708da532bSDmitry Karpeev     Input Parameters:
3338907376e6SBarry Smith .   dm - the DM object
333908da532bSDmitry Karpeev 
334008da532bSDmitry Karpeev     Output parameters:
334108da532bSDmitry Karpeev +   xl - lower bound
334208da532bSDmitry Karpeev -   xu - upper bound
334308da532bSDmitry Karpeev 
3344907376e6SBarry Smith     Level: advanced
3345907376e6SBarry Smith 
334695452b02SPatrick Sanan     Notes:
334795452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
334808da532bSDmitry Karpeev 
334974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
335008da532bSDmitry Karpeev 
335108da532bSDmitry Karpeev @*/
335208da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
335308da532bSDmitry Karpeev {
335408da532bSDmitry Karpeev   PetscErrorCode ierr;
33555fd66863SKarl Rupp 
335608da532bSDmitry Karpeev   PetscFunctionBegin;
33575a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
335808da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
33595a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3360b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
336108da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
336208da532bSDmitry Karpeev   PetscFunctionReturn(0);
336308da532bSDmitry Karpeev }
336408da532bSDmitry Karpeev 
3365b0ae01b7SPeter Brune /*@
3366b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3367b0ae01b7SPeter Brune 
3368b0ae01b7SPeter Brune     Not Collective
3369b0ae01b7SPeter Brune 
3370b0ae01b7SPeter Brune     Input Parameter:
3371b0ae01b7SPeter Brune .   dm - the DM object
3372b0ae01b7SPeter Brune 
3373b0ae01b7SPeter Brune     Output Parameter:
3374b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3375b0ae01b7SPeter Brune 
3376b0ae01b7SPeter Brune     Level: developer
3377b0ae01b7SPeter Brune 
33781565f0a7SPatrick Sanan .seealso DMCreateColoring()
3379b0ae01b7SPeter Brune 
3380b0ae01b7SPeter Brune @*/
3381b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3382b0ae01b7SPeter Brune {
3383b0ae01b7SPeter Brune   PetscFunctionBegin;
33845a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3385534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3386b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3387b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3388b0ae01b7SPeter Brune }
3389b0ae01b7SPeter Brune 
33903ad4599aSBarry Smith /*@
33913ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
33923ad4599aSBarry Smith 
33933ad4599aSBarry Smith     Not Collective
33943ad4599aSBarry Smith 
33953ad4599aSBarry Smith     Input Parameter:
33963ad4599aSBarry Smith .   dm - the DM object
33973ad4599aSBarry Smith 
33983ad4599aSBarry Smith     Output Parameter:
33993ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
34003ad4599aSBarry Smith 
34013ad4599aSBarry Smith     Level: developer
34023ad4599aSBarry Smith 
34031565f0a7SPatrick Sanan .seealso DMCreateRestriction()
34043ad4599aSBarry Smith 
34053ad4599aSBarry Smith @*/
34063ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
34073ad4599aSBarry Smith {
34083ad4599aSBarry Smith   PetscFunctionBegin;
34095a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3410534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34113ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
34123ad4599aSBarry Smith   PetscFunctionReturn(0);
34133ad4599aSBarry Smith }
34143ad4599aSBarry Smith 
3415a7058e45SLawrence Mitchell 
3416a7058e45SLawrence Mitchell /*@
3417a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3418a7058e45SLawrence Mitchell 
3419a7058e45SLawrence Mitchell     Not Collective
3420a7058e45SLawrence Mitchell 
3421a7058e45SLawrence Mitchell     Input Parameter:
3422a7058e45SLawrence Mitchell .   dm - the DM object
3423a7058e45SLawrence Mitchell 
3424a7058e45SLawrence Mitchell     Output Parameter:
3425a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3426a7058e45SLawrence Mitchell 
3427a7058e45SLawrence Mitchell     Level: developer
3428a7058e45SLawrence Mitchell 
34291565f0a7SPatrick Sanan .seealso DMCreateInjection()
3430a7058e45SLawrence Mitchell 
3431a7058e45SLawrence Mitchell @*/
3432a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3433a7058e45SLawrence Mitchell {
34344a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
34355a84ad33SLisandro Dalcin 
3436a7058e45SLawrence Mitchell   PetscFunctionBegin;
34375a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3438534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
34395a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
34404a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
34415a84ad33SLisandro Dalcin   } else {
34425a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
34435a84ad33SLisandro Dalcin   }
3444a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3445a7058e45SLawrence Mitchell }
3446a7058e45SLawrence Mitchell 
34475a84ad33SLisandro Dalcin 
3448748fac09SDmitry Karpeev /*@C
344908da532bSDmitry Karpeev     DMSetVec - set the vector at which to compute residual, Jacobian and VI bounds, if the problem is nonlinear.
345008da532bSDmitry Karpeev 
3451d083f849SBarry Smith     Collective on dm
345208da532bSDmitry Karpeev 
345308da532bSDmitry Karpeev     Input Parameter:
345408da532bSDmitry Karpeev +   dm - the DM object
34550298fd71SBarry Smith -   x - location to compute residual and Jacobian, if NULL is passed to those routines; will be NULL for linear problems.
345608da532bSDmitry Karpeev 
345708da532bSDmitry Karpeev     Level: developer
345808da532bSDmitry Karpeev 
345974e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
346008da532bSDmitry Karpeev 
346108da532bSDmitry Karpeev @*/
346208da532bSDmitry Karpeev PetscErrorCode  DMSetVec(DM dm,Vec x)
346308da532bSDmitry Karpeev {
346408da532bSDmitry Karpeev   PetscErrorCode ierr;
34655fd66863SKarl Rupp 
346608da532bSDmitry Karpeev   PetscFunctionBegin;
3467b9d85ea2SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
346808da532bSDmitry Karpeev   if (x) {
346908da532bSDmitry Karpeev     if (!dm->x) {
347008da532bSDmitry Karpeev       ierr = DMCreateGlobalVector(dm,&dm->x);CHKERRQ(ierr);
347108da532bSDmitry Karpeev     }
347208da532bSDmitry Karpeev     ierr = VecCopy(x,dm->x);CHKERRQ(ierr);
34738865f1eaSKarl Rupp   } else if (dm->x) {
347408da532bSDmitry Karpeev     ierr = VecDestroy(&dm->x);CHKERRQ(ierr);
347508da532bSDmitry Karpeev   }
347608da532bSDmitry Karpeev   PetscFunctionReturn(0);
347708da532bSDmitry Karpeev }
347808da532bSDmitry Karpeev 
34790298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3480264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3481264ace61SBarry Smith 
3482264ace61SBarry Smith /*@C
3483264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3484264ace61SBarry Smith 
3485d083f849SBarry Smith   Collective on dm
3486264ace61SBarry Smith 
3487264ace61SBarry Smith   Input Parameters:
3488264ace61SBarry Smith + dm     - The DM object
3489264ace61SBarry Smith - method - The name of the DM type
3490264ace61SBarry Smith 
3491264ace61SBarry Smith   Options Database Key:
3492264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3493264ace61SBarry Smith 
3494264ace61SBarry Smith   Notes:
3495e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3496264ace61SBarry Smith 
3497264ace61SBarry Smith   Level: intermediate
3498264ace61SBarry Smith 
3499264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3500264ace61SBarry Smith @*/
350119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3502264ace61SBarry Smith {
3503264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3504264ace61SBarry Smith   PetscBool      match;
3505264ace61SBarry Smith   PetscErrorCode ierr;
3506264ace61SBarry Smith 
3507264ace61SBarry Smith   PetscFunctionBegin;
3508264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3509251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3510264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3511264ace61SBarry Smith 
35120f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
35131c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3514ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3515264ace61SBarry Smith 
3516264ace61SBarry Smith   if (dm->ops->destroy) {
3517264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3518264ace61SBarry Smith   }
3519d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3520264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3521d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3522264ace61SBarry Smith   PetscFunctionReturn(0);
3523264ace61SBarry Smith }
3524264ace61SBarry Smith 
3525264ace61SBarry Smith /*@C
3526264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3527264ace61SBarry Smith 
3528264ace61SBarry Smith   Not Collective
3529264ace61SBarry Smith 
3530264ace61SBarry Smith   Input Parameter:
3531264ace61SBarry Smith . dm  - The DM
3532264ace61SBarry Smith 
3533264ace61SBarry Smith   Output Parameter:
3534264ace61SBarry Smith . type - The DM type name
3535264ace61SBarry Smith 
3536264ace61SBarry Smith   Level: intermediate
3537264ace61SBarry Smith 
3538264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3539264ace61SBarry Smith @*/
354019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3541264ace61SBarry Smith {
3542264ace61SBarry Smith   PetscErrorCode ierr;
3543264ace61SBarry Smith 
3544264ace61SBarry Smith   PetscFunctionBegin;
3545264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3546c959eef4SJed Brown   PetscValidPointer(type,2);
3547607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3548264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3549264ace61SBarry Smith   PetscFunctionReturn(0);
3550264ace61SBarry Smith }
3551264ace61SBarry Smith 
355267a56275SMatthew G Knepley /*@C
355367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
355467a56275SMatthew G Knepley 
3555d083f849SBarry Smith   Collective on dm
355667a56275SMatthew G Knepley 
355767a56275SMatthew G Knepley   Input Parameters:
355867a56275SMatthew G Knepley + dm - the DM
355967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
356067a56275SMatthew G Knepley 
356167a56275SMatthew G Knepley   Output Parameter:
356267a56275SMatthew G Knepley . M - pointer to new DM
356367a56275SMatthew G Knepley 
356467a56275SMatthew G Knepley   Notes:
356567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
356667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
356767a56275SMatthew G Knepley   of the input DM.
356867a56275SMatthew G Knepley 
356967a56275SMatthew G Knepley   Level: intermediate
357067a56275SMatthew G Knepley 
357167a56275SMatthew G Knepley .seealso: DMCreate()
357267a56275SMatthew G Knepley @*/
357319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
357467a56275SMatthew G Knepley {
357567a56275SMatthew G Knepley   DM             B;
357667a56275SMatthew G Knepley   char           convname[256];
3577c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
357867a56275SMatthew G Knepley   PetscErrorCode ierr;
357967a56275SMatthew G Knepley 
358067a56275SMatthew G Knepley   PetscFunctionBegin;
358167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
358267a56275SMatthew G Knepley   PetscValidType(dm,1);
358367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3584251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3585c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3586c067b6caSMatthew G. Knepley   if (sametype) {
3587c067b6caSMatthew G. Knepley     *M   = dm;
3588c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3589c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3590c067b6caSMatthew G. Knepley   } else {
35910298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
359267a56275SMatthew G Knepley 
359367a56275SMatthew G Knepley     /*
359467a56275SMatthew G Knepley        Order of precedence:
359567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
359667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
359767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
359867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
359967a56275SMatthew G Knepley        5) Use a really basic converter.
360067a56275SMatthew G Knepley     */
360167a56275SMatthew G Knepley 
360267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3603a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3604a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3605a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3606a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3607a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36080005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
360967a56275SMatthew G Knepley     if (conv) goto foundconv;
361067a56275SMatthew G Knepley 
361167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
361282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
361367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3614a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3615a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3616a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3617a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3618a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
36190005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
362067a56275SMatthew G Knepley     if (conv) {
3621fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
362267a56275SMatthew G Knepley       goto foundconv;
362367a56275SMatthew G Knepley     }
362467a56275SMatthew G Knepley 
362567a56275SMatthew G Knepley #if 0
362667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
362767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3628fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
362967a56275SMatthew G Knepley     if (conv) goto foundconv;
363067a56275SMatthew G Knepley 
363167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
363267a56275SMatthew G Knepley     if (dm->ops->convert) {
363367a56275SMatthew G Knepley       conv = dm->ops->convert;
363467a56275SMatthew G Knepley     }
363567a56275SMatthew G Knepley     if (conv) goto foundconv;
363667a56275SMatthew G Knepley #endif
363767a56275SMatthew G Knepley 
363867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
363982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
364067a56275SMatthew G Knepley 
364167a56275SMatthew G Knepley foundconv:
364267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
364367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
364412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
364590b157c4SStefano Zampini     {
364690b157c4SStefano Zampini       PetscBool             isper;
364712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
364812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
364990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
365090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
365112fa691eSMatthew G. Knepley     }
365267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
365367a56275SMatthew G Knepley   }
365467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
365567a56275SMatthew G Knepley   PetscFunctionReturn(0);
365667a56275SMatthew G Knepley }
3657264ace61SBarry Smith 
3658264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3659264ace61SBarry Smith 
3660264ace61SBarry Smith /*@C
36611c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
36621c84c290SBarry Smith 
36631c84c290SBarry Smith   Not Collective
36641c84c290SBarry Smith 
36651c84c290SBarry Smith   Input Parameters:
36661c84c290SBarry Smith + name        - The name of a new user-defined creation routine
36671c84c290SBarry Smith - create_func - The creation routine itself
36681c84c290SBarry Smith 
36691c84c290SBarry Smith   Notes:
36701c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
36711c84c290SBarry Smith 
36721c84c290SBarry Smith 
36731c84c290SBarry Smith   Sample usage:
36741c84c290SBarry Smith .vb
3675bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
36761c84c290SBarry Smith .ve
36771c84c290SBarry Smith 
36781c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
36791c84c290SBarry Smith .vb
36801c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
36811c84c290SBarry Smith     DMSetType(DM,"my_da");
36821c84c290SBarry Smith .ve
36831c84c290SBarry Smith    or at runtime via the option
36841c84c290SBarry Smith .vb
36851c84c290SBarry Smith     -da_type my_da
36861c84c290SBarry Smith .ve
3687264ace61SBarry Smith 
3688264ace61SBarry Smith   Level: advanced
36891c84c290SBarry Smith 
3690bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
36911c84c290SBarry Smith 
3692264ace61SBarry Smith @*/
3693bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3694264ace61SBarry Smith {
3695264ace61SBarry Smith   PetscErrorCode ierr;
3696264ace61SBarry Smith 
3697264ace61SBarry Smith   PetscFunctionBegin;
36981d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3699a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3700264ace61SBarry Smith   PetscFunctionReturn(0);
3701264ace61SBarry Smith }
3702264ace61SBarry Smith 
3703b859378eSBarry Smith /*@C
370455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3705b859378eSBarry Smith 
3706d083f849SBarry Smith   Collective on viewer
3707b859378eSBarry Smith 
3708b859378eSBarry Smith   Input Parameters:
3709b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3710b859378eSBarry Smith            some related function before a call to DMLoad().
3711b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3712b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3713b859378eSBarry Smith 
3714b859378eSBarry Smith    Level: intermediate
3715b859378eSBarry Smith 
3716b859378eSBarry Smith   Notes:
371755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3718b859378eSBarry Smith 
3719b859378eSBarry Smith   Notes for advanced users:
3720b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3721b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3722b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3723b859378eSBarry Smith   format is
3724b859378eSBarry Smith .vb
3725b859378eSBarry Smith      has not yet been determined
3726b859378eSBarry Smith .ve
3727b859378eSBarry Smith 
3728b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3729b859378eSBarry Smith @*/
3730b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3731b859378eSBarry Smith {
37329331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3733b859378eSBarry Smith   PetscErrorCode ierr;
3734b859378eSBarry Smith 
3735b859378eSBarry Smith   PetscFunctionBegin;
3736b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3737b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3738fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
373932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
37409331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
37419331c7a4SMatthew G. Knepley   if (isbinary) {
37429331c7a4SMatthew G. Knepley     PetscInt classid;
37439331c7a4SMatthew G. Knepley     char     type[256];
3744b859378eSBarry Smith 
3745060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
37469200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3747060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
374832c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
37499331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37509331c7a4SMatthew G. Knepley   } else if (ishdf5) {
37519331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
37529331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
3753b859378eSBarry Smith   PetscFunctionReturn(0);
3754b859378eSBarry Smith }
3755b859378eSBarry Smith 
3756b2e4378dSMatthew G. Knepley /*@
3757b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3758b2e4378dSMatthew G. Knepley 
3759b2e4378dSMatthew G. Knepley   Not collective
3760b2e4378dSMatthew G. Knepley 
3761b2e4378dSMatthew G. Knepley   Input Parameter:
3762b2e4378dSMatthew G. Knepley . dm - the DM
3763b2e4378dSMatthew G. Knepley 
3764b2e4378dSMatthew G. Knepley   Output Parameters:
3765b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3766b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3767b2e4378dSMatthew G. Knepley 
3768b2e4378dSMatthew G. Knepley   Level: beginner
3769b2e4378dSMatthew G. Knepley 
3770b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3771b2e4378dSMatthew G. Knepley 
3772b2e4378dSMatthew G. Knepley 
3773b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3774b2e4378dSMatthew G. Knepley @*/
3775b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3776b2e4378dSMatthew G. Knepley {
3777b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3778b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3779b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3780b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3781b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3782b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3783b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3784b2e4378dSMatthew G. Knepley 
3785b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3786b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3787b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3788b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3789b2e4378dSMatthew G. Knepley   if (coords) {
3790b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3791b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3792b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3793b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3794b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3795b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3796b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3797b2e4378dSMatthew G. Knepley       }
3798b2e4378dSMatthew G. Knepley     }
3799b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3800b2e4378dSMatthew G. Knepley   } else {
3801b2e4378dSMatthew G. Knepley     PetscBool isda;
3802b2e4378dSMatthew G. Knepley 
3803b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3804b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3805b2e4378dSMatthew G. Knepley   }
3806b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3807b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3808b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3809b2e4378dSMatthew G. Knepley }
3810b2e4378dSMatthew G. Knepley 
3811b2e4378dSMatthew G. Knepley /*@
3812b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3813b2e4378dSMatthew G. Knepley 
3814b2e4378dSMatthew G. Knepley   Collective
3815b2e4378dSMatthew G. Knepley 
3816b2e4378dSMatthew G. Knepley   Input Parameter:
3817b2e4378dSMatthew G. Knepley . dm - the DM
3818b2e4378dSMatthew G. Knepley 
3819b2e4378dSMatthew G. Knepley   Output Parameters:
3820b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3821b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3822b2e4378dSMatthew G. Knepley 
3823b2e4378dSMatthew G. Knepley   Level: beginner
3824b2e4378dSMatthew G. Knepley 
3825b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3826b2e4378dSMatthew G. Knepley @*/
3827b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3828b2e4378dSMatthew G. Knepley {
3829b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
38306ce308c4SMatthew G. Knepley   PetscInt       cdim;
38316ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3832b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3833b2e4378dSMatthew G. Knepley 
3834b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3835b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3836b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3837b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3838b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3839b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3840b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3841b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3842b2e4378dSMatthew G. Knepley }
3843b2e4378dSMatthew G. Knepley 
38447da65231SMatthew G Knepley /******************************** FEM Support **********************************/
38457da65231SMatthew G Knepley 
3846a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3847a6dfd86eSKarl Rupp {
38481d47ebbbSSatish Balay   PetscInt       f;
38491b30c384SMatthew G Knepley   PetscErrorCode ierr;
38501b30c384SMatthew G Knepley 
38517da65231SMatthew G Knepley   PetscFunctionBegin;
385274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38531d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
385457622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
38557da65231SMatthew G Knepley   }
38567da65231SMatthew G Knepley   PetscFunctionReturn(0);
38577da65231SMatthew G Knepley }
38587da65231SMatthew G Knepley 
3859a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
3860a6dfd86eSKarl Rupp {
38611b30c384SMatthew G Knepley   PetscInt       f, g;
38627da65231SMatthew G Knepley   PetscErrorCode ierr;
38637da65231SMatthew G Knepley 
38647da65231SMatthew G Knepley   PetscFunctionBegin;
386574778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
38661d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
386774778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
38681d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
3869e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
38707da65231SMatthew G Knepley     }
387174778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
38727da65231SMatthew G Knepley   }
38737da65231SMatthew G Knepley   PetscFunctionReturn(0);
38747da65231SMatthew G Knepley }
3875e7c4fc90SDmitry Karpeev 
38766113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
3877e759306cSMatthew G. Knepley {
38780c5b8624SToby Isaac   PetscInt          localSize, bs;
38790c5b8624SToby Isaac   PetscMPIInt       size;
38800c5b8624SToby Isaac   Vec               x, xglob;
38810c5b8624SToby Isaac   const PetscScalar *xarray;
3882e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
3883e759306cSMatthew G. Knepley 
3884e759306cSMatthew G. Knepley   PetscFunctionBegin;
38859852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
3886e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
3887e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
38886113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
38890c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
38900c5b8624SToby Isaac   if (size > 1) {
38910c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
38920c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
38930c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
38940c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
38950c5b8624SToby Isaac   } else {
38960c5b8624SToby Isaac     xglob = x;
38970c5b8624SToby Isaac   }
38980c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
38990c5b8624SToby Isaac   if (size > 1) {
39000c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
39010c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
39020c5b8624SToby Isaac   }
3903e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
3904e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
3905e759306cSMatthew G. Knepley }
3906e759306cSMatthew G. Knepley 
390788ed4aceSMatthew G Knepley /*@
39081bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
3909061576a5SJed Brown 
3910061576a5SJed Brown   Input Parameter:
3911061576a5SJed Brown . dm - The DM
3912061576a5SJed Brown 
3913061576a5SJed Brown   Output Parameter:
3914061576a5SJed Brown . section - The PetscSection
3915061576a5SJed Brown 
3916061576a5SJed Brown   Options Database Keys:
3917061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
3918061576a5SJed Brown 
3919061576a5SJed Brown   Level: advanced
3920061576a5SJed Brown 
3921061576a5SJed Brown   Notes:
3922061576a5SJed Brown   Use DMGetLocalSection() in new code.
3923061576a5SJed Brown 
3924061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
3925061576a5SJed Brown 
3926061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
3927061576a5SJed Brown @*/
3928061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
3929061576a5SJed Brown {
3930061576a5SJed Brown   PetscErrorCode ierr;
3931061576a5SJed Brown 
3932061576a5SJed Brown   PetscFunctionBegin;
3933061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
3934061576a5SJed Brown   PetscFunctionReturn(0);
3935061576a5SJed Brown }
3936061576a5SJed Brown 
3937061576a5SJed Brown /*@
3938061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
393988ed4aceSMatthew G Knepley 
394088ed4aceSMatthew G Knepley   Input Parameter:
394188ed4aceSMatthew G Knepley . dm - The DM
394288ed4aceSMatthew G Knepley 
394388ed4aceSMatthew G Knepley   Output Parameter:
394488ed4aceSMatthew G Knepley . section - The PetscSection
394588ed4aceSMatthew G Knepley 
3946e5893cccSMatthew G. Knepley   Options Database Keys:
3947e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
3948e5893cccSMatthew G. Knepley 
394988ed4aceSMatthew G Knepley   Level: intermediate
395088ed4aceSMatthew G Knepley 
395188ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
395288ed4aceSMatthew G Knepley 
3953061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
395488ed4aceSMatthew G Knepley @*/
3955061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
39560adebc6cSBarry Smith {
3957fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
3958fd59a867SMatthew G. Knepley 
395988ed4aceSMatthew G Knepley   PetscFunctionBegin;
396088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
396188ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
39621bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
3963e5e52638SMatthew G. Knepley     PetscInt d;
3964e5e52638SMatthew G. Knepley 
3965e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
39661bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
39671bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
39682f0f8703SMatthew G. Knepley   }
39691bb6d2a8SBarry Smith   *section = dm->localSection;
397088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
397188ed4aceSMatthew G Knepley }
397288ed4aceSMatthew G Knepley 
397388ed4aceSMatthew G Knepley /*@
39741bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
3975061576a5SJed Brown 
3976061576a5SJed Brown   Input Parameters:
3977061576a5SJed Brown + dm - The DM
3978061576a5SJed Brown - section - The PetscSection
3979061576a5SJed Brown 
3980061576a5SJed Brown   Level: advanced
3981061576a5SJed Brown 
3982061576a5SJed Brown   Notes:
3983061576a5SJed Brown   Use DMSetLocalSection() in new code.
3984061576a5SJed Brown 
3985061576a5SJed Brown   Any existing Section will be destroyed
3986061576a5SJed Brown 
3987061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
3988061576a5SJed Brown @*/
3989061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
3990061576a5SJed Brown {
3991061576a5SJed Brown   PetscErrorCode ierr;
3992061576a5SJed Brown 
3993061576a5SJed Brown   PetscFunctionBegin;
3994061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
3995061576a5SJed Brown   PetscFunctionReturn(0);
3996061576a5SJed Brown }
3997061576a5SJed Brown 
3998061576a5SJed Brown /*@
3999061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
400088ed4aceSMatthew G Knepley 
400188ed4aceSMatthew G Knepley   Input Parameters:
400288ed4aceSMatthew G Knepley + dm - The DM
400388ed4aceSMatthew G Knepley - section - The PetscSection
400488ed4aceSMatthew G Knepley 
400588ed4aceSMatthew G Knepley   Level: intermediate
400688ed4aceSMatthew G Knepley 
400788ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
400888ed4aceSMatthew G Knepley 
4009061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
401088ed4aceSMatthew G Knepley @*/
4011061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
40120adebc6cSBarry Smith {
4013c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4014af122d2aSMatthew G Knepley   PetscInt       f;
401588ed4aceSMatthew G Knepley   PetscErrorCode ierr;
401688ed4aceSMatthew G Knepley 
401788ed4aceSMatthew G Knepley   PetscFunctionBegin;
401888ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4019b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
40201d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
40211bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
40221bb6d2a8SBarry Smith   dm->localSection = section;
40231bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4024af122d2aSMatthew G Knepley   if (numFields) {
4025af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4026af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
40270f21e855SMatthew G. Knepley       PetscObject disc;
4028af122d2aSMatthew G Knepley       const char *name;
4029af122d2aSMatthew G Knepley 
40301bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
403144a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
40320f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4033af122d2aSMatthew G Knepley     }
4034af122d2aSMatthew G Knepley   }
4035e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
40361bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
403788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
403888ed4aceSMatthew G Knepley }
403988ed4aceSMatthew G Knepley 
40409435951eSToby Isaac /*@
4041b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
40429435951eSToby Isaac 
4043e228b242SToby Isaac   not collective
4044e228b242SToby Isaac 
40459435951eSToby Isaac   Input Parameter:
40469435951eSToby Isaac . dm - The DM
40479435951eSToby Isaac 
40489435951eSToby Isaac   Output Parameter:
40499435951eSToby 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.
40509435951eSToby 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.
40519435951eSToby Isaac 
40529435951eSToby Isaac   Level: advanced
40539435951eSToby Isaac 
40549435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
40559435951eSToby Isaac 
40569435951eSToby Isaac .seealso: DMSetDefaultConstraints()
40579435951eSToby Isaac @*/
40589435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
40599435951eSToby Isaac {
40609435951eSToby Isaac   PetscErrorCode ierr;
40619435951eSToby Isaac 
40629435951eSToby Isaac   PetscFunctionBegin;
40639435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
40649435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
406545a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
406645a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
40679435951eSToby Isaac   PetscFunctionReturn(0);
40689435951eSToby Isaac }
40699435951eSToby Isaac 
40709435951eSToby Isaac /*@
4071b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
40729435951eSToby Isaac 
40739435951eSToby 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().
40749435951eSToby Isaac 
40759435951eSToby 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.
40769435951eSToby Isaac 
4077e228b242SToby Isaac   collective on dm
4078e228b242SToby Isaac 
40799435951eSToby Isaac   Input Parameters:
40809435951eSToby Isaac + dm - The DM
4081e228b242SToby 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).
4082e228b242SToby 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).
40839435951eSToby Isaac 
40849435951eSToby Isaac   Level: advanced
40859435951eSToby Isaac 
40869435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
40879435951eSToby Isaac 
40889435951eSToby Isaac .seealso: DMGetDefaultConstraints()
40899435951eSToby Isaac @*/
40909435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
40919435951eSToby Isaac {
4092e228b242SToby Isaac   PetscMPIInt result;
40939435951eSToby Isaac   PetscErrorCode ierr;
40949435951eSToby Isaac 
40959435951eSToby Isaac   PetscFunctionBegin;
40969435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4097e228b242SToby Isaac   if (section) {
4098e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4099e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4100f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4101e228b242SToby Isaac   }
4102e228b242SToby Isaac   if (mat) {
4103e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4104e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4105f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4106e228b242SToby Isaac   }
41079435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41089435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
41099435951eSToby Isaac   dm->defaultConstraintSection = section;
41109435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
41119435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
41129435951eSToby Isaac   dm->defaultConstraintMat = mat;
41139435951eSToby Isaac   PetscFunctionReturn(0);
41149435951eSToby Isaac }
41159435951eSToby Isaac 
4116497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4117507e4973SMatthew G. Knepley /*
4118507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4119507e4973SMatthew G. Knepley 
4120507e4973SMatthew G. Knepley   Input Parameters:
4121507e4973SMatthew G. Knepley + dm - The DM
4122507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4123507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4124507e4973SMatthew G. Knepley 
4125507e4973SMatthew G. Knepley   Level: intermediate
4126507e4973SMatthew G. Knepley 
41271bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4128507e4973SMatthew G. Knepley */
4129f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4130507e4973SMatthew G. Knepley {
4131507e4973SMatthew G. Knepley   MPI_Comm        comm;
4132507e4973SMatthew G. Knepley   PetscLayout     layout;
4133507e4973SMatthew G. Knepley   const PetscInt *ranges;
4134507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4135507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4136507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4137507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4138507e4973SMatthew G. Knepley 
4139507e4973SMatthew G. Knepley   PetscFunctionBegin;
4140507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4141507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4142507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4143507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4144507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4145507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4146507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4147507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4148507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4149507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4150507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4151507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4152f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4153507e4973SMatthew G. Knepley 
4154507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4155507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4156507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4157507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4158507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4159507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4160507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4161507e4973SMatthew 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;}
4162507e4973SMatthew 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;}
4163507e4973SMatthew G. Knepley     if (gdof < 0) {
4164507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4165507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4166507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4167507e4973SMatthew G. Knepley 
4168507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4169507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4170507e4973SMatthew 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;}
4171507e4973SMatthew G. Knepley       }
4172507e4973SMatthew G. Knepley     }
4173507e4973SMatthew G. Knepley   }
4174507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4175507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4176b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4177507e4973SMatthew G. Knepley   if (!gvalid) {
4178507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4179507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4180507e4973SMatthew G. Knepley   }
4181507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4182507e4973SMatthew G. Knepley }
4183f741bcd2SMatthew G. Knepley #endif
4184507e4973SMatthew G. Knepley 
418588ed4aceSMatthew G Knepley /*@
4186e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
418788ed4aceSMatthew G Knepley 
4188d083f849SBarry Smith   Collective on dm
41898b1ab98fSJed Brown 
419088ed4aceSMatthew G Knepley   Input Parameter:
419188ed4aceSMatthew G Knepley . dm - The DM
419288ed4aceSMatthew G Knepley 
419388ed4aceSMatthew G Knepley   Output Parameter:
419488ed4aceSMatthew G Knepley . section - The PetscSection
419588ed4aceSMatthew G Knepley 
419688ed4aceSMatthew G Knepley   Level: intermediate
419788ed4aceSMatthew G Knepley 
419888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
419988ed4aceSMatthew G Knepley 
420092fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
420188ed4aceSMatthew G Knepley @*/
4202e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
42030adebc6cSBarry Smith {
420488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
420588ed4aceSMatthew G Knepley 
420688ed4aceSMatthew G Knepley   PetscFunctionBegin;
420788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
420888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
42091bb6d2a8SBarry Smith   if (!dm->globalSection) {
4210fd59a867SMatthew G. Knepley     PetscSection s;
4211fd59a867SMatthew G. Knepley 
421292fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4213fd59a867SMatthew 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");
421433907cc2SStefano 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");
42151bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4216cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
42171bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
42181bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
421988ed4aceSMatthew G Knepley   }
42201bb6d2a8SBarry Smith   *section = dm->globalSection;
422188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
422288ed4aceSMatthew G Knepley }
422388ed4aceSMatthew G Knepley 
4224b21d0597SMatthew G Knepley /*@
4225e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4226b21d0597SMatthew G Knepley 
4227b21d0597SMatthew G Knepley   Input Parameters:
4228b21d0597SMatthew G Knepley + dm - The DM
42295080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4230b21d0597SMatthew G Knepley 
4231b21d0597SMatthew G Knepley   Level: intermediate
4232b21d0597SMatthew G Knepley 
4233b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4234b21d0597SMatthew G Knepley 
423592fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4236b21d0597SMatthew G Knepley @*/
4237e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
42380adebc6cSBarry Smith {
4239b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4240b21d0597SMatthew G Knepley 
4241b21d0597SMatthew G Knepley   PetscFunctionBegin;
4242b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42435080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
42441d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42451bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
42461bb6d2a8SBarry Smith   dm->globalSection = section;
4247497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
42481bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4249507e4973SMatthew G. Knepley #endif
4250b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4251b21d0597SMatthew G Knepley }
4252b21d0597SMatthew G Knepley 
425388ed4aceSMatthew G Knepley /*@
42541bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
425588ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
425688ed4aceSMatthew G Knepley 
425788ed4aceSMatthew G Knepley   Input Parameter:
425888ed4aceSMatthew G Knepley . dm - The DM
425988ed4aceSMatthew G Knepley 
426088ed4aceSMatthew G Knepley   Output Parameter:
426188ed4aceSMatthew G Knepley . sf - The PetscSF
426288ed4aceSMatthew G Knepley 
426388ed4aceSMatthew G Knepley   Level: intermediate
426488ed4aceSMatthew G Knepley 
426588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
426688ed4aceSMatthew G Knepley 
42671bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
426888ed4aceSMatthew G Knepley @*/
42691bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
42700adebc6cSBarry Smith {
427188ed4aceSMatthew G Knepley   PetscInt       nroots;
427288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
427388ed4aceSMatthew G Knepley 
427488ed4aceSMatthew G Knepley   PetscFunctionBegin;
427588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
427688ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
42771bb6d2a8SBarry Smith   if (!dm->sectionSF) {
42781bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
427933907cc2SStefano Zampini   }
42801bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
428188ed4aceSMatthew G Knepley   if (nroots < 0) {
428288ed4aceSMatthew G Knepley     PetscSection section, gSection;
428388ed4aceSMatthew G Knepley 
428492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
428531ea6d37SMatthew G Knepley     if (section) {
4286e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
42871bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
428831ea6d37SMatthew G Knepley     } else {
42890298fd71SBarry Smith       *sf = NULL;
429031ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
429131ea6d37SMatthew G Knepley     }
429288ed4aceSMatthew G Knepley   }
42931bb6d2a8SBarry Smith   *sf = dm->sectionSF;
429488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
429588ed4aceSMatthew G Knepley }
429688ed4aceSMatthew G Knepley 
429788ed4aceSMatthew G Knepley /*@
42981bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
429988ed4aceSMatthew G Knepley 
430088ed4aceSMatthew G Knepley   Input Parameters:
430188ed4aceSMatthew G Knepley + dm - The DM
430288ed4aceSMatthew G Knepley - sf - The PetscSF
430388ed4aceSMatthew G Knepley 
430488ed4aceSMatthew G Knepley   Level: intermediate
430588ed4aceSMatthew G Knepley 
430688ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
430788ed4aceSMatthew G Knepley 
43081bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
430988ed4aceSMatthew G Knepley @*/
43101bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
43110adebc6cSBarry Smith {
431288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
431388ed4aceSMatthew G Knepley 
431488ed4aceSMatthew G Knepley   PetscFunctionBegin;
431588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4316b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
431733907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
43181bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
43191bb6d2a8SBarry Smith   dm->sectionSF = sf;
432088ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
432188ed4aceSMatthew G Knepley }
432288ed4aceSMatthew G Knepley 
432388ed4aceSMatthew G Knepley /*@C
43241bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
432588ed4aceSMatthew G Knepley   describing the data layout.
432688ed4aceSMatthew G Knepley 
432788ed4aceSMatthew G Knepley   Input Parameters:
432888ed4aceSMatthew G Knepley + dm - The DM
432988ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
433088ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
433188ed4aceSMatthew G Knepley 
43321bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
433388ed4aceSMatthew G Knepley 
43341bb6d2a8SBarry Smith   Level: developer
43351bb6d2a8SBarry Smith 
43361bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
43371bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
43381bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
43391bb6d2a8SBarry Smith 
43401bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
434188ed4aceSMatthew G Knepley @*/
43421bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
434388ed4aceSMatthew G Knepley {
434482f516ccSBarry Smith   MPI_Comm       comm;
434588ed4aceSMatthew G Knepley   PetscLayout    layout;
434688ed4aceSMatthew G Knepley   const PetscInt *ranges;
434788ed4aceSMatthew G Knepley   PetscInt       *local;
434888ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4349ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
435088ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
435188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
435288ed4aceSMatthew G Knepley 
435388ed4aceSMatthew G Knepley   PetscFunctionBegin;
435488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4355367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
435688ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
435788ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
435888ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
435988ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
436088ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
436188ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
436288ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
436388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
436488ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4365ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
43666636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
436788ed4aceSMatthew G Knepley 
43686636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43696636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4370235fbf56SMatthew 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));
43716636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
437288ed4aceSMatthew G Knepley   }
4373785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4374785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
437588ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
43761f588964SMatthew G Knepley     const PetscInt *cind;
43776636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
437888ed4aceSMatthew G Knepley 
437988ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
438088ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
438188ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
438288ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
438388ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
43846636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
438588ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
43866636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
43876636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
43886636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4389057b4bcdSMatthew 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);
43906636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
43916636e97aSMatthew G Knepley     }
439288ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
439388ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
439488ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
439588ed4aceSMatthew G Knepley     }
439688ed4aceSMatthew G Knepley     if (gdof < 0) {
43976636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
439888ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
439988ed4aceSMatthew G Knepley 
440005376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
440131d3f06eSJed Brown         if (r < 0) r = -(r+2);
440205376888SMatthew 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);
440388ed4aceSMatthew G Knepley         remote[l].rank  = r;
440488ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
440588ed4aceSMatthew G Knepley       }
440688ed4aceSMatthew G Knepley     } else {
44076636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
440888ed4aceSMatthew G Knepley         remote[l].rank  = rank;
440988ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
441088ed4aceSMatthew G Knepley       }
441188ed4aceSMatthew G Knepley     }
441288ed4aceSMatthew G Knepley   }
44136636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
441488ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
44151bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
441688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
441788ed4aceSMatthew G Knepley }
4418af122d2aSMatthew G Knepley 
4419b21d0597SMatthew G Knepley /*@
4420b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4421b21d0597SMatthew G Knepley 
4422b21d0597SMatthew G Knepley   Input Parameter:
4423b21d0597SMatthew G Knepley . dm - The DM
4424b21d0597SMatthew G Knepley 
4425b21d0597SMatthew G Knepley   Output Parameter:
4426b21d0597SMatthew G Knepley . sf - The PetscSF
4427b21d0597SMatthew G Knepley 
4428b21d0597SMatthew G Knepley   Level: intermediate
4429b21d0597SMatthew G Knepley 
4430b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4431b21d0597SMatthew G Knepley 
44321bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4433b21d0597SMatthew G Knepley @*/
44340adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
44350adebc6cSBarry Smith {
4436b21d0597SMatthew G Knepley   PetscFunctionBegin;
4437b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4438b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4439b21d0597SMatthew G Knepley   *sf = dm->sf;
4440b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4441b21d0597SMatthew G Knepley }
4442b21d0597SMatthew G Knepley 
4443057b4bcdSMatthew G Knepley /*@
4444057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4445057b4bcdSMatthew G Knepley 
4446057b4bcdSMatthew G Knepley   Input Parameters:
4447057b4bcdSMatthew G Knepley + dm - The DM
4448057b4bcdSMatthew G Knepley - sf - The PetscSF
4449057b4bcdSMatthew G Knepley 
4450057b4bcdSMatthew G Knepley   Level: intermediate
4451057b4bcdSMatthew G Knepley 
44521bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4453057b4bcdSMatthew G Knepley @*/
44540adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
44550adebc6cSBarry Smith {
4456057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4457057b4bcdSMatthew G Knepley 
4458057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4459057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4460b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4461057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
446233907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4463057b4bcdSMatthew G Knepley   dm->sf = sf;
4464057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4465057b4bcdSMatthew G Knepley }
4466057b4bcdSMatthew G Knepley 
446734aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
446834aa8a36SMatthew G. Knepley {
446934aa8a36SMatthew G. Knepley   PetscClassId   id;
447034aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
447134aa8a36SMatthew G. Knepley 
447234aa8a36SMatthew G. Knepley   PetscFunctionBegin;
447334aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
447434aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
447534aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
447634aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
447734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
447817c1d62eSMatthew G. Knepley   } else {
447917c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
448034aa8a36SMatthew G. Knepley   }
448134aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
448234aa8a36SMatthew G. Knepley }
448334aa8a36SMatthew G. Knepley 
448444a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
448544a7f3ddSMatthew G. Knepley {
448644a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
448744a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
448844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
448944a7f3ddSMatthew G. Knepley 
449044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
449144a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
449244a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
449344a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
449444a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
449544a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
449644a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
449744a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
449844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
449944a7f3ddSMatthew G. Knepley }
450044a7f3ddSMatthew G. Knepley 
450144a7f3ddSMatthew G. Knepley /*@
450244a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
450344a7f3ddSMatthew G. Knepley 
4504d083f849SBarry Smith   Logically collective on dm
450544a7f3ddSMatthew G. Knepley 
450644a7f3ddSMatthew G. Knepley   Input Parameter:
450744a7f3ddSMatthew G. Knepley . dm - The DM
450844a7f3ddSMatthew G. Knepley 
450944a7f3ddSMatthew G. Knepley   Level: intermediate
451044a7f3ddSMatthew G. Knepley 
451144a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
451244a7f3ddSMatthew G. Knepley @*/
451344a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
451444a7f3ddSMatthew G. Knepley {
451544a7f3ddSMatthew G. Knepley   PetscInt       f;
451644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
451744a7f3ddSMatthew G. Knepley 
451844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
451944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
452044a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
452144a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
452244a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
452344a7f3ddSMatthew G. Knepley   }
452444a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
452544a7f3ddSMatthew G. Knepley   dm->fields = NULL;
452644a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
452744a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
452844a7f3ddSMatthew G. Knepley }
452944a7f3ddSMatthew G. Knepley 
4530689b5837SMatthew G. Knepley /*@
4531689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4532689b5837SMatthew G. Knepley 
4533689b5837SMatthew G. Knepley   Not collective
4534689b5837SMatthew G. Knepley 
4535689b5837SMatthew G. Knepley   Input Parameter:
4536689b5837SMatthew G. Knepley . dm - The DM
4537689b5837SMatthew G. Knepley 
4538689b5837SMatthew G. Knepley   Output Parameter:
4539689b5837SMatthew G. Knepley . Nf - The number of fields
4540689b5837SMatthew G. Knepley 
4541689b5837SMatthew G. Knepley   Level: intermediate
4542689b5837SMatthew G. Knepley 
4543689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4544689b5837SMatthew G. Knepley @*/
45450f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
45460f21e855SMatthew G. Knepley {
45470f21e855SMatthew G. Knepley   PetscFunctionBegin;
45480f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4549534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
455044a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4551af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4552af122d2aSMatthew G Knepley }
4553af122d2aSMatthew G Knepley 
4554689b5837SMatthew G. Knepley /*@
4555689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4556689b5837SMatthew G. Knepley 
4557d083f849SBarry Smith   Logically collective on dm
4558689b5837SMatthew G. Knepley 
4559689b5837SMatthew G. Knepley   Input Parameters:
4560689b5837SMatthew G. Knepley + dm - The DM
4561689b5837SMatthew G. Knepley - Nf - The number of fields
4562689b5837SMatthew G. Knepley 
4563689b5837SMatthew G. Knepley   Level: intermediate
4564689b5837SMatthew G. Knepley 
4565689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4566689b5837SMatthew G. Knepley @*/
4567af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4568af122d2aSMatthew G Knepley {
45690f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4570af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4571af122d2aSMatthew G Knepley 
4572af122d2aSMatthew G Knepley   PetscFunctionBegin;
4573af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
457444a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
45750f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
45760f21e855SMatthew G. Knepley     PetscContainer obj;
45770f21e855SMatthew G. Knepley 
45780f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
457944a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
45800f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4581af122d2aSMatthew G Knepley   }
4582af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4583af122d2aSMatthew G Knepley }
4584af122d2aSMatthew G Knepley 
4585c1929be8SMatthew G. Knepley /*@
4586c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4587c1929be8SMatthew G. Knepley 
4588c1929be8SMatthew G. Knepley   Not collective
4589c1929be8SMatthew G. Knepley 
4590c1929be8SMatthew G. Knepley   Input Parameters:
4591c1929be8SMatthew G. Knepley + dm - The DM
4592c1929be8SMatthew G. Knepley - f  - The field number
4593c1929be8SMatthew G. Knepley 
459444a7f3ddSMatthew G. Knepley   Output Parameters:
459544a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
459644a7f3ddSMatthew G. Knepley - field - The discretization object
4597c1929be8SMatthew G. Knepley 
459844a7f3ddSMatthew G. Knepley   Level: intermediate
4599c1929be8SMatthew G. Knepley 
460044a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4601c1929be8SMatthew G. Knepley @*/
460244a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4603af122d2aSMatthew G Knepley {
4604af122d2aSMatthew G Knepley   PetscFunctionBegin;
4605af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
460644a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
460744a7f3ddSMatthew 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);
460844a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
460944a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4610decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4611decb47aaSMatthew G. Knepley }
4612decb47aaSMatthew G. Knepley 
4613c1929be8SMatthew G. Knepley /*@
4614c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4615c1929be8SMatthew G. Knepley 
4616d083f849SBarry Smith   Logically collective on dm
4617c1929be8SMatthew G. Knepley 
4618c1929be8SMatthew G. Knepley   Input Parameters:
4619c1929be8SMatthew G. Knepley + dm    - The DM
4620c1929be8SMatthew G. Knepley . f     - The field number
462144a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4622c1929be8SMatthew G. Knepley - field - The discretization object
4623c1929be8SMatthew G. Knepley 
462444a7f3ddSMatthew G. Knepley   Level: intermediate
4625c1929be8SMatthew G. Knepley 
462644a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4627c1929be8SMatthew G. Knepley @*/
462844a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4629decb47aaSMatthew G. Knepley {
4630decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4631decb47aaSMatthew G. Knepley 
4632decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4633decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4634e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
463544a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4636e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
463744a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
463844a7f3ddSMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
463944a7f3ddSMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
464044a7f3ddSMatthew G. Knepley   dm->fields[f].label = label;
464144a7f3ddSMatthew G. Knepley   dm->fields[f].disc  = field;
464244a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
464344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
464434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
46452df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
464644a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
464744a7f3ddSMatthew G. Knepley }
464844a7f3ddSMatthew G. Knepley 
464944a7f3ddSMatthew G. Knepley /*@
465044a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
465144a7f3ddSMatthew G. Knepley 
4652d083f849SBarry Smith   Logically collective on dm
465344a7f3ddSMatthew G. Knepley 
465444a7f3ddSMatthew G. Knepley   Input Parameters:
465544a7f3ddSMatthew G. Knepley + dm    - The DM
465644a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
465744a7f3ddSMatthew G. Knepley - field - The discretization object
465844a7f3ddSMatthew G. Knepley 
465944a7f3ddSMatthew G. Knepley   Level: intermediate
466044a7f3ddSMatthew G. Knepley 
466144a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
466244a7f3ddSMatthew G. Knepley @*/
466344a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
466444a7f3ddSMatthew G. Knepley {
466544a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
466644a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
466744a7f3ddSMatthew G. Knepley 
466844a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
466944a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
467044a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
467144a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
467244a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
467344a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
467444a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
467544a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
467644a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
467734aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
46782df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4679af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4680af122d2aSMatthew G Knepley }
46816636e97aSMatthew G Knepley 
4682e5e52638SMatthew G. Knepley /*@
4683e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4684e5e52638SMatthew G. Knepley 
4685d083f849SBarry Smith   Collective on dm
4686e5e52638SMatthew G. Knepley 
4687e5e52638SMatthew G. Knepley   Input Parameter:
4688e5e52638SMatthew G. Knepley . dm - The DM
4689e5e52638SMatthew G. Knepley 
4690e5e52638SMatthew G. Knepley   Output Parameter:
4691e5e52638SMatthew G. Knepley . newdm - The DM
4692e5e52638SMatthew G. Knepley 
4693e5e52638SMatthew G. Knepley   Level: advanced
4694e5e52638SMatthew G. Knepley 
4695e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4696e5e52638SMatthew G. Knepley @*/
4697e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4698e5e52638SMatthew G. Knepley {
4699e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4700e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4701e5e52638SMatthew G. Knepley 
4702e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4703e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4704e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4705e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4706e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4707e5e52638SMatthew G. Knepley     DMLabel     label;
4708e5e52638SMatthew G. Knepley     PetscObject field;
470934aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4710e5e52638SMatthew G. Knepley 
4711e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4712e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
471334aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
471434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
471534aa8a36SMatthew G. Knepley   }
471634aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
471734aa8a36SMatthew G. Knepley }
471834aa8a36SMatthew G. Knepley 
471934aa8a36SMatthew G. Knepley /*@
472034aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
472134aa8a36SMatthew G. Knepley 
472234aa8a36SMatthew G. Knepley   Not collective
472334aa8a36SMatthew G. Knepley 
472434aa8a36SMatthew G. Knepley   Input Parameters:
472534aa8a36SMatthew G. Knepley + dm - The DM object
472634aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
472734aa8a36SMatthew G. Knepley 
472834aa8a36SMatthew G. Knepley   Output Parameter:
472934aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
473034aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
473134aa8a36SMatthew G. Knepley 
473234aa8a36SMatthew G. Knepley   Notes:
473334aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
473434aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
473534aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4736979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
473734aa8a36SMatthew G. Knepley 
473834aa8a36SMatthew G. Knepley   Level: developer
473934aa8a36SMatthew G. Knepley 
474034aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
474134aa8a36SMatthew G. Knepley @*/
474234aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
474334aa8a36SMatthew G. Knepley {
474434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
474534aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4746534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4747534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
474834aa8a36SMatthew G. Knepley   if (f < 0) {
474934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
475034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
475134aa8a36SMatthew G. Knepley   } else {
475234aa8a36SMatthew G. Knepley     PetscInt       Nf;
475334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
475434aa8a36SMatthew G. Knepley 
475534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
475634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
475734aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
475834aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
475934aa8a36SMatthew G. Knepley   }
476034aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
476134aa8a36SMatthew G. Knepley }
476234aa8a36SMatthew G. Knepley 
476334aa8a36SMatthew G. Knepley /*@
476434aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
476534aa8a36SMatthew G. Knepley 
476634aa8a36SMatthew G. Knepley   Not collective
476734aa8a36SMatthew G. Knepley 
476834aa8a36SMatthew G. Knepley   Input Parameters:
476934aa8a36SMatthew G. Knepley + dm         - The DM object
477034aa8a36SMatthew G. Knepley . f          - The field number
477134aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
477234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
477334aa8a36SMatthew G. Knepley 
477434aa8a36SMatthew G. Knepley   Notes:
477534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
477634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
477734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4778979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
477934aa8a36SMatthew G. Knepley 
478034aa8a36SMatthew G. Knepley   Level: developer
478134aa8a36SMatthew G. Knepley 
478234aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
478334aa8a36SMatthew G. Knepley @*/
478434aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
478534aa8a36SMatthew G. Knepley {
478634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
478734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
478834aa8a36SMatthew G. Knepley   if (f < 0) {
478934aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
479034aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
479134aa8a36SMatthew G. Knepley   } else {
479234aa8a36SMatthew G. Knepley     PetscInt       Nf;
479334aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
479434aa8a36SMatthew G. Knepley 
479534aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
479634aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
479734aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
479834aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4799e5e52638SMatthew G. Knepley   }
4800e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4801e5e52638SMatthew G. Knepley }
4802e5e52638SMatthew G. Knepley 
4803b0441da4SMatthew G. Knepley /*@
4804b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4805b0441da4SMatthew G. Knepley 
4806b0441da4SMatthew G. Knepley   Not collective
4807b0441da4SMatthew G. Knepley 
4808b0441da4SMatthew G. Knepley   Input Parameters:
4809b0441da4SMatthew G. Knepley . dm - The DM object
4810b0441da4SMatthew G. Knepley 
4811b0441da4SMatthew G. Knepley   Output Parameter:
4812b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4813b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4814b0441da4SMatthew G. Knepley 
4815b0441da4SMatthew G. Knepley   Notes:
4816b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4817b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4818b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4819b0441da4SMatthew G. Knepley 
4820b0441da4SMatthew G. Knepley   Level: developer
4821b0441da4SMatthew G. Knepley 
4822b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4823b0441da4SMatthew G. Knepley @*/
4824b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4825b0441da4SMatthew G. Knepley {
4826b0441da4SMatthew G. Knepley   PetscInt       Nf;
4827b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4828b0441da4SMatthew G. Knepley 
4829b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4830b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4831534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4832534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4833b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4834b0441da4SMatthew G. Knepley   if (!Nf) {
4835b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4836b0441da4SMatthew G. Knepley   } else {
4837b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4838b0441da4SMatthew G. Knepley   }
4839b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4840b0441da4SMatthew G. Knepley }
4841b0441da4SMatthew G. Knepley 
4842b0441da4SMatthew G. Knepley /*@
4843b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4844b0441da4SMatthew G. Knepley 
4845b0441da4SMatthew G. Knepley   Not collective
4846b0441da4SMatthew G. Knepley 
4847b0441da4SMatthew G. Knepley   Input Parameters:
4848b0441da4SMatthew G. Knepley + dm         - The DM object
4849b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
4850b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4851b0441da4SMatthew G. Knepley 
4852b0441da4SMatthew G. Knepley   Notes:
4853b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4854b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4855b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4856b0441da4SMatthew G. Knepley 
4857b0441da4SMatthew G. Knepley   Level: developer
4858b0441da4SMatthew G. Knepley 
4859b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
4860b0441da4SMatthew G. Knepley @*/
4861b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
4862b0441da4SMatthew G. Knepley {
4863b0441da4SMatthew G. Knepley   PetscInt       Nf;
4864b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4865b0441da4SMatthew G. Knepley 
4866b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4867b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4868b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4869b0441da4SMatthew G. Knepley   if (!Nf) {
4870b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4871b0441da4SMatthew G. Knepley   } else {
4872b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4873e5e52638SMatthew G. Knepley   }
4874e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4875e5e52638SMatthew G. Knepley }
4876e5e52638SMatthew G. Knepley 
4877e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
4878e5e52638SMatthew G. Knepley {
4879e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
4880e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
4881e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4882e5e52638SMatthew G. Knepley 
4883e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4884e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
4885e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
4886e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
4887b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
4888e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4889e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
4890e5e52638SMatthew G. Knepley   dm->probs = tmpd;
4891e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4892e5e52638SMatthew G. Knepley }
4893e5e52638SMatthew G. Knepley 
4894e5e52638SMatthew G. Knepley /*@
4895e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
4896e5e52638SMatthew G. Knepley 
4897e5e52638SMatthew G. Knepley   Not collective
4898e5e52638SMatthew G. Knepley 
4899e5e52638SMatthew G. Knepley   Input Parameter:
4900e5e52638SMatthew G. Knepley . dm - The DM
4901e5e52638SMatthew G. Knepley 
4902e5e52638SMatthew G. Knepley   Output Parameter:
4903e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
4904e5e52638SMatthew G. Knepley 
4905e5e52638SMatthew G. Knepley   Level: intermediate
4906e5e52638SMatthew G. Knepley 
4907e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
4908e5e52638SMatthew G. Knepley @*/
4909e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
4910e5e52638SMatthew G. Knepley {
4911e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4912e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4913534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
4914e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
4915e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4916e5e52638SMatthew G. Knepley }
4917e5e52638SMatthew G. Knepley 
4918e5e52638SMatthew G. Knepley /*@
4919e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
4920e5e52638SMatthew G. Knepley 
4921d083f849SBarry Smith   Logically collective on dm
4922e5e52638SMatthew G. Knepley 
4923e5e52638SMatthew G. Knepley   Input Parameter:
4924e5e52638SMatthew G. Knepley . dm - The DM
4925e5e52638SMatthew G. Knepley 
4926e5e52638SMatthew G. Knepley   Level: intermediate
4927e5e52638SMatthew G. Knepley 
4928e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
4929e5e52638SMatthew G. Knepley @*/
4930e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
4931e5e52638SMatthew G. Knepley {
4932e5e52638SMatthew G. Knepley   PetscInt       s;
4933e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4934e5e52638SMatthew G. Knepley 
4935e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4936e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4937e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
4938e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
4939e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
4940b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
4941e5e52638SMatthew G. Knepley   }
4942e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
4943e5e52638SMatthew G. Knepley   dm->probs = NULL;
4944e5e52638SMatthew G. Knepley   dm->Nds   = 0;
4945e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4946e5e52638SMatthew G. Knepley }
4947e5e52638SMatthew G. Knepley 
4948e5e52638SMatthew G. Knepley /*@
4949e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
4950e5e52638SMatthew G. Knepley 
4951e5e52638SMatthew G. Knepley   Not collective
4952e5e52638SMatthew G. Knepley 
4953e5e52638SMatthew G. Knepley   Input Parameter:
4954e5e52638SMatthew G. Knepley . dm    - The DM
4955e5e52638SMatthew G. Knepley 
4956e5e52638SMatthew G. Knepley   Output Parameter:
4957e5e52638SMatthew G. Knepley . prob - The default PetscDS
4958e5e52638SMatthew G. Knepley 
4959e5e52638SMatthew G. Knepley   Level: intermediate
4960e5e52638SMatthew G. Knepley 
4961e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
4962e5e52638SMatthew G. Knepley @*/
4963e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
4964e5e52638SMatthew G. Knepley {
4965b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
4966b0143b4dSMatthew G. Knepley 
4967e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
4968e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4969e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
4970b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
4971b0143b4dSMatthew G. Knepley     PetscDS ds;
4972b0143b4dSMatthew G. Knepley 
4973b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
4974b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
4975b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
4976b0143b4dSMatthew G. Knepley   }
4977b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
4978e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4979e5e52638SMatthew G. Knepley }
4980e5e52638SMatthew G. Knepley 
4981e5e52638SMatthew G. Knepley /*@
4982e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
4983e5e52638SMatthew G. Knepley 
4984e5e52638SMatthew G. Knepley   Not collective
4985e5e52638SMatthew G. Knepley 
4986e5e52638SMatthew G. Knepley   Input Parameters:
4987e5e52638SMatthew G. Knepley + dm    - The DM
4988e5e52638SMatthew G. Knepley - point - Cell for the DS
4989e5e52638SMatthew G. Knepley 
4990e5e52638SMatthew G. Knepley   Output Parameter:
4991e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
4992e5e52638SMatthew G. Knepley 
4993e5e52638SMatthew G. Knepley   Level: developer
4994e5e52638SMatthew G. Knepley 
4995b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
4996e5e52638SMatthew G. Knepley @*/
4997e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
4998e5e52638SMatthew G. Knepley {
4999e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5000e5e52638SMatthew G. Knepley   PetscInt       s;
5001e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5002e5e52638SMatthew G. Knepley 
5003e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5004e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5005e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5006e5e52638SMatthew G. Knepley   *prob = NULL;
5007e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5008e5e52638SMatthew G. Knepley     PetscInt val;
5009e5e52638SMatthew G. Knepley 
5010e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5011e5e52638SMatthew G. Knepley     else {
5012e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5013e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5014e5e52638SMatthew G. Knepley     }
5015e5e52638SMatthew G. Knepley   }
5016e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5017e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5018e5e52638SMatthew G. Knepley }
5019e5e52638SMatthew G. Knepley 
5020e5e52638SMatthew G. Knepley /*@
5021e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5022e5e52638SMatthew G. Knepley 
5023e5e52638SMatthew G. Knepley   Not collective
5024e5e52638SMatthew G. Knepley 
5025e5e52638SMatthew G. Knepley   Input Parameters:
5026e5e52638SMatthew G. Knepley + dm    - The DM
5027e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5028e5e52638SMatthew G. Knepley 
5029b3cf3223SMatthew G. Knepley   Output Parameters:
5030b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5031b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5032e5e52638SMatthew G. Knepley 
5033e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5034e5e52638SMatthew G. Knepley 
5035e5e52638SMatthew G. Knepley   Level: advanced
5036e5e52638SMatthew G. Knepley 
5037e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5038e5e52638SMatthew G. Knepley @*/
5039b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5040e5e52638SMatthew G. Knepley {
5041e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5042e5e52638SMatthew G. Knepley 
5043e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5044e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5045e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5046b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5047b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5048e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5049b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5050b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5051b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5052b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5053b3cf3223SMatthew G. Knepley     }
5054e5e52638SMatthew G. Knepley   }
50552df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5056e5e52638SMatthew G. Knepley }
5057e5e52638SMatthew G. Knepley 
5058e5e52638SMatthew G. Knepley /*@
5059e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5060e5e52638SMatthew G. Knepley 
5061e5e52638SMatthew G. Knepley   Not collective
5062e5e52638SMatthew G. Knepley 
5063e5e52638SMatthew G. Knepley   Input Parameters:
5064e5e52638SMatthew G. Knepley + dm  - The DM
5065e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5066e5e52638SMatthew G. Knepley 
5067e5e52638SMatthew G. Knepley   Output Parameters:
5068b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5069b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5070b3cf3223SMatthew G. Knepley - prob   - The PetscDS defined on the given region, or NULL
5071e5e52638SMatthew G. Knepley 
5072e5e52638SMatthew G. Knepley   Level: advanced
5073e5e52638SMatthew G. Knepley 
5074e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5075e5e52638SMatthew G. Knepley @*/
5076b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5077e5e52638SMatthew G. Knepley {
5078e5e52638SMatthew G. Knepley   PetscInt       Nds;
5079e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5080e5e52638SMatthew G. Knepley 
5081e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5082e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5083e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5084e5e52638SMatthew 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);
5085e5e52638SMatthew G. Knepley   if (label) {
5086e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5087e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5088e5e52638SMatthew G. Knepley   }
5089b3cf3223SMatthew G. Knepley   if (fields) {
5090b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5091b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5092b3cf3223SMatthew G. Knepley   }
5093e5e52638SMatthew G. Knepley   if (ds) {
5094b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5095e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5096e5e52638SMatthew G. Knepley   }
5097e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5098e5e52638SMatthew G. Knepley }
5099e5e52638SMatthew G. Knepley 
5100e5e52638SMatthew G. Knepley /*@
5101e5e52638SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5102e5e52638SMatthew G. Knepley 
5103d083f849SBarry Smith   Collective on dm
5104e5e52638SMatthew G. Knepley 
5105e5e52638SMatthew G. Knepley   Input Parameters:
5106e5e52638SMatthew G. Knepley + dm     - The DM
5107e5e52638SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5108b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5109e5e52638SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5110e5e52638SMatthew G. Knepley 
5111b3cf3223SMatthew 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,
5112b3cf3223SMatthew G. Knepley   the fields argument is ignored.
5113e5e52638SMatthew G. Knepley 
5114e5e52638SMatthew G. Knepley   Level: advanced
5115e5e52638SMatthew G. Knepley 
5116e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMGetDS(), DMGetCellDS()
5117e5e52638SMatthew G. Knepley @*/
5118b3cf3223SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5119e5e52638SMatthew G. Knepley {
5120e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5121e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5122e5e52638SMatthew G. Knepley 
5123e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5124e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5125e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5126e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5127e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5128e5e52638SMatthew G. Knepley     if (dm->probs[s].label == label) {
5129b3cf3223SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5130e5e52638SMatthew G. Knepley       dm->probs[s].ds = ds;
5131e5e52638SMatthew G. Knepley       PetscFunctionReturn(0);
5132e5e52638SMatthew G. Knepley     }
5133e5e52638SMatthew G. Knepley   }
51341b5e6740SSatish Balay   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5135e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5136b3cf3223SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5137e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5138e5e52638SMatthew G. Knepley   if (!label) {
5139e5e52638SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5140e5e52638SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5141e5e52638SMatthew G. Knepley     Nds = 0;
5142e5e52638SMatthew G. Knepley   }
5143e5e52638SMatthew G. Knepley   dm->probs[Nds].label  = label;
5144b3cf3223SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5145e5e52638SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5146e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5147e5e52638SMatthew G. Knepley }
5148e5e52638SMatthew G. Knepley 
5149e5e52638SMatthew G. Knepley /*@
5150e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5151e5e52638SMatthew G. Knepley 
5152d083f849SBarry Smith   Collective on dm
5153e5e52638SMatthew G. Knepley 
5154e5e52638SMatthew G. Knepley   Input Parameter:
5155e5e52638SMatthew G. Knepley . dm - The DM
5156e5e52638SMatthew G. Knepley 
5157e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5158e5e52638SMatthew G. Knepley 
5159e5e52638SMatthew G. Knepley   Level: intermediate
5160e5e52638SMatthew G. Knepley 
5161e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5162e5e52638SMatthew G. Knepley @*/
5163e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5164e5e52638SMatthew G. Knepley {
5165e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5166e5e52638SMatthew G. Knepley   PetscDS        prob, probh = NULL;
5167b3cf3223SMatthew G. Knepley   PetscInt       dimEmbed, Nf = dm->Nf, f, s, field = 0, fieldh = 0;
5168e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5169e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5170e5e52638SMatthew G. Knepley 
5171e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5172e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5173e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5174e5e52638SMatthew G. Knepley   /* Can only handle two label cases right now:
5175e5e52638SMatthew G. Knepley    1) NULL
5176e5e52638SMatthew G. Knepley    2) Hybrid cells
5177e5e52638SMatthew G. Knepley   */
5178e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5179e5e52638SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dimEmbed);CHKERRQ(ierr);
5180e5e52638SMatthew G. Knepley   /* Create default DS */
5181b3cf3223SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
51822df9ee95SMatthew G. Knepley   if (!prob) {
5183b3cf3223SMatthew G. Knepley     IS        fields;
5184b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5185b3cf3223SMatthew G. Knepley 
5186b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5187b3cf3223SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5188b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
518988f0c812SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
519088f0c812SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
519188f0c812SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
519288f0c812SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
519388f0c812SMatthew G. Knepley 
51942df9ee95SMatthew G. Knepley     ierr = PetscDSCreate(comm, &prob);CHKERRQ(ierr);
5195b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, fields, prob);CHKERRQ(ierr);
51962df9ee95SMatthew G. Knepley     ierr = PetscDSDestroy(&prob);CHKERRQ(ierr);
5197b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5198b3cf3223SMatthew G. Knepley     ierr = DMGetRegionDS(dm, NULL, NULL, &prob);CHKERRQ(ierr);
51992df9ee95SMatthew G. Knepley   }
5200e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(prob, dimEmbed);CHKERRQ(ierr);
5201e5e52638SMatthew G. Knepley   /* Optionally create hybrid DS */
5202b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5203e5e52638SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5204e5e52638SMatthew G. Knepley     PetscInt lStart, lEnd;
5205e5e52638SMatthew G. Knepley 
5206e5e52638SMatthew G. Knepley     if (label) {
52070122748bSMatthew G. Knepley       DM        plex;
5208a2d47024SMatthew G. Knepley       IS        fields;
5209a2d47024SMatthew G. Knepley       PetscInt *fld;
52100122748bSMatthew G. Knepley       PetscInt  depth, pMax[4];
52110122748bSMatthew G. Knepley 
52120122748bSMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
52130122748bSMatthew G. Knepley       ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
52140122748bSMatthew 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);
52150122748bSMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
52160122748bSMatthew G. Knepley 
5217e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5218e5e52638SMatthew G. Knepley       if (lStart < pMax[depth]) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Only support labels over hybrid cells right now");
5219e5e52638SMatthew G. Knepley       ierr = PetscDSCreate(comm, &probh);CHKERRQ(ierr);
5220a2d47024SMatthew G. Knepley       ierr = PetscMalloc1(1, &fld);CHKERRQ(ierr);
5221a2d47024SMatthew G. Knepley       fld[0] = f;
5222a2d47024SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5223a2d47024SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5224a2d47024SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5225a2d47024SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, 1, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5226a2d47024SMatthew G. Knepley       ierr = DMSetRegionDS(dm, label, fields, probh);CHKERRQ(ierr);
5227a2d47024SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
5228e5e52638SMatthew G. Knepley       ierr = PetscDSSetHybrid(probh, PETSC_TRUE);CHKERRQ(ierr);
5229e5e52638SMatthew G. Knepley       ierr = PetscDSSetCoordinateDimension(probh, dimEmbed);CHKERRQ(ierr);
5230e5e52638SMatthew G. Knepley       break;
5231e5e52638SMatthew G. Knepley     }
5232e5e52638SMatthew G. Knepley   }
5233e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5234b3cf3223SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5235e5e52638SMatthew G. Knepley     DMLabel     label = dm->fields[f].label;
5236e5e52638SMatthew G. Knepley     PetscObject disc  = dm->fields[f].disc;
5237e5e52638SMatthew G. Knepley 
5238e5e52638SMatthew G. Knepley     if (!label) {
5239e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(prob,  field++,  disc);CHKERRQ(ierr);
5240e5e52638SMatthew G. Knepley       if (probh) {
5241e5e52638SMatthew G. Knepley         PetscFE subfe;
5242e5e52638SMatthew G. Knepley 
5243e5e52638SMatthew G. Knepley         ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, &subfe);CHKERRQ(ierr);
5244e5e52638SMatthew G. Knepley         ierr = PetscDSSetDiscretization(probh, fieldh++, (PetscObject) subfe);CHKERRQ(ierr);
5245e5e52638SMatthew G. Knepley       }
5246e5e52638SMatthew G. Knepley     } else {
5247e5e52638SMatthew G. Knepley       ierr = PetscDSSetDiscretization(probh, fieldh++, disc);CHKERRQ(ierr);
5248e5e52638SMatthew G. Knepley     }
5249e5e52638SMatthew G. Knepley     /* We allow people to have placeholder fields and construct the Section by hand */
5250e5e52638SMatthew G. Knepley     {
5251e5e52638SMatthew G. Knepley       PetscClassId id;
5252e5e52638SMatthew G. Knepley 
5253e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5254e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5255e5e52638SMatthew G. Knepley     }
5256e5e52638SMatthew G. Knepley   }
5257e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&probh);CHKERRQ(ierr);
5258e5e52638SMatthew G. Knepley   /* Setup DSes */
5259e5e52638SMatthew G. Knepley   if (doSetup) {
5260e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5261e5e52638SMatthew G. Knepley   }
5262e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5263e5e52638SMatthew G. Knepley }
5264e5e52638SMatthew G. Knepley 
5265e5e52638SMatthew G. Knepley /*@
5266e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5267e5e52638SMatthew G. Knepley 
5268d083f849SBarry Smith   Collective on dm
5269e5e52638SMatthew G. Knepley 
5270e5e52638SMatthew G. Knepley   Input Parameter:
5271e5e52638SMatthew G. Knepley . dm - The DM
5272e5e52638SMatthew G. Knepley 
5273e5e52638SMatthew G. Knepley   Output Parameter:
5274e5e52638SMatthew G. Knepley . newdm - The DM
5275e5e52638SMatthew G. Knepley 
5276e5e52638SMatthew G. Knepley   Level: advanced
5277e5e52638SMatthew G. Knepley 
5278e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5279e5e52638SMatthew G. Knepley @*/
5280e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5281e5e52638SMatthew G. Knepley {
5282e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5283e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5284e5e52638SMatthew G. Knepley 
5285e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5286e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5287e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5288e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5289e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5290e5e52638SMatthew G. Knepley     DMLabel label;
5291b3cf3223SMatthew G. Knepley     IS      fields;
5292e5e52638SMatthew G. Knepley     PetscDS ds;
5293e5e52638SMatthew G. Knepley 
5294b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5295b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5296e5e52638SMatthew G. Knepley   }
5297e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5298e5e52638SMatthew G. Knepley }
5299e5e52638SMatthew G. Knepley 
5300e5e52638SMatthew G. Knepley /*@
5301e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5302e5e52638SMatthew G. Knepley 
5303d083f849SBarry Smith   Collective on dm
5304e5e52638SMatthew G. Knepley 
5305e5e52638SMatthew G. Knepley   Input Parameter:
5306e5e52638SMatthew G. Knepley . dm - The DM
5307e5e52638SMatthew G. Knepley 
5308e5e52638SMatthew G. Knepley   Output Parameter:
5309e5e52638SMatthew G. Knepley . newdm - The DM
5310e5e52638SMatthew G. Knepley 
5311e5e52638SMatthew G. Knepley   Level: advanced
5312e5e52638SMatthew G. Knepley 
5313e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5314e5e52638SMatthew G. Knepley @*/
5315e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5316e5e52638SMatthew G. Knepley {
5317e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5318e5e52638SMatthew G. Knepley 
5319e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5320e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5321e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5322e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5323e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5324e5e52638SMatthew G. Knepley }
5325e5e52638SMatthew G. Knepley 
5326b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5327b64e0483SPeter Brune {
5328b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5329b64e0483SPeter Brune   PetscErrorCode ierr;
5330b64e0483SPeter Brune   Vec coords,ccoords;
53316dbf9973SLawrence Mitchell   Mat inject;
5332b64e0483SPeter Brune   PetscFunctionBegin;
5333b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5334b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5335b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5336b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5337b64e0483SPeter Brune   if (coords && !ccoords) {
5338b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
53396668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
53406dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
53412adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
53426dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5343b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5344b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5345b64e0483SPeter Brune   }
5346b64e0483SPeter Brune   PetscFunctionReturn(0);
5347b64e0483SPeter Brune }
5348b64e0483SPeter Brune 
534903dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
535003dadc2fSPeter Brune {
535103dadc2fSPeter Brune   DM dm_coord,subdm_coord;
535203dadc2fSPeter Brune   PetscErrorCode ierr;
535303dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
535403dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
535503dadc2fSPeter Brune   PetscFunctionBegin;
535603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
535703dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
535803dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
535903dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
536003dadc2fSPeter Brune   if (coords && !ccoords) {
536103dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
53626668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
536303dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
536424640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
536503dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
536603dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
536703dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
53681ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
536903dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
537003dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
537103dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
537203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
537303dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
537403dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
537503dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
537603dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
537703dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
537803dadc2fSPeter Brune   }
537903dadc2fSPeter Brune   PetscFunctionReturn(0);
538003dadc2fSPeter Brune }
538103dadc2fSPeter Brune 
5382c73cfb54SMatthew G. Knepley /*@
5383c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5384c73cfb54SMatthew G. Knepley 
5385c73cfb54SMatthew G. Knepley   Not collective
5386c73cfb54SMatthew G. Knepley 
5387c73cfb54SMatthew G. Knepley   Input Parameter:
5388c73cfb54SMatthew G. Knepley . dm - The DM
5389c73cfb54SMatthew G. Knepley 
5390c73cfb54SMatthew G. Knepley   Output Parameter:
5391c73cfb54SMatthew G. Knepley . dim - The topological dimension
5392c73cfb54SMatthew G. Knepley 
5393c73cfb54SMatthew G. Knepley   Level: beginner
5394c73cfb54SMatthew G. Knepley 
5395c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5396c73cfb54SMatthew G. Knepley @*/
5397c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5398c73cfb54SMatthew G. Knepley {
5399c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5400c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5401534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5402c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5403c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5404c73cfb54SMatthew G. Knepley }
5405c73cfb54SMatthew G. Knepley 
5406c73cfb54SMatthew G. Knepley /*@
5407c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5408c73cfb54SMatthew G. Knepley 
5409c73cfb54SMatthew G. Knepley   Collective on dm
5410c73cfb54SMatthew G. Knepley 
5411c73cfb54SMatthew G. Knepley   Input Parameters:
5412c73cfb54SMatthew G. Knepley + dm - The DM
5413c73cfb54SMatthew G. Knepley - dim - The topological dimension
5414c73cfb54SMatthew G. Knepley 
5415c73cfb54SMatthew G. Knepley   Level: beginner
5416c73cfb54SMatthew G. Knepley 
5417c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5418c73cfb54SMatthew G. Knepley @*/
5419c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5420c73cfb54SMatthew G. Knepley {
5421e5e52638SMatthew G. Knepley   PetscDS        ds;
5422f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5423f17e8794SMatthew G. Knepley 
5424c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5425c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5426c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5427c73cfb54SMatthew G. Knepley   dm->dim = dim;
5428e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5429e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5430c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5431c73cfb54SMatthew G. Knepley }
5432c73cfb54SMatthew G. Knepley 
5433793f3fe5SMatthew G. Knepley /*@
5434793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5435793f3fe5SMatthew G. Knepley 
5436d083f849SBarry Smith   Collective on dm
5437793f3fe5SMatthew G. Knepley 
5438793f3fe5SMatthew G. Knepley   Input Parameters:
5439793f3fe5SMatthew G. Knepley + dm - the DM
5440793f3fe5SMatthew G. Knepley - dim - the dimension
5441793f3fe5SMatthew G. Knepley 
5442793f3fe5SMatthew G. Knepley   Output Parameters:
5443793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5444aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5445793f3fe5SMatthew G. Knepley 
5446793f3fe5SMatthew G. Knepley   Note:
5447793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5448a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5449793f3fe5SMatthew G. Knepley   then the interval is empty.
5450793f3fe5SMatthew G. Knepley 
5451793f3fe5SMatthew G. Knepley   Level: intermediate
5452793f3fe5SMatthew G. Knepley 
5453793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5454793f3fe5SMatthew G. Knepley @*/
5455793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5456793f3fe5SMatthew G. Knepley {
5457793f3fe5SMatthew G. Knepley   PetscInt       d;
5458793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5459793f3fe5SMatthew G. Knepley 
5460793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5461793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5462793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5463793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5464b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5465793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5466793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5467793f3fe5SMatthew G. Knepley }
5468793f3fe5SMatthew G. Knepley 
54696636e97aSMatthew G Knepley /*@
54706636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
54716636e97aSMatthew G Knepley 
5472d083f849SBarry Smith   Collective on dm
54736636e97aSMatthew G Knepley 
54746636e97aSMatthew G Knepley   Input Parameters:
54756636e97aSMatthew G Knepley + dm - the DM
54766636e97aSMatthew G Knepley - c - coordinate vector
54776636e97aSMatthew G Knepley 
54782dd40e9bSPatrick Sanan   Notes:
54792dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
54802dd40e9bSPatrick Sanan 
54812dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
54826636e97aSMatthew G Knepley 
54836636e97aSMatthew G Knepley   Level: intermediate
54846636e97aSMatthew G Knepley 
54852dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
54866636e97aSMatthew G Knepley @*/
54876636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
54886636e97aSMatthew G Knepley {
54896636e97aSMatthew G Knepley   PetscErrorCode ierr;
54906636e97aSMatthew G Knepley 
54916636e97aSMatthew G Knepley   PetscFunctionBegin;
54926636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
54936636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
54946636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
54956636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
54966636e97aSMatthew G Knepley   dm->coordinates = c;
54976636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5498b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
549903dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
55006636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55016636e97aSMatthew G Knepley }
55026636e97aSMatthew G Knepley 
55036636e97aSMatthew G Knepley /*@
55046636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
55056636e97aSMatthew G Knepley 
55067058e716SVaclav Hapla   Not collective
55076636e97aSMatthew G Knepley 
55086636e97aSMatthew G Knepley    Input Parameters:
55096636e97aSMatthew G Knepley +  dm - the DM
55106636e97aSMatthew G Knepley -  c - coordinate vector
55116636e97aSMatthew G Knepley 
55122dd40e9bSPatrick Sanan   Notes:
55136636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
55146636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
55156636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
55166636e97aSMatthew G Knepley 
55172dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
55182dd40e9bSPatrick Sanan 
55196636e97aSMatthew G Knepley   Level: intermediate
55206636e97aSMatthew G Knepley 
55216636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
55226636e97aSMatthew G Knepley @*/
55236636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
55246636e97aSMatthew G Knepley {
55256636e97aSMatthew G Knepley   PetscErrorCode ierr;
55266636e97aSMatthew G Knepley 
55276636e97aSMatthew G Knepley   PetscFunctionBegin;
55286636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55296636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
55306636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
55316636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
55328865f1eaSKarl Rupp 
55336636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
55348865f1eaSKarl Rupp 
55356636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
55366636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55376636e97aSMatthew G Knepley }
55386636e97aSMatthew G Knepley 
55396636e97aSMatthew G Knepley /*@
55406636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
55416636e97aSMatthew G Knepley 
5542d083f849SBarry Smith   Collective on dm
55436636e97aSMatthew G Knepley 
55446636e97aSMatthew G Knepley   Input Parameter:
55456636e97aSMatthew G Knepley . dm - the DM
55466636e97aSMatthew G Knepley 
55476636e97aSMatthew G Knepley   Output Parameter:
55486636e97aSMatthew G Knepley . c - global coordinate vector
55496636e97aSMatthew G Knepley 
55506636e97aSMatthew G Knepley   Note:
55516636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
55526636e97aSMatthew G Knepley 
55536636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
55546636e97aSMatthew G Knepley 
55556636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
55566636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
55576636e97aSMatthew G Knepley 
55586636e97aSMatthew G Knepley   Level: intermediate
55596636e97aSMatthew G Knepley 
55606636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
55616636e97aSMatthew G Knepley @*/
55626636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
55636636e97aSMatthew G Knepley {
55646636e97aSMatthew G Knepley   PetscErrorCode ierr;
55656636e97aSMatthew G Knepley 
55666636e97aSMatthew G Knepley   PetscFunctionBegin;
55676636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
55686636e97aSMatthew G Knepley   PetscValidPointer(c,2);
55691f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
55700298fd71SBarry Smith     DM        cdm = NULL;
55711970a576SMatthew G. Knepley     PetscBool localized;
55726636e97aSMatthew G Knepley 
55736636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
55746636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
55751970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
55761970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
55771970a576SMatthew G. Knepley     if (localized) {
55781970a576SMatthew G. Knepley       PetscInt cdim;
55791970a576SMatthew G. Knepley 
55801970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
55811970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
55821970a576SMatthew G. Knepley     }
55836636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
55846636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55856636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
55866636e97aSMatthew G Knepley   }
55876636e97aSMatthew G Knepley   *c = dm->coordinates;
55886636e97aSMatthew G Knepley   PetscFunctionReturn(0);
55896636e97aSMatthew G Knepley }
55906636e97aSMatthew G Knepley 
55916636e97aSMatthew G Knepley /*@
559281e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
559381e9a530SVaclav Hapla 
5594d083f849SBarry Smith   Collective on dm
559581e9a530SVaclav Hapla 
559681e9a530SVaclav Hapla   Input Parameter:
559781e9a530SVaclav Hapla . dm - the DM
559881e9a530SVaclav Hapla 
559981e9a530SVaclav Hapla   Level: advanced
560081e9a530SVaclav Hapla 
560181e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
560281e9a530SVaclav Hapla @*/
560381e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
560481e9a530SVaclav Hapla {
560581e9a530SVaclav Hapla   PetscErrorCode ierr;
560681e9a530SVaclav Hapla 
560781e9a530SVaclav Hapla   PetscFunctionBegin;
560881e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
560981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
56101970a576SMatthew G. Knepley     DM        cdm = NULL;
56111970a576SMatthew G. Knepley     PetscBool localized;
56121970a576SMatthew G. Knepley 
561381e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
561481e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
56151970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
56161970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
56171970a576SMatthew G. Knepley     if (localized) {
56181970a576SMatthew G. Knepley       PetscInt cdim;
56191970a576SMatthew G. Knepley 
56201970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
56211970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
56221970a576SMatthew G. Knepley     }
562381e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
562481e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
562581e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
562681e9a530SVaclav Hapla   }
562781e9a530SVaclav Hapla   PetscFunctionReturn(0);
562881e9a530SVaclav Hapla }
562981e9a530SVaclav Hapla 
563081e9a530SVaclav Hapla /*@
56316636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
56326636e97aSMatthew G Knepley 
5633d083f849SBarry Smith   Collective on dm
56346636e97aSMatthew G Knepley 
56356636e97aSMatthew G Knepley   Input Parameter:
56366636e97aSMatthew G Knepley . dm - the DM
56376636e97aSMatthew G Knepley 
56386636e97aSMatthew G Knepley   Output Parameter:
56396636e97aSMatthew G Knepley . c - coordinate vector
56406636e97aSMatthew G Knepley 
56416636e97aSMatthew G Knepley   Note:
56426636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
56436636e97aSMatthew G Knepley 
56446636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
56456636e97aSMatthew G Knepley 
56466636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
56476636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
56486636e97aSMatthew G Knepley 
56496636e97aSMatthew G Knepley   Level: intermediate
56506636e97aSMatthew G Knepley 
565181e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
56526636e97aSMatthew G Knepley @*/
56536636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
56546636e97aSMatthew G Knepley {
56556636e97aSMatthew G Knepley   PetscErrorCode ierr;
56566636e97aSMatthew G Knepley 
56576636e97aSMatthew G Knepley   PetscFunctionBegin;
56586636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
56596636e97aSMatthew G Knepley   PetscValidPointer(c,2);
566081e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
56616636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56626636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56636636e97aSMatthew G Knepley }
56646636e97aSMatthew G Knepley 
566581e9a530SVaclav Hapla /*@
566681e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
566781e9a530SVaclav Hapla 
566881e9a530SVaclav Hapla   Not collective
566981e9a530SVaclav Hapla 
567081e9a530SVaclav Hapla   Input Parameter:
567181e9a530SVaclav Hapla . dm - the DM
567281e9a530SVaclav Hapla 
567381e9a530SVaclav Hapla   Output Parameter:
567481e9a530SVaclav Hapla . c - coordinate vector
567581e9a530SVaclav Hapla 
567681e9a530SVaclav Hapla   Level: advanced
567781e9a530SVaclav Hapla 
567881e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
567981e9a530SVaclav Hapla @*/
568081e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
568181e9a530SVaclav Hapla {
568281e9a530SVaclav Hapla   PetscFunctionBegin;
568381e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
568481e9a530SVaclav Hapla   PetscValidPointer(c,2);
568581e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
56866636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
56876636e97aSMatthew G Knepley   PetscFunctionReturn(0);
56886636e97aSMatthew G Knepley }
56896636e97aSMatthew G Knepley 
56902db98f8dSVaclav Hapla /*@
56912db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
56922db98f8dSVaclav Hapla 
56932db98f8dSVaclav Hapla   Not collective
56942db98f8dSVaclav Hapla 
56952db98f8dSVaclav Hapla   Input Parameter:
56962db98f8dSVaclav Hapla + dm - the DM
56972db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
56982db98f8dSVaclav Hapla 
56992db98f8dSVaclav Hapla   Output Parameter:
57002db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
57012db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
57022db98f8dSVaclav Hapla 
57032db98f8dSVaclav Hapla   Note:
57042db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
57052db98f8dSVaclav Hapla 
57062db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
57072db98f8dSVaclav Hapla 
57082db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
57092db98f8dSVaclav Hapla 
57102db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
57112db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
57122db98f8dSVaclav Hapla 
57132db98f8dSVaclav Hapla   Level: advanced
57142db98f8dSVaclav Hapla 
57152db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
57162db98f8dSVaclav Hapla @*/
57172db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
57182db98f8dSVaclav Hapla {
57192db98f8dSVaclav Hapla   PetscSection        cs, newcs;
57202db98f8dSVaclav Hapla   Vec                 coords;
57212db98f8dSVaclav Hapla   const PetscScalar   *arr;
57222db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
57232db98f8dSVaclav Hapla   PetscInt            n;
57242db98f8dSVaclav Hapla   PetscErrorCode      ierr;
57252db98f8dSVaclav Hapla 
57262db98f8dSVaclav Hapla   PetscFunctionBegin;
57272db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
57282db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
57292db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
57302db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
57312db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
57321bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
57331bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
57342db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
57352db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
57362db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
57372db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
57382db98f8dSVaclav Hapla   if (pCoord) {
57392db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
57402db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
57412db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
57422db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
5743ad9ac99dSVaclav Hapla   } else {
5744ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
57452db98f8dSVaclav Hapla   }
5746ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
5747ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
57482db98f8dSVaclav Hapla   PetscFunctionReturn(0);
57492db98f8dSVaclav Hapla }
57502db98f8dSVaclav Hapla 
5751f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
5752f19dbd58SToby Isaac {
5753f19dbd58SToby Isaac   PetscErrorCode ierr;
5754f19dbd58SToby Isaac 
5755f19dbd58SToby Isaac   PetscFunctionBegin;
5756f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5757f19dbd58SToby Isaac   PetscValidPointer(field,2);
5758f19dbd58SToby Isaac   if (!dm->coordinateField) {
5759f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
5760f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
5761f19dbd58SToby Isaac     }
5762f19dbd58SToby Isaac   }
5763f19dbd58SToby Isaac   *field = dm->coordinateField;
5764f19dbd58SToby Isaac   PetscFunctionReturn(0);
5765f19dbd58SToby Isaac }
5766f19dbd58SToby Isaac 
5767f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
5768f19dbd58SToby Isaac {
5769f19dbd58SToby Isaac   PetscErrorCode ierr;
5770f19dbd58SToby Isaac 
5771f19dbd58SToby Isaac   PetscFunctionBegin;
5772f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5773f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
5774c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
5775f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
5776f19dbd58SToby Isaac   dm->coordinateField = field;
5777f19dbd58SToby Isaac   PetscFunctionReturn(0);
5778f19dbd58SToby Isaac }
5779f19dbd58SToby Isaac 
57806636e97aSMatthew G Knepley /*@
57811cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
57826636e97aSMatthew G Knepley 
5783d083f849SBarry Smith   Collective on dm
57846636e97aSMatthew G Knepley 
57856636e97aSMatthew G Knepley   Input Parameter:
57866636e97aSMatthew G Knepley . dm - the DM
57876636e97aSMatthew G Knepley 
57886636e97aSMatthew G Knepley   Output Parameter:
57896636e97aSMatthew G Knepley . cdm - coordinate DM
57906636e97aSMatthew G Knepley 
57916636e97aSMatthew G Knepley   Level: intermediate
57926636e97aSMatthew G Knepley 
57931cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
57946636e97aSMatthew G Knepley @*/
57956636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
57966636e97aSMatthew G Knepley {
57976636e97aSMatthew G Knepley   PetscErrorCode ierr;
57986636e97aSMatthew G Knepley 
57996636e97aSMatthew G Knepley   PetscFunctionBegin;
58006636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58016636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
58026636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
5803308f8a94SToby Isaac     DM cdm;
5804308f8a94SToby Isaac 
580582f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
5806308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
5807308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
5808308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
5809308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
5810308f8a94SToby Isaac     dm->coordinateDM = cdm;
58116636e97aSMatthew G Knepley   }
58126636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
58136636e97aSMatthew G Knepley   PetscFunctionReturn(0);
58146636e97aSMatthew G Knepley }
5815e87bb0d3SMatthew G Knepley 
58161cfe2091SMatthew G. Knepley /*@
58171cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
58181cfe2091SMatthew G. Knepley 
5819d083f849SBarry Smith   Logically Collective on dm
58201cfe2091SMatthew G. Knepley 
58211cfe2091SMatthew G. Knepley   Input Parameters:
58221cfe2091SMatthew G. Knepley + dm - the DM
58231cfe2091SMatthew G. Knepley - cdm - coordinate DM
58241cfe2091SMatthew G. Knepley 
58251cfe2091SMatthew G. Knepley   Level: intermediate
58261cfe2091SMatthew G. Knepley 
58271cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
58281cfe2091SMatthew G. Knepley @*/
58291cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
58301cfe2091SMatthew G. Knepley {
58311cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
58321cfe2091SMatthew G. Knepley 
58331cfe2091SMatthew G. Knepley   PetscFunctionBegin;
58341cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
58351cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
5836f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
58371cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
58381cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
58391cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
58401cfe2091SMatthew G. Knepley }
58411cfe2091SMatthew G. Knepley 
584246e270d4SMatthew G. Knepley /*@
584346e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
584446e270d4SMatthew G. Knepley 
584546e270d4SMatthew G. Knepley   Not Collective
584646e270d4SMatthew G. Knepley 
584746e270d4SMatthew G. Knepley   Input Parameter:
584846e270d4SMatthew G. Knepley . dm - The DM object
584946e270d4SMatthew G. Knepley 
585046e270d4SMatthew G. Knepley   Output Parameter:
585146e270d4SMatthew G. Knepley . dim - The embedding dimension
585246e270d4SMatthew G. Knepley 
585346e270d4SMatthew G. Knepley   Level: intermediate
585446e270d4SMatthew G. Knepley 
585592fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
585646e270d4SMatthew G. Knepley @*/
585746e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
585846e270d4SMatthew G. Knepley {
585946e270d4SMatthew G. Knepley   PetscFunctionBegin;
586046e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5861534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
58629a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
58639a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
58649a9a41abSToby Isaac   }
586546e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
586646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
586746e270d4SMatthew G. Knepley }
586846e270d4SMatthew G. Knepley 
586946e270d4SMatthew G. Knepley /*@
587046e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
587146e270d4SMatthew G. Knepley 
587246e270d4SMatthew G. Knepley   Not Collective
587346e270d4SMatthew G. Knepley 
587446e270d4SMatthew G. Knepley   Input Parameters:
587546e270d4SMatthew G. Knepley + dm  - The DM object
587646e270d4SMatthew G. Knepley - dim - The embedding dimension
587746e270d4SMatthew G. Knepley 
587846e270d4SMatthew G. Knepley   Level: intermediate
587946e270d4SMatthew G. Knepley 
588092fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
588146e270d4SMatthew G. Knepley @*/
588246e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
588346e270d4SMatthew G. Knepley {
5884e5e52638SMatthew G. Knepley   PetscDS        ds;
5885f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5886f17e8794SMatthew G. Knepley 
588746e270d4SMatthew G. Knepley   PetscFunctionBegin;
588846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
588946e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
5890e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5891e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
589246e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
589346e270d4SMatthew G. Knepley }
589446e270d4SMatthew G. Knepley 
5895e8abe2deSMatthew G. Knepley /*@
5896e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
5897e8abe2deSMatthew G. Knepley 
5898d083f849SBarry Smith   Collective on dm
5899e8abe2deSMatthew G. Knepley 
5900e8abe2deSMatthew G. Knepley   Input Parameter:
5901e8abe2deSMatthew G. Knepley . dm - The DM object
5902e8abe2deSMatthew G. Knepley 
5903e8abe2deSMatthew G. Knepley   Output Parameter:
5904e8abe2deSMatthew G. Knepley . section - The PetscSection object
5905e8abe2deSMatthew G. Knepley 
5906e8abe2deSMatthew G. Knepley   Level: intermediate
5907e8abe2deSMatthew G. Knepley 
590892fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
5909e8abe2deSMatthew G. Knepley @*/
5910e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
5911e8abe2deSMatthew G. Knepley {
5912e8abe2deSMatthew G. Knepley   DM             cdm;
5913e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5914e8abe2deSMatthew G. Knepley 
5915e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5916e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5917e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
5918e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
591992fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
5920e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5921e8abe2deSMatthew G. Knepley }
5922e8abe2deSMatthew G. Knepley 
5923e8abe2deSMatthew G. Knepley /*@
5924e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
5925e8abe2deSMatthew G. Knepley 
5926e8abe2deSMatthew G. Knepley   Not Collective
5927e8abe2deSMatthew G. Knepley 
5928e8abe2deSMatthew G. Knepley   Input Parameters:
5929e8abe2deSMatthew G. Knepley + dm      - The DM object
593046e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
5931e8abe2deSMatthew G. Knepley - section - The PetscSection object
5932e8abe2deSMatthew G. Knepley 
5933e8abe2deSMatthew G. Knepley   Level: intermediate
5934e8abe2deSMatthew G. Knepley 
593592fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
5936e8abe2deSMatthew G. Knepley @*/
593746e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
5938e8abe2deSMatthew G. Knepley {
5939e8abe2deSMatthew G. Knepley   DM             cdm;
5940e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
5941e8abe2deSMatthew G. Knepley 
5942e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
5943e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
594446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
5945e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
594692fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
594746e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
59484c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
594946e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
595046e270d4SMatthew G. Knepley 
595146e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
595246e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
595346e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
595446e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
595546e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
595646e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
595746e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
595846e270d4SMatthew G. Knepley     }
5959ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
596046e270d4SMatthew G. Knepley   }
5961e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
5962e8abe2deSMatthew G. Knepley }
5963e8abe2deSMatthew G. Knepley 
59645dc8c3f7SMatthew G. Knepley /*@C
596590b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
59665dc8c3f7SMatthew G. Knepley 
59675dc8c3f7SMatthew G. Knepley   Input Parameters:
596890b157c4SStefano Zampini . dm      - The DM object
596990b157c4SStefano Zampini 
597090b157c4SStefano Zampini   Output Parameters:
597190b157c4SStefano Zampini + per     - Whether the DM is periodic or not
59725dc8c3f7SMatthew 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
59735dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59745dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
59755dc8c3f7SMatthew G. Knepley 
59765dc8c3f7SMatthew G. Knepley   Level: developer
59775dc8c3f7SMatthew G. Knepley 
59785dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
59795dc8c3f7SMatthew G. Knepley @*/
598090b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
5981c6b900c6SMatthew G. Knepley {
5982c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
5983c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
598490b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
5985c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
5986c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
59875dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
5988c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
5989c6b900c6SMatthew G. Knepley }
5990c6b900c6SMatthew G. Knepley 
59915dc8c3f7SMatthew G. Knepley /*@C
59925dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
59935dc8c3f7SMatthew G. Knepley 
59945dc8c3f7SMatthew G. Knepley   Input Parameters:
59955dc8c3f7SMatthew G. Knepley + dm      - The DM object
599690b157c4SStefano Zampini . per     - Whether the DM is periodic or not. If maxCell is not provided, coordinates need to be localized
59975dc8c3f7SMatthew 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
59985dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
59995dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
60005dc8c3f7SMatthew G. Knepley 
60015dc8c3f7SMatthew G. Knepley   Level: developer
60025dc8c3f7SMatthew G. Knepley 
60035dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
60045dc8c3f7SMatthew G. Knepley @*/
600590b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6006c6b900c6SMatthew G. Knepley {
6007c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6008c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6009c6b900c6SMatthew G. Knepley 
6010c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6011c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
601290b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
601390b157c4SStefano Zampini   if (maxCell) {
6014534a8f05SLisandro Dalcin     PetscValidRealPointer(maxCell,3);
6015534a8f05SLisandro Dalcin     PetscValidRealPointer(L,4);
601690b157c4SStefano Zampini     PetscValidPointer(bd,5);
601790b157c4SStefano Zampini   }
60185dc8c3f7SMatthew G. Knepley   ierr = PetscFree3(dm->L,dm->maxCell,dm->bdtype);CHKERRQ(ierr);
60195dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
602090b157c4SStefano Zampini   if (maxCell) {
60215dc8c3f7SMatthew G. Knepley     ierr = PetscMalloc3(dim,&dm->L,dim,&dm->maxCell,dim,&dm->bdtype);CHKERRQ(ierr);
60225dc8c3f7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {dm->L[d] = L[d]; dm->maxCell[d] = maxCell[d]; dm->bdtype[d] = bd[d];}
602390b157c4SStefano Zampini   }
6024072d7d67SStefano Zampini   dm->periodic = per;
6025c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6026c6b900c6SMatthew G. Knepley }
6027c6b900c6SMatthew G. Knepley 
60282e17dfb7SMatthew G. Knepley /*@
60292e17dfb7SMatthew 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.
60302e17dfb7SMatthew G. Knepley 
60312e17dfb7SMatthew G. Knepley   Input Parameters:
60322e17dfb7SMatthew G. Knepley + dm     - The DM
603365da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
603465da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
60352e17dfb7SMatthew G. Knepley 
60362e17dfb7SMatthew G. Knepley   Output Parameter:
60372e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60382e17dfb7SMatthew G. Knepley 
60392e17dfb7SMatthew G. Knepley   Level: developer
60402e17dfb7SMatthew G. Knepley 
60412e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60422e17dfb7SMatthew G. Knepley @*/
604365da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
60442e17dfb7SMatthew G. Knepley {
60452e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
60462e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
60472e17dfb7SMatthew G. Knepley 
60482e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60492e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
60502e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60512e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60522e17dfb7SMatthew G. Knepley   } else {
605365da65dcSMatthew G. Knepley     if (endpoint) {
605465da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6055da3333bfSMatthew 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)) {
6056da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
605765da65dcSMatthew G. Knepley         } else {
6058da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
605965da65dcSMatthew G. Knepley         }
606065da65dcSMatthew G. Knepley       }
606165da65dcSMatthew G. Knepley     } else {
60622e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
60631118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
60642e17dfb7SMatthew G. Knepley       }
60652e17dfb7SMatthew G. Knepley     }
606665da65dcSMatthew G. Knepley   }
60672e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
60682e17dfb7SMatthew G. Knepley }
60692e17dfb7SMatthew G. Knepley 
60702e17dfb7SMatthew G. Knepley /*
60712e17dfb7SMatthew 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.
60722e17dfb7SMatthew G. Knepley 
60732e17dfb7SMatthew G. Knepley   Input Parameters:
60742e17dfb7SMatthew G. Knepley + dm     - The DM
60752e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
60762e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
60772e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
60782e17dfb7SMatthew G. Knepley 
60792e17dfb7SMatthew G. Knepley   Output Parameter:
60802e17dfb7SMatthew G. Knepley . out - The localized coordinate point
60812e17dfb7SMatthew G. Knepley 
60822e17dfb7SMatthew G. Knepley   Level: developer
60832e17dfb7SMatthew G. Knepley 
60842e17dfb7SMatthew 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
60852e17dfb7SMatthew G. Knepley 
60862e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
60872e17dfb7SMatthew G. Knepley */
60882e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
60892e17dfb7SMatthew G. Knepley {
60902e17dfb7SMatthew G. Knepley   PetscInt d;
60912e17dfb7SMatthew G. Knepley 
60922e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
60932e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
60942e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
60952e17dfb7SMatthew G. Knepley   } else {
60962e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6097908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
60982e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
60992e17dfb7SMatthew G. Knepley       } else {
61002e17dfb7SMatthew G. Knepley         out[d] = in[d];
61012e17dfb7SMatthew G. Knepley       }
61022e17dfb7SMatthew G. Knepley     }
61032e17dfb7SMatthew G. Knepley   }
61042e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61052e17dfb7SMatthew G. Knepley }
61062e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
61072e17dfb7SMatthew G. Knepley {
61082e17dfb7SMatthew G. Knepley   PetscInt d;
61092e17dfb7SMatthew G. Knepley 
61102e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61112e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
61132e17dfb7SMatthew G. Knepley   } else {
61142e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6115908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
61162e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
61172e17dfb7SMatthew G. Knepley       } else {
61182e17dfb7SMatthew G. Knepley         out[d] = in[d];
61192e17dfb7SMatthew G. Knepley       }
61202e17dfb7SMatthew G. Knepley     }
61212e17dfb7SMatthew G. Knepley   }
61222e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61232e17dfb7SMatthew G. Knepley }
61242e17dfb7SMatthew G. Knepley 
61252e17dfb7SMatthew G. Knepley /*
61262e17dfb7SMatthew 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.
61272e17dfb7SMatthew G. Knepley 
61282e17dfb7SMatthew G. Knepley   Input Parameters:
61292e17dfb7SMatthew G. Knepley + dm     - The DM
61302e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
61312e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
61322e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
61332e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
61342e17dfb7SMatthew G. Knepley 
61352e17dfb7SMatthew G. Knepley   Output Parameter:
61362e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
61372e17dfb7SMatthew G. Knepley 
61382e17dfb7SMatthew G. Knepley   Level: developer
61392e17dfb7SMatthew G. Knepley 
61402e17dfb7SMatthew 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
61412e17dfb7SMatthew G. Knepley 
61422e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
61432e17dfb7SMatthew G. Knepley */
61442e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
61452e17dfb7SMatthew G. Knepley {
61462e17dfb7SMatthew G. Knepley   PetscInt d;
61472e17dfb7SMatthew G. Knepley 
61482e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
61492e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
61502e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
61512e17dfb7SMatthew G. Knepley   } else {
61522e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6153908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
61542e17dfb7SMatthew G. Knepley         out[d] += PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
61552e17dfb7SMatthew G. Knepley       } else {
61562e17dfb7SMatthew G. Knepley         out[d] += in[d];
61572e17dfb7SMatthew G. Knepley       }
61582e17dfb7SMatthew G. Knepley     }
61592e17dfb7SMatthew G. Knepley   }
61602e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
61612e17dfb7SMatthew G. Knepley }
61622e17dfb7SMatthew G. Knepley 
616336447a5eSToby Isaac /*@
61648f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
61658f700142SStefano Zampini 
61668f700142SStefano Zampini   Not collective
616736447a5eSToby Isaac 
616836447a5eSToby Isaac   Input Parameter:
616936447a5eSToby Isaac . dm - The DM
617036447a5eSToby Isaac 
617136447a5eSToby Isaac   Output Parameter:
617236447a5eSToby Isaac   areLocalized - True if localized
617336447a5eSToby Isaac 
617436447a5eSToby Isaac   Level: developer
617536447a5eSToby Isaac 
61768f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
617736447a5eSToby Isaac @*/
61788f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
617936447a5eSToby Isaac {
618036447a5eSToby Isaac   DM             cdm;
618136447a5eSToby Isaac   PetscSection   coordSection;
618246a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
618346a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
618436447a5eSToby Isaac   PetscErrorCode ierr;
618536447a5eSToby Isaac 
618636447a5eSToby Isaac   PetscFunctionBegin;
618736447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6188534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
61898b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
619046a3a80fSLisandro Dalcin 
619136447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
619236447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
619346a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
61949f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
619546a3a80fSLisandro Dalcin 
61969f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
619746a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
619836447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
619936447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
620046a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
620146a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
620236447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
620346a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
620436447a5eSToby Isaac   }
62058f700142SStefano Zampini   *areLocalized = alreadyLocalized;
620636447a5eSToby Isaac   PetscFunctionReturn(0);
620736447a5eSToby Isaac }
620836447a5eSToby Isaac 
62098f700142SStefano Zampini /*@
62108f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
62118f700142SStefano Zampini 
62128f700142SStefano Zampini   Collective on dm
62138f700142SStefano Zampini 
62148f700142SStefano Zampini   Input Parameter:
62158f700142SStefano Zampini . dm - The DM
62168f700142SStefano Zampini 
62178f700142SStefano Zampini   Output Parameter:
62188f700142SStefano Zampini   areLocalized - True if localized
62198f700142SStefano Zampini 
62208f700142SStefano Zampini   Level: developer
62218f700142SStefano Zampini 
62228f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
62238f700142SStefano Zampini @*/
62248f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
62258f700142SStefano Zampini {
62268f700142SStefano Zampini   PetscBool      localized;
62278f700142SStefano Zampini   PetscErrorCode ierr;
62288f700142SStefano Zampini 
62298f700142SStefano Zampini   PetscFunctionBegin;
62308f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6231534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
62328f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
62338f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
62348f700142SStefano Zampini   PetscFunctionReturn(0);
62358f700142SStefano Zampini }
623636447a5eSToby Isaac 
62372e17dfb7SMatthew G. Knepley /*@
6238492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
62392e17dfb7SMatthew G. Knepley 
62408f700142SStefano Zampini   Collective on dm
62418f700142SStefano Zampini 
62422e17dfb7SMatthew G. Knepley   Input Parameter:
62432e17dfb7SMatthew G. Knepley . dm - The DM
62442e17dfb7SMatthew G. Knepley 
62452e17dfb7SMatthew G. Knepley   Level: developer
62462e17dfb7SMatthew G. Knepley 
62478f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
62482e17dfb7SMatthew G. Knepley @*/
62492e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
62502e17dfb7SMatthew G. Knepley {
62512e17dfb7SMatthew G. Knepley   DM             cdm;
62522e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
62532e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
62543e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
62553e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6256e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
62573e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
62583e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
62592e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
62602e17dfb7SMatthew G. Knepley 
62612e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
62622e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
626392c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6264f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6265f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6266f7cbd40bSStefano Zampini 
62672e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
62682e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
62692e17dfb7SMatthew G. Knepley   {
62702e17dfb7SMatthew G. Knepley     PetscBool isplex;
62712e17dfb7SMatthew G. Knepley 
62722e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
62732e17dfb7SMatthew G. Knepley     if (isplex) {
62742e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
62753e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
627669291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
62773e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
62783e922f36SToby Isaac       newStart = vStart;
62793e922f36SToby Isaac       newEnd   = vEnd;
62803e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
62813e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
62823e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
62833e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
62843e922f36SToby Isaac       }
62852e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
62862e17dfb7SMatthew G. Knepley   }
62872e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
628843eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
62892e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
62903e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6291e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
62923e922f36SToby Isaac 
62932e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
62942e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
62952e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
62962e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
62973e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
62983e922f36SToby Isaac 
629969291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63003e922f36SToby Isaac   localized = &anchor[bs];
63013e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
63023e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63033e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63043e922f36SToby Isaac 
63053e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
63063e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
63073e922f36SToby Isaac       PetscInt     b;
63083e922f36SToby Isaac 
63093e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
63103e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63113e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63123e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
63133e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
63143e922f36SToby Isaac         for (b = 0; b < bs; b++) {
63153e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
63163e922f36SToby Isaac         }
63173e922f36SToby Isaac         if (b < bs) break;
63183e922f36SToby Isaac       }
63193e922f36SToby Isaac       if (d < dof/bs) {
63203e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
63213e922f36SToby Isaac           PetscInt cdof;
63223e922f36SToby Isaac 
63233e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
63243e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
63253e922f36SToby Isaac         }
63263e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
63273e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
63283e922f36SToby Isaac       }
63293e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63303e922f36SToby Isaac     }
63313e922f36SToby Isaac   }
63323e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
63333e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
633469291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
63353e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
633669291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
63373e922f36SToby Isaac     PetscFunctionReturn(0);
63383e922f36SToby Isaac   }
63392e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63402e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63412e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
63422e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
63432e17dfb7SMatthew G. Knepley   }
63442e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
63452e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6346c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
63472e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
63482e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
63492e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
63502e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6351c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63522e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
63532e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
63542e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
63552e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
63562e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
63572e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
63582e17dfb7SMatthew G. Knepley   }
63593e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
63603e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
63613e922f36SToby Isaac 
63622e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
63632e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
63643e922f36SToby Isaac       PetscInt     b, cdof;
63652e17dfb7SMatthew G. Knepley 
63663e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
63673e922f36SToby Isaac       if (!cdof) continue;
63682e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63692e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
63702e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
63712e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
63722e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
63732e17dfb7SMatthew G. Knepley     }
63743e922f36SToby Isaac   }
637569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
637669291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6377c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
63782e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
63792e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
63802e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
63812e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
63822e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
63832e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
63842e17dfb7SMatthew G. Knepley }
63852e17dfb7SMatthew G. Knepley 
6386e87bb0d3SMatthew G Knepley /*@
63873a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6388e87bb0d3SMatthew G Knepley 
6389d083f849SBarry Smith   Collective on v (see explanation below)
6390e87bb0d3SMatthew G Knepley 
6391e87bb0d3SMatthew G Knepley   Input Parameters:
6392e87bb0d3SMatthew G Knepley + dm - The DM
63933a93e3b7SToby Isaac . v - The Vec of points
639462a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
63953a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6396e87bb0d3SMatthew G Knepley 
639761e3bb9bSMatthew G Knepley   Output Parameter:
639862a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
639962a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
64003a93e3b7SToby Isaac 
6401e87bb0d3SMatthew G Knepley 
6402e87bb0d3SMatthew G Knepley   Level: developer
640361e3bb9bSMatthew G Knepley 
640462a38674SMatthew G. Knepley   Notes:
64053a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
640662a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
64073a93e3b7SToby Isaac 
64083a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
640962a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
64103a93e3b7SToby Isaac 
64113a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
64123a93e3b7SToby Isaac 
641362a38674SMatthew G. Knepley $    const PetscSFNode *cells;
641462a38674SMatthew G. Knepley $    PetscInt           nFound;
6415a6216909SToby Isaac $    const PetscInt    *found;
641662a38674SMatthew G. Knepley $
6417a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
64183a93e3b7SToby Isaac 
64193a93e3b7SToby 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
64203a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
64213a93e3b7SToby Isaac 
642262a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
642361e3bb9bSMatthew G Knepley @*/
642462a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6425e87bb0d3SMatthew G Knepley {
6426735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6427735aa83eSMatthew G Knepley 
6428e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6429e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6430e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6431e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
64323a93e3b7SToby Isaac   if (*cellSF) {
64333a93e3b7SToby Isaac     PetscMPIInt result;
64343a93e3b7SToby Isaac 
6435e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6436a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
64373a93e3b7SToby 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");
6438e0fc9d1bSMatthew G. Knepley   } else {
64393a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
64403a93e3b7SToby Isaac   }
6441b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
644247a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
644362a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
644447a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6445e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6446e87bb0d3SMatthew G Knepley }
644714f150ffSMatthew G. Knepley 
6448f4d763aaSMatthew G. Knepley /*@
6449f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6450f4d763aaSMatthew G. Knepley 
64518f700142SStefano Zampini   Collective on dm
64528f700142SStefano Zampini 
6453f4d763aaSMatthew G. Knepley   Input Parameter:
6454f4d763aaSMatthew G. Knepley . dm - The original DM
6455f4d763aaSMatthew G. Knepley 
6456f4d763aaSMatthew G. Knepley   Output Parameter:
6457f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6458f4d763aaSMatthew G. Knepley 
6459f4d763aaSMatthew G. Knepley   Level: intermediate
6460f4d763aaSMatthew G. Knepley 
6461e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6462f4d763aaSMatthew G. Knepley @*/
646314f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
646414f150ffSMatthew G. Knepley {
6465c26acbdeSMatthew G. Knepley   PetscSection   section;
64662d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
646714f150ffSMatthew G. Knepley   PetscErrorCode ierr;
646814f150ffSMatthew G. Knepley 
646914f150ffSMatthew G. Knepley   PetscFunctionBegin;
647014f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
647114f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
647292fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6473c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6474127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
64752d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6476c26acbdeSMatthew G. Knepley     *odm = dm;
6477c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6478c26acbdeSMatthew G. Knepley   }
647914f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6480c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
648114f150ffSMatthew G. Knepley     PetscSF      sf;
648214f150ffSMatthew G. Knepley 
648314f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6484e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
648514f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
648692fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
648714f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
648814f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
648915b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6490e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
649114f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
649214f150ffSMatthew G. Knepley   }
649314f150ffSMatthew G. Knepley   *odm = dm->dmBC;
649414f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
649514f150ffSMatthew G. Knepley }
6496f4d763aaSMatthew G. Knepley 
6497f4d763aaSMatthew G. Knepley /*@
6498cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6499f4d763aaSMatthew G. Knepley 
6500f4d763aaSMatthew G. Knepley   Input Parameter:
6501f4d763aaSMatthew G. Knepley . dm - The original DM
6502f4d763aaSMatthew G. Knepley 
6503cdb7a50dSMatthew G. Knepley   Output Parameters:
6504cdb7a50dSMatthew G. Knepley + num - The output sequence number
6505cdb7a50dSMatthew G. Knepley - val - The output sequence value
6506f4d763aaSMatthew G. Knepley 
6507f4d763aaSMatthew G. Knepley   Level: intermediate
6508f4d763aaSMatthew G. Knepley 
6509f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6510f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6511f4d763aaSMatthew G. Knepley 
6512f4d763aaSMatthew G. Knepley .seealso: VecView()
6513f4d763aaSMatthew G. Knepley @*/
6514cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
6515f4d763aaSMatthew G. Knepley {
6516f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6517f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6518534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
6519534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
6520f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6521f4d763aaSMatthew G. Knepley }
6522f4d763aaSMatthew G. Knepley 
6523f4d763aaSMatthew G. Knepley /*@
6524cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
6525f4d763aaSMatthew G. Knepley 
6526f4d763aaSMatthew G. Knepley   Input Parameters:
6527f4d763aaSMatthew G. Knepley + dm - The original DM
6528cdb7a50dSMatthew G. Knepley . num - The output sequence number
6529cdb7a50dSMatthew G. Knepley - val - The output sequence value
6530f4d763aaSMatthew G. Knepley 
6531f4d763aaSMatthew G. Knepley   Level: intermediate
6532f4d763aaSMatthew G. Knepley 
6533f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6534f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6535f4d763aaSMatthew G. Knepley 
6536f4d763aaSMatthew G. Knepley .seealso: VecView()
6537f4d763aaSMatthew G. Knepley @*/
6538cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
6539f4d763aaSMatthew G. Knepley {
6540f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
6541f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6542f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
6543cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
6544cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
6545cdb7a50dSMatthew G. Knepley }
6546cdb7a50dSMatthew G. Knepley 
6547cdb7a50dSMatthew G. Knepley /*@C
6548cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
6549cdb7a50dSMatthew G. Knepley 
6550cdb7a50dSMatthew G. Knepley   Input Parameters:
6551cdb7a50dSMatthew G. Knepley + dm   - The original DM
6552cdb7a50dSMatthew G. Knepley . name - The sequence name
6553cdb7a50dSMatthew G. Knepley - num  - The output sequence number
6554cdb7a50dSMatthew G. Knepley 
6555cdb7a50dSMatthew G. Knepley   Output Parameter:
6556cdb7a50dSMatthew G. Knepley . val  - The output sequence value
6557cdb7a50dSMatthew G. Knepley 
6558cdb7a50dSMatthew G. Knepley   Level: intermediate
6559cdb7a50dSMatthew G. Knepley 
6560cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
6561cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
6562cdb7a50dSMatthew G. Knepley 
6563cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
6564cdb7a50dSMatthew G. Knepley @*/
6565cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
6566cdb7a50dSMatthew G. Knepley {
6567cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
6568cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
6569cdb7a50dSMatthew G. Knepley 
6570cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
6571cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6572cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
6573534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
6574cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
6575cdb7a50dSMatthew G. Knepley   if (ishdf5) {
6576cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
6577cdb7a50dSMatthew G. Knepley     PetscScalar value;
6578cdb7a50dSMatthew G. Knepley 
657939d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
65804aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
6581cdb7a50dSMatthew G. Knepley #endif
6582cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
6583f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
6584f4d763aaSMatthew G. Knepley }
65858e4ac7eaSMatthew G. Knepley 
65868e4ac7eaSMatthew G. Knepley /*@
65878e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
65888e4ac7eaSMatthew G. Knepley 
65898e4ac7eaSMatthew G. Knepley   Not collective
65908e4ac7eaSMatthew G. Knepley 
65918e4ac7eaSMatthew G. Knepley   Input Parameter:
65928e4ac7eaSMatthew G. Knepley . dm - The DM
65938e4ac7eaSMatthew G. Knepley 
65948e4ac7eaSMatthew G. Knepley   Output Parameter:
65958e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
65968e4ac7eaSMatthew G. Knepley 
65978e4ac7eaSMatthew G. Knepley   Level: beginner
65988e4ac7eaSMatthew G. Knepley 
65998e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
66008e4ac7eaSMatthew G. Knepley @*/
66018e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
66028e4ac7eaSMatthew G. Knepley {
66038e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66048e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6605534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
66068e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
66078e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66088e4ac7eaSMatthew G. Knepley }
66098e4ac7eaSMatthew G. Knepley 
66108e4ac7eaSMatthew G. Knepley /*@
66115d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
66128e4ac7eaSMatthew G. Knepley 
66138e4ac7eaSMatthew G. Knepley   Collective on dm
66148e4ac7eaSMatthew G. Knepley 
66158e4ac7eaSMatthew G. Knepley   Input Parameters:
66168e4ac7eaSMatthew G. Knepley + dm - The DM
66178e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
66188e4ac7eaSMatthew G. Knepley 
66195d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
66205d3b26e6SMatthew G. Knepley 
66218e4ac7eaSMatthew G. Knepley   Level: beginner
66228e4ac7eaSMatthew G. Knepley 
66235d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
66248e4ac7eaSMatthew G. Knepley @*/
66258e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
66268e4ac7eaSMatthew G. Knepley {
66278e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
66288e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
66298833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
66308e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
66318e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
66328e4ac7eaSMatthew G. Knepley }
6633c58f1c22SToby Isaac 
6634c58f1c22SToby Isaac 
6635c58f1c22SToby Isaac /*@C
6636c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
6637c58f1c22SToby Isaac 
6638c58f1c22SToby Isaac   Not Collective
6639c58f1c22SToby Isaac 
6640c58f1c22SToby Isaac   Input Parameters:
6641c58f1c22SToby Isaac + dm   - The DM object
6642c58f1c22SToby Isaac - name - The label name
6643c58f1c22SToby Isaac 
6644c58f1c22SToby Isaac   Level: intermediate
6645c58f1c22SToby Isaac 
6646c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6647c58f1c22SToby Isaac @*/
6648c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
6649c58f1c22SToby Isaac {
66505d80c0bfSVaclav Hapla   PetscBool      flg;
66515d80c0bfSVaclav Hapla   DMLabel        label;
6652c58f1c22SToby Isaac   PetscErrorCode ierr;
6653c58f1c22SToby Isaac 
6654c58f1c22SToby Isaac   PetscFunctionBegin;
6655c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6656c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
66575d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
6658c58f1c22SToby Isaac   if (!flg) {
66595d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
66605d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
66615d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
6662c58f1c22SToby Isaac   }
6663c58f1c22SToby Isaac   PetscFunctionReturn(0);
6664c58f1c22SToby Isaac }
6665c58f1c22SToby Isaac 
6666c58f1c22SToby Isaac /*@C
6667c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
6668c58f1c22SToby Isaac 
6669c58f1c22SToby Isaac   Not Collective
6670c58f1c22SToby Isaac 
6671c58f1c22SToby Isaac   Input Parameters:
6672c58f1c22SToby Isaac + dm   - The DM object
6673c58f1c22SToby Isaac . name - The label name
6674c58f1c22SToby Isaac - point - The mesh point
6675c58f1c22SToby Isaac 
6676c58f1c22SToby Isaac   Output Parameter:
6677c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
6678c58f1c22SToby Isaac 
6679c58f1c22SToby Isaac   Level: beginner
6680c58f1c22SToby Isaac 
6681c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
6682c58f1c22SToby Isaac @*/
6683c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
6684c58f1c22SToby Isaac {
6685c58f1c22SToby Isaac   DMLabel        label;
6686c58f1c22SToby Isaac   PetscErrorCode ierr;
6687c58f1c22SToby Isaac 
6688c58f1c22SToby Isaac   PetscFunctionBegin;
6689c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6690c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6691c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
669213903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
6693c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
6694c58f1c22SToby Isaac   PetscFunctionReturn(0);
6695c58f1c22SToby Isaac }
6696c58f1c22SToby Isaac 
6697c58f1c22SToby Isaac /*@C
6698c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
6699c58f1c22SToby Isaac 
6700c58f1c22SToby Isaac   Not Collective
6701c58f1c22SToby Isaac 
6702c58f1c22SToby Isaac   Input Parameters:
6703c58f1c22SToby Isaac + dm   - The DM object
6704c58f1c22SToby Isaac . name - The label name
6705c58f1c22SToby Isaac . point - The mesh point
6706c58f1c22SToby Isaac - value - The label value for this point
6707c58f1c22SToby Isaac 
6708c58f1c22SToby Isaac   Output Parameter:
6709c58f1c22SToby Isaac 
6710c58f1c22SToby Isaac   Level: beginner
6711c58f1c22SToby Isaac 
6712c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
6713c58f1c22SToby Isaac @*/
6714c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6715c58f1c22SToby Isaac {
6716c58f1c22SToby Isaac   DMLabel        label;
6717c58f1c22SToby Isaac   PetscErrorCode ierr;
6718c58f1c22SToby Isaac 
6719c58f1c22SToby Isaac   PetscFunctionBegin;
6720c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6721c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6722c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6723c58f1c22SToby Isaac   if (!label) {
6724c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
6725c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6726c58f1c22SToby Isaac   }
6727c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
6728c58f1c22SToby Isaac   PetscFunctionReturn(0);
6729c58f1c22SToby Isaac }
6730c58f1c22SToby Isaac 
6731c58f1c22SToby Isaac /*@C
6732c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
6733c58f1c22SToby Isaac 
6734c58f1c22SToby Isaac   Not Collective
6735c58f1c22SToby Isaac 
6736c58f1c22SToby Isaac   Input Parameters:
6737c58f1c22SToby Isaac + dm   - The DM object
6738c58f1c22SToby Isaac . name - The label name
6739c58f1c22SToby Isaac . point - The mesh point
6740c58f1c22SToby Isaac - value - The label value for this point
6741c58f1c22SToby Isaac 
6742c58f1c22SToby Isaac   Output Parameter:
6743c58f1c22SToby Isaac 
6744c58f1c22SToby Isaac   Level: beginner
6745c58f1c22SToby Isaac 
6746c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
6747c58f1c22SToby Isaac @*/
6748c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
6749c58f1c22SToby Isaac {
6750c58f1c22SToby Isaac   DMLabel        label;
6751c58f1c22SToby Isaac   PetscErrorCode ierr;
6752c58f1c22SToby Isaac 
6753c58f1c22SToby Isaac   PetscFunctionBegin;
6754c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6755c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6756c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6757c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6758c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
6759c58f1c22SToby Isaac   PetscFunctionReturn(0);
6760c58f1c22SToby Isaac }
6761c58f1c22SToby Isaac 
6762c58f1c22SToby Isaac /*@C
6763c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
6764c58f1c22SToby Isaac 
6765c58f1c22SToby Isaac   Not Collective
6766c58f1c22SToby Isaac 
6767c58f1c22SToby Isaac   Input Parameters:
6768c58f1c22SToby Isaac + dm   - The DM object
6769c58f1c22SToby Isaac - name - The label name
6770c58f1c22SToby Isaac 
6771c58f1c22SToby Isaac   Output Parameter:
6772c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
6773c58f1c22SToby Isaac 
6774c58f1c22SToby Isaac   Level: beginner
6775c58f1c22SToby Isaac 
6776df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
6777c58f1c22SToby Isaac @*/
6778c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
6779c58f1c22SToby Isaac {
6780c58f1c22SToby Isaac   DMLabel        label;
6781c58f1c22SToby Isaac   PetscErrorCode ierr;
6782c58f1c22SToby Isaac 
6783c58f1c22SToby Isaac   PetscFunctionBegin;
6784c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6785c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6786534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
6787c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6788c58f1c22SToby Isaac   *size = 0;
6789c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6790c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
6791c58f1c22SToby Isaac   PetscFunctionReturn(0);
6792c58f1c22SToby Isaac }
6793c58f1c22SToby Isaac 
6794c58f1c22SToby Isaac /*@C
6795c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
6796c58f1c22SToby Isaac 
6797c58f1c22SToby Isaac   Not Collective
6798c58f1c22SToby Isaac 
6799c58f1c22SToby Isaac   Input Parameters:
6800c58f1c22SToby Isaac + mesh - The DM object
6801c58f1c22SToby Isaac - name - The label name
6802c58f1c22SToby Isaac 
6803c58f1c22SToby Isaac   Output Parameter:
6804c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
6805c58f1c22SToby Isaac 
6806c58f1c22SToby Isaac   Level: beginner
6807c58f1c22SToby Isaac 
6808c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
6809c58f1c22SToby Isaac @*/
6810c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
6811c58f1c22SToby Isaac {
6812c58f1c22SToby Isaac   DMLabel        label;
6813c58f1c22SToby Isaac   PetscErrorCode ierr;
6814c58f1c22SToby Isaac 
6815c58f1c22SToby Isaac   PetscFunctionBegin;
6816c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6817c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6818c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
6819c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6820c58f1c22SToby Isaac   *ids = NULL;
6821dab2e251SBlaise Bourdin  if (label) {
6822c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
6823dab2e251SBlaise Bourdin   } else {
6824dab2e251SBlaise Bourdin     /* returning an empty IS */
6825dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
6826dab2e251SBlaise Bourdin   }
6827c58f1c22SToby Isaac   PetscFunctionReturn(0);
6828c58f1c22SToby Isaac }
6829c58f1c22SToby Isaac 
6830c58f1c22SToby Isaac /*@C
6831c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
6832c58f1c22SToby Isaac 
6833c58f1c22SToby Isaac   Not Collective
6834c58f1c22SToby Isaac 
6835c58f1c22SToby Isaac   Input Parameters:
6836c58f1c22SToby Isaac + dm - The DM object
6837c58f1c22SToby Isaac . name - The label name
6838c58f1c22SToby Isaac - value - The stratum value
6839c58f1c22SToby Isaac 
6840c58f1c22SToby Isaac   Output Parameter:
6841c58f1c22SToby Isaac . size - The stratum size
6842c58f1c22SToby Isaac 
6843c58f1c22SToby Isaac   Level: beginner
6844c58f1c22SToby Isaac 
6845c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
6846c58f1c22SToby Isaac @*/
6847c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
6848c58f1c22SToby Isaac {
6849c58f1c22SToby Isaac   DMLabel        label;
6850c58f1c22SToby Isaac   PetscErrorCode ierr;
6851c58f1c22SToby Isaac 
6852c58f1c22SToby Isaac   PetscFunctionBegin;
6853c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6854c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6855534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
6856c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6857c58f1c22SToby Isaac   *size = 0;
6858c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6859c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
6860c58f1c22SToby Isaac   PetscFunctionReturn(0);
6861c58f1c22SToby Isaac }
6862c58f1c22SToby Isaac 
6863c58f1c22SToby Isaac /*@C
6864c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
6865c58f1c22SToby Isaac 
6866c58f1c22SToby Isaac   Not Collective
6867c58f1c22SToby Isaac 
6868c58f1c22SToby Isaac   Input Parameters:
6869c58f1c22SToby Isaac + dm - The DM object
6870c58f1c22SToby Isaac . name - The label name
6871c58f1c22SToby Isaac - value - The stratum value
6872c58f1c22SToby Isaac 
6873c58f1c22SToby Isaac   Output Parameter:
6874c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
6875c58f1c22SToby Isaac 
6876c58f1c22SToby Isaac   Level: beginner
6877c58f1c22SToby Isaac 
6878c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
6879c58f1c22SToby Isaac @*/
6880c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
6881c58f1c22SToby Isaac {
6882c58f1c22SToby Isaac   DMLabel        label;
6883c58f1c22SToby Isaac   PetscErrorCode ierr;
6884c58f1c22SToby Isaac 
6885c58f1c22SToby Isaac   PetscFunctionBegin;
6886c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6887c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6888c58f1c22SToby Isaac   PetscValidPointer(points, 4);
6889c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6890c58f1c22SToby Isaac   *points = NULL;
6891c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6892c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
6893c58f1c22SToby Isaac   PetscFunctionReturn(0);
6894c58f1c22SToby Isaac }
6895c58f1c22SToby Isaac 
68964de306b1SToby Isaac /*@C
68979044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
68984de306b1SToby Isaac 
68994de306b1SToby Isaac   Not Collective
69004de306b1SToby Isaac 
69014de306b1SToby Isaac   Input Parameters:
69024de306b1SToby Isaac + dm - The DM object
69034de306b1SToby Isaac . name - The label name
69044de306b1SToby Isaac . value - The stratum value
69054de306b1SToby Isaac - points - The stratum points
69064de306b1SToby Isaac 
69074de306b1SToby Isaac   Level: beginner
69084de306b1SToby Isaac 
69094de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
69104de306b1SToby Isaac @*/
69114de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
69124de306b1SToby Isaac {
69134de306b1SToby Isaac   DMLabel        label;
69144de306b1SToby Isaac   PetscErrorCode ierr;
69154de306b1SToby Isaac 
69164de306b1SToby Isaac   PetscFunctionBegin;
69174de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
69184de306b1SToby Isaac   PetscValidCharPointer(name, 2);
69194de306b1SToby Isaac   PetscValidPointer(points, 4);
69204de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
69214de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
69224de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
69234de306b1SToby Isaac   PetscFunctionReturn(0);
69244de306b1SToby Isaac }
69254de306b1SToby Isaac 
6926c58f1c22SToby Isaac /*@C
6927c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
6928c58f1c22SToby Isaac 
6929c58f1c22SToby Isaac   Not Collective
6930c58f1c22SToby Isaac 
6931c58f1c22SToby Isaac   Input Parameters:
6932c58f1c22SToby Isaac + dm   - The DM object
6933c58f1c22SToby Isaac . name - The label name
6934c58f1c22SToby Isaac - value - The label value for this point
6935c58f1c22SToby Isaac 
6936c58f1c22SToby Isaac   Output Parameter:
6937c58f1c22SToby Isaac 
6938c58f1c22SToby Isaac   Level: beginner
6939c58f1c22SToby Isaac 
6940c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
6941c58f1c22SToby Isaac @*/
6942c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
6943c58f1c22SToby Isaac {
6944c58f1c22SToby Isaac   DMLabel        label;
6945c58f1c22SToby Isaac   PetscErrorCode ierr;
6946c58f1c22SToby Isaac 
6947c58f1c22SToby Isaac   PetscFunctionBegin;
6948c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6949c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
6950c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
6951c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
6952c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
6953c58f1c22SToby Isaac   PetscFunctionReturn(0);
6954c58f1c22SToby Isaac }
6955c58f1c22SToby Isaac 
6956c58f1c22SToby Isaac /*@
6957c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
6958c58f1c22SToby Isaac 
6959c58f1c22SToby Isaac   Not Collective
6960c58f1c22SToby Isaac 
6961c58f1c22SToby Isaac   Input Parameter:
6962c58f1c22SToby Isaac . dm   - The DM object
6963c58f1c22SToby Isaac 
6964c58f1c22SToby Isaac   Output Parameter:
6965c58f1c22SToby Isaac . numLabels - the number of Labels
6966c58f1c22SToby Isaac 
6967c58f1c22SToby Isaac   Level: intermediate
6968c58f1c22SToby Isaac 
6969c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6970c58f1c22SToby Isaac @*/
6971c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
6972c58f1c22SToby Isaac {
69735d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
6974c58f1c22SToby Isaac   PetscInt  n    = 0;
6975c58f1c22SToby Isaac 
6976c58f1c22SToby Isaac   PetscFunctionBegin;
6977c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6978534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
6979c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
6980c58f1c22SToby Isaac   *numLabels = n;
6981c58f1c22SToby Isaac   PetscFunctionReturn(0);
6982c58f1c22SToby Isaac }
6983c58f1c22SToby Isaac 
6984c58f1c22SToby Isaac /*@C
6985c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
6986c58f1c22SToby Isaac 
6987c58f1c22SToby Isaac   Not Collective
6988c58f1c22SToby Isaac 
6989c58f1c22SToby Isaac   Input Parameters:
6990c58f1c22SToby Isaac + dm - The DM object
6991c58f1c22SToby Isaac - n  - the label number
6992c58f1c22SToby Isaac 
6993c58f1c22SToby Isaac   Output Parameter:
6994c58f1c22SToby Isaac . name - the label name
6995c58f1c22SToby Isaac 
6996c58f1c22SToby Isaac   Level: intermediate
6997c58f1c22SToby Isaac 
6998c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
6999c58f1c22SToby Isaac @*/
7000c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7001c58f1c22SToby Isaac {
70025d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7003c58f1c22SToby Isaac   PetscInt       l    = 0;
7004d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7005c58f1c22SToby Isaac 
7006c58f1c22SToby Isaac   PetscFunctionBegin;
7007c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7008c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7009c58f1c22SToby Isaac   while (next) {
7010c58f1c22SToby Isaac     if (l == n) {
7011d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7012c58f1c22SToby Isaac       PetscFunctionReturn(0);
7013c58f1c22SToby Isaac     }
7014c58f1c22SToby Isaac     ++l;
7015c58f1c22SToby Isaac     next = next->next;
7016c58f1c22SToby Isaac   }
7017c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7018c58f1c22SToby Isaac }
7019c58f1c22SToby Isaac 
7020c58f1c22SToby Isaac /*@C
7021c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7022c58f1c22SToby Isaac 
7023c58f1c22SToby Isaac   Not Collective
7024c58f1c22SToby Isaac 
7025c58f1c22SToby Isaac   Input Parameters:
7026c58f1c22SToby Isaac + dm   - The DM object
7027c58f1c22SToby Isaac - name - The label name
7028c58f1c22SToby Isaac 
7029c58f1c22SToby Isaac   Output Parameter:
7030c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7031c58f1c22SToby Isaac 
7032c58f1c22SToby Isaac   Level: intermediate
7033c58f1c22SToby Isaac 
7034c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7035c58f1c22SToby Isaac @*/
7036c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7037c58f1c22SToby Isaac {
70385d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7039d67d17b1SMatthew G. Knepley   const char    *lname;
7040c58f1c22SToby Isaac   PetscErrorCode ierr;
7041c58f1c22SToby Isaac 
7042c58f1c22SToby Isaac   PetscFunctionBegin;
7043c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7044c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7045534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7046c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7047c58f1c22SToby Isaac   while (next) {
7048d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7049d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7050c58f1c22SToby Isaac     if (*hasLabel) break;
7051c58f1c22SToby Isaac     next = next->next;
7052c58f1c22SToby Isaac   }
7053c58f1c22SToby Isaac   PetscFunctionReturn(0);
7054c58f1c22SToby Isaac }
7055c58f1c22SToby Isaac 
7056c58f1c22SToby Isaac /*@C
7057c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7058c58f1c22SToby Isaac 
7059c58f1c22SToby Isaac   Not Collective
7060c58f1c22SToby Isaac 
7061c58f1c22SToby Isaac   Input Parameters:
7062c58f1c22SToby Isaac + dm   - The DM object
7063c58f1c22SToby Isaac - name - The label name
7064c58f1c22SToby Isaac 
7065c58f1c22SToby Isaac   Output Parameter:
7066c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7067c58f1c22SToby Isaac 
7068c58f1c22SToby Isaac   Level: intermediate
7069c58f1c22SToby Isaac 
7070c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7071c58f1c22SToby Isaac @*/
7072c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7073c58f1c22SToby Isaac {
70745d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7075c58f1c22SToby Isaac   PetscBool      hasLabel;
7076d67d17b1SMatthew G. Knepley   const char    *lname;
7077c58f1c22SToby Isaac   PetscErrorCode ierr;
7078c58f1c22SToby Isaac 
7079c58f1c22SToby Isaac   PetscFunctionBegin;
7080c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7081c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7082c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7083c58f1c22SToby Isaac   *label = NULL;
7084c58f1c22SToby Isaac   while (next) {
7085d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7086d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7087c58f1c22SToby Isaac     if (hasLabel) {
7088c58f1c22SToby Isaac       *label = next->label;
7089c58f1c22SToby Isaac       break;
7090c58f1c22SToby Isaac     }
7091c58f1c22SToby Isaac     next = next->next;
7092c58f1c22SToby Isaac   }
7093c58f1c22SToby Isaac   PetscFunctionReturn(0);
7094c58f1c22SToby Isaac }
7095c58f1c22SToby Isaac 
7096c58f1c22SToby Isaac /*@C
7097c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7098c58f1c22SToby Isaac 
7099c58f1c22SToby Isaac   Not Collective
7100c58f1c22SToby Isaac 
7101c58f1c22SToby Isaac   Input Parameters:
7102c58f1c22SToby Isaac + dm - The DM object
7103c58f1c22SToby Isaac - n  - the label number
7104c58f1c22SToby Isaac 
7105c58f1c22SToby Isaac   Output Parameter:
7106c58f1c22SToby Isaac . label - the label
7107c58f1c22SToby Isaac 
7108c58f1c22SToby Isaac   Level: intermediate
7109c58f1c22SToby Isaac 
7110c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7111c58f1c22SToby Isaac @*/
7112c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7113c58f1c22SToby Isaac {
71145d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7115c58f1c22SToby Isaac   PetscInt    l    = 0;
7116c58f1c22SToby Isaac 
7117c58f1c22SToby Isaac   PetscFunctionBegin;
7118c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7119c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7120c58f1c22SToby Isaac   while (next) {
7121c58f1c22SToby Isaac     if (l == n) {
7122c58f1c22SToby Isaac       *label = next->label;
7123c58f1c22SToby Isaac       PetscFunctionReturn(0);
7124c58f1c22SToby Isaac     }
7125c58f1c22SToby Isaac     ++l;
7126c58f1c22SToby Isaac     next = next->next;
7127c58f1c22SToby Isaac   }
7128c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7129c58f1c22SToby Isaac }
7130c58f1c22SToby Isaac 
7131c58f1c22SToby Isaac /*@C
7132c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7133c58f1c22SToby Isaac 
7134c58f1c22SToby Isaac   Not Collective
7135c58f1c22SToby Isaac 
7136c58f1c22SToby Isaac   Input Parameters:
7137c58f1c22SToby Isaac + dm   - The DM object
7138c58f1c22SToby Isaac - label - The DMLabel
7139c58f1c22SToby Isaac 
7140c58f1c22SToby Isaac   Level: developer
7141c58f1c22SToby Isaac 
7142c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7143c58f1c22SToby Isaac @*/
7144c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7145c58f1c22SToby Isaac {
71465d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7147c58f1c22SToby Isaac   PetscBool      hasLabel;
7148d67d17b1SMatthew G. Knepley   const char    *lname;
71495d80c0bfSVaclav Hapla   PetscBool      flg;
7150c58f1c22SToby Isaac   PetscErrorCode ierr;
7151c58f1c22SToby Isaac 
7152c58f1c22SToby Isaac   PetscFunctionBegin;
7153c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7154d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7155d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7156d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7157c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7158c58f1c22SToby Isaac   tmpLabel->label  = label;
7159c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
71605d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
71615d80c0bfSVaclav Hapla   *p = tmpLabel;
716208f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
71635d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
71645d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7165c58f1c22SToby Isaac   PetscFunctionReturn(0);
7166c58f1c22SToby Isaac }
7167c58f1c22SToby Isaac 
7168c58f1c22SToby Isaac /*@C
7169e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7170c58f1c22SToby Isaac 
7171c58f1c22SToby Isaac   Not Collective
7172c58f1c22SToby Isaac 
7173c58f1c22SToby Isaac   Input Parameters:
7174c58f1c22SToby Isaac + dm   - The DM object
7175c58f1c22SToby Isaac - name - The label name
7176c58f1c22SToby Isaac 
7177c58f1c22SToby Isaac   Output Parameter:
7178c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7179c58f1c22SToby Isaac 
7180c58f1c22SToby Isaac   Level: developer
7181c58f1c22SToby Isaac 
7182e5472504SVaclav Hapla   Notes:
7183e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7184e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7185e5472504SVaclav Hapla 
7186e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7187e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7188e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7189e5472504SVaclav Hapla 
7190e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7191c58f1c22SToby Isaac @*/
7192c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7193c58f1c22SToby Isaac {
719495d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7195c58f1c22SToby Isaac   PetscBool      hasLabel;
7196d67d17b1SMatthew G. Knepley   const char    *lname;
7197c58f1c22SToby Isaac   PetscErrorCode ierr;
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac   PetscFunctionBegin;
7200c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7201e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7202e5472504SVaclav Hapla   if (label) {
7203e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7204c58f1c22SToby Isaac     *label = NULL;
7205e5472504SVaclav Hapla   }
72065d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
720795d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7208d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7209c58f1c22SToby Isaac     if (hasLabel) {
721095d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7211c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
721295d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
721395d578d6SVaclav Hapla       if (label) *label = link->label;
721495d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
721595d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7216c58f1c22SToby Isaac       break;
7217c58f1c22SToby Isaac     }
7218c58f1c22SToby Isaac   }
7219c58f1c22SToby Isaac   PetscFunctionReturn(0);
7220c58f1c22SToby Isaac }
7221c58f1c22SToby Isaac 
7222306894acSVaclav Hapla /*@
7223306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7224306894acSVaclav Hapla 
7225306894acSVaclav Hapla   Not Collective
7226306894acSVaclav Hapla 
7227306894acSVaclav Hapla   Input Parameters:
7228306894acSVaclav Hapla + dm   - The DM object
7229306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7230306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7231306894acSVaclav Hapla 
7232306894acSVaclav Hapla   Level: developer
7233306894acSVaclav Hapla 
7234306894acSVaclav Hapla   Notes:
7235306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7236306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7237306894acSVaclav Hapla   *label nullified.
7238306894acSVaclav Hapla 
7239306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7240306894acSVaclav Hapla @*/
7241306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7242306894acSVaclav Hapla {
724343e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7244306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7245306894acSVaclav Hapla   PetscErrorCode ierr;
7246306894acSVaclav Hapla 
7247306894acSVaclav Hapla   PetscFunctionBegin;
7248306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7249306894acSVaclav Hapla   PetscValidPointer(label, 2);
7250f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7251306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7252306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
72535d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
725443e45a93SVaclav Hapla     if (*label == link->label) {
7255306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
725643e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7257306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
725843e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
725943e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
726043e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7261306894acSVaclav Hapla       break;
7262306894acSVaclav Hapla     }
7263306894acSVaclav Hapla   }
7264306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7265306894acSVaclav Hapla   PetscFunctionReturn(0);
7266306894acSVaclav Hapla }
7267306894acSVaclav Hapla 
7268c58f1c22SToby Isaac /*@C
7269c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7270c58f1c22SToby Isaac 
7271c58f1c22SToby Isaac   Not Collective
7272c58f1c22SToby Isaac 
7273c58f1c22SToby Isaac   Input Parameters:
7274c58f1c22SToby Isaac + dm   - The DM object
7275c58f1c22SToby Isaac - name - The label name
7276c58f1c22SToby Isaac 
7277c58f1c22SToby Isaac   Output Parameter:
7278c58f1c22SToby Isaac . output - The flag for output
7279c58f1c22SToby Isaac 
7280c58f1c22SToby Isaac   Level: developer
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7283c58f1c22SToby Isaac @*/
7284c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7285c58f1c22SToby Isaac {
72865d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7287d67d17b1SMatthew G. Knepley   const char    *lname;
7288c58f1c22SToby Isaac   PetscErrorCode ierr;
7289c58f1c22SToby Isaac 
7290c58f1c22SToby Isaac   PetscFunctionBegin;
7291c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7292c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7293c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7294c58f1c22SToby Isaac   while (next) {
7295c58f1c22SToby Isaac     PetscBool flg;
7296c58f1c22SToby Isaac 
7297d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7298d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7299c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7300c58f1c22SToby Isaac     next = next->next;
7301c58f1c22SToby Isaac   }
7302c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7303c58f1c22SToby Isaac }
7304c58f1c22SToby Isaac 
7305c58f1c22SToby Isaac /*@C
7306c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7307c58f1c22SToby Isaac 
7308c58f1c22SToby Isaac   Not Collective
7309c58f1c22SToby Isaac 
7310c58f1c22SToby Isaac   Input Parameters:
7311c58f1c22SToby Isaac + dm     - The DM object
7312c58f1c22SToby Isaac . name   - The label name
7313c58f1c22SToby Isaac - output - The flag for output
7314c58f1c22SToby Isaac 
7315c58f1c22SToby Isaac   Level: developer
7316c58f1c22SToby Isaac 
7317c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7318c58f1c22SToby Isaac @*/
7319c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7320c58f1c22SToby Isaac {
73215d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7322d67d17b1SMatthew G. Knepley   const char    *lname;
7323c58f1c22SToby Isaac   PetscErrorCode ierr;
7324c58f1c22SToby Isaac 
7325c58f1c22SToby Isaac   PetscFunctionBegin;
7326c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7327534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7328c58f1c22SToby Isaac   while (next) {
7329c58f1c22SToby Isaac     PetscBool flg;
7330c58f1c22SToby Isaac 
7331d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7332d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7333c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7334c58f1c22SToby Isaac     next = next->next;
7335c58f1c22SToby Isaac   }
7336c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7337c58f1c22SToby Isaac }
7338c58f1c22SToby Isaac 
7339c58f1c22SToby Isaac /*@
7340c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7341c58f1c22SToby Isaac 
7342d083f849SBarry Smith   Collective on dmA
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac   Input Parameter:
73455d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
73462e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
73475d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
73485d80c0bfSVaclav Hapla - all  - Copy all labels including "depth" and "dim" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7349c58f1c22SToby Isaac 
7350c58f1c22SToby Isaac   Level: intermediate
7351c58f1c22SToby Isaac 
7352c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7353c58f1c22SToby Isaac 
73545d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7355c58f1c22SToby Isaac @*/
73565d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7357c58f1c22SToby Isaac {
7358c58f1c22SToby Isaac   DMLabel        label, labelNew;
7359c58f1c22SToby Isaac   const char    *name;
7360c58f1c22SToby Isaac   PetscBool      flg;
73615d80c0bfSVaclav Hapla   DMLabelLink    link;
73625d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7363c58f1c22SToby Isaac 
73645d80c0bfSVaclav Hapla   PetscFunctionBegin;
73655d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
73665d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
73675d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
73685d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
73695d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
73705d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
73715d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
73725d80c0bfSVaclav Hapla     label=link->label;
73735d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
73745d80c0bfSVaclav Hapla     if (!all) {
7375c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7376c58f1c22SToby Isaac       if (flg) continue;
73777d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
73787d5acc75SStefano Zampini       if (flg) continue;
73795d80c0bfSVaclav Hapla     } else {
73805d80c0bfSVaclav Hapla       dmB->depthLabel = dmA->depthLabel;
73815d80c0bfSVaclav Hapla     }
73825d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7383c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
73845d80c0bfSVaclav Hapla     } else {
73855d80c0bfSVaclav Hapla       labelNew = label;
73865d80c0bfSVaclav Hapla     }
7387c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
73885d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7389c58f1c22SToby Isaac   }
7390c58f1c22SToby Isaac   PetscFunctionReturn(0);
7391c58f1c22SToby Isaac }
7392a8fb8f29SToby Isaac 
7393a8fb8f29SToby Isaac /*@
7394a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7395a8fb8f29SToby Isaac 
7396a8fb8f29SToby Isaac   Input Parameter:
7397a8fb8f29SToby Isaac . dm - The DM object
7398a8fb8f29SToby Isaac 
7399a8fb8f29SToby Isaac   Output Parameter:
7400a8fb8f29SToby Isaac . cdm - The coarse DM
7401a8fb8f29SToby Isaac 
7402a8fb8f29SToby Isaac   Level: intermediate
7403a8fb8f29SToby Isaac 
7404a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7405a8fb8f29SToby Isaac @*/
7406a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7407a8fb8f29SToby Isaac {
7408a8fb8f29SToby Isaac   PetscFunctionBegin;
7409a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7410a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7411a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7412a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7413a8fb8f29SToby Isaac }
7414a8fb8f29SToby Isaac 
7415a8fb8f29SToby Isaac /*@
7416a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7417a8fb8f29SToby Isaac 
7418a8fb8f29SToby Isaac   Input Parameters:
7419a8fb8f29SToby Isaac + dm - The DM object
7420a8fb8f29SToby Isaac - cdm - The coarse DM
7421a8fb8f29SToby Isaac 
7422a8fb8f29SToby Isaac   Level: intermediate
7423a8fb8f29SToby Isaac 
7424a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7425a8fb8f29SToby Isaac @*/
7426a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7427a8fb8f29SToby Isaac {
7428a8fb8f29SToby Isaac   PetscErrorCode ierr;
7429a8fb8f29SToby Isaac 
7430a8fb8f29SToby Isaac   PetscFunctionBegin;
7431a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7432a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7433a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7434a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7435a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7436a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7437a8fb8f29SToby Isaac }
7438a8fb8f29SToby Isaac 
743988bdff64SToby Isaac /*@
744088bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
744188bdff64SToby Isaac 
744288bdff64SToby Isaac   Input Parameter:
744388bdff64SToby Isaac . dm - The DM object
744488bdff64SToby Isaac 
744588bdff64SToby Isaac   Output Parameter:
744688bdff64SToby Isaac . fdm - The fine DM
744788bdff64SToby Isaac 
744888bdff64SToby Isaac   Level: intermediate
744988bdff64SToby Isaac 
745088bdff64SToby Isaac .seealso: DMSetFineDM()
745188bdff64SToby Isaac @*/
745288bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
745388bdff64SToby Isaac {
745488bdff64SToby Isaac   PetscFunctionBegin;
745588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
745688bdff64SToby Isaac   PetscValidPointer(fdm, 2);
745788bdff64SToby Isaac   *fdm = dm->fineMesh;
745888bdff64SToby Isaac   PetscFunctionReturn(0);
745988bdff64SToby Isaac }
746088bdff64SToby Isaac 
746188bdff64SToby Isaac /*@
746288bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
746388bdff64SToby Isaac 
746488bdff64SToby Isaac   Input Parameters:
746588bdff64SToby Isaac + dm - The DM object
746688bdff64SToby Isaac - fdm - The fine DM
746788bdff64SToby Isaac 
746888bdff64SToby Isaac   Level: intermediate
746988bdff64SToby Isaac 
747088bdff64SToby Isaac .seealso: DMGetFineDM()
747188bdff64SToby Isaac @*/
747288bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
747388bdff64SToby Isaac {
747488bdff64SToby Isaac   PetscErrorCode ierr;
747588bdff64SToby Isaac 
747688bdff64SToby Isaac   PetscFunctionBegin;
747788bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
747888bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
747988bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
748088bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
748188bdff64SToby Isaac   dm->fineMesh = fdm;
748288bdff64SToby Isaac   PetscFunctionReturn(0);
748388bdff64SToby Isaac }
748488bdff64SToby Isaac 
7485a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7486a6ba4734SToby Isaac 
7487a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7488a6ba4734SToby Isaac {
7489e5e52638SMatthew G. Knepley   PetscInt       d;
7490a6ba4734SToby Isaac   PetscErrorCode ierr;
7491a6ba4734SToby Isaac 
7492a6ba4734SToby Isaac   PetscFunctionBegin;
7493e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7494e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7495e5e52638SMatthew G. Knepley   }
7496a6ba4734SToby Isaac   PetscFunctionReturn(0);
7497a6ba4734SToby Isaac }
7498a6ba4734SToby Isaac 
7499a6ba4734SToby Isaac /*@C
7500a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
7501a6ba4734SToby Isaac 
7502a6ba4734SToby Isaac   Input Parameters:
75034c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
7504f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7505a6ba4734SToby Isaac . name        - The BC name
7506a6ba4734SToby Isaac . labelname   - The label defining constrained points
7507a6ba4734SToby Isaac . field       - The field to constrain
7508e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
7509a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7510a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7511a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7512a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7513a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7514a6ba4734SToby Isaac 
7515a6ba4734SToby Isaac   Options Database Keys:
7516a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7517a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7518a6ba4734SToby Isaac 
7519a6ba4734SToby Isaac   Level: developer
7520a6ba4734SToby Isaac 
7521a6ba4734SToby Isaac .seealso: DMGetBoundary()
7522a6ba4734SToby Isaac @*/
7523a30ec4eaSSatish 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)
7524a6ba4734SToby Isaac {
7525e5e52638SMatthew G. Knepley   PetscDS        ds;
7526a6ba4734SToby Isaac   PetscErrorCode ierr;
7527a6ba4734SToby Isaac 
7528a6ba4734SToby Isaac   PetscFunctionBegin;
7529a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7530e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7531e5e52638SMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, numids, ids, ctx);CHKERRQ(ierr);
7532a6ba4734SToby Isaac   PetscFunctionReturn(0);
7533a6ba4734SToby Isaac }
7534a6ba4734SToby Isaac 
7535a6ba4734SToby Isaac /*@
7536a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
7537a6ba4734SToby Isaac 
7538a6ba4734SToby Isaac   Input Parameters:
7539a6ba4734SToby Isaac . dm - The mesh object
7540a6ba4734SToby Isaac 
7541a6ba4734SToby Isaac   Output Parameters:
7542a6ba4734SToby Isaac . numBd - The number of BC
7543a6ba4734SToby Isaac 
7544a6ba4734SToby Isaac   Level: intermediate
7545a6ba4734SToby Isaac 
7546a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
7547a6ba4734SToby Isaac @*/
7548a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
7549a6ba4734SToby Isaac {
7550e5e52638SMatthew G. Knepley   PetscDS        ds;
755158ebd649SToby Isaac   PetscErrorCode ierr;
7552a6ba4734SToby Isaac 
7553a6ba4734SToby Isaac   PetscFunctionBegin;
7554a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7555e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7556e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
7557a6ba4734SToby Isaac   PetscFunctionReturn(0);
7558a6ba4734SToby Isaac }
7559a6ba4734SToby Isaac 
7560a6ba4734SToby Isaac /*@C
75611c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
7562a6ba4734SToby Isaac 
7563a6ba4734SToby Isaac   Input Parameters:
7564a6ba4734SToby Isaac + dm          - The mesh object
7565a6ba4734SToby Isaac - bd          - The BC number
7566a6ba4734SToby Isaac 
7567a6ba4734SToby Isaac   Output Parameters:
7568f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
7569a6ba4734SToby Isaac . name        - The BC name
7570a6ba4734SToby Isaac . labelname   - The label defining constrained points
7571a6ba4734SToby Isaac . field       - The field to constrain
7572a6ba4734SToby Isaac . numcomps    - The number of constrained field components
7573a6ba4734SToby Isaac . comps       - An array of constrained component numbers
7574a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
7575a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
7576a6ba4734SToby Isaac . ids         - An array of ids for constrained points
7577a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
7578a6ba4734SToby Isaac 
7579a6ba4734SToby Isaac   Options Database Keys:
7580a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
7581a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
7582a6ba4734SToby Isaac 
7583a6ba4734SToby Isaac   Level: developer
7584a6ba4734SToby Isaac 
7585a6ba4734SToby Isaac .seealso: DMAddBoundary()
7586a6ba4734SToby Isaac @*/
7587a30ec4eaSSatish 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)
7588a6ba4734SToby Isaac {
7589e5e52638SMatthew G. Knepley   PetscDS        ds;
759058ebd649SToby Isaac   PetscErrorCode ierr;
7591a6ba4734SToby Isaac 
7592a6ba4734SToby Isaac   PetscFunctionBegin;
7593a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7594e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7595e5e52638SMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, numids, ids, ctx);CHKERRQ(ierr);
7596a6ba4734SToby Isaac   PetscFunctionReturn(0);
7597a6ba4734SToby Isaac }
7598a6ba4734SToby Isaac 
7599e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
7600e6f8dbb6SToby Isaac {
7601e5e52638SMatthew G. Knepley   PetscDS        ds;
7602dff059c6SToby Isaac   DMBoundary    *lastnext;
7603e6f8dbb6SToby Isaac   DSBoundary     dsbound;
7604e6f8dbb6SToby Isaac   PetscErrorCode ierr;
7605e6f8dbb6SToby Isaac 
7606e6f8dbb6SToby Isaac   PetscFunctionBegin;
7607e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
7608e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
760947a1f5adSToby Isaac   if (dm->boundary) {
761047a1f5adSToby Isaac     DMBoundary next = dm->boundary;
761147a1f5adSToby Isaac 
761247a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
761347a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
761447a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
761547a1f5adSToby Isaac     while (next) {
761647a1f5adSToby Isaac       DMBoundary b = next;
761747a1f5adSToby Isaac 
761847a1f5adSToby Isaac       next = b->next;
761947a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
7620a6ba4734SToby Isaac     }
762147a1f5adSToby Isaac     dm->boundary = NULL;
7622a6ba4734SToby Isaac   }
762347a1f5adSToby Isaac 
7624dff059c6SToby Isaac   lastnext = &(dm->boundary);
7625e6f8dbb6SToby Isaac   while (dsbound) {
7626e6f8dbb6SToby Isaac     DMBoundary dmbound;
7627e6f8dbb6SToby Isaac 
7628e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
7629e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
7630e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
7631994fe344SLisandro 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);}
763247a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
7633dff059c6SToby Isaac     *lastnext = dmbound;
7634dff059c6SToby Isaac     lastnext = &(dmbound->next);
7635dff059c6SToby Isaac     dsbound = dsbound->next;
7636a6ba4734SToby Isaac   }
7637a6ba4734SToby Isaac   PetscFunctionReturn(0);
7638a6ba4734SToby Isaac }
7639a6ba4734SToby Isaac 
7640a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
7641a6ba4734SToby Isaac {
7642b95f2879SToby Isaac   DMBoundary     b;
7643a6ba4734SToby Isaac   PetscErrorCode ierr;
7644a6ba4734SToby Isaac 
7645a6ba4734SToby Isaac   PetscFunctionBegin;
7646a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7647534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
7648a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
7649e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
7650b95f2879SToby Isaac   b = dm->boundary;
7651a6ba4734SToby Isaac   while (b && !(*isBd)) {
7652e6f8dbb6SToby Isaac     DMLabel    label = b->label;
7653e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
76543424c85cSToby Isaac 
76553424c85cSToby Isaac     if (label) {
7656a6ba4734SToby Isaac       PetscInt i;
7657a6ba4734SToby Isaac 
7658e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
7659e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
7660a6ba4734SToby Isaac       }
7661a6ba4734SToby Isaac     }
7662a6ba4734SToby Isaac     b = b->next;
7663a6ba4734SToby Isaac   }
7664a6ba4734SToby Isaac   PetscFunctionReturn(0);
7665a6ba4734SToby Isaac }
76664d6f44ffSToby Isaac 
76674d6f44ffSToby Isaac /*@C
76684d6f44ffSToby Isaac   DMProjectFunction - This projects the given function into the function space provided.
76694d6f44ffSToby Isaac 
76704d6f44ffSToby Isaac   Input Parameters:
76714d6f44ffSToby Isaac + dm      - The DM
76720709b2feSToby Isaac . time    - The time
76734d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
76744d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
76754d6f44ffSToby Isaac - mode    - The insertion mode for values
76764d6f44ffSToby Isaac 
76774d6f44ffSToby Isaac   Output Parameter:
76784d6f44ffSToby Isaac . X - vector
76794d6f44ffSToby Isaac 
76804d6f44ffSToby Isaac    Calling sequence of func:
76810709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
76824d6f44ffSToby Isaac 
76834d6f44ffSToby Isaac +  dim - The spatial dimension
76844d6f44ffSToby Isaac .  x   - The coordinates
76854d6f44ffSToby Isaac .  Nf  - The number of fields
76864d6f44ffSToby Isaac .  u   - The output field values
76874d6f44ffSToby Isaac -  ctx - optional user-defined function context
76884d6f44ffSToby Isaac 
76894d6f44ffSToby Isaac   Level: developer
76904d6f44ffSToby Isaac 
76912716604bSToby Isaac .seealso: DMComputeL2Diff()
76924d6f44ffSToby Isaac @*/
76930709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
76944d6f44ffSToby Isaac {
76954d6f44ffSToby Isaac   Vec            localX;
76964d6f44ffSToby Isaac   PetscErrorCode ierr;
76974d6f44ffSToby Isaac 
76984d6f44ffSToby Isaac   PetscFunctionBegin;
76994d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77004d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77010709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77024d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77034d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77044d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77054d6f44ffSToby Isaac   PetscFunctionReturn(0);
77064d6f44ffSToby Isaac }
77074d6f44ffSToby Isaac 
77080709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
77094d6f44ffSToby Isaac {
77104d6f44ffSToby Isaac   PetscErrorCode ierr;
77114d6f44ffSToby Isaac 
77124d6f44ffSToby Isaac   PetscFunctionBegin;
77134d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77144d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
77150918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
77160709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77174d6f44ffSToby Isaac   PetscFunctionReturn(0);
77184d6f44ffSToby Isaac }
77194d6f44ffSToby Isaac 
77202c53366bSMatthew 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)
77212c53366bSMatthew G. Knepley {
77222c53366bSMatthew G. Knepley   Vec            localX;
77232c53366bSMatthew G. Knepley   PetscErrorCode ierr;
77242c53366bSMatthew G. Knepley 
77252c53366bSMatthew G. Knepley   PetscFunctionBegin;
77262c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
77272c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
77282c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77292c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
77302c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
77312c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
77322c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
77332c53366bSMatthew G. Knepley }
77342c53366bSMatthew G. Knepley 
77351c531cf8SMatthew 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)
77364d6f44ffSToby Isaac {
77374d6f44ffSToby Isaac   PetscErrorCode ierr;
77384d6f44ffSToby Isaac 
77394d6f44ffSToby Isaac   PetscFunctionBegin;
77404d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77414d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
77420918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
77431c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
77444d6f44ffSToby Isaac   PetscFunctionReturn(0);
77454d6f44ffSToby Isaac }
77462716604bSToby Isaac 
77478c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
77488c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
77498c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
77508c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7751191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
77528c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
77538c6c5593SMatthew G. Knepley {
77548c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
77558c6c5593SMatthew G. Knepley 
77568c6c5593SMatthew G. Knepley   PetscFunctionBegin;
77578c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77588c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
77598c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
77600918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
77618c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
77628c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
77638c6c5593SMatthew G. Knepley }
77648c6c5593SMatthew G. Knepley 
77651c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
77668c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
77678c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
77688c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
7769191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
77708c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
77718c6c5593SMatthew G. Knepley {
77728c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
77738c6c5593SMatthew G. Knepley 
77748c6c5593SMatthew G. Knepley   PetscFunctionBegin;
77758c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
77768c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
77778c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
77780918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
77791c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
77808c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
77818c6c5593SMatthew G. Knepley }
77828c6c5593SMatthew G. Knepley 
77832716604bSToby Isaac /*@C
77842716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
77852716604bSToby Isaac 
77862716604bSToby Isaac   Input Parameters:
77872716604bSToby Isaac + dm    - The DM
77880709b2feSToby Isaac . time  - The time
77892716604bSToby Isaac . funcs - The functions to evaluate for each field component
77902716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7791574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
77922716604bSToby Isaac 
77932716604bSToby Isaac   Output Parameter:
77942716604bSToby Isaac . diff - The diff ||u - u_h||_2
77952716604bSToby Isaac 
77962716604bSToby Isaac   Level: developer
77972716604bSToby Isaac 
77981189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
77992716604bSToby Isaac @*/
78000709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
78012716604bSToby Isaac {
78022716604bSToby Isaac   PetscErrorCode ierr;
78032716604bSToby Isaac 
78042716604bSToby Isaac   PetscFunctionBegin;
78052716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7806b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
78070918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
78080709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
78092716604bSToby Isaac   PetscFunctionReturn(0);
78102716604bSToby Isaac }
7811b698f381SToby Isaac 
7812b698f381SToby Isaac /*@C
7813b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
7814b698f381SToby Isaac 
7815d083f849SBarry Smith   Collective on dm
7816d083f849SBarry Smith 
7817b698f381SToby Isaac   Input Parameters:
7818b698f381SToby Isaac + dm    - The DM
7819b698f381SToby Isaac , time  - The time
7820b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
7821b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7822574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
7823b698f381SToby Isaac - n     - The vector to project along
7824b698f381SToby Isaac 
7825b698f381SToby Isaac   Output Parameter:
7826b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
7827b698f381SToby Isaac 
7828b698f381SToby Isaac   Level: developer
7829b698f381SToby Isaac 
7830b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
7831b698f381SToby Isaac @*/
7832b698f381SToby 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)
7833b698f381SToby Isaac {
7834b698f381SToby Isaac   PetscErrorCode ierr;
7835b698f381SToby Isaac 
7836b698f381SToby Isaac   PetscFunctionBegin;
7837b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7838b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
7839b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
7840b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
7841b698f381SToby Isaac   PetscFunctionReturn(0);
7842b698f381SToby Isaac }
7843b698f381SToby Isaac 
78442a16baeaSToby Isaac /*@C
78452a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
78462a16baeaSToby Isaac 
7847d083f849SBarry Smith   Collective on dm
7848d083f849SBarry Smith 
78492a16baeaSToby Isaac   Input Parameters:
78502a16baeaSToby Isaac + dm    - The DM
78512a16baeaSToby Isaac . time  - The time
78522a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
78532a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
7854574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
78552a16baeaSToby Isaac 
78562a16baeaSToby Isaac   Output Parameter:
78572a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
78582a16baeaSToby Isaac 
78592a16baeaSToby Isaac   Level: developer
78602a16baeaSToby Isaac 
78611189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
78622a16baeaSToby Isaac @*/
78631189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
78642a16baeaSToby Isaac {
78652a16baeaSToby Isaac   PetscErrorCode ierr;
78662a16baeaSToby Isaac 
78672a16baeaSToby Isaac   PetscFunctionBegin;
78682a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
78692a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
78700918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
78712a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
78722a16baeaSToby Isaac   PetscFunctionReturn(0);
78732a16baeaSToby Isaac }
78742a16baeaSToby Isaac 
7875df0b854cSToby Isaac /*@C
7876df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
7877cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
7878df0b854cSToby Isaac 
7879df0b854cSToby Isaac   Collective on dm
7880df0b854cSToby Isaac 
7881df0b854cSToby Isaac   Input parameters:
7882df0b854cSToby Isaac + dm - the pre-adaptation DM object
7883a1b0c543SToby Isaac - label - label with the flags
7884df0b854cSToby Isaac 
7885df0b854cSToby Isaac   Output parameters:
78860d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
7887df0b854cSToby Isaac 
7888df0b854cSToby Isaac   Level: intermediate
78890d1cd5e0SMatthew G. Knepley 
78900d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
7891df0b854cSToby Isaac @*/
78920d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
7893df0b854cSToby Isaac {
7894df0b854cSToby Isaac   PetscErrorCode ierr;
7895df0b854cSToby Isaac 
7896df0b854cSToby Isaac   PetscFunctionBegin;
7897df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7898a1b0c543SToby Isaac   PetscValidPointer(label,2);
78990d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
79000d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
79016f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
79020d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
79030d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
79040d1cd5e0SMatthew G. Knepley }
79050d1cd5e0SMatthew G. Knepley 
79060d1cd5e0SMatthew G. Knepley /*@C
79070d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
79080d1cd5e0SMatthew G. Knepley 
79090d1cd5e0SMatthew G. Knepley   Input Parameters:
79100d1cd5e0SMatthew G. Knepley + dm - The DM object
79110d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
79126f25b0d8SLisandro 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_".
79130d1cd5e0SMatthew G. Knepley 
79140d1cd5e0SMatthew G. Knepley   Output Parameter:
79150d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
79160d1cd5e0SMatthew G. Knepley 
79170d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
79180d1cd5e0SMatthew G. Knepley 
79190d1cd5e0SMatthew G. Knepley   Level: advanced
79200d1cd5e0SMatthew G. Knepley 
79210d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
79220d1cd5e0SMatthew G. Knepley @*/
79230d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
79240d1cd5e0SMatthew G. Knepley {
79250d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
79260d1cd5e0SMatthew G. Knepley 
79270d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
79280d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
79290d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
79300d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
79310d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
79326f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
79336f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
79340d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
7935df0b854cSToby Isaac   PetscFunctionReturn(0);
7936df0b854cSToby Isaac }
7937c4088d22SMatthew G. Knepley 
7938502a2867SDave May /*@C
7939502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
7940502a2867SDave May 
7941502a2867SDave May  Not Collective
7942502a2867SDave May 
7943502a2867SDave May  Input Parameter:
7944502a2867SDave May  . dm    - The DM
7945502a2867SDave May 
7946502a2867SDave May  Output Parameter:
7947502a2867SDave May  . nranks - the number of neighbours
7948502a2867SDave May  . ranks - the neighbors ranks
7949502a2867SDave May 
7950502a2867SDave May  Notes:
7951502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
7952502a2867SDave May 
7953502a2867SDave May  Level: beginner
7954502a2867SDave May 
7955dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
7956502a2867SDave May @*/
7957502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
7958502a2867SDave May {
7959502a2867SDave May   PetscErrorCode ierr;
7960502a2867SDave May 
7961502a2867SDave May   PetscFunctionBegin;
7962502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
79630918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
7964502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
7965502a2867SDave May   PetscFunctionReturn(0);
7966502a2867SDave May }
7967502a2867SDave May 
7968531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
7969531c7667SBarry Smith 
7970531c7667SBarry Smith /*
7971531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
7972531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
7973531c7667SBarry Smith */
7974531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
7975531c7667SBarry Smith {
7976531c7667SBarry Smith   PetscErrorCode ierr;
7977531c7667SBarry Smith 
7978531c7667SBarry Smith   PetscFunctionBegin;
7979531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7980531c7667SBarry Smith     Vec x1local;
7981531c7667SBarry Smith     DM  dm;
7982531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7983531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
7984531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
7985531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7986531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
7987531c7667SBarry Smith     x1   = x1local;
7988531c7667SBarry Smith   }
7989531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
7990531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
7991531c7667SBarry Smith     DM  dm;
7992531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
7993531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
7994531c7667SBarry Smith   }
7995531c7667SBarry Smith   PetscFunctionReturn(0);
7996531c7667SBarry Smith }
7997531c7667SBarry Smith 
7998531c7667SBarry Smith /*@
7999531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8000531c7667SBarry Smith 
8001531c7667SBarry Smith     Input Parameter:
8002531c7667SBarry Smith .    coloring - the MatFDColoring object
8003531c7667SBarry Smith 
800495452b02SPatrick Sanan     Developer Notes:
800595452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8006531c7667SBarry Smith 
80071b266c99SBarry Smith     Level: advanced
80081b266c99SBarry Smith 
8009531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8010531c7667SBarry Smith @*/
8011531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8012531c7667SBarry Smith {
8013531c7667SBarry Smith   PetscFunctionBegin;
8014531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8015531c7667SBarry Smith   PetscFunctionReturn(0);
8016531c7667SBarry Smith }
80178320bc6fSPatrick Sanan 
80188320bc6fSPatrick Sanan /*@
80198320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
80208320bc6fSPatrick Sanan 
80218320bc6fSPatrick Sanan     Collective
80228320bc6fSPatrick Sanan 
80238320bc6fSPatrick Sanan     Input Parameters:
80248320bc6fSPatrick Sanan +    dm - the first DM
80258320bc6fSPatrick Sanan -    dm2 - the second DM
80268320bc6fSPatrick Sanan 
80278320bc6fSPatrick Sanan     Output Parameters:
80288320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
80298320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
80308320bc6fSPatrick Sanan 
80318320bc6fSPatrick Sanan     Notes:
80328320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
80333d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
80348320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
80353d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
80363d862458SPatrick Sanan     decomposition, but hold different data.
80378320bc6fSPatrick Sanan 
80388320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
80398320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
80408320bc6fSPatrick Sanan 
80418320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
80428320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
80438320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
80448320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
80458320bc6fSPatrick Sanan 
80468320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
80478320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
80488320bc6fSPatrick Sanan .vb
80498320bc6fSPatrick Sanan   ...
80508320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
80518320bc6fSPatrick Sanan   if (set && compatible)  {
80528320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
80538320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
80543d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
80558320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
80568320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
80578320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
80588320bc6fSPatrick Sanan       }
80598320bc6fSPatrick Sanan     }
80608320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
80618320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
80628320bc6fSPatrick Sanan   } else {
80638320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
80648320bc6fSPatrick Sanan   }
80658320bc6fSPatrick Sanan   ...
80668320bc6fSPatrick Sanan .ve
80678320bc6fSPatrick Sanan 
80688320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
80698320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
80708320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
80718320bc6fSPatrick Sanan     always check the "set" output parameter.
80728320bc6fSPatrick Sanan 
80738320bc6fSPatrick Sanan     A DM is always compatible with itself.
80748320bc6fSPatrick Sanan 
80758320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
80768320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
80778320bc6fSPatrick Sanan     incompatible.
80788320bc6fSPatrick Sanan 
80798320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
80808320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
80818320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
80828320bc6fSPatrick Sanan 
80838320bc6fSPatrick Sanan     Developer Notes:
80843d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
80853d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
80868320bc6fSPatrick Sanan     of both dm and dm2 (if they are of different types), attempting to determine
80878320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
80888320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
80893d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
80903d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
80918320bc6fSPatrick Sanan 
80928320bc6fSPatrick Sanan     Level: advanced
80938320bc6fSPatrick Sanan 
80943d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
80958320bc6fSPatrick Sanan @*/
80968320bc6fSPatrick Sanan 
80978320bc6fSPatrick Sanan PetscErrorCode DMGetCompatibility(DM dm,DM dm2,PetscBool *compatible,PetscBool *set)
80988320bc6fSPatrick Sanan {
80998320bc6fSPatrick Sanan   PetscErrorCode ierr;
81008320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
81018320bc6fSPatrick Sanan   DMType         type,type2;
81028320bc6fSPatrick Sanan   PetscBool      sameType;
81038320bc6fSPatrick Sanan 
81048320bc6fSPatrick Sanan   PetscFunctionBegin;
81058320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
81068320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
81078320bc6fSPatrick Sanan 
81088320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
81098320bc6fSPatrick Sanan   if (dm == dm2) {
81108320bc6fSPatrick Sanan     *set = PETSC_TRUE;
81118320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
81128320bc6fSPatrick Sanan     PetscFunctionReturn(0);
81138320bc6fSPatrick Sanan   }
81148320bc6fSPatrick Sanan 
81158320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
81168320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
81178320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
81188320bc6fSPatrick Sanan      determined by the implementation-specific logic */
81198320bc6fSPatrick Sanan   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
81208320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
81218320bc6fSPatrick Sanan     *set = PETSC_TRUE;
81228320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
81238320bc6fSPatrick Sanan     PetscFunctionReturn(0);
81248320bc6fSPatrick Sanan   }
81258320bc6fSPatrick Sanan 
81268320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
81278320bc6fSPatrick Sanan   if (dm->ops->getcompatibility) {
81288320bc6fSPatrick Sanan     ierr = (*dm->ops->getcompatibility)(dm,dm2,compatible,set);CHKERRQ(ierr);
8129b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
81308320bc6fSPatrick Sanan   }
81318320bc6fSPatrick Sanan 
81328320bc6fSPatrick Sanan   /* If dm and dm2 are of different types, then attempt to check compatibility
81338320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
81348320bc6fSPatrick Sanan   ierr = DMGetType(dm,&type);CHKERRQ(ierr);
81358320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
81368320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
81378320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
81388320bc6fSPatrick Sanan     ierr = (*dm2->ops->getcompatibility)(dm2,dm,compatible,set);CHKERRQ(ierr); /* Note argument order */
81398320bc6fSPatrick Sanan   } else {
81408320bc6fSPatrick Sanan     *set = PETSC_FALSE;
81418320bc6fSPatrick Sanan   }
81428320bc6fSPatrick Sanan   PetscFunctionReturn(0);
81438320bc6fSPatrick Sanan }
8144