xref: /petsc/src/dm/interface/dm.c (revision da9060c4ebf05b549ae65df4db64c7bee7b2b8a9)
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 
900d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1000d952a4SJed Brown #  include <valgrind/memcheck.h>
1100d952a4SJed Brown #endif
1200d952a4SJed Brown 
13732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
14d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
15557cf195SMatthew G. Knepley PetscLogEvent DM_Convert, DM_GlobalToLocal, DM_LocalToGlobal, DM_LocalToLocal, DM_LocatePoints, DM_Coarsen, DM_Refine, DM_CreateInterpolation, DM_CreateRestriction, DM_CreateInjection, DM_CreateMatrix, DM_Load, DM_AdaptInterpolator;
1667a56275SMatthew G Knepley 
17ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_", NULL};
18ea78f98cSLisandro Dalcin const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_", NULL};
19*da9060c4SMatthew G. Knepley const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "pyramid", "FV_ghost_cell", "interior_ghost_cell", "unknown", "invalid", "DMPolytopeType", "DM_POLYTOPE_", NULL};
20a4121054SBarry Smith /*@
21de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
22a4121054SBarry Smith 
23a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
24a4121054SBarry Smith    error when you try to use the vector.
25a4121054SBarry Smith 
26d083f849SBarry Smith   Collective
27a4121054SBarry Smith 
28a4121054SBarry Smith   Input Parameter:
29a4121054SBarry Smith . comm - The communicator for the DM object
30a4121054SBarry Smith 
31a4121054SBarry Smith   Output Parameter:
32a4121054SBarry Smith . dm - The DM object
33a4121054SBarry Smith 
34a4121054SBarry Smith   Level: beginner
35a4121054SBarry Smith 
368472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
37a4121054SBarry Smith @*/
387087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
39a4121054SBarry Smith {
40a4121054SBarry Smith   DM             v;
41e5e52638SMatthew G. Knepley   PetscDS        ds;
42a4121054SBarry Smith   PetscErrorCode ierr;
43a4121054SBarry Smith 
44a4121054SBarry Smith   PetscFunctionBegin;
451411c6eeSJed Brown   PetscValidPointer(dm,2);
460298fd71SBarry Smith   *dm = NULL;
47607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
48a4121054SBarry Smith 
4973107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
50e7c4fc90SDmitry Karpeev 
5149be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5249be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
530298fd71SBarry Smith   v->ltogmap                  = NULL;
541411c6eeSJed Brown   v->bs                       = 1;
55171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5688ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
571bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
58c58f1c22SToby Isaac   v->labels                   = NULL;
5934aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6034aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
61c58f1c22SToby Isaac   v->depthLabel               = NULL;
62ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
631bb6d2a8SBarry Smith   v->localSection             = NULL;
641bb6d2a8SBarry Smith   v->globalSection            = NULL;
65fba222abSToby Isaac   v->defaultConstraintSection = NULL;
66fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
67c6b900c6SMatthew G. Knepley   v->L                        = NULL;
68c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
695dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
709a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7196173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
72435a35e8SMatthew G Knepley   {
73435a35e8SMatthew G Knepley     PetscInt i;
74435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
750298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
76f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
77435a35e8SMatthew G Knepley     }
78435a35e8SMatthew G Knepley   }
79e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
80b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
81e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
8214f150ffSMatthew G. Knepley   v->dmBC = NULL;
83a8fb8f29SToby Isaac   v->coarseMesh = NULL;
84f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
85cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
86c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
87b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
884a7a4c06SLawrence Mitchell 
891411c6eeSJed Brown   *dm = v;
90a4121054SBarry Smith   PetscFunctionReturn(0);
91a4121054SBarry Smith }
92a4121054SBarry Smith 
9338221697SMatthew G. Knepley /*@
9438221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9538221697SMatthew G. Knepley 
96d083f849SBarry Smith   Collective
9738221697SMatthew G. Knepley 
9838221697SMatthew G. Knepley   Input Parameter:
9938221697SMatthew G. Knepley . dm - The original DM object
10038221697SMatthew G. Knepley 
10138221697SMatthew G. Knepley   Output Parameter:
10238221697SMatthew G. Knepley . newdm  - The new DM object
10338221697SMatthew G. Knepley 
10438221697SMatthew G. Knepley   Level: beginner
10538221697SMatthew G. Knepley 
1061cb8cacdSPatrick Sanan   Notes:
1071cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1081cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1091cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1101bb6d2a8SBarry Smith 
11189706ed2SPatrick Sanan   The clone is considered set up iff the original is.
11289706ed2SPatrick Sanan 
11392cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1141bb6d2a8SBarry Smith 
11538221697SMatthew G. Knepley @*/
11638221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11738221697SMatthew G. Knepley {
11838221697SMatthew G. Knepley   PetscSF        sf;
11938221697SMatthew G. Knepley   Vec            coords;
12038221697SMatthew G. Knepley   void          *ctx;
121a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12238221697SMatthew G. Knepley   PetscErrorCode ierr;
12338221697SMatthew G. Knepley 
12438221697SMatthew G. Knepley   PetscFunctionBegin;
12538221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12638221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12738221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1285d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
129ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
130ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1311de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1321de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13338221697SMatthew G. Knepley   if (dm->ops->clone) {
13438221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13538221697SMatthew G. Knepley   }
1363f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13738221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13838221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
141be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
142be4c1c3eSMatthew G. Knepley     DM           ncdm;
143be4c1c3eSMatthew G. Knepley     PetscSection cs;
1445a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
145be4c1c3eSMatthew G. Knepley 
14692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
147be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1485a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1495a0206caSToby Isaac     if (pEndMax >= 0) {
150be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15183bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
15292fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
153a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
154be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
155be4c1c3eSMatthew G. Knepley     }
156be4c1c3eSMatthew G. Knepley   }
157a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
158a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
15938221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16038221697SMatthew G. Knepley   if (coords) {
16138221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16238221697SMatthew G. Knepley   } else {
16338221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16438221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16538221697SMatthew G. Knepley   }
16690b157c4SStefano Zampini   {
16790b157c4SStefano Zampini     PetscBool             isper;
168c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1695dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17090b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17190b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
172c6b900c6SMatthew G. Knepley   }
17334aa8a36SMatthew G. Knepley   {
17434aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17534aa8a36SMatthew G. Knepley 
17634aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17834aa8a36SMatthew G. Knepley   }
17938221697SMatthew G. Knepley   PetscFunctionReturn(0);
18038221697SMatthew G. Knepley }
18138221697SMatthew G. Knepley 
1829a42bb27SBarry Smith /*@C
183564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1849a42bb27SBarry Smith 
185d083f849SBarry Smith    Logically Collective on da
1869a42bb27SBarry Smith 
1879a42bb27SBarry Smith    Input Parameter:
1889a42bb27SBarry Smith +  da - initial distributed array
189e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1909a42bb27SBarry Smith 
1919a42bb27SBarry Smith    Options Database:
192dd85299cSBarry Smith .   -dm_vec_type ctype
1939a42bb27SBarry Smith 
1949a42bb27SBarry Smith    Level: intermediate
1959a42bb27SBarry Smith 
196a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1979a42bb27SBarry Smith @*/
19819fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
1999a42bb27SBarry Smith {
2009a42bb27SBarry Smith   PetscErrorCode ierr;
2019a42bb27SBarry Smith 
2029a42bb27SBarry Smith   PetscFunctionBegin;
2039a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2049a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20519fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2069a42bb27SBarry Smith   PetscFunctionReturn(0);
2079a42bb27SBarry Smith }
2089a42bb27SBarry Smith 
209c0dedaeaSBarry Smith /*@C
210c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
211c0dedaeaSBarry Smith 
212d083f849SBarry Smith    Logically Collective on da
213c0dedaeaSBarry Smith 
214c0dedaeaSBarry Smith    Input Parameter:
215c0dedaeaSBarry Smith .  da - initial distributed array
216c0dedaeaSBarry Smith 
217c0dedaeaSBarry Smith    Output Parameter:
218c0dedaeaSBarry Smith .  ctype - the vector type
219c0dedaeaSBarry Smith 
220c0dedaeaSBarry Smith    Level: intermediate
221c0dedaeaSBarry Smith 
222a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
223c0dedaeaSBarry Smith @*/
224c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
225c0dedaeaSBarry Smith {
226c0dedaeaSBarry Smith   PetscFunctionBegin;
227c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
228c0dedaeaSBarry Smith   *ctype = da->vectype;
229c0dedaeaSBarry Smith   PetscFunctionReturn(0);
230c0dedaeaSBarry Smith }
231c0dedaeaSBarry Smith 
2325f1ad066SMatthew G Knepley /*@
23334f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2345f1ad066SMatthew G Knepley 
2355f1ad066SMatthew G Knepley   Not collective
2365f1ad066SMatthew G Knepley 
2375f1ad066SMatthew G Knepley   Input Parameter:
2385f1ad066SMatthew G Knepley . v - The Vec
2395f1ad066SMatthew G Knepley 
2405f1ad066SMatthew G Knepley   Output Parameter:
2415f1ad066SMatthew G Knepley . dm - The DM
2425f1ad066SMatthew G Knepley 
2435f1ad066SMatthew G Knepley   Level: intermediate
2445f1ad066SMatthew G Knepley 
2455f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2465f1ad066SMatthew G Knepley @*/
2475f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2485f1ad066SMatthew G Knepley {
2495f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2505f1ad066SMatthew G Knepley 
2515f1ad066SMatthew G Knepley   PetscFunctionBegin;
2525f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2535f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2545f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2555f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2565f1ad066SMatthew G Knepley }
2575f1ad066SMatthew G Knepley 
2585f1ad066SMatthew G Knepley /*@
259d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2605f1ad066SMatthew G Knepley 
2615f1ad066SMatthew G Knepley   Not collective
2625f1ad066SMatthew G Knepley 
2635f1ad066SMatthew G Knepley   Input Parameters:
2645f1ad066SMatthew G Knepley + v - The Vec
2655f1ad066SMatthew G Knepley - dm - The DM
2665f1ad066SMatthew G Knepley 
267d9805387SMatthew 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.
268d9805387SMatthew G. Knepley 
2695f1ad066SMatthew G Knepley   Level: intermediate
2705f1ad066SMatthew G Knepley 
2715f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2725f1ad066SMatthew G Knepley @*/
2735f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2745f1ad066SMatthew G Knepley {
2755f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2765f1ad066SMatthew G Knepley 
2775f1ad066SMatthew G Knepley   PetscFunctionBegin;
2785f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
279d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2805f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2815f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2825f1ad066SMatthew G Knepley }
2835f1ad066SMatthew G Knepley 
284521d9a4cSLisandro Dalcin /*@C
2858f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2868f1509bcSBarry Smith 
287d083f849SBarry Smith    Logically Collective on dm
2888f1509bcSBarry Smith 
2898f1509bcSBarry Smith    Input Parameters:
2908f1509bcSBarry Smith +  dm - the DM context
2918f1509bcSBarry Smith -  ctype - the matrix type
2928f1509bcSBarry Smith 
2938f1509bcSBarry Smith    Options Database:
2948f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2958f1509bcSBarry Smith 
2968f1509bcSBarry Smith    Level: intermediate
2978f1509bcSBarry Smith 
2988f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
2998f1509bcSBarry Smith           DMGetISColoringType()
3008f1509bcSBarry Smith @*/
3018f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3028f1509bcSBarry Smith {
3038f1509bcSBarry Smith   PetscFunctionBegin;
3048f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3058f1509bcSBarry Smith   dm->coloringtype = ctype;
3068f1509bcSBarry Smith   PetscFunctionReturn(0);
3078f1509bcSBarry Smith }
3088f1509bcSBarry Smith 
3098f1509bcSBarry Smith /*@C
3108f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
311521d9a4cSLisandro Dalcin 
312d083f849SBarry Smith    Logically Collective on dm
313521d9a4cSLisandro Dalcin 
314521d9a4cSLisandro Dalcin    Input Parameter:
3158f1509bcSBarry Smith .  dm - the DM context
3168f1509bcSBarry Smith 
3178f1509bcSBarry Smith    Output Parameter:
3188f1509bcSBarry Smith .  ctype - the matrix type
3198f1509bcSBarry Smith 
3208f1509bcSBarry Smith    Options Database:
3218f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3228f1509bcSBarry Smith 
3238f1509bcSBarry Smith    Level: intermediate
3248f1509bcSBarry Smith 
3258f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3268f1509bcSBarry Smith           DMGetISColoringType()
3278f1509bcSBarry Smith @*/
3288f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3298f1509bcSBarry Smith {
3308f1509bcSBarry Smith   PetscFunctionBegin;
3318f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3328f1509bcSBarry Smith   *ctype = dm->coloringtype;
3338f1509bcSBarry Smith   PetscFunctionReturn(0);
3348f1509bcSBarry Smith }
3358f1509bcSBarry Smith 
3368f1509bcSBarry Smith /*@C
3378f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3388f1509bcSBarry Smith 
339d083f849SBarry Smith    Logically Collective on dm
3408f1509bcSBarry Smith 
3418f1509bcSBarry Smith    Input Parameters:
342521d9a4cSLisandro Dalcin +  dm - the DM context
343a2b5a043SBarry Smith -  ctype - the matrix type
344521d9a4cSLisandro Dalcin 
345521d9a4cSLisandro Dalcin    Options Database:
346521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
347521d9a4cSLisandro Dalcin 
348521d9a4cSLisandro Dalcin    Level: intermediate
349521d9a4cSLisandro Dalcin 
350a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
351521d9a4cSLisandro Dalcin @*/
35219fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
353521d9a4cSLisandro Dalcin {
354521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35588f0584fSBarry Smith 
356521d9a4cSLisandro Dalcin   PetscFunctionBegin;
357521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
358521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
35919fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
360521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
361521d9a4cSLisandro Dalcin }
362521d9a4cSLisandro Dalcin 
363c0dedaeaSBarry Smith /*@C
364c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
365c0dedaeaSBarry Smith 
366d083f849SBarry Smith    Logically Collective on dm
367c0dedaeaSBarry Smith 
368c0dedaeaSBarry Smith    Input Parameter:
369c0dedaeaSBarry Smith .  dm - the DM context
370c0dedaeaSBarry Smith 
371c0dedaeaSBarry Smith    Output Parameter:
372c0dedaeaSBarry Smith .  ctype - the matrix type
373c0dedaeaSBarry Smith 
374c0dedaeaSBarry Smith    Options Database:
375c0dedaeaSBarry Smith .   -dm_mat_type ctype
376c0dedaeaSBarry Smith 
377c0dedaeaSBarry Smith    Level: intermediate
378c0dedaeaSBarry Smith 
379a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
380c0dedaeaSBarry Smith @*/
381c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
382c0dedaeaSBarry Smith {
383c0dedaeaSBarry Smith   PetscFunctionBegin;
384c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
385c0dedaeaSBarry Smith   *ctype = dm->mattype;
386c0dedaeaSBarry Smith   PetscFunctionReturn(0);
387c0dedaeaSBarry Smith }
388c0dedaeaSBarry Smith 
389c688c046SMatthew G Knepley /*@
39034f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
391c688c046SMatthew G Knepley 
392c688c046SMatthew G Knepley   Not collective
393c688c046SMatthew G Knepley 
394c688c046SMatthew G Knepley   Input Parameter:
395c688c046SMatthew G Knepley . A - The Mat
396c688c046SMatthew G Knepley 
397c688c046SMatthew G Knepley   Output Parameter:
398c688c046SMatthew G Knepley . dm - The DM
399c688c046SMatthew G Knepley 
400c688c046SMatthew G Knepley   Level: intermediate
401c688c046SMatthew G Knepley 
4028f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4038f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4048f1509bcSBarry Smith 
405c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
406c688c046SMatthew G Knepley @*/
407c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
408c688c046SMatthew G Knepley {
409c688c046SMatthew G Knepley   PetscErrorCode ierr;
410c688c046SMatthew G Knepley 
411c688c046SMatthew G Knepley   PetscFunctionBegin;
412c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
413c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
414c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
415c688c046SMatthew G Knepley   PetscFunctionReturn(0);
416c688c046SMatthew G Knepley }
417c688c046SMatthew G Knepley 
418c688c046SMatthew G Knepley /*@
419c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
420c688c046SMatthew G Knepley 
421c688c046SMatthew G Knepley   Not collective
422c688c046SMatthew G Knepley 
423c688c046SMatthew G Knepley   Input Parameters:
424c688c046SMatthew G Knepley + A - The Mat
425c688c046SMatthew G Knepley - dm - The DM
426c688c046SMatthew G Knepley 
427c688c046SMatthew G Knepley   Level: intermediate
428c688c046SMatthew G Knepley 
4298f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4308f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4318f1509bcSBarry Smith 
4328f1509bcSBarry Smith 
433c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
434c688c046SMatthew G Knepley @*/
435c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
436c688c046SMatthew G Knepley {
437c688c046SMatthew G Knepley   PetscErrorCode ierr;
438c688c046SMatthew G Knepley 
439c688c046SMatthew G Knepley   PetscFunctionBegin;
440c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4418865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
442c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
443c688c046SMatthew G Knepley   PetscFunctionReturn(0);
444c688c046SMatthew G Knepley }
445c688c046SMatthew G Knepley 
4469a42bb27SBarry Smith /*@C
4479a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4486757b960SDave May    DM options in the database.
4499a42bb27SBarry Smith 
450d083f849SBarry Smith    Logically Collective on dm
4519a42bb27SBarry Smith 
4529a42bb27SBarry Smith    Input Parameter:
4538353ddbbSDave May +  da - the DM context
4549a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4559a42bb27SBarry Smith 
4569a42bb27SBarry Smith    Notes:
4579a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4589a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4599a42bb27SBarry Smith 
4609a42bb27SBarry Smith    Level: advanced
4619a42bb27SBarry Smith 
4629a42bb27SBarry Smith .seealso: DMSetFromOptions()
4639a42bb27SBarry Smith @*/
4647087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4659a42bb27SBarry Smith {
4669a42bb27SBarry Smith   PetscErrorCode ierr;
4679a42bb27SBarry Smith 
4689a42bb27SBarry Smith   PetscFunctionBegin;
4699a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4709a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
471691be533SLawrence Mitchell   if (dm->sf) {
472691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
473691be533SLawrence Mitchell   }
4741bb6d2a8SBarry Smith   if (dm->sectionSF) {
4751bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
476691be533SLawrence Mitchell   }
4779a42bb27SBarry Smith   PetscFunctionReturn(0);
4789a42bb27SBarry Smith }
4799a42bb27SBarry Smith 
48031697293SDave May /*@C
48131697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48231697293SDave May    DM options in the database.
48331697293SDave May 
484d083f849SBarry Smith    Logically Collective on dm
48531697293SDave May 
48631697293SDave May    Input Parameters:
48731697293SDave May +  dm - the DM context
48831697293SDave May -  prefix - the prefix string to prepend to all DM option requests
48931697293SDave May 
49031697293SDave May    Notes:
49131697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49231697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49331697293SDave May 
49431697293SDave May    Level: advanced
49531697293SDave May 
49631697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49731697293SDave May @*/
49831697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
49931697293SDave May {
50031697293SDave May   PetscErrorCode ierr;
50131697293SDave May 
50231697293SDave May   PetscFunctionBegin;
50331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50431697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50531697293SDave May   PetscFunctionReturn(0);
50631697293SDave May }
50731697293SDave May 
50831697293SDave May /*@C
50931697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51031697293SDave May    DM options in the database.
51131697293SDave May 
51231697293SDave May    Not Collective
51331697293SDave May 
51431697293SDave May    Input Parameters:
51531697293SDave May .  dm - the DM context
51631697293SDave May 
51731697293SDave May    Output Parameters:
51831697293SDave May .  prefix - pointer to the prefix string used is returned
51931697293SDave May 
52095452b02SPatrick Sanan    Notes:
52195452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52231697293SDave May    sufficient length to hold the prefix.
52331697293SDave May 
52431697293SDave May    Level: advanced
52531697293SDave May 
52631697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52731697293SDave May @*/
52831697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
52931697293SDave May {
53031697293SDave May   PetscErrorCode ierr;
53131697293SDave May 
53231697293SDave May   PetscFunctionBegin;
53331697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53431697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53531697293SDave May   PetscFunctionReturn(0);
53631697293SDave May }
53731697293SDave May 
53888bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
53988bdff64SToby Isaac {
5406eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
54188bdff64SToby Isaac   PetscErrorCode ierr;
54288bdff64SToby Isaac 
54388bdff64SToby Isaac   PetscFunctionBegin;
544aab5bcd8SJed Brown   *ncrefct = 0;
54588bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54688bdff64SToby Isaac     refct--;
54788bdff64SToby Isaac     if (recurseCoarse) {
54888bdff64SToby Isaac       PetscInt coarseCount;
54988bdff64SToby Isaac 
55088bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55188bdff64SToby Isaac       refct += coarseCount;
55288bdff64SToby Isaac     }
55388bdff64SToby Isaac   }
55488bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55588bdff64SToby Isaac     refct--;
55688bdff64SToby Isaac     if (recurseFine) {
55788bdff64SToby Isaac       PetscInt fineCount;
55888bdff64SToby Isaac 
55988bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56088bdff64SToby Isaac       refct += fineCount;
56188bdff64SToby Isaac     }
56288bdff64SToby Isaac   }
56388bdff64SToby Isaac   *ncrefct = refct;
56488bdff64SToby Isaac   PetscFunctionReturn(0);
56588bdff64SToby Isaac }
56688bdff64SToby Isaac 
567f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
568354557abSToby Isaac {
5695d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
570354557abSToby Isaac   PetscErrorCode ierr;
571354557abSToby Isaac 
572354557abSToby Isaac   PetscFunctionBegin;
573354557abSToby Isaac   /* destroy the labels */
574354557abSToby Isaac   while (next) {
575354557abSToby Isaac     DMLabelLink tmp = next->next;
576354557abSToby Isaac 
5775d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
578ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
579354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
580354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
581354557abSToby Isaac     next = tmp;
582354557abSToby Isaac   }
5835d80c0bfSVaclav Hapla   dm->labels = NULL;
584354557abSToby Isaac   PetscFunctionReturn(0);
585354557abSToby Isaac }
586354557abSToby Isaac 
58747c6ae99SBarry Smith /*@
5888472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
58947c6ae99SBarry Smith 
590d083f849SBarry Smith     Collective on dm
59147c6ae99SBarry Smith 
59247c6ae99SBarry Smith     Input Parameter:
59347c6ae99SBarry Smith .   dm - the DM object to destroy
59447c6ae99SBarry Smith 
59547c6ae99SBarry Smith     Level: developer
59647c6ae99SBarry Smith 
597e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59847c6ae99SBarry Smith 
59947c6ae99SBarry Smith @*/
600fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60147c6ae99SBarry Smith {
6026eb26441SStefano Zampini   PetscInt       cnt;
603dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60447c6ae99SBarry Smith   PetscErrorCode ierr;
60547c6ae99SBarry Smith 
60647c6ae99SBarry Smith   PetscFunctionBegin;
6076bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6086bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
60987e657c6SBarry Smith 
61088bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61188bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61288bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
613ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6146bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6156bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6166eb26441SStefano Zampini 
6176eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6186eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6196eb26441SStefano Zampini 
620f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6210298fd71SBarry Smith   (*dm)->namedglobal = NULL;
622f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6232348bcf4SPeter Brune     nnext = nlink->next;
6242348bcf4SPeter 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);
6252348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6262348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6272348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6282348bcf4SPeter Brune   }
629f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6300298fd71SBarry Smith   (*dm)->namedlocal = NULL;
631f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
632f490541aSPeter Brune     nnext = nlink->next;
633f490541aSPeter 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);
634f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
635f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
636f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
637f490541aSPeter Brune   }
6382348bcf4SPeter Brune 
639b17ce1afSJed Brown   /* Destroy the list of hooks */
640c833c3b5SJed Brown   {
641c833c3b5SJed Brown     DMCoarsenHookLink link,next;
642b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
643b17ce1afSJed Brown       next = link->next;
644b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
645b17ce1afSJed Brown     }
6460298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
647c833c3b5SJed Brown   }
648c833c3b5SJed Brown   {
649c833c3b5SJed Brown     DMRefineHookLink link,next;
650c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
651c833c3b5SJed Brown       next = link->next;
652c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
653c833c3b5SJed Brown     }
6540298fd71SBarry Smith     (*dm)->refinehook = NULL;
655c833c3b5SJed Brown   }
656be081cd6SPeter Brune   {
657be081cd6SPeter Brune     DMSubDomainHookLink link,next;
658be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
659be081cd6SPeter Brune       next = link->next;
660be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
661be081cd6SPeter Brune     }
6620298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
663be081cd6SPeter Brune   }
664baf369e7SPeter Brune   {
665baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
666baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
667baf369e7SPeter Brune       next = link->next;
668baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
669baf369e7SPeter Brune     }
6700298fd71SBarry Smith     (*dm)->gtolhook = NULL;
671baf369e7SPeter Brune   }
672d4d07f1eSToby Isaac   {
673d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
674d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
675d4d07f1eSToby Isaac       next = link->next;
676d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
677d4d07f1eSToby Isaac     }
678d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
679d4d07f1eSToby Isaac   }
680aa1993deSMatthew G Knepley   /* Destroy the work arrays */
681aa1993deSMatthew G Knepley   {
682aa1993deSMatthew G Knepley     DMWorkLink link,next;
683aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
684aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
685aa1993deSMatthew G Knepley       next = link->next;
686aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
687aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
688aa1993deSMatthew G Knepley     }
6890298fd71SBarry Smith     (*dm)->workin = NULL;
690aa1993deSMatthew G Knepley   }
691c58f1c22SToby Isaac   /* destroy the labels */
692f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
693f4cdcedcSVaclav Hapla   /* destroy the fields */
694e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
695f4cdcedcSVaclav Hapla   /* destroy the boundaries */
696e6f8dbb6SToby Isaac   {
697e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
698e6f8dbb6SToby Isaac     while (next) {
699e6f8dbb6SToby Isaac       DMBoundary b = next;
700e6f8dbb6SToby Isaac 
701e6f8dbb6SToby Isaac       next = b->next;
702e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
703e6f8dbb6SToby Isaac     }
704e6f8dbb6SToby Isaac   }
705b17ce1afSJed Brown 
70652536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
70852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
70952536dc3SBarry Smith 
7101a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7111a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7121a266240SBarry Smith   }
71371cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7146bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7156bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
716073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71788ed4aceSMatthew G Knepley 
7181bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7191bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7208b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
721fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
722fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
72388ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7241bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
725736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
726736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7278e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
728736995cdSBlaise Bourdin     }
729736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
730736995cdSBlaise Bourdin   }
73188bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
73288bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
73388bdff64SToby Isaac   }
7346eb26441SStefano Zampini 
735a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73688bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73788bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
73888bdff64SToby Isaac   }
73988bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
740f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7416636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7426636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7436636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
744412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
745412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
746412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
747ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
748ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
749ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7506636e97aSMatthew G Knepley 
751e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
75214f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
753e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
754e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
755732e2eb9SMatthew G Knepley 
756ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7576bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
758ed3c66a1SDave May   }
759c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
760435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
761732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
76247c6ae99SBarry Smith   PetscFunctionReturn(0);
76347c6ae99SBarry Smith }
76447c6ae99SBarry Smith 
765d7bf68aeSBarry Smith /*@
766d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
767d7bf68aeSBarry Smith 
768d083f849SBarry Smith     Collective on dm
769d7bf68aeSBarry Smith 
770d7bf68aeSBarry Smith     Input Parameter:
771d7bf68aeSBarry Smith .   dm - the DM object to setup
772d7bf68aeSBarry Smith 
773d7bf68aeSBarry Smith     Level: developer
774d7bf68aeSBarry Smith 
775e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
776d7bf68aeSBarry Smith 
777d7bf68aeSBarry Smith @*/
7787087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
779d7bf68aeSBarry Smith {
780d7bf68aeSBarry Smith   PetscErrorCode ierr;
781d7bf68aeSBarry Smith 
782d7bf68aeSBarry Smith   PetscFunctionBegin;
783171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7848387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
785d7bf68aeSBarry Smith   if (dm->ops->setup) {
786d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
787d7bf68aeSBarry Smith   }
7888387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
789d7bf68aeSBarry Smith   PetscFunctionReturn(0);
790d7bf68aeSBarry Smith }
791d7bf68aeSBarry Smith 
792d7bf68aeSBarry Smith /*@
793d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
794d7bf68aeSBarry Smith 
795d083f849SBarry Smith     Collective on dm
796d7bf68aeSBarry Smith 
797d7bf68aeSBarry Smith     Input Parameter:
798d7bf68aeSBarry Smith .   dm - the DM object to set options for
799d7bf68aeSBarry Smith 
800732e2eb9SMatthew G Knepley     Options Database:
8014164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8024164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8034164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8048f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
805732e2eb9SMatthew G Knepley 
806384a6580SVaclav Hapla     DMPLEX Specific Checks
807384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
808384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
809384a6580SVaclav Hapla .   -dm_plex_check_faces           - Check that the faces of each cell give a vertex order this is consistent with what we expect from the cell type - DMPlexCheckFaces()
810384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
811384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
812384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
813384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
814d7bf68aeSBarry Smith 
81595eb5ee5SVaclav Hapla     Level: intermediate
81695eb5ee5SVaclav Hapla 
81795eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
81895eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
819d7bf68aeSBarry Smith 
820d7bf68aeSBarry Smith @*/
8217087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
822d7bf68aeSBarry Smith {
8237781c08eSBarry Smith   char           typeName[256];
824ca266f36SBarry Smith   PetscBool      flg;
825d7bf68aeSBarry Smith   PetscErrorCode ierr;
826d7bf68aeSBarry Smith 
827d7bf68aeSBarry Smith   PetscFunctionBegin;
828171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82949be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
83049be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8311bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8323194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8330298fd71SBarry 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);
834a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
835f9ba7244SBarry Smith   if (flg) {
836f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
837f9ba7244SBarry Smith   }
838a264d7a6SBarry 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);
839073dac72SJed Brown   if (flg) {
840521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
841073dac72SJed Brown   }
8428f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
843f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
844e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
845f9ba7244SBarry Smith   }
846f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8470633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84882fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
849d7bf68aeSBarry Smith   PetscFunctionReturn(0);
850d7bf68aeSBarry Smith }
851d7bf68aeSBarry Smith 
852fc9bc008SSatish Balay /*@C
853fe2efc57SMark    DMViewFromOptions - View from Options
854fe2efc57SMark 
855fe2efc57SMark    Collective on DM
856fe2efc57SMark 
857fe2efc57SMark    Input Parameters:
858fe2efc57SMark +  dm - the DM object
859736c3998SJose E. Roman .  obj - Optional object
860736c3998SJose E. Roman -  name - command line option
861fe2efc57SMark 
862fe2efc57SMark    Level: intermediate
863fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
864fe2efc57SMark @*/
865fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
866fe2efc57SMark {
867fe2efc57SMark   PetscErrorCode ierr;
868fe2efc57SMark 
869fe2efc57SMark   PetscFunctionBegin;
870fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
871fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
872fe2efc57SMark   PetscFunctionReturn(0);
873fe2efc57SMark }
874fe2efc57SMark 
875fe2efc57SMark /*@C
876224748a4SBarry Smith     DMView - Views a DM
87747c6ae99SBarry Smith 
878d083f849SBarry Smith     Collective on dm
87947c6ae99SBarry Smith 
88047c6ae99SBarry Smith     Input Parameter:
88147c6ae99SBarry Smith +   dm - the DM object to view
88247c6ae99SBarry Smith -   v - the viewer
88347c6ae99SBarry Smith 
884224748a4SBarry Smith     Level: beginner
88547c6ae99SBarry Smith 
886e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88747c6ae99SBarry Smith 
88847c6ae99SBarry Smith @*/
8897087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
89047c6ae99SBarry Smith {
89147c6ae99SBarry Smith   PetscErrorCode    ierr;
89232c0f0efSBarry Smith   PetscBool         isbinary;
89376a8abe0SBarry Smith   PetscMPIInt       size;
89476a8abe0SBarry Smith   PetscViewerFormat format;
89547c6ae99SBarry Smith 
89647c6ae99SBarry Smith   PetscFunctionBegin;
897171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8983014e516SBarry Smith   if (!v) {
899ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9003014e516SBarry Smith   }
901b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
90274903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
90374903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90474903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90574903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90674903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90774903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90874903a4fSStefano Zampini      in an error here */
90974903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9108bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
911b1b135c8SBarry Smith 
91276a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
91376a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91476a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91598c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91632c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91732c0f0efSBarry Smith   if (isbinary) {
91855849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
91932c0f0efSBarry Smith     char     type[256];
92032c0f0efSBarry Smith 
921f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
92232c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
923f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
92432c0f0efSBarry Smith   }
9250c010503SBarry Smith   if (dm->ops->view) {
9260c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92747c6ae99SBarry Smith   }
92847c6ae99SBarry Smith   PetscFunctionReturn(0);
92947c6ae99SBarry Smith }
93047c6ae99SBarry Smith 
93147c6ae99SBarry Smith /*@
9328472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
93347c6ae99SBarry Smith 
934d083f849SBarry Smith     Collective on dm
93547c6ae99SBarry Smith 
93647c6ae99SBarry Smith     Input Parameter:
93747c6ae99SBarry Smith .   dm - the DM object
93847c6ae99SBarry Smith 
93947c6ae99SBarry Smith     Output Parameter:
94047c6ae99SBarry Smith .   vec - the global vector
94147c6ae99SBarry Smith 
942073dac72SJed Brown     Level: beginner
94347c6ae99SBarry Smith 
944e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94547c6ae99SBarry Smith 
94647c6ae99SBarry Smith @*/
9477087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94847c6ae99SBarry Smith {
94947c6ae99SBarry Smith   PetscErrorCode ierr;
95047c6ae99SBarry Smith 
95147c6ae99SBarry Smith   PetscFunctionBegin;
952171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
953b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
954b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95547c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
957c6b011d8SStefano Zampini     DM vdm;
958c6b011d8SStefano Zampini 
959c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
960c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
961c6b011d8SStefano Zampini   }
96247c6ae99SBarry Smith   PetscFunctionReturn(0);
96347c6ae99SBarry Smith }
96447c6ae99SBarry Smith 
96547c6ae99SBarry Smith /*@
9668472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
96747c6ae99SBarry Smith 
96847c6ae99SBarry Smith     Not Collective
96947c6ae99SBarry Smith 
97047c6ae99SBarry Smith     Input Parameter:
97147c6ae99SBarry Smith .   dm - the DM object
97247c6ae99SBarry Smith 
97347c6ae99SBarry Smith     Output Parameter:
97447c6ae99SBarry Smith .   vec - the local vector
97547c6ae99SBarry Smith 
976073dac72SJed Brown     Level: beginner
97747c6ae99SBarry Smith 
978e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
97947c6ae99SBarry Smith 
98047c6ae99SBarry Smith @*/
9817087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98247c6ae99SBarry Smith {
98347c6ae99SBarry Smith   PetscErrorCode ierr;
98447c6ae99SBarry Smith 
98547c6ae99SBarry Smith   PetscFunctionBegin;
986171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
987b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
988b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
98947c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99076bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
991c6b011d8SStefano Zampini     DM vdm;
992c6b011d8SStefano Zampini 
993c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
994c6b011d8SStefano Zampini     if (!vdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"DM type '%s' did not attach the DM to the vector\n",((PetscObject)dm)->type_name);
995c6b011d8SStefano Zampini   }
99647c6ae99SBarry Smith   PetscFunctionReturn(0);
99747c6ae99SBarry Smith }
99847c6ae99SBarry Smith 
9991411c6eeSJed Brown /*@
10001411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10011411c6eeSJed Brown 
1002d083f849SBarry Smith    Collective on dm
10031411c6eeSJed Brown 
10041411c6eeSJed Brown    Input Parameter:
10051411c6eeSJed Brown .  dm - the DM that provides the mapping
10061411c6eeSJed Brown 
10071411c6eeSJed Brown    Output Parameter:
10081411c6eeSJed Brown .  ltog - the mapping
10091411c6eeSJed Brown 
10101411c6eeSJed Brown    Level: intermediate
10111411c6eeSJed Brown 
10121411c6eeSJed Brown    Notes:
10131411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10141411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10151411c6eeSJed Brown 
1016fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10171411c6eeSJed Brown @*/
10187087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10191411c6eeSJed Brown {
10200be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10211411c6eeSJed Brown   PetscErrorCode ierr;
10221411c6eeSJed Brown 
10231411c6eeSJed Brown   PetscFunctionBegin;
10241411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10251411c6eeSJed Brown   PetscValidPointer(ltog,2);
10261411c6eeSJed Brown   if (!dm->ltogmap) {
102737d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102837d0c07bSMatthew G Knepley 
102992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
103037d0c07bSMatthew G Knepley     if (section) {
1031a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103237d0c07bSMatthew G Knepley       PetscInt       *ltog;
1033ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103437d0c07bSMatthew G Knepley 
1035e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103637d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1037ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1038ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
103937d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1040a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
104137d0c07bSMatthew G Knepley 
104237d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104337d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1044a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1045a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10471a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10481a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10491a7dc684SMatthew G. Knepley         if (dof) {
1050b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1051b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10521a7dc684SMatthew G. Knepley         }
105337d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1054a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1055a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105637d0c07bSMatthew G Knepley         }
105737d0c07bSMatthew G Knepley       }
1058bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10590be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10600be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10610be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10620be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10637591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10647591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1065ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1066ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1067ccf3bd66SMatthew G. Knepley         n /= bs;
1068ccf3bd66SMatthew G. Knepley       }
1069ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10703bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
107137d0c07bSMatthew G Knepley     } else {
1072b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1073184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10741411c6eeSJed Brown     }
107537d0c07bSMatthew G Knepley   }
10761411c6eeSJed Brown   *ltog = dm->ltogmap;
10771411c6eeSJed Brown   PetscFunctionReturn(0);
10781411c6eeSJed Brown }
10791411c6eeSJed Brown 
10801411c6eeSJed Brown /*@
10811411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10821411c6eeSJed Brown 
10831411c6eeSJed Brown    Not Collective
10841411c6eeSJed Brown 
10851411c6eeSJed Brown    Input Parameter:
10861411c6eeSJed Brown .  dm - the DM with block structure
10871411c6eeSJed Brown 
10881411c6eeSJed Brown    Output Parameter:
10891411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10901411c6eeSJed Brown 
10911411c6eeSJed Brown    Level: intermediate
10921411c6eeSJed Brown 
1093fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10941411c6eeSJed Brown @*/
10957087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10961411c6eeSJed Brown {
10971411c6eeSJed Brown   PetscFunctionBegin;
10981411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1099534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11001411c6eeSJed 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");
11011411c6eeSJed Brown   *bs = dm->bs;
11021411c6eeSJed Brown   PetscFunctionReturn(0);
11031411c6eeSJed Brown }
11041411c6eeSJed Brown 
110547c6ae99SBarry Smith /*@
11068472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110747c6ae99SBarry Smith 
1108a5bc1bf3SBarry Smith     Collective on dmc
110947c6ae99SBarry Smith 
111047c6ae99SBarry Smith     Input Parameter:
1111a5bc1bf3SBarry Smith +   dmc - the DM object
1112a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
111347c6ae99SBarry Smith 
111447c6ae99SBarry Smith     Output Parameter:
111547c6ae99SBarry Smith +  mat - the interpolation
111647c6ae99SBarry Smith -  vec - the scaling (optional)
111747c6ae99SBarry Smith 
111847c6ae99SBarry Smith     Level: developer
111947c6ae99SBarry Smith 
112095452b02SPatrick Sanan     Notes:
112195452b02SPatrick 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
112285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1123d52bd9f3SBarry Smith 
11241f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1125d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112685afcc9aSBarry Smith 
112785afcc9aSBarry Smith 
11282ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
112947c6ae99SBarry Smith 
113047c6ae99SBarry Smith @*/
1131a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
113247c6ae99SBarry Smith {
113347c6ae99SBarry Smith   PetscErrorCode ierr;
113447c6ae99SBarry Smith 
113547c6ae99SBarry Smith   PetscFunctionBegin;
1136a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1137a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1138c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1139a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1140a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1141a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1142a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
114347c6ae99SBarry Smith   PetscFunctionReturn(0);
114447c6ae99SBarry Smith }
114547c6ae99SBarry Smith 
11463ad4599aSBarry Smith /*@
1147d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11482ed6491fSPatrick Sanan 
11492ed6491fSPatrick Sanan   Input Parameters:
11502ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11512ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11522ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11532ed6491fSPatrick Sanan 
11542ed6491fSPatrick Sanan   Output Parameter:
11552ed6491fSPatrick Sanan .    scale - the scaled vector
11562ed6491fSPatrick Sanan 
11572ed6491fSPatrick Sanan   Level: developer
11582ed6491fSPatrick Sanan 
11592ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11602ed6491fSPatrick Sanan 
11612ed6491fSPatrick Sanan @*/
11622ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11632ed6491fSPatrick Sanan {
11642ed6491fSPatrick Sanan   PetscErrorCode ierr;
11652ed6491fSPatrick Sanan   Vec            fine;
11662ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11672ed6491fSPatrick Sanan 
11682ed6491fSPatrick Sanan   PetscFunctionBegin;
11692ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11702ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11712ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11722ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11732ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11742ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11752ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11762ed6491fSPatrick Sanan }
11772ed6491fSPatrick Sanan 
11782ed6491fSPatrick Sanan /*@
11793ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11803ad4599aSBarry Smith 
1181a5bc1bf3SBarry Smith     Collective on dmc
11823ad4599aSBarry Smith 
11833ad4599aSBarry Smith     Input Parameter:
1184a5bc1bf3SBarry Smith +   dmc - the DM object
1185a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11863ad4599aSBarry Smith 
11873ad4599aSBarry Smith     Output Parameter:
11883ad4599aSBarry Smith .  mat - the restriction
11893ad4599aSBarry Smith 
11903ad4599aSBarry Smith 
11913ad4599aSBarry Smith     Level: developer
11923ad4599aSBarry Smith 
119395452b02SPatrick Sanan     Notes:
119495452b02SPatrick 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
11953ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11963ad4599aSBarry Smith 
11973ad4599aSBarry Smith 
11983ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
11993ad4599aSBarry Smith 
12003ad4599aSBarry Smith @*/
1201a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12023ad4599aSBarry Smith {
12033ad4599aSBarry Smith   PetscErrorCode ierr;
12043ad4599aSBarry Smith 
12053ad4599aSBarry Smith   PetscFunctionBegin;
1206a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1207a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12085a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1209a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1210a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1211a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1212a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12133ad4599aSBarry Smith   PetscFunctionReturn(0);
12143ad4599aSBarry Smith }
12153ad4599aSBarry Smith 
121647c6ae99SBarry Smith /*@
12178472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
121847c6ae99SBarry Smith 
1219a5bc1bf3SBarry Smith     Collective on dac
122047c6ae99SBarry Smith 
122147c6ae99SBarry Smith     Input Parameter:
1222a5bc1bf3SBarry Smith +   dac - the DM object
1223a5bc1bf3SBarry Smith -   daf - the second, finer DM object
122447c6ae99SBarry Smith 
122547c6ae99SBarry Smith     Output Parameter:
12266dbf9973SLawrence Mitchell .   mat - the injection
122747c6ae99SBarry Smith 
122847c6ae99SBarry Smith     Level: developer
122947c6ae99SBarry Smith 
123095452b02SPatrick Sanan    Notes:
123195452b02SPatrick 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
123285afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123385afcc9aSBarry Smith 
1234e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123547c6ae99SBarry Smith 
123647c6ae99SBarry Smith @*/
1237a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
123847c6ae99SBarry Smith {
123947c6ae99SBarry Smith   PetscErrorCode ierr;
124047c6ae99SBarry Smith 
124147c6ae99SBarry Smith   PetscFunctionBegin;
1242a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1243a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12445a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1245a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1246a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1247a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1248a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
124947c6ae99SBarry Smith   PetscFunctionReturn(0);
125047c6ae99SBarry Smith }
125147c6ae99SBarry Smith 
1252b412c318SBarry Smith /*@
1253bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1254bd041c0cSMatthew G. Knepley 
1255a5bc1bf3SBarry Smith   Collective on dac
1256bd041c0cSMatthew G. Knepley 
1257bd041c0cSMatthew G. Knepley   Input Parameter:
1258a5bc1bf3SBarry Smith + dac - the DM object
1259a5bc1bf3SBarry Smith - daf - the second, finer DM object
1260bd041c0cSMatthew G. Knepley 
1261bd041c0cSMatthew G. Knepley   Output Parameter:
1262bd041c0cSMatthew G. Knepley . mat - the interpolation
1263bd041c0cSMatthew G. Knepley 
1264bd041c0cSMatthew G. Knepley   Level: developer
1265bd041c0cSMatthew G. Knepley 
1266bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1267bd041c0cSMatthew G. Knepley @*/
1268a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1269bd041c0cSMatthew G. Knepley {
1270bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1271bd041c0cSMatthew G. Knepley 
1272bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1273a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1274a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12755a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1276a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1277a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1278bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1279bd041c0cSMatthew G. Knepley }
1280bd041c0cSMatthew G. Knepley 
1281bd041c0cSMatthew G. Knepley /*@
1282b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128347c6ae99SBarry Smith 
1284d083f849SBarry Smith     Collective on dm
128547c6ae99SBarry Smith 
128647c6ae99SBarry Smith     Input Parameter:
128747c6ae99SBarry Smith +   dm - the DM object
12885bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
128947c6ae99SBarry Smith 
129047c6ae99SBarry Smith     Output Parameter:
129147c6ae99SBarry Smith .   coloring - the coloring
129247c6ae99SBarry Smith 
1293ec5066bdSBarry Smith     Notes:
1294ec5066bdSBarry Smith        Coloring of matrices can be computed directly from the sparse matrix nonzero structure via the MatColoring object or from the mesh from which the
1295ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1296ec5066bdSBarry Smith 
1297ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1298ec5066bdSBarry Smith 
129947c6ae99SBarry Smith     Level: developer
130047c6ae99SBarry Smith 
1301ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130247c6ae99SBarry Smith 
1303aab9d709SJed Brown @*/
1304b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130547c6ae99SBarry Smith {
130647c6ae99SBarry Smith   PetscErrorCode ierr;
130747c6ae99SBarry Smith 
130847c6ae99SBarry Smith   PetscFunctionBegin;
1309171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13105a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1311b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1312b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131347c6ae99SBarry Smith   PetscFunctionReturn(0);
131447c6ae99SBarry Smith }
131547c6ae99SBarry Smith 
1316b412c318SBarry Smith /*@
13178472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
131847c6ae99SBarry Smith 
1319d083f849SBarry Smith     Collective on dm
132047c6ae99SBarry Smith 
132147c6ae99SBarry Smith     Input Parameter:
1322b412c318SBarry Smith .   dm - the DM object
132347c6ae99SBarry Smith 
132447c6ae99SBarry Smith     Output Parameter:
132547c6ae99SBarry Smith .   mat - the empty Jacobian
132647c6ae99SBarry Smith 
1327073dac72SJed Brown     Level: beginner
132847c6ae99SBarry Smith 
132995452b02SPatrick Sanan     Notes:
133095452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133194013140SBarry Smith        do not need to do it yourself.
133294013140SBarry Smith 
133394013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13347889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133594013140SBarry Smith 
133694013140SBarry 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
133794013140SBarry Smith        internally by PETSc.
133894013140SBarry Smith 
133994013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1340aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134194013140SBarry Smith 
1342b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134347c6ae99SBarry Smith 
1344aab9d709SJed Brown @*/
1345b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134647c6ae99SBarry Smith {
134747c6ae99SBarry Smith   PetscErrorCode ierr;
134847c6ae99SBarry Smith 
134947c6ae99SBarry Smith   PetscFunctionBegin;
1350171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1351c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1352b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13535a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1354fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1355b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
135676bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1357c6b011d8SStefano Zampini     DM mdm;
1358c6b011d8SStefano Zampini 
1359c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1360c6b011d8SStefano Zampini     if (!mdm) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_PLIB,"DM type '%s' did not attach the DM to the matrix\n",((PetscObject)dm)->type_name);
1361c6b011d8SStefano Zampini   }
1362e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1363e5e52638SMatthew G. Knepley   if (dm->Nf) {
1364e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1365e571a35bSMatthew G. Knepley     PetscInt     Nf;
1366e571a35bSMatthew G. Knepley 
1367e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1368e571a35bSMatthew G. Knepley     if (Nf == 1) {
1369e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
13708cda7954SMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, 0, &nullSpace);CHKERRQ(ierr);
1371e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1372e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1373e571a35bSMatthew G. Knepley       }
1374e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
13758cda7954SMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, 0, &nullSpace);CHKERRQ(ierr);
1376e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1377e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1378e571a35bSMatthew G. Knepley       }
1379e571a35bSMatthew G. Knepley     }
1380e571a35bSMatthew G. Knepley   }
1381fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
138247c6ae99SBarry Smith   PetscFunctionReturn(0);
138347c6ae99SBarry Smith }
138447c6ae99SBarry Smith 
1385732e2eb9SMatthew G Knepley /*@
1386950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1387732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1388732e2eb9SMatthew G Knepley 
1389d083f849SBarry Smith   Logically Collective on dm
1390732e2eb9SMatthew G Knepley 
1391732e2eb9SMatthew G Knepley   Input Parameter:
1392732e2eb9SMatthew G Knepley + dm - the DM
1393732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1394732e2eb9SMatthew G Knepley 
1395732e2eb9SMatthew G Knepley   Level: developer
1396b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1397732e2eb9SMatthew G Knepley @*/
1398732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1399732e2eb9SMatthew G Knepley {
1400732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1401732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1402732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1403732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1404732e2eb9SMatthew G Knepley }
1405732e2eb9SMatthew G Knepley 
1406b06ff27eSHong Zhang /*@
1407b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1408b06ff27eSHong Zhang     but the array for values will not be allocated.
1409b06ff27eSHong Zhang 
1410d083f849SBarry Smith   Logically Collective on dm
1411b06ff27eSHong Zhang 
1412b06ff27eSHong Zhang   Input Parameter:
1413b06ff27eSHong Zhang + dm - the DM
1414b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1415b06ff27eSHong Zhang 
1416b06ff27eSHong Zhang   Level: developer
1417b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1418b06ff27eSHong Zhang @*/
1419b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1420b06ff27eSHong Zhang {
1421b06ff27eSHong Zhang   PetscFunctionBegin;
1422b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1423b06ff27eSHong Zhang   dm->structure_only = only;
1424b06ff27eSHong Zhang   PetscFunctionReturn(0);
1425b06ff27eSHong Zhang }
1426b06ff27eSHong Zhang 
1427a89ea682SMatthew G Knepley /*@C
1428aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1429a89ea682SMatthew G Knepley 
1430a89ea682SMatthew G Knepley   Not Collective
1431a89ea682SMatthew G Knepley 
1432a89ea682SMatthew G Knepley   Input Parameters:
1433a89ea682SMatthew G Knepley + dm - the DM object
1434aa1993deSMatthew G Knepley . count - The minium size
143569291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1436a89ea682SMatthew G Knepley 
1437a89ea682SMatthew G Knepley   Output Parameter:
1438a89ea682SMatthew G Knepley . array - the work array
1439a89ea682SMatthew G Knepley 
1440a89ea682SMatthew G Knepley   Level: developer
1441a89ea682SMatthew G Knepley 
1442a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1443a89ea682SMatthew G Knepley @*/
144469291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1445a89ea682SMatthew G Knepley {
1446a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1447aa1993deSMatthew G Knepley   DMWorkLink     link;
144869291d52SBarry Smith   PetscMPIInt    dsize;
1449a89ea682SMatthew G Knepley 
1450a89ea682SMatthew G Knepley   PetscFunctionBegin;
1451a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1452aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1453aa1993deSMatthew G Knepley   if (dm->workin) {
1454aa1993deSMatthew G Knepley     link       = dm->workin;
1455aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1456aa1993deSMatthew G Knepley   } else {
1457b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1458a89ea682SMatthew G Knepley   }
145969291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14605056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1461aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1462854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1463854ce69bSBarry Smith     link->bytes = dsize*count;
1464aa1993deSMatthew G Knepley   }
1465aa1993deSMatthew G Knepley   link->next   = dm->workout;
1466aa1993deSMatthew G Knepley   dm->workout  = link;
146700d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
146800d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
146900d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
147000d952a4SJed Brown #endif
1471aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1472a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1473a89ea682SMatthew G Knepley }
1474a89ea682SMatthew G Knepley 
1475aa1993deSMatthew G Knepley /*@C
1476aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1477aa1993deSMatthew G Knepley 
1478aa1993deSMatthew G Knepley   Not Collective
1479aa1993deSMatthew G Knepley 
1480aa1993deSMatthew G Knepley   Input Parameters:
1481aa1993deSMatthew G Knepley + dm - the DM object
1482aa1993deSMatthew G Knepley . count - The minium size
148369291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1484aa1993deSMatthew G Knepley 
1485aa1993deSMatthew G Knepley   Output Parameter:
1486aa1993deSMatthew G Knepley . array - the work array
1487aa1993deSMatthew G Knepley 
1488aa1993deSMatthew G Knepley   Level: developer
1489aa1993deSMatthew G Knepley 
149095452b02SPatrick Sanan   Developer Notes:
149195452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1492aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1493aa1993deSMatthew G Knepley @*/
149469291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1495aa1993deSMatthew G Knepley {
1496aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1497aa1993deSMatthew G Knepley 
1498aa1993deSMatthew G Knepley   PetscFunctionBegin;
1499aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1500aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1501aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1502aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1503aa1993deSMatthew G Knepley       *p           = link->next;
1504aa1993deSMatthew G Knepley       link->next   = dm->workin;
1505aa1993deSMatthew G Knepley       dm->workin   = link;
15060298fd71SBarry Smith       *(void**)mem = NULL;
1507aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1508aa1993deSMatthew G Knepley     }
1509aa1993deSMatthew G Knepley   }
1510aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1511aa1993deSMatthew G Knepley }
1512e7c4fc90SDmitry Karpeev 
15138cda7954SMatthew G. Knepley /*@C
15148cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15158cda7954SMatthew G. Knepley 
15168cda7954SMatthew G. Knepley   Logically collective on DM
15178cda7954SMatthew G. Knepley 
15188cda7954SMatthew G. Knepley   Input Parameters:
15198cda7954SMatthew G. Knepley + dm     - The DM
15208cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15218cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15228cda7954SMatthew G. Knepley 
15238cda7954SMatthew G. Knepley   Notes:
15248cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15258cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15268cda7954SMatthew G. Knepley $ dm        - The present DM
15278cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15288cda7954SMatthew G. Knepley $ field     - The field number in dm
15298cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15308cda7954SMatthew G. Knepley 
15318cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15328cda7954SMatthew G. Knepley 
15338cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15348cda7954SMatthew G. Knepley */
15358cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1536435a35e8SMatthew G Knepley {
1537435a35e8SMatthew G Knepley   PetscFunctionBegin;
1538435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
153982f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1540435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1541435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1542435a35e8SMatthew G Knepley }
1543435a35e8SMatthew G Knepley 
15448cda7954SMatthew G. Knepley /*@C
15458cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
15468cda7954SMatthew G. Knepley 
15478cda7954SMatthew G. Knepley   Not collective
15488cda7954SMatthew G. Knepley 
15498cda7954SMatthew G. Knepley   Input Parameters:
15508cda7954SMatthew G. Knepley + dm     - The DM
15518cda7954SMatthew G. Knepley - field  - The field number for the nullspace
15528cda7954SMatthew G. Knepley 
15538cda7954SMatthew G. Knepley   Output Parameter:
15548cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
15558cda7954SMatthew G. Knepley 
15568cda7954SMatthew G. Knepley   Notes:
15578cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15588cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15598cda7954SMatthew G. Knepley $ dm        - The present DM
15608cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15618cda7954SMatthew G. Knepley $ field     - The field number in dm
15628cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15638cda7954SMatthew G. Knepley 
15648cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15658cda7954SMatthew G. Knepley 
15668cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15678cda7954SMatthew G. Knepley */
15688cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
15690a50eb56SMatthew G. Knepley {
15700a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15710a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1572f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15730a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15740a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15750a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15760a50eb56SMatthew G. Knepley }
15770a50eb56SMatthew G. Knepley 
15788cda7954SMatthew G. Knepley /*@C
15798cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
15808cda7954SMatthew G. Knepley 
15818cda7954SMatthew G. Knepley   Logically collective on DM
15828cda7954SMatthew G. Knepley 
15838cda7954SMatthew G. Knepley   Input Parameters:
15848cda7954SMatthew G. Knepley + dm     - The DM
15858cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15868cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
15878cda7954SMatthew G. Knepley 
15888cda7954SMatthew G. Knepley   Notes:
15898cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15908cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15918cda7954SMatthew G. Knepley $ dm        - The present DM
15928cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15938cda7954SMatthew G. Knepley $ field     - The field number in dm
15948cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15958cda7954SMatthew G. Knepley 
15968cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15978cda7954SMatthew G. Knepley 
15988cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15998cda7954SMatthew G. Knepley */
16008cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1601f9d4088aSMatthew G. Knepley {
1602f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1603f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1604f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1605f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1606f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1607f9d4088aSMatthew G. Knepley }
1608f9d4088aSMatthew G. Knepley 
16098cda7954SMatthew G. Knepley /*@C
16108cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16118cda7954SMatthew G. Knepley 
16128cda7954SMatthew G. Knepley   Not collective
16138cda7954SMatthew G. Knepley 
16148cda7954SMatthew G. Knepley   Input Parameters:
16158cda7954SMatthew G. Knepley + dm     - The DM
16168cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16178cda7954SMatthew G. Knepley 
16188cda7954SMatthew G. Knepley   Output Parameter:
16198cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16208cda7954SMatthew G. Knepley 
16218cda7954SMatthew G. Knepley   Notes:
16228cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16238cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16248cda7954SMatthew G. Knepley $ dm        - The present DM
16258cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16268cda7954SMatthew G. Knepley $ field     - The field number in dm
16278cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16288cda7954SMatthew G. Knepley 
16298cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16308cda7954SMatthew G. Knepley 
16318cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16328cda7954SMatthew G. Knepley */
16338cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1634f9d4088aSMatthew G. Knepley {
1635f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1636f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1637f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1638f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1639f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1640f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1641f9d4088aSMatthew G. Knepley }
1642f9d4088aSMatthew G. Knepley 
16434f3b5142SJed Brown /*@C
16444d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
16454d343eeaSMatthew G Knepley 
16464d343eeaSMatthew G Knepley   Not collective
16474d343eeaSMatthew G Knepley 
16484d343eeaSMatthew G Knepley   Input Parameter:
16494d343eeaSMatthew G Knepley . dm - the DM object
16504d343eeaSMatthew G Knepley 
16514d343eeaSMatthew G Knepley   Output Parameters:
16520298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
16530298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
16540298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
16554d343eeaSMatthew G Knepley 
16564d343eeaSMatthew G Knepley   Level: intermediate
16574d343eeaSMatthew G Knepley 
165821c9b008SJed Brown   Notes:
165921c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
166021c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
166121c9b008SJed Brown   PetscFree().
166221c9b008SJed Brown 
16634d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16644d343eeaSMatthew G Knepley @*/
166537d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
16664d343eeaSMatthew G Knepley {
166737d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
16684d343eeaSMatthew G Knepley   PetscErrorCode ierr;
16694d343eeaSMatthew G Knepley 
16704d343eeaSMatthew G Knepley   PetscFunctionBegin;
16714d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
167269ca1f37SDmitry Karpeev   if (numFields) {
1673534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
167469ca1f37SDmitry Karpeev     *numFields = 0;
167569ca1f37SDmitry Karpeev   }
167637d0c07bSMatthew G Knepley   if (fieldNames) {
167737d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
16780298fd71SBarry Smith     *fieldNames = NULL;
167969ca1f37SDmitry Karpeev   }
168069ca1f37SDmitry Karpeev   if (fields) {
168169ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
16820298fd71SBarry Smith     *fields = NULL;
168369ca1f37SDmitry Karpeev   }
168492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
168537d0c07bSMatthew G Knepley   if (section) {
16863a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
168737d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
168837d0c07bSMatthew G Knepley 
1689e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
169037d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
16913a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
169237d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
169337d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
169437d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
16953a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
169637d0c07bSMatthew G Knepley     }
169737d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
169837d0c07bSMatthew G Knepley       PetscInt gdof;
169937d0c07bSMatthew G Knepley 
170037d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
170137d0c07bSMatthew G Knepley       if (gdof > 0) {
170237d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17033a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
170437d0c07bSMatthew G Knepley 
170537d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
170637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17073a544194SStefano Zampini           fpdof = fdof-fcdof;
17083a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17093a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17103a544194SStefano Zampini             fieldNc[f] = 1;
17113a544194SStefano Zampini           }
17123a544194SStefano Zampini           fieldSizes[f] += fpdof;
171337d0c07bSMatthew G Knepley         }
171437d0c07bSMatthew G Knepley       }
171537d0c07bSMatthew G Knepley     }
171637d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1717785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
171837d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
171937d0c07bSMatthew G Knepley     }
172037d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
172137d0c07bSMatthew G Knepley       PetscInt gdof, goff;
172237d0c07bSMatthew G Knepley 
172337d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
172437d0c07bSMatthew G Knepley       if (gdof > 0) {
172537d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
172637d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
172737d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
172837d0c07bSMatthew G Knepley 
172937d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
173037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
173137d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
173237d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
173337d0c07bSMatthew G Knepley           }
173437d0c07bSMatthew G Knepley         }
173537d0c07bSMatthew G Knepley       }
173637d0c07bSMatthew G Knepley     }
17378865f1eaSKarl Rupp     if (numFields) *numFields = nF;
173837d0c07bSMatthew G Knepley     if (fieldNames) {
1739785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
174037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
174137d0c07bSMatthew G Knepley         const char *fieldName;
174237d0c07bSMatthew G Knepley 
174337d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
174437d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
174537d0c07bSMatthew G Knepley       }
174637d0c07bSMatthew G Knepley     }
174737d0c07bSMatthew G Knepley     if (fields) {
1748785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
174937d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
17503a544194SStefano Zampini         PetscInt bs, in[2], out[2];
17513a544194SStefano Zampini 
175282f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
17533a544194SStefano Zampini         in[0] = -fieldNc[f];
17543a544194SStefano Zampini         in[1] = fieldNc[f];
17553a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
17563a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
17573a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
175837d0c07bSMatthew G Knepley       }
175937d0c07bSMatthew G Knepley     }
17603a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
17618865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
17628865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
176369ca1f37SDmitry Karpeev   }
17644d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
17654d343eeaSMatthew G Knepley }
17664d343eeaSMatthew G Knepley 
176716621825SDmitry Karpeev 
176816621825SDmitry Karpeev /*@C
176916621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
177016621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
177116621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1772e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1773e7c4fc90SDmitry Karpeev 
1774e7c4fc90SDmitry Karpeev   Not collective
1775e7c4fc90SDmitry Karpeev 
1776e7c4fc90SDmitry Karpeev   Input Parameter:
1777e7c4fc90SDmitry Karpeev . dm - the DM object
1778e7c4fc90SDmitry Karpeev 
1779e7c4fc90SDmitry Karpeev   Output Parameters:
17800298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
17810298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
17820298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
17830298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1784e7c4fc90SDmitry Karpeev 
1785e7c4fc90SDmitry Karpeev   Level: intermediate
1786e7c4fc90SDmitry Karpeev 
1787e7c4fc90SDmitry Karpeev   Notes:
1788e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1789e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1790e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1791e7c4fc90SDmitry Karpeev 
1792e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1793e7c4fc90SDmitry Karpeev @*/
179416621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1795e7c4fc90SDmitry Karpeev {
1796e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1797e7c4fc90SDmitry Karpeev 
1798e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1799e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18008865f1eaSKarl Rupp   if (len) {
1801534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18028865f1eaSKarl Rupp     *len = 0;
18038865f1eaSKarl Rupp   }
18048865f1eaSKarl Rupp   if (namelist) {
18058865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1806ea78f98cSLisandro Dalcin     *namelist = NULL;
18078865f1eaSKarl Rupp   }
18088865f1eaSKarl Rupp   if (islist) {
18098865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1810ea78f98cSLisandro Dalcin     *islist = NULL;
18118865f1eaSKarl Rupp   }
18128865f1eaSKarl Rupp   if (dmlist) {
18138865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1814ea78f98cSLisandro Dalcin     *dmlist = NULL;
18158865f1eaSKarl Rupp   }
1816f3f0edfdSDmitry Karpeev   /*
1817f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1818f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1819f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1820f3f0edfdSDmitry Karpeev    */
1821ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
182216621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1823435a35e8SMatthew G Knepley     PetscSection section;
1824435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1825435a35e8SMatthew G Knepley 
182692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1827435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1828435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1829f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
183003dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
183103dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
183203dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1833435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1834435a35e8SMatthew G Knepley         const char *fieldName;
1835435a35e8SMatthew G Knepley 
183603dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
183703dc3394SMatthew G. Knepley         if (namelist) {
1838435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1839435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1840435a35e8SMatthew G Knepley         }
184103dc3394SMatthew G. Knepley       }
1842435a35e8SMatthew G Knepley     } else {
184369ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1844e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
18450298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1846e7c4fc90SDmitry Karpeev     }
18478865f1eaSKarl Rupp   } else {
184816621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
184916621825SDmitry Karpeev   }
185016621825SDmitry Karpeev   PetscFunctionReturn(0);
185116621825SDmitry Karpeev }
185216621825SDmitry Karpeev 
1853564cec59SMatthew G. Knepley /*@
1854435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1855435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1856435a35e8SMatthew G Knepley 
1857435a35e8SMatthew G Knepley   Not collective
1858435a35e8SMatthew G Knepley 
1859435a35e8SMatthew G Knepley   Input Parameters:
18602adcc780SMatthew G. Knepley + dm        - The DM object
18612adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
18622adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1863435a35e8SMatthew G Knepley 
1864435a35e8SMatthew G Knepley   Output Parameters:
18652adcc780SMatthew G. Knepley + is - The global indices for the subproblem
18662adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1867435a35e8SMatthew G Knepley 
18685d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18695d3b26e6SMatthew G. Knepley 
1870435a35e8SMatthew G Knepley   Level: intermediate
1871435a35e8SMatthew G Knepley 
18725d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1873435a35e8SMatthew G Knepley @*/
187437bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1875435a35e8SMatthew G Knepley {
1876435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1877435a35e8SMatthew G Knepley 
1878435a35e8SMatthew G Knepley   PetscFunctionBegin;
1879435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1880435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
18818865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
18828865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1883b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1884435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1885435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1886435a35e8SMatthew G Knepley }
1887435a35e8SMatthew G Knepley 
18882adcc780SMatthew G. Knepley /*@C
18892adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
18902adcc780SMatthew G. Knepley 
18912adcc780SMatthew G. Knepley   Not collective
18922adcc780SMatthew G. Knepley 
18932adcc780SMatthew G. Knepley   Input Parameter:
18942adcc780SMatthew G. Knepley + dms - The DM objects
18952adcc780SMatthew G. Knepley - len - The number of DMs
18962adcc780SMatthew G. Knepley 
18972adcc780SMatthew G. Knepley   Output Parameters:
1898a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
18992adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19002adcc780SMatthew G. Knepley 
19015d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19025d3b26e6SMatthew G. Knepley 
19032adcc780SMatthew G. Knepley   Level: intermediate
19042adcc780SMatthew G. Knepley 
19055d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19062adcc780SMatthew G. Knepley @*/
19072adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19082adcc780SMatthew G. Knepley {
19092adcc780SMatthew G. Knepley   PetscInt       i;
19102adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19112adcc780SMatthew G. Knepley 
19122adcc780SMatthew G. Knepley   PetscFunctionBegin;
19132adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19142adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19152adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1916a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19172adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19182adcc780SMatthew G. Knepley   if (len) {
1919b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1920b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1921b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19222adcc780SMatthew G. Knepley   }
19232adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19242adcc780SMatthew G. Knepley }
19252adcc780SMatthew G. Knepley 
192616621825SDmitry Karpeev 
192716621825SDmitry Karpeev /*@C
19288d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19298d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19308d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19318d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19328d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
193316621825SDmitry Karpeev 
193416621825SDmitry Karpeev   Not collective
193516621825SDmitry Karpeev 
193616621825SDmitry Karpeev   Input Parameter:
193716621825SDmitry Karpeev . dm - the DM object
193816621825SDmitry Karpeev 
193916621825SDmitry Karpeev   Output Parameters:
19400298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
19410298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
19420298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
19430298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
19440298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
194516621825SDmitry Karpeev 
194616621825SDmitry Karpeev   Level: intermediate
194716621825SDmitry Karpeev 
194816621825SDmitry Karpeev   Notes:
194916621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
195016621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
195116621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
195216621825SDmitry Karpeev 
1953245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
195416621825SDmitry Karpeev @*/
19558d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
195616621825SDmitry Karpeev {
195716621825SDmitry Karpeev   PetscErrorCode      ierr;
1958be081cd6SPeter Brune   DMSubDomainHookLink link;
1959be081cd6SPeter Brune   PetscInt            i,l;
196016621825SDmitry Karpeev 
196116621825SDmitry Karpeev   PetscFunctionBegin;
196216621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
196314a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
19640298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
19650298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
19660298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
19670298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1968f3f0edfdSDmitry Karpeev   /*
1969f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1970f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1971f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1972f3f0edfdSDmitry Karpeev    */
1973ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
197416621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1975be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
197614a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1977f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1978be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1979be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1980be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1981be081cd6SPeter Brune         }
1982648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1983e7c4fc90SDmitry Karpeev       }
198414a18fd3SPeter Brune     }
198514a18fd3SPeter Brune     if (len) *len = l;
198614a18fd3SPeter Brune   }
1987e30e807fSPeter Brune   PetscFunctionReturn(0);
1988e30e807fSPeter Brune }
1989e30e807fSPeter Brune 
1990e30e807fSPeter Brune 
1991e30e807fSPeter Brune /*@C
1992e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1993e30e807fSPeter Brune 
1994e30e807fSPeter Brune   Not collective
1995e30e807fSPeter Brune 
1996e30e807fSPeter Brune   Input Parameters:
1997e30e807fSPeter Brune + dm - the DM object
1998e30e807fSPeter Brune . n  - the number of subdomain scatters
1999e30e807fSPeter Brune - subdms - the local subdomains
2000e30e807fSPeter Brune 
2001e30e807fSPeter Brune   Output Parameters:
2002e30e807fSPeter Brune + n     - the number of scatters returned
2003e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2004e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2005e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2006e30e807fSPeter Brune 
200795452b02SPatrick Sanan   Notes:
200895452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2009e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2010e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2011e30e807fSPeter Brune   solution and residual data.
2012e30e807fSPeter Brune 
2013e30e807fSPeter Brune   Level: developer
2014e30e807fSPeter Brune 
2015e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2016e30e807fSPeter Brune @*/
2017e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2018e30e807fSPeter Brune {
2019e30e807fSPeter Brune   PetscErrorCode ierr;
2020e30e807fSPeter Brune 
2021e30e807fSPeter Brune   PetscFunctionBegin;
2022e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2023e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2024b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2025e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2026e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2027e7c4fc90SDmitry Karpeev }
2028e7c4fc90SDmitry Karpeev 
202947c6ae99SBarry Smith /*@
203047c6ae99SBarry Smith   DMRefine - Refines a DM object
203147c6ae99SBarry Smith 
2032d083f849SBarry Smith   Collective on dm
203347c6ae99SBarry Smith 
203447c6ae99SBarry Smith   Input Parameter:
203547c6ae99SBarry Smith + dm   - the DM object
203691d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
203747c6ae99SBarry Smith 
203847c6ae99SBarry Smith   Output Parameter:
20390298fd71SBarry Smith . dmf - the refined DM, or NULL
2040ae0a1c52SMatthew G Knepley 
2041412e9a14SMatthew G. Knepley   Options Dtabase Keys:
2042412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2043412e9a14SMatthew G. Knepley 
20440298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
204547c6ae99SBarry Smith 
204647c6ae99SBarry Smith   Level: developer
204747c6ae99SBarry Smith 
2048e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
204947c6ae99SBarry Smith @*/
20507087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
205147c6ae99SBarry Smith {
205247c6ae99SBarry Smith   PetscErrorCode   ierr;
2053c833c3b5SJed Brown   DMRefineHookLink link;
205447c6ae99SBarry Smith 
205547c6ae99SBarry Smith   PetscFunctionBegin;
2056732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2057b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
20581ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
205947c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
20604057135bSMatthew G Knepley   if (*dmf) {
206143842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
20628865f1eaSKarl Rupp 
20638cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
20648865f1eaSKarl Rupp 
2065644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
20660598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2067656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
20688865f1eaSKarl Rupp 
2069e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2070c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
20718865f1eaSKarl Rupp       if (link->refinehook) {
20728865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
20738865f1eaSKarl Rupp       }
2074c833c3b5SJed Brown     }
2075c833c3b5SJed Brown   }
20761ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2077c833c3b5SJed Brown   PetscFunctionReturn(0);
2078c833c3b5SJed Brown }
2079c833c3b5SJed Brown 
2080bb9467b5SJed Brown /*@C
2081c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2082c833c3b5SJed Brown 
2083c833c3b5SJed Brown    Logically Collective
2084c833c3b5SJed Brown 
2085c833c3b5SJed Brown    Input Arguments:
2086c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2087c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2088c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20890298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2090c833c3b5SJed Brown 
2091c833c3b5SJed Brown    Calling sequence of refinehook:
2092c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2093c833c3b5SJed Brown 
2094c833c3b5SJed Brown +  coarse - coarse level DM
2095c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2096c833c3b5SJed Brown -  ctx - optional user-defined function context
2097c833c3b5SJed Brown 
2098c833c3b5SJed Brown    Calling sequence for interphook:
2099c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2100c833c3b5SJed Brown 
2101c833c3b5SJed Brown +  coarse - coarse level DM
2102c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2103c833c3b5SJed Brown .  fine - fine level DM to update
2104c833c3b5SJed Brown -  ctx - optional user-defined function context
2105c833c3b5SJed Brown 
2106c833c3b5SJed Brown    Level: advanced
2107c833c3b5SJed Brown 
2108c833c3b5SJed Brown    Notes:
2109c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2110c833c3b5SJed Brown 
2111c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2112c833c3b5SJed Brown 
2113bb9467b5SJed Brown    This function is currently not available from Fortran.
2114bb9467b5SJed Brown 
2115c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2116c833c3b5SJed Brown @*/
2117c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2118c833c3b5SJed Brown {
2119c833c3b5SJed Brown   PetscErrorCode   ierr;
2120c833c3b5SJed Brown   DMRefineHookLink link,*p;
2121c833c3b5SJed Brown 
2122c833c3b5SJed Brown   PetscFunctionBegin;
2123c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21243d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21253d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21263d8e3701SJed Brown   }
212795dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2128c833c3b5SJed Brown   link->refinehook = refinehook;
2129c833c3b5SJed Brown   link->interphook = interphook;
2130c833c3b5SJed Brown   link->ctx        = ctx;
21310298fd71SBarry Smith   link->next       = NULL;
2132c833c3b5SJed Brown   *p               = link;
2133c833c3b5SJed Brown   PetscFunctionReturn(0);
2134c833c3b5SJed Brown }
2135c833c3b5SJed Brown 
21363d8e3701SJed Brown /*@C
21373d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
21383d8e3701SJed Brown 
21393d8e3701SJed Brown    Logically Collective
21403d8e3701SJed Brown 
21413d8e3701SJed Brown    Input Arguments:
21423d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
21433d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
21443d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21453d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
21463d8e3701SJed Brown 
21473d8e3701SJed Brown    Level: advanced
21483d8e3701SJed Brown 
21493d8e3701SJed Brown    Notes:
21503d8e3701SJed Brown    This function does nothing if the hook is not in the list.
21513d8e3701SJed Brown 
21523d8e3701SJed Brown    This function is currently not available from Fortran.
21533d8e3701SJed Brown 
21543d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
21553d8e3701SJed Brown @*/
21563d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
21573d8e3701SJed Brown {
21583d8e3701SJed Brown   PetscErrorCode   ierr;
21593d8e3701SJed Brown   DMRefineHookLink link,*p;
21603d8e3701SJed Brown 
21613d8e3701SJed Brown   PetscFunctionBegin;
21623d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21633d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
21643d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
21653d8e3701SJed Brown       link = *p;
21663d8e3701SJed Brown       *p = link->next;
21673d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
21683d8e3701SJed Brown       break;
21693d8e3701SJed Brown     }
21703d8e3701SJed Brown   }
21713d8e3701SJed Brown   PetscFunctionReturn(0);
21723d8e3701SJed Brown }
21733d8e3701SJed Brown 
2174c833c3b5SJed Brown /*@
2175c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2176c833c3b5SJed Brown 
2177c833c3b5SJed Brown    Collective if any hooks are
2178c833c3b5SJed Brown 
2179c833c3b5SJed Brown    Input Arguments:
2180c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2181e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2182c833c3b5SJed Brown -  fine - finer DM to update
2183c833c3b5SJed Brown 
2184c833c3b5SJed Brown    Level: developer
2185c833c3b5SJed Brown 
2186c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2187c833c3b5SJed Brown @*/
2188c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2189c833c3b5SJed Brown {
2190c833c3b5SJed Brown   PetscErrorCode   ierr;
2191c833c3b5SJed Brown   DMRefineHookLink link;
2192c833c3b5SJed Brown 
2193c833c3b5SJed Brown   PetscFunctionBegin;
2194c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
21958865f1eaSKarl Rupp     if (link->interphook) {
21968865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
21978865f1eaSKarl Rupp     }
21984057135bSMatthew G Knepley   }
219947c6ae99SBarry Smith   PetscFunctionReturn(0);
220047c6ae99SBarry Smith }
220147c6ae99SBarry Smith 
2202eb3f98d2SBarry Smith /*@
2203aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2204eb3f98d2SBarry Smith 
2205eb3f98d2SBarry Smith     Not Collective
2206eb3f98d2SBarry Smith 
2207eb3f98d2SBarry Smith     Input Parameter:
2208eb3f98d2SBarry Smith .   dm - the DM object
2209eb3f98d2SBarry Smith 
2210eb3f98d2SBarry Smith     Output Parameter:
2211eb3f98d2SBarry Smith .   level - number of refinements
2212eb3f98d2SBarry Smith 
2213eb3f98d2SBarry Smith     Level: developer
2214eb3f98d2SBarry Smith 
22156a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2216eb3f98d2SBarry Smith 
2217eb3f98d2SBarry Smith @*/
2218eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2219eb3f98d2SBarry Smith {
2220eb3f98d2SBarry Smith   PetscFunctionBegin;
2221eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2222eb3f98d2SBarry Smith   *level = dm->levelup;
2223eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2224eb3f98d2SBarry Smith }
2225eb3f98d2SBarry Smith 
2226fef3a512SBarry Smith /*@
2227aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2228fef3a512SBarry Smith 
2229fef3a512SBarry Smith     Not Collective
2230fef3a512SBarry Smith 
2231fef3a512SBarry Smith     Input Parameter:
2232fef3a512SBarry Smith +   dm - the DM object
2233fef3a512SBarry Smith -   level - number of refinements
2234fef3a512SBarry Smith 
2235fef3a512SBarry Smith     Level: advanced
2236fef3a512SBarry Smith 
223795452b02SPatrick Sanan     Notes:
223895452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2239fef3a512SBarry Smith 
2240fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2241fef3a512SBarry Smith 
2242fef3a512SBarry Smith @*/
2243fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2244fef3a512SBarry Smith {
2245fef3a512SBarry Smith   PetscFunctionBegin;
2246fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2247fef3a512SBarry Smith   dm->levelup = level;
2248fef3a512SBarry Smith   PetscFunctionReturn(0);
2249fef3a512SBarry Smith }
2250fef3a512SBarry Smith 
2251ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2252ca3d3a14SMatthew G. Knepley {
2253ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2254ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2255ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2256ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2257ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2258ca3d3a14SMatthew G. Knepley }
2259ca3d3a14SMatthew G. Knepley 
2260ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2261ca3d3a14SMatthew G. Knepley {
2262ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2263ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2264ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2265ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2266ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2267ca3d3a14SMatthew G. Knepley }
2268ca3d3a14SMatthew G. Knepley 
2269ca3d3a14SMatthew G. Knepley /*@
2270c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2271ca3d3a14SMatthew G. Knepley 
2272ca3d3a14SMatthew G. Knepley   Input Parameter:
2273ca3d3a14SMatthew G. Knepley . dm - The DM
2274ca3d3a14SMatthew G. Knepley 
2275ca3d3a14SMatthew G. Knepley   Output Parameter:
2276ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2277ca3d3a14SMatthew G. Knepley 
2278ca3d3a14SMatthew G. Knepley   Level: developer
2279ca3d3a14SMatthew G. Knepley 
2280436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2281ca3d3a14SMatthew G. Knepley @*/
2282ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2283ca3d3a14SMatthew G. Knepley {
2284ca3d3a14SMatthew G. Knepley   Vec            tv;
2285ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2286ca3d3a14SMatthew G. Knepley 
2287ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2288ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2289534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2290ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2291ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2292ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2293ca3d3a14SMatthew G. Knepley }
2294ca3d3a14SMatthew G. Knepley 
2295ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2296ca3d3a14SMatthew G. Knepley {
2297ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2298ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2299ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2300ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2301ca3d3a14SMatthew G. Knepley 
2302ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2303ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
230492fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2305ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2306ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2307ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
230892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2309ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2310ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2311ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2312ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2313ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2314ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2315ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2316ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2317ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2318ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2319ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2320ca3d3a14SMatthew G. Knepley     }
2321ca3d3a14SMatthew G. Knepley   }
2322ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2323ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2324ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2325ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2326ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2327ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2328ca3d3a14SMatthew G. Knepley       if (dof) {
2329ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2330ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2331ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2332ca3d3a14SMatthew G. Knepley 
2333ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2334ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2335ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2336580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2337ca3d3a14SMatthew G. Knepley       }
2338ca3d3a14SMatthew G. Knepley     }
2339ca3d3a14SMatthew G. Knepley   }
2340ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2341ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2342ca3d3a14SMatthew G. Knepley }
2343ca3d3a14SMatthew G. Knepley 
2344ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2345ca3d3a14SMatthew G. Knepley {
2346ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2347ca3d3a14SMatthew G. Knepley 
2348ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2349ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2350ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2351ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2352ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2353ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2354ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2355ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2356ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2357ca3d3a14SMatthew G. Knepley }
2358ca3d3a14SMatthew G. Knepley 
2359bb9467b5SJed Brown /*@C
2360baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2361baf369e7SPeter Brune 
2362baf369e7SPeter Brune    Logically Collective
2363baf369e7SPeter Brune 
2364baf369e7SPeter Brune    Input Arguments:
2365baf369e7SPeter Brune +  dm - the DM
2366baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2367baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
23680298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2369baf369e7SPeter Brune 
2370baf369e7SPeter Brune    Calling sequence for beginhook:
2371baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2372baf369e7SPeter Brune 
2373baf369e7SPeter Brune +  dm - global DM
2374baf369e7SPeter Brune .  g - global vector
2375baf369e7SPeter Brune .  mode - mode
2376baf369e7SPeter Brune .  l - local vector
2377baf369e7SPeter Brune -  ctx - optional user-defined function context
2378baf369e7SPeter Brune 
2379baf369e7SPeter Brune 
2380baf369e7SPeter Brune    Calling sequence for endhook:
2381ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2382baf369e7SPeter Brune 
2383baf369e7SPeter Brune +  global - global DM
2384baf369e7SPeter Brune -  ctx - optional user-defined function context
2385baf369e7SPeter Brune 
2386baf369e7SPeter Brune    Level: advanced
2387baf369e7SPeter Brune 
2388baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2389baf369e7SPeter Brune @*/
2390baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2391baf369e7SPeter Brune {
2392baf369e7SPeter Brune   PetscErrorCode          ierr;
2393baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2394baf369e7SPeter Brune 
2395baf369e7SPeter Brune   PetscFunctionBegin;
2396baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2397baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
239895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2399baf369e7SPeter Brune   link->beginhook = beginhook;
2400baf369e7SPeter Brune   link->endhook   = endhook;
2401baf369e7SPeter Brune   link->ctx       = ctx;
24020298fd71SBarry Smith   link->next      = NULL;
2403baf369e7SPeter Brune   *p              = link;
2404baf369e7SPeter Brune   PetscFunctionReturn(0);
2405baf369e7SPeter Brune }
2406baf369e7SPeter Brune 
24074c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
24084c274da1SToby Isaac {
24094c274da1SToby Isaac   Mat cMat;
24104c274da1SToby Isaac   Vec cVec;
24114c274da1SToby Isaac   PetscSection section, cSec;
24124c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24134c274da1SToby Isaac   PetscErrorCode ierr;
24144c274da1SToby Isaac 
24154c274da1SToby Isaac   PetscFunctionBegin;
24164c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24174c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24184c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
24195db9a05bSToby Isaac     PetscInt nRows;
24205db9a05bSToby Isaac 
24215db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24225db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
242392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24247711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24254c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
24264c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24274c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24284c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24294c274da1SToby Isaac       if (dof) {
24304c274da1SToby Isaac         PetscScalar *vals;
24314c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
24324c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
24334c274da1SToby Isaac       }
24344c274da1SToby Isaac     }
24354c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24364c274da1SToby Isaac   }
24374c274da1SToby Isaac   PetscFunctionReturn(0);
24384c274da1SToby Isaac }
24394c274da1SToby Isaac 
244047c6ae99SBarry Smith /*@
244101729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
244201729b5cSPatrick Sanan 
2443d083f849SBarry Smith     Neighbor-wise Collective on dm
244401729b5cSPatrick Sanan 
244501729b5cSPatrick Sanan     Input Parameters:
244601729b5cSPatrick Sanan +   dm - the DM object
244701729b5cSPatrick Sanan .   g - the global vector
244801729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
244901729b5cSPatrick Sanan -   l - the local vector
245001729b5cSPatrick Sanan 
245101729b5cSPatrick Sanan     Notes:
245201729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
245301729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
245401729b5cSPatrick Sanan 
245501729b5cSPatrick Sanan     Level: beginner
245601729b5cSPatrick Sanan 
245701729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
245801729b5cSPatrick Sanan 
245901729b5cSPatrick Sanan @*/
246001729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
246101729b5cSPatrick Sanan {
246201729b5cSPatrick Sanan   PetscErrorCode ierr;
246301729b5cSPatrick Sanan 
246401729b5cSPatrick Sanan   PetscFunctionBegin;
246501729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
246601729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
246701729b5cSPatrick Sanan   PetscFunctionReturn(0);
246801729b5cSPatrick Sanan }
246901729b5cSPatrick Sanan 
247001729b5cSPatrick Sanan /*@
247147c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
247247c6ae99SBarry Smith 
2473d083f849SBarry Smith     Neighbor-wise Collective on dm
247447c6ae99SBarry Smith 
247547c6ae99SBarry Smith     Input Parameters:
247647c6ae99SBarry Smith +   dm - the DM object
247747c6ae99SBarry Smith .   g - the global vector
247847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
247947c6ae99SBarry Smith -   l - the local vector
248047c6ae99SBarry Smith 
248101729b5cSPatrick Sanan     Level: intermediate
248247c6ae99SBarry Smith 
248301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
248447c6ae99SBarry Smith 
248547c6ae99SBarry Smith @*/
24867087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
248747c6ae99SBarry Smith {
24887128ae9fSMatthew G Knepley   PetscSF                 sf;
248947c6ae99SBarry Smith   PetscErrorCode          ierr;
2490baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
249147c6ae99SBarry Smith 
249247c6ae99SBarry Smith   PetscFunctionBegin;
2493171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2494baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
24958865f1eaSKarl Rupp     if (link->beginhook) {
24968865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
24978865f1eaSKarl Rupp     }
2498baf369e7SPeter Brune   }
24991bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
25007128ae9fSMatthew G Knepley   if (sf) {
2501ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2502ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
25037128ae9fSMatthew G Knepley 
250482f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2505fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2506fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
25077128ae9fSMatthew G Knepley     ierr = PetscSFBcastBegin(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2508fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2509fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
25107128ae9fSMatthew G Knepley   } else {
251133907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2512843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25137128ae9fSMatthew G Knepley   }
251447c6ae99SBarry Smith   PetscFunctionReturn(0);
251547c6ae99SBarry Smith }
251647c6ae99SBarry Smith 
251747c6ae99SBarry Smith /*@
251847c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
251947c6ae99SBarry Smith 
2520d083f849SBarry Smith     Neighbor-wise Collective on dm
252147c6ae99SBarry Smith 
252247c6ae99SBarry Smith     Input Parameters:
252347c6ae99SBarry Smith +   dm - the DM object
252447c6ae99SBarry Smith .   g - the global vector
252547c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
252647c6ae99SBarry Smith -   l - the local vector
252747c6ae99SBarry Smith 
252801729b5cSPatrick Sanan     Level: intermediate
252947c6ae99SBarry Smith 
253001729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
253147c6ae99SBarry Smith 
253247c6ae99SBarry Smith @*/
25337087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
253447c6ae99SBarry Smith {
25357128ae9fSMatthew G Knepley   PetscSF                 sf;
253647c6ae99SBarry Smith   PetscErrorCode          ierr;
2537ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2538ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2539ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2540baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
254147c6ae99SBarry Smith 
254247c6ae99SBarry Smith   PetscFunctionBegin;
2543171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25441bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2545ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
25467128ae9fSMatthew G Knepley   if (sf) {
254782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25487128ae9fSMatthew G Knepley 
2549fa88e482SJed Brown     ierr = VecGetArrayInPlace(l, &lArray);CHKERRQ(ierr);
2550fa88e482SJed Brown     ierr = VecGetArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
25517128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2552fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2553fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
2554ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
25557128ae9fSMatthew G Knepley   } else {
255633907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2557843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25587128ae9fSMatthew G Knepley   }
25594c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2560baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2561baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2562baf369e7SPeter Brune   }
256347c6ae99SBarry Smith   PetscFunctionReturn(0);
256447c6ae99SBarry Smith }
256547c6ae99SBarry Smith 
2566d4d07f1eSToby Isaac /*@C
2567d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2568d4d07f1eSToby Isaac 
2569d4d07f1eSToby Isaac    Logically Collective
2570d4d07f1eSToby Isaac 
2571d4d07f1eSToby Isaac    Input Arguments:
2572d4d07f1eSToby Isaac +  dm - the DM
2573d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2574d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2575d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2576d4d07f1eSToby Isaac 
2577d4d07f1eSToby Isaac    Calling sequence for beginhook:
2578d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2579d4d07f1eSToby Isaac 
2580d4d07f1eSToby Isaac +  dm - global DM
2581d4d07f1eSToby Isaac .  l - local vector
2582d4d07f1eSToby Isaac .  mode - mode
2583d4d07f1eSToby Isaac .  g - global vector
2584d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2585d4d07f1eSToby Isaac 
2586d4d07f1eSToby Isaac 
2587d4d07f1eSToby Isaac    Calling sequence for endhook:
2588d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2589d4d07f1eSToby Isaac 
2590d4d07f1eSToby Isaac +  global - global DM
2591d4d07f1eSToby Isaac .  l - local vector
2592d4d07f1eSToby Isaac .  mode - mode
2593d4d07f1eSToby Isaac .  g - global vector
2594d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2595d4d07f1eSToby Isaac 
2596d4d07f1eSToby Isaac    Level: advanced
2597d4d07f1eSToby Isaac 
2598d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2599d4d07f1eSToby Isaac @*/
2600d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2601d4d07f1eSToby Isaac {
2602d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2603d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2604d4d07f1eSToby Isaac 
2605d4d07f1eSToby Isaac   PetscFunctionBegin;
2606d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2607d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
260895dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2609d4d07f1eSToby Isaac   link->beginhook = beginhook;
2610d4d07f1eSToby Isaac   link->endhook   = endhook;
2611d4d07f1eSToby Isaac   link->ctx       = ctx;
2612d4d07f1eSToby Isaac   link->next      = NULL;
2613d4d07f1eSToby Isaac   *p              = link;
2614d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2615d4d07f1eSToby Isaac }
2616d4d07f1eSToby Isaac 
26174c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
26184c274da1SToby Isaac {
26194c274da1SToby Isaac   Mat cMat;
26204c274da1SToby Isaac   Vec cVec;
26214c274da1SToby Isaac   PetscSection section, cSec;
26224c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
26234c274da1SToby Isaac   PetscErrorCode ierr;
26244c274da1SToby Isaac 
26254c274da1SToby Isaac   PetscFunctionBegin;
26264c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26274c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
26284c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
26295db9a05bSToby Isaac     PetscInt nRows;
26305db9a05bSToby Isaac 
26315db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
26325db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
263392fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
26347711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
26354c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
26364c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26374c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
26384c274da1SToby Isaac       if (dof) {
26394c274da1SToby Isaac         PetscInt d;
26404c274da1SToby Isaac         PetscScalar *vals;
26414c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
26424c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
26434c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
26444c274da1SToby Isaac          * we just extracted */
26454c274da1SToby Isaac         for (d = 0; d < dof; d++) {
26464c274da1SToby Isaac           vals[d] = 0.;
26474c274da1SToby Isaac         }
26484c274da1SToby Isaac       }
26494c274da1SToby Isaac     }
26504c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
26514c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
26524c274da1SToby Isaac   }
26534c274da1SToby Isaac   PetscFunctionReturn(0);
26544c274da1SToby Isaac }
265501729b5cSPatrick Sanan /*@
265601729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
265701729b5cSPatrick Sanan 
2658d083f849SBarry Smith     Neighbor-wise Collective on dm
265901729b5cSPatrick Sanan 
266001729b5cSPatrick Sanan     Input Parameters:
266101729b5cSPatrick Sanan +   dm - the DM object
266201729b5cSPatrick Sanan .   l - the local vector
266301729b5cSPatrick 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.
266401729b5cSPatrick Sanan -   g - the global vector
266501729b5cSPatrick Sanan 
266601729b5cSPatrick Sanan     Notes:
266701729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
266801729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
266901729b5cSPatrick Sanan 
267001729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
267101729b5cSPatrick 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.
267201729b5cSPatrick Sanan 
267301729b5cSPatrick Sanan     Level: beginner
267401729b5cSPatrick Sanan 
267501729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
267601729b5cSPatrick Sanan 
267701729b5cSPatrick Sanan @*/
267801729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
267901729b5cSPatrick Sanan {
268001729b5cSPatrick Sanan   PetscErrorCode ierr;
268101729b5cSPatrick Sanan 
268201729b5cSPatrick Sanan   PetscFunctionBegin;
268301729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
268401729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
268501729b5cSPatrick Sanan   PetscFunctionReturn(0);
268601729b5cSPatrick Sanan }
26874c274da1SToby Isaac 
268847c6ae99SBarry Smith /*@
268901729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
26909a42bb27SBarry Smith 
2691d083f849SBarry Smith     Neighbor-wise Collective on dm
26929a42bb27SBarry Smith 
26939a42bb27SBarry Smith     Input Parameters:
26949a42bb27SBarry Smith +   dm - the DM object
2695f6813fd5SJed Brown .   l - the local vector
26961eb28f2eSBarry 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.
26971eb28f2eSBarry Smith -   g - the global vector
26989a42bb27SBarry Smith 
269995452b02SPatrick Sanan     Notes:
270095452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
270184330215SMatthew 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.
27029a42bb27SBarry Smith 
270301729b5cSPatrick Sanan     Level: intermediate
27049a42bb27SBarry Smith 
270501729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
27069a42bb27SBarry Smith 
27079a42bb27SBarry Smith @*/
27087087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
27099a42bb27SBarry Smith {
27107128ae9fSMatthew G Knepley   PetscSF                 sf;
271184330215SMatthew G. Knepley   PetscSection            s, gs;
2712d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2713ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2714ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2715ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2716fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
271784330215SMatthew G. Knepley   PetscErrorCode          ierr;
27189a42bb27SBarry Smith 
27199a42bb27SBarry Smith   PetscFunctionBegin;
2720171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2721d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2722d4d07f1eSToby Isaac     if (link->beginhook) {
2723d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2724d4d07f1eSToby Isaac     }
2725d4d07f1eSToby Isaac   }
27264c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
27271bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
272892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27297128ae9fSMatthew G Knepley   switch (mode) {
27307128ae9fSMatthew G Knepley   case INSERT_VALUES:
27317128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2732304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
273384330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27347128ae9fSMatthew G Knepley   case ADD_VALUES:
27357128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2736304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
273784330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27387128ae9fSMatthew G Knepley   default:
273982f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27407128ae9fSMatthew G Knepley   }
2741ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2742ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2743ca3d3a14SMatthew G. Knepley     if (transform) {
2744ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2745ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2746ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2747ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2748fa88e482SJed Brown     } else if (isInsert) {
2749ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2750fa88e482SJed Brown     } else {
2751fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2752fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2753ca3d3a14SMatthew G. Knepley     }
2754fa88e482SJed Brown     if (s && isInsert) {
27557128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2756fa88e482SJed Brown     } else {
2757fa88e482SJed Brown       ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2758fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2759fa88e482SJed Brown     }
2760ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2761a9b180a6SBarry Smith       ierr = PetscSFReduceBegin(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
276284330215SMatthew G. Knepley     } else if (s && isInsert) {
276384330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
276484330215SMatthew G. Knepley 
2765e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
276684330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
276784330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
276884330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2769b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
277084330215SMatthew G. Knepley 
277184330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
277203442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
277384330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2774b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
277584330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
277684330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2777b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
277803442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2779b3b16f48SMatthew 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);
2780b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2781b3b16f48SMatthew G. Knepley         if (!gcdof) {
278284330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2783b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2784b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
278584330215SMatthew G. Knepley           const PetscInt *cdofs;
278684330215SMatthew G. Knepley           PetscInt        cind = 0;
278784330215SMatthew G. Knepley 
278884330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2789b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
279084330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2791b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
279284330215SMatthew G. Knepley           }
2793b3b16f48SMatthew 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);
279484330215SMatthew G. Knepley       }
2795ca3d3a14SMatthew G. Knepley     }
2796fa88e482SJed Brown     if (g_inplace) {
2797fa88e482SJed Brown       ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
2798fa88e482SJed Brown     } else {
27997128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2800fa88e482SJed Brown     }
2801ca3d3a14SMatthew G. Knepley     if (transform) {
2802ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2803ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2804fa88e482SJed Brown     } else if (l_inplace) {
2805fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2806ca3d3a14SMatthew G. Knepley     } else {
2807ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2808ca3d3a14SMatthew G. Knepley     }
28097128ae9fSMatthew G Knepley   } else {
2810b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2811843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28127128ae9fSMatthew G Knepley   }
28139a42bb27SBarry Smith   PetscFunctionReturn(0);
28149a42bb27SBarry Smith }
28159a42bb27SBarry Smith 
28169a42bb27SBarry Smith /*@
28179a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
281847c6ae99SBarry Smith 
2819d083f849SBarry Smith     Neighbor-wise Collective on dm
282047c6ae99SBarry Smith 
282147c6ae99SBarry Smith     Input Parameters:
282247c6ae99SBarry Smith +   dm - the DM object
2823f6813fd5SJed Brown .   l - the local vector
282447c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2825f6813fd5SJed Brown -   g - the global vector
282647c6ae99SBarry Smith 
282701729b5cSPatrick Sanan     Level: intermediate
282847c6ae99SBarry Smith 
2829e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
283047c6ae99SBarry Smith 
283147c6ae99SBarry Smith @*/
28327087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
283347c6ae99SBarry Smith {
28347128ae9fSMatthew G Knepley   PetscSF                 sf;
283584330215SMatthew G. Knepley   PetscSection            s;
2836d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2837ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
283884330215SMatthew G. Knepley   PetscErrorCode          ierr;
283947c6ae99SBarry Smith 
284047c6ae99SBarry Smith   PetscFunctionBegin;
2841171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28421bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
284392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28447128ae9fSMatthew G Knepley   switch (mode) {
28457128ae9fSMatthew G Knepley   case INSERT_VALUES:
28467128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
284784330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28487128ae9fSMatthew G Knepley   case ADD_VALUES:
28497128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
285084330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28517128ae9fSMatthew G Knepley   default:
285282f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28537128ae9fSMatthew G Knepley   }
285484330215SMatthew G. Knepley   if (sf && !isInsert) {
2855ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2856ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2857ca3d3a14SMatthew G. Knepley     Vec                tmpl;
285884330215SMatthew G. Knepley 
2859ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2860ca3d3a14SMatthew G. Knepley     if (transform) {
2861ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2862ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2863ca3d3a14SMatthew G. Knepley     } else {
2864fa88e482SJed Brown       ierr = VecGetArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2865ca3d3a14SMatthew G. Knepley     }
2866fa88e482SJed Brown     ierr = VecGetArrayInPlace(g, &gArray);CHKERRQ(ierr);
2867a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2868ca3d3a14SMatthew G. Knepley     if (transform) {
2869ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2870ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2871ca3d3a14SMatthew G. Knepley     } else {
2872fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2873ca3d3a14SMatthew G. Knepley     }
2874fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
287584330215SMatthew G. Knepley   } else if (s && isInsert) {
28767128ae9fSMatthew G Knepley   } else {
2877b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2878843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28797128ae9fSMatthew G Knepley   }
2880d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2881d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2882d4d07f1eSToby Isaac   }
288347c6ae99SBarry Smith   PetscFunctionReturn(0);
288447c6ae99SBarry Smith }
288547c6ae99SBarry Smith 
2886f089877aSRichard Tran Mills /*@
2887bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2888bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2889d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2890f089877aSRichard Tran Mills 
2891d083f849SBarry Smith    Neighbor-wise Collective on dm
2892f089877aSRichard Tran Mills 
2893f089877aSRichard Tran Mills    Input Parameters:
2894f089877aSRichard Tran Mills +  dm - the DM object
2895bc0a1609SRichard Tran Mills .  g - the original local vector
2896bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2897f089877aSRichard Tran Mills 
2898bc0a1609SRichard Tran Mills    Output Parameter:
2899bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2900f089877aSRichard Tran Mills 
2901f089877aSRichard Tran Mills    Level: intermediate
2902f089877aSRichard Tran Mills 
2903bc0a1609SRichard Tran Mills    Notes:
2904bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2905bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2906bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2907bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2908bc0a1609SRichard Tran Mills 
2909bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2910f089877aSRichard Tran Mills 
2911f089877aSRichard Tran Mills @*/
2912f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2913f089877aSRichard Tran Mills {
2914f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2915f089877aSRichard Tran Mills 
2916f089877aSRichard Tran Mills   PetscFunctionBegin;
2917f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2918bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2919f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2920f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2921f089877aSRichard Tran Mills }
2922f089877aSRichard Tran Mills 
2923f089877aSRichard Tran Mills /*@
2924bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2925bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2926d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2927f089877aSRichard Tran Mills 
2928d083f849SBarry Smith    Neighbor-wise Collective on dm
2929f089877aSRichard Tran Mills 
2930f089877aSRichard Tran Mills    Input Parameters:
2931bc0a1609SRichard Tran Mills +  da - the DM object
2932bc0a1609SRichard Tran Mills .  g - the original local vector
2933bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2934f089877aSRichard Tran Mills 
2935bc0a1609SRichard Tran Mills    Output Parameter:
2936bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2937f089877aSRichard Tran Mills 
2938f089877aSRichard Tran Mills    Level: intermediate
2939f089877aSRichard Tran Mills 
2940bc0a1609SRichard Tran Mills    Notes:
2941bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2942bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2943bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2944bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2945bc0a1609SRichard Tran Mills 
2946bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2947f089877aSRichard Tran Mills 
2948f089877aSRichard Tran Mills @*/
2949f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2950f089877aSRichard Tran Mills {
2951f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2952f089877aSRichard Tran Mills 
2953f089877aSRichard Tran Mills   PetscFunctionBegin;
2954f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2955bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2956f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2957f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2958f089877aSRichard Tran Mills }
2959f089877aSRichard Tran Mills 
2960f089877aSRichard Tran Mills 
296147c6ae99SBarry Smith /*@
296247c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
296347c6ae99SBarry Smith 
2964d083f849SBarry Smith     Collective on dm
296547c6ae99SBarry Smith 
296647c6ae99SBarry Smith     Input Parameter:
296747c6ae99SBarry Smith +   dm - the DM object
296891d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
296947c6ae99SBarry Smith 
297047c6ae99SBarry Smith     Output Parameter:
297147c6ae99SBarry Smith .   dmc - the coarsened DM
297247c6ae99SBarry Smith 
297347c6ae99SBarry Smith     Level: developer
297447c6ae99SBarry Smith 
2975e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
297647c6ae99SBarry Smith 
297747c6ae99SBarry Smith @*/
29787087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
297947c6ae99SBarry Smith {
298047c6ae99SBarry Smith   PetscErrorCode    ierr;
2981b17ce1afSJed Brown   DMCoarsenHookLink link;
298247c6ae99SBarry Smith 
298347c6ae99SBarry Smith   PetscFunctionBegin;
2984171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2985b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
298647a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
298747c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2988b9d85ea2SLisandro Dalcin   if (*dmc) {
2989a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
299043842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
29918cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2992644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
29930598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2994656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
2995e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
2996b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
2997b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
2998b17ce1afSJed Brown     }
2999b9d85ea2SLisandro Dalcin   }
300047a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3001b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3002b17ce1afSJed Brown   PetscFunctionReturn(0);
3003b17ce1afSJed Brown }
3004b17ce1afSJed Brown 
3005bb9467b5SJed Brown /*@C
3006b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3007b17ce1afSJed Brown 
3008b17ce1afSJed Brown    Logically Collective
3009b17ce1afSJed Brown 
3010b17ce1afSJed Brown    Input Arguments:
3011b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3012b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3013b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
30140298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3015b17ce1afSJed Brown 
3016b17ce1afSJed Brown    Calling sequence of coarsenhook:
3017b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3018b17ce1afSJed Brown 
3019b17ce1afSJed Brown +  fine - fine level DM
3020b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3021b17ce1afSJed Brown -  ctx - optional user-defined function context
3022b17ce1afSJed Brown 
3023b17ce1afSJed Brown    Calling sequence for restricthook:
3024c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3025b17ce1afSJed Brown 
3026b17ce1afSJed Brown +  fine - fine level DM
3027b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3028c833c3b5SJed Brown .  rscale - scaling vector for restriction
3029c833c3b5SJed Brown .  inject - matrix restricting by injection
3030b17ce1afSJed Brown .  coarse - coarse level DM to update
3031b17ce1afSJed Brown -  ctx - optional user-defined function context
3032b17ce1afSJed Brown 
3033b17ce1afSJed Brown    Level: advanced
3034b17ce1afSJed Brown 
3035b17ce1afSJed Brown    Notes:
3036b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3037b17ce1afSJed Brown 
3038b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3039b17ce1afSJed Brown 
3040b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3041b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3042b17ce1afSJed Brown 
3043bb9467b5SJed Brown    This function is currently not available from Fortran.
3044bb9467b5SJed Brown 
3045dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3046b17ce1afSJed Brown @*/
3047b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3048b17ce1afSJed Brown {
3049b17ce1afSJed Brown   PetscErrorCode    ierr;
3050b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3051b17ce1afSJed Brown 
3052b17ce1afSJed Brown   PetscFunctionBegin;
3053b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
30541e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
30551e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
30561e3d8eccSJed Brown   }
305795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3058b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3059b17ce1afSJed Brown   link->restricthook = restricthook;
3060b17ce1afSJed Brown   link->ctx          = ctx;
30610298fd71SBarry Smith   link->next         = NULL;
3062b17ce1afSJed Brown   *p                 = link;
3063b17ce1afSJed Brown   PetscFunctionReturn(0);
3064b17ce1afSJed Brown }
3065b17ce1afSJed Brown 
3066dc822a44SJed Brown /*@C
3067dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3068dc822a44SJed Brown 
3069dc822a44SJed Brown    Logically Collective
3070dc822a44SJed Brown 
3071dc822a44SJed Brown    Input Arguments:
3072dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3073dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3074dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3075dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3076dc822a44SJed Brown 
3077dc822a44SJed Brown    Level: advanced
3078dc822a44SJed Brown 
3079dc822a44SJed Brown    Notes:
3080dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3081dc822a44SJed Brown 
3082dc822a44SJed Brown    This function is currently not available from Fortran.
3083dc822a44SJed Brown 
3084dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3085dc822a44SJed Brown @*/
3086dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3087dc822a44SJed Brown {
3088dc822a44SJed Brown   PetscErrorCode    ierr;
3089dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3090dc822a44SJed Brown 
3091dc822a44SJed Brown   PetscFunctionBegin;
3092dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3093dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3094dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3095dc822a44SJed Brown       link = *p;
3096dc822a44SJed Brown       *p = link->next;
3097dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3098dc822a44SJed Brown       break;
3099dc822a44SJed Brown     }
3100dc822a44SJed Brown   }
3101dc822a44SJed Brown   PetscFunctionReturn(0);
3102dc822a44SJed Brown }
3103dc822a44SJed Brown 
3104dc822a44SJed Brown 
3105b17ce1afSJed Brown /*@
3106b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3107b17ce1afSJed Brown 
3108b17ce1afSJed Brown    Collective if any hooks are
3109b17ce1afSJed Brown 
3110b17ce1afSJed Brown    Input Arguments:
3111b17ce1afSJed Brown +  fine - finer DM to use as a base
3112b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3113e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3114b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3115e91eccc2SStefano Zampini -  coarse - coarser DM to update
3116b17ce1afSJed Brown 
3117b17ce1afSJed Brown    Level: developer
3118b17ce1afSJed Brown 
3119b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3120b17ce1afSJed Brown @*/
3121b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3122b17ce1afSJed Brown {
3123b17ce1afSJed Brown   PetscErrorCode    ierr;
3124b17ce1afSJed Brown   DMCoarsenHookLink link;
3125b17ce1afSJed Brown 
3126b17ce1afSJed Brown   PetscFunctionBegin;
3127b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
31288865f1eaSKarl Rupp     if (link->restricthook) {
31298865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
31308865f1eaSKarl Rupp     }
3131b17ce1afSJed Brown   }
313247c6ae99SBarry Smith   PetscFunctionReturn(0);
313347c6ae99SBarry Smith }
313447c6ae99SBarry Smith 
3135bb9467b5SJed Brown /*@C
3136be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
31375dbd56e3SPeter Brune 
3138d083f849SBarry Smith    Logically Collective on global
31395dbd56e3SPeter Brune 
31405dbd56e3SPeter Brune    Input Arguments:
31415dbd56e3SPeter Brune +  global - global DM
3142ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
31435dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
31440298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
31455dbd56e3SPeter Brune 
3146ec4806b8SPeter Brune 
3147ec4806b8SPeter Brune    Calling sequence for ddhook:
3148ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3149ec4806b8SPeter Brune 
3150ec4806b8SPeter Brune +  global - global DM
3151ec4806b8SPeter Brune .  block  - block DM
3152ec4806b8SPeter Brune -  ctx - optional user-defined function context
3153ec4806b8SPeter Brune 
31545dbd56e3SPeter Brune    Calling sequence for restricthook:
3155ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
31565dbd56e3SPeter Brune 
31575dbd56e3SPeter Brune +  global - global DM
31585dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
31595dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3160ec4806b8SPeter Brune .  block  - block DM
31615dbd56e3SPeter Brune -  ctx - optional user-defined function context
31625dbd56e3SPeter Brune 
31635dbd56e3SPeter Brune    Level: advanced
31645dbd56e3SPeter Brune 
31655dbd56e3SPeter Brune    Notes:
3166ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
31675dbd56e3SPeter Brune 
31685dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
31695dbd56e3SPeter Brune 
31705dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3171ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
31725dbd56e3SPeter Brune 
3173bb9467b5SJed Brown    This function is currently not available from Fortran.
3174bb9467b5SJed Brown 
31755dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
31765dbd56e3SPeter Brune @*/
3177be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
31785dbd56e3SPeter Brune {
31795dbd56e3SPeter Brune   PetscErrorCode      ierr;
3180be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
31815dbd56e3SPeter Brune 
31825dbd56e3SPeter Brune   PetscFunctionBegin;
31835dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3184b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3185b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3186b3a6b972SJed Brown   }
318795dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
31885dbd56e3SPeter Brune   link->restricthook = restricthook;
3189be081cd6SPeter Brune   link->ddhook       = ddhook;
31905dbd56e3SPeter Brune   link->ctx          = ctx;
31910298fd71SBarry Smith   link->next         = NULL;
31925dbd56e3SPeter Brune   *p                 = link;
31935dbd56e3SPeter Brune   PetscFunctionReturn(0);
31945dbd56e3SPeter Brune }
31955dbd56e3SPeter Brune 
3196b3a6b972SJed Brown /*@C
3197b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3198b3a6b972SJed Brown 
3199b3a6b972SJed Brown    Logically Collective
3200b3a6b972SJed Brown 
3201b3a6b972SJed Brown    Input Arguments:
3202b3a6b972SJed Brown +  global - global DM
3203b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3204b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3205b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3206b3a6b972SJed Brown 
3207b3a6b972SJed Brown    Level: advanced
3208b3a6b972SJed Brown 
3209b3a6b972SJed Brown    Notes:
3210b3a6b972SJed Brown 
3211b3a6b972SJed Brown    This function is currently not available from Fortran.
3212b3a6b972SJed Brown 
3213b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3214b3a6b972SJed Brown @*/
3215b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3216b3a6b972SJed Brown {
3217b3a6b972SJed Brown   PetscErrorCode      ierr;
3218b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3219b3a6b972SJed Brown 
3220b3a6b972SJed Brown   PetscFunctionBegin;
3221b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3222b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3223b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3224b3a6b972SJed Brown       link = *p;
3225b3a6b972SJed Brown       *p = link->next;
3226b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3227b3a6b972SJed Brown       break;
3228b3a6b972SJed Brown     }
3229b3a6b972SJed Brown   }
3230b3a6b972SJed Brown   PetscFunctionReturn(0);
3231b3a6b972SJed Brown }
3232b3a6b972SJed Brown 
32335dbd56e3SPeter Brune /*@
3234be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
32355dbd56e3SPeter Brune 
32365dbd56e3SPeter Brune    Collective if any hooks are
32375dbd56e3SPeter Brune 
32385dbd56e3SPeter Brune    Input Arguments:
32395dbd56e3SPeter Brune +  fine - finer DM to use as a base
3240be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3241be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
32425dbd56e3SPeter Brune -  coarse - coarer DM to update
32435dbd56e3SPeter Brune 
32445dbd56e3SPeter Brune    Level: developer
32455dbd56e3SPeter Brune 
32465dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
32475dbd56e3SPeter Brune @*/
3248be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
32495dbd56e3SPeter Brune {
32505dbd56e3SPeter Brune   PetscErrorCode      ierr;
3251be081cd6SPeter Brune   DMSubDomainHookLink link;
32525dbd56e3SPeter Brune 
32535dbd56e3SPeter Brune   PetscFunctionBegin;
3254be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
32558865f1eaSKarl Rupp     if (link->restricthook) {
32568865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
32578865f1eaSKarl Rupp     }
32585dbd56e3SPeter Brune   }
32595dbd56e3SPeter Brune   PetscFunctionReturn(0);
32605dbd56e3SPeter Brune }
32615dbd56e3SPeter Brune 
32625fe1f584SPeter Brune /*@
32636a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
32645fe1f584SPeter Brune 
32655fe1f584SPeter Brune     Not Collective
32665fe1f584SPeter Brune 
32675fe1f584SPeter Brune     Input Parameter:
32685fe1f584SPeter Brune .   dm - the DM object
32695fe1f584SPeter Brune 
32705fe1f584SPeter Brune     Output Parameter:
32716a7d9d85SPeter Brune .   level - number of coarsenings
32725fe1f584SPeter Brune 
32735fe1f584SPeter Brune     Level: developer
32745fe1f584SPeter Brune 
32756a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32765fe1f584SPeter Brune 
32775fe1f584SPeter Brune @*/
32785fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
32795fe1f584SPeter Brune {
32805fe1f584SPeter Brune   PetscFunctionBegin;
32815fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3282b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
32835fe1f584SPeter Brune   *level = dm->leveldown;
32845fe1f584SPeter Brune   PetscFunctionReturn(0);
32855fe1f584SPeter Brune }
32865fe1f584SPeter Brune 
32879a64c4a8SMatthew G. Knepley /*@
32889a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
32899a64c4a8SMatthew G. Knepley 
32909a64c4a8SMatthew G. Knepley     Not Collective
32919a64c4a8SMatthew G. Knepley 
32929a64c4a8SMatthew G. Knepley     Input Parameters:
32939a64c4a8SMatthew G. Knepley +   dm - the DM object
32949a64c4a8SMatthew G. Knepley -   level - number of coarsenings
32959a64c4a8SMatthew G. Knepley 
32969a64c4a8SMatthew G. Knepley     Level: developer
32979a64c4a8SMatthew G. Knepley 
32989a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32999a64c4a8SMatthew G. Knepley @*/
33009a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
33019a64c4a8SMatthew G. Knepley {
33029a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
33039a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33049a64c4a8SMatthew G. Knepley   dm->leveldown = level;
33059a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
33069a64c4a8SMatthew G. Knepley }
33079a64c4a8SMatthew G. Knepley 
33085fe1f584SPeter Brune 
33095fe1f584SPeter Brune 
331047c6ae99SBarry Smith /*@C
331147c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
331247c6ae99SBarry Smith 
3313d083f849SBarry Smith     Collective on dm
331447c6ae99SBarry Smith 
331547c6ae99SBarry Smith     Input Parameter:
331647c6ae99SBarry Smith +   dm - the DM object
331747c6ae99SBarry Smith -   nlevels - the number of levels of refinement
331847c6ae99SBarry Smith 
331947c6ae99SBarry Smith     Output Parameter:
332047c6ae99SBarry Smith .   dmf - the refined DM hierarchy
332147c6ae99SBarry Smith 
332247c6ae99SBarry Smith     Level: developer
332347c6ae99SBarry Smith 
3324e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
332547c6ae99SBarry Smith 
332647c6ae99SBarry Smith @*/
33277087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
332847c6ae99SBarry Smith {
332947c6ae99SBarry Smith   PetscErrorCode ierr;
333047c6ae99SBarry Smith 
333147c6ae99SBarry Smith   PetscFunctionBegin;
3332171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3333ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
333447c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3335b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
333647c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
333747c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
333847c6ae99SBarry Smith   } else if (dm->ops->refine) {
333947c6ae99SBarry Smith     PetscInt i;
334047c6ae99SBarry Smith 
3341ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
334247c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3343ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
334447c6ae99SBarry Smith     }
3345ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
334647c6ae99SBarry Smith   PetscFunctionReturn(0);
334747c6ae99SBarry Smith }
334847c6ae99SBarry Smith 
334947c6ae99SBarry Smith /*@C
335047c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
335147c6ae99SBarry Smith 
3352d083f849SBarry Smith     Collective on dm
335347c6ae99SBarry Smith 
335447c6ae99SBarry Smith     Input Parameter:
335547c6ae99SBarry Smith +   dm - the DM object
335647c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
335747c6ae99SBarry Smith 
335847c6ae99SBarry Smith     Output Parameter:
335947c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
336047c6ae99SBarry Smith 
336147c6ae99SBarry Smith     Level: developer
336247c6ae99SBarry Smith 
3363e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
336447c6ae99SBarry Smith 
336547c6ae99SBarry Smith @*/
33667087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
336747c6ae99SBarry Smith {
336847c6ae99SBarry Smith   PetscErrorCode ierr;
336947c6ae99SBarry Smith 
337047c6ae99SBarry Smith   PetscFunctionBegin;
3371171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3372ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
337347c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
337447c6ae99SBarry Smith   PetscValidPointer(dmc,3);
337547c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
337647c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
337747c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
337847c6ae99SBarry Smith     PetscInt i;
337947c6ae99SBarry Smith 
3380ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
338147c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3382ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
338347c6ae99SBarry Smith     }
3384ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
338547c6ae99SBarry Smith   PetscFunctionReturn(0);
338647c6ae99SBarry Smith }
338747c6ae99SBarry Smith 
33881a266240SBarry Smith /*@C
33891a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
33901a266240SBarry Smith 
33911a266240SBarry Smith     Not Collective
33921a266240SBarry Smith 
33931a266240SBarry Smith     Input Parameters:
33941a266240SBarry Smith +   dm - the DM object
33951a266240SBarry Smith -   destroy - the destroy function
33961a266240SBarry Smith 
33971a266240SBarry Smith     Level: intermediate
33981a266240SBarry Smith 
3399e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
34001a266240SBarry Smith 
3401f07f9ceaSJed Brown @*/
34021a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
34031a266240SBarry Smith {
34041a266240SBarry Smith   PetscFunctionBegin;
3405171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34061a266240SBarry Smith   dm->ctxdestroy = destroy;
34071a266240SBarry Smith   PetscFunctionReturn(0);
34081a266240SBarry Smith }
34091a266240SBarry Smith 
3410b07ff414SBarry Smith /*@
34111b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
341247c6ae99SBarry Smith 
341347c6ae99SBarry Smith     Not Collective
341447c6ae99SBarry Smith 
341547c6ae99SBarry Smith     Input Parameters:
341647c6ae99SBarry Smith +   dm - the DM object
341747c6ae99SBarry Smith -   ctx - the user context
341847c6ae99SBarry Smith 
341947c6ae99SBarry Smith     Level: intermediate
342047c6ae99SBarry Smith 
3421e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
342247c6ae99SBarry Smith 
342347c6ae99SBarry Smith @*/
34241b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
342547c6ae99SBarry Smith {
342647c6ae99SBarry Smith   PetscFunctionBegin;
3427171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
342847c6ae99SBarry Smith   dm->ctx = ctx;
342947c6ae99SBarry Smith   PetscFunctionReturn(0);
343047c6ae99SBarry Smith }
343147c6ae99SBarry Smith 
343247c6ae99SBarry Smith /*@
34331b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
343447c6ae99SBarry Smith 
343547c6ae99SBarry Smith     Not Collective
343647c6ae99SBarry Smith 
343747c6ae99SBarry Smith     Input Parameter:
343847c6ae99SBarry Smith .   dm - the DM object
343947c6ae99SBarry Smith 
344047c6ae99SBarry Smith     Output Parameter:
344147c6ae99SBarry Smith .   ctx - the user context
344247c6ae99SBarry Smith 
344347c6ae99SBarry Smith     Level: intermediate
344447c6ae99SBarry Smith 
3445e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
344647c6ae99SBarry Smith 
344747c6ae99SBarry Smith @*/
34481b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
344947c6ae99SBarry Smith {
345047c6ae99SBarry Smith   PetscFunctionBegin;
3451171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34521b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
345347c6ae99SBarry Smith   PetscFunctionReturn(0);
345447c6ae99SBarry Smith }
345547c6ae99SBarry Smith 
345608da532bSDmitry Karpeev /*@C
3457df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
345808da532bSDmitry Karpeev 
3459d083f849SBarry Smith     Logically Collective on dm
346008da532bSDmitry Karpeev 
346108da532bSDmitry Karpeev     Input Parameter:
346208da532bSDmitry Karpeev +   dm - the DM object
34630298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
346408da532bSDmitry Karpeev 
346508da532bSDmitry Karpeev     Level: intermediate
346608da532bSDmitry Karpeev 
3467835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
346808da532bSDmitry Karpeev          DMSetJacobian()
346908da532bSDmitry Karpeev 
347008da532bSDmitry Karpeev @*/
347108da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
347208da532bSDmitry Karpeev {
347308da532bSDmitry Karpeev   PetscFunctionBegin;
34745a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
347508da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
347608da532bSDmitry Karpeev   PetscFunctionReturn(0);
347708da532bSDmitry Karpeev }
347808da532bSDmitry Karpeev 
347908da532bSDmitry Karpeev /*@
348008da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
348108da532bSDmitry Karpeev 
348208da532bSDmitry Karpeev     Not Collective
348308da532bSDmitry Karpeev 
348408da532bSDmitry Karpeev     Input Parameter:
348508da532bSDmitry Karpeev .   dm - the DM object to destroy
348608da532bSDmitry Karpeev 
348708da532bSDmitry Karpeev     Output Parameter:
348808da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
348908da532bSDmitry Karpeev 
349008da532bSDmitry Karpeev     Level: developer
349108da532bSDmitry Karpeev 
349274e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
349308da532bSDmitry Karpeev 
349408da532bSDmitry Karpeev @*/
349508da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
349608da532bSDmitry Karpeev {
349708da532bSDmitry Karpeev   PetscFunctionBegin;
34985a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3499534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
350008da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
350108da532bSDmitry Karpeev   PetscFunctionReturn(0);
350208da532bSDmitry Karpeev }
350308da532bSDmitry Karpeev 
350408da532bSDmitry Karpeev /*@C
350508da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
350608da532bSDmitry Karpeev 
3507d083f849SBarry Smith     Logically Collective on dm
350808da532bSDmitry Karpeev 
350908da532bSDmitry Karpeev     Input Parameters:
3510907376e6SBarry Smith .   dm - the DM object
351108da532bSDmitry Karpeev 
351208da532bSDmitry Karpeev     Output parameters:
351308da532bSDmitry Karpeev +   xl - lower bound
351408da532bSDmitry Karpeev -   xu - upper bound
351508da532bSDmitry Karpeev 
3516907376e6SBarry Smith     Level: advanced
3517907376e6SBarry Smith 
351895452b02SPatrick Sanan     Notes:
351995452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
352008da532bSDmitry Karpeev 
352174e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
352208da532bSDmitry Karpeev 
352308da532bSDmitry Karpeev @*/
352408da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
352508da532bSDmitry Karpeev {
352608da532bSDmitry Karpeev   PetscErrorCode ierr;
35275fd66863SKarl Rupp 
352808da532bSDmitry Karpeev   PetscFunctionBegin;
35295a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
353008da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
35315a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3532b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
353308da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
353408da532bSDmitry Karpeev   PetscFunctionReturn(0);
353508da532bSDmitry Karpeev }
353608da532bSDmitry Karpeev 
3537b0ae01b7SPeter Brune /*@
3538b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3539b0ae01b7SPeter Brune 
3540b0ae01b7SPeter Brune     Not Collective
3541b0ae01b7SPeter Brune 
3542b0ae01b7SPeter Brune     Input Parameter:
3543b0ae01b7SPeter Brune .   dm - the DM object
3544b0ae01b7SPeter Brune 
3545b0ae01b7SPeter Brune     Output Parameter:
3546b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3547b0ae01b7SPeter Brune 
3548b0ae01b7SPeter Brune     Level: developer
3549b0ae01b7SPeter Brune 
35501565f0a7SPatrick Sanan .seealso DMCreateColoring()
3551b0ae01b7SPeter Brune 
3552b0ae01b7SPeter Brune @*/
3553b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3554b0ae01b7SPeter Brune {
3555b0ae01b7SPeter Brune   PetscFunctionBegin;
35565a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3557534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3558b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3559b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3560b0ae01b7SPeter Brune }
3561b0ae01b7SPeter Brune 
35623ad4599aSBarry Smith /*@
35633ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
35643ad4599aSBarry Smith 
35653ad4599aSBarry Smith     Not Collective
35663ad4599aSBarry Smith 
35673ad4599aSBarry Smith     Input Parameter:
35683ad4599aSBarry Smith .   dm - the DM object
35693ad4599aSBarry Smith 
35703ad4599aSBarry Smith     Output Parameter:
35713ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
35723ad4599aSBarry Smith 
35733ad4599aSBarry Smith     Level: developer
35743ad4599aSBarry Smith 
35751565f0a7SPatrick Sanan .seealso DMCreateRestriction()
35763ad4599aSBarry Smith 
35773ad4599aSBarry Smith @*/
35783ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
35793ad4599aSBarry Smith {
35803ad4599aSBarry Smith   PetscFunctionBegin;
35815a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3582534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35833ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
35843ad4599aSBarry Smith   PetscFunctionReturn(0);
35853ad4599aSBarry Smith }
35863ad4599aSBarry Smith 
3587a7058e45SLawrence Mitchell 
3588a7058e45SLawrence Mitchell /*@
3589a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3590a7058e45SLawrence Mitchell 
3591a7058e45SLawrence Mitchell     Not Collective
3592a7058e45SLawrence Mitchell 
3593a7058e45SLawrence Mitchell     Input Parameter:
3594a7058e45SLawrence Mitchell .   dm - the DM object
3595a7058e45SLawrence Mitchell 
3596a7058e45SLawrence Mitchell     Output Parameter:
3597a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3598a7058e45SLawrence Mitchell 
3599a7058e45SLawrence Mitchell     Level: developer
3600a7058e45SLawrence Mitchell 
36011565f0a7SPatrick Sanan .seealso DMCreateInjection()
3602a7058e45SLawrence Mitchell 
3603a7058e45SLawrence Mitchell @*/
3604a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3605a7058e45SLawrence Mitchell {
36064a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
36075a84ad33SLisandro Dalcin 
3608a7058e45SLawrence Mitchell   PetscFunctionBegin;
36095a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3610534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
36115a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
36124a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
36135a84ad33SLisandro Dalcin   } else {
36145a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
36155a84ad33SLisandro Dalcin   }
3616a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3617a7058e45SLawrence Mitchell }
3618a7058e45SLawrence Mitchell 
36190298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3620264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3621264ace61SBarry Smith 
3622264ace61SBarry Smith /*@C
3623264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3624264ace61SBarry Smith 
3625d083f849SBarry Smith   Collective on dm
3626264ace61SBarry Smith 
3627264ace61SBarry Smith   Input Parameters:
3628264ace61SBarry Smith + dm     - The DM object
3629264ace61SBarry Smith - method - The name of the DM type
3630264ace61SBarry Smith 
3631264ace61SBarry Smith   Options Database Key:
3632264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3633264ace61SBarry Smith 
3634264ace61SBarry Smith   Notes:
3635e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3636264ace61SBarry Smith 
3637264ace61SBarry Smith   Level: intermediate
3638264ace61SBarry Smith 
3639264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3640264ace61SBarry Smith @*/
364119fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3642264ace61SBarry Smith {
3643264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3644264ace61SBarry Smith   PetscBool      match;
3645264ace61SBarry Smith   PetscErrorCode ierr;
3646264ace61SBarry Smith 
3647264ace61SBarry Smith   PetscFunctionBegin;
3648264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3649251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3650264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3651264ace61SBarry Smith 
36520f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
36531c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3654ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3655264ace61SBarry Smith 
3656264ace61SBarry Smith   if (dm->ops->destroy) {
3657264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3658264ace61SBarry Smith   }
3659d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3660264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3661d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3662264ace61SBarry Smith   PetscFunctionReturn(0);
3663264ace61SBarry Smith }
3664264ace61SBarry Smith 
3665264ace61SBarry Smith /*@C
3666264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3667264ace61SBarry Smith 
3668264ace61SBarry Smith   Not Collective
3669264ace61SBarry Smith 
3670264ace61SBarry Smith   Input Parameter:
3671264ace61SBarry Smith . dm  - The DM
3672264ace61SBarry Smith 
3673264ace61SBarry Smith   Output Parameter:
3674264ace61SBarry Smith . type - The DM type name
3675264ace61SBarry Smith 
3676264ace61SBarry Smith   Level: intermediate
3677264ace61SBarry Smith 
3678264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3679264ace61SBarry Smith @*/
368019fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3681264ace61SBarry Smith {
3682264ace61SBarry Smith   PetscErrorCode ierr;
3683264ace61SBarry Smith 
3684264ace61SBarry Smith   PetscFunctionBegin;
3685264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3686c959eef4SJed Brown   PetscValidPointer(type,2);
3687607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3688264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3689264ace61SBarry Smith   PetscFunctionReturn(0);
3690264ace61SBarry Smith }
3691264ace61SBarry Smith 
369267a56275SMatthew G Knepley /*@C
369367a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
369467a56275SMatthew G Knepley 
3695d083f849SBarry Smith   Collective on dm
369667a56275SMatthew G Knepley 
369767a56275SMatthew G Knepley   Input Parameters:
369867a56275SMatthew G Knepley + dm - the DM
369967a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
370067a56275SMatthew G Knepley 
370167a56275SMatthew G Knepley   Output Parameter:
370267a56275SMatthew G Knepley . M - pointer to new DM
370367a56275SMatthew G Knepley 
370467a56275SMatthew G Knepley   Notes:
370567a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
370667a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
370767a56275SMatthew G Knepley   of the input DM.
370867a56275SMatthew G Knepley 
370967a56275SMatthew G Knepley   Level: intermediate
371067a56275SMatthew G Knepley 
371167a56275SMatthew G Knepley .seealso: DMCreate()
371267a56275SMatthew G Knepley @*/
371319fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
371467a56275SMatthew G Knepley {
371567a56275SMatthew G Knepley   DM             B;
371667a56275SMatthew G Knepley   char           convname[256];
3717c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
371867a56275SMatthew G Knepley   PetscErrorCode ierr;
371967a56275SMatthew G Knepley 
372067a56275SMatthew G Knepley   PetscFunctionBegin;
372167a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
372267a56275SMatthew G Knepley   PetscValidType(dm,1);
372367a56275SMatthew G Knepley   PetscValidPointer(M,3);
3724251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3725c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3726c067b6caSMatthew G. Knepley   if (sametype) {
3727c067b6caSMatthew G. Knepley     *M   = dm;
3728c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3729c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3730c067b6caSMatthew G. Knepley   } else {
37310298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
373267a56275SMatthew G Knepley 
373367a56275SMatthew G Knepley     /*
373467a56275SMatthew G Knepley        Order of precedence:
373567a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
373667a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
373767a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
373867a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
373967a56275SMatthew G Knepley        5) Use a really basic converter.
374067a56275SMatthew G Knepley     */
374167a56275SMatthew G Knepley 
374267a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3743a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3744a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3745a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3746a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3747a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37480005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
374967a56275SMatthew G Knepley     if (conv) goto foundconv;
375067a56275SMatthew G Knepley 
375167a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
375282f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
375367a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3754a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3755a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3756a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3757a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3758a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37590005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
376067a56275SMatthew G Knepley     if (conv) {
3761fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
376267a56275SMatthew G Knepley       goto foundconv;
376367a56275SMatthew G Knepley     }
376467a56275SMatthew G Knepley 
376567a56275SMatthew G Knepley #if 0
376667a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
376767a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3768fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
376967a56275SMatthew G Knepley     if (conv) goto foundconv;
377067a56275SMatthew G Knepley 
377167a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
377267a56275SMatthew G Knepley     if (dm->ops->convert) {
377367a56275SMatthew G Knepley       conv = dm->ops->convert;
377467a56275SMatthew G Knepley     }
377567a56275SMatthew G Knepley     if (conv) goto foundconv;
377667a56275SMatthew G Knepley #endif
377767a56275SMatthew G Knepley 
377867a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
377982f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
378067a56275SMatthew G Knepley 
378167a56275SMatthew G Knepley foundconv:
378267a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
378367a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
378412fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
378590b157c4SStefano Zampini     {
378690b157c4SStefano Zampini       PetscBool             isper;
378712fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
378812fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
378990b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
379090b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
379112fa691eSMatthew G. Knepley     }
379267a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
379367a56275SMatthew G Knepley   }
379467a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
379567a56275SMatthew G Knepley   PetscFunctionReturn(0);
379667a56275SMatthew G Knepley }
3797264ace61SBarry Smith 
3798264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3799264ace61SBarry Smith 
3800264ace61SBarry Smith /*@C
38011c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
38021c84c290SBarry Smith 
38031c84c290SBarry Smith   Not Collective
38041c84c290SBarry Smith 
38051c84c290SBarry Smith   Input Parameters:
38061c84c290SBarry Smith + name        - The name of a new user-defined creation routine
38071c84c290SBarry Smith - create_func - The creation routine itself
38081c84c290SBarry Smith 
38091c84c290SBarry Smith   Notes:
38101c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
38111c84c290SBarry Smith 
38121c84c290SBarry Smith 
38131c84c290SBarry Smith   Sample usage:
38141c84c290SBarry Smith .vb
3815bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
38161c84c290SBarry Smith .ve
38171c84c290SBarry Smith 
38181c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
38191c84c290SBarry Smith .vb
38201c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
38211c84c290SBarry Smith     DMSetType(DM,"my_da");
38221c84c290SBarry Smith .ve
38231c84c290SBarry Smith    or at runtime via the option
38241c84c290SBarry Smith .vb
38251c84c290SBarry Smith     -da_type my_da
38261c84c290SBarry Smith .ve
3827264ace61SBarry Smith 
3828264ace61SBarry Smith   Level: advanced
38291c84c290SBarry Smith 
3830bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
38311c84c290SBarry Smith 
3832264ace61SBarry Smith @*/
3833bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3834264ace61SBarry Smith {
3835264ace61SBarry Smith   PetscErrorCode ierr;
3836264ace61SBarry Smith 
3837264ace61SBarry Smith   PetscFunctionBegin;
38381d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3839a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3840264ace61SBarry Smith   PetscFunctionReturn(0);
3841264ace61SBarry Smith }
3842264ace61SBarry Smith 
3843b859378eSBarry Smith /*@C
384455849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3845b859378eSBarry Smith 
3846d083f849SBarry Smith   Collective on viewer
3847b859378eSBarry Smith 
3848b859378eSBarry Smith   Input Parameters:
3849b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3850b859378eSBarry Smith            some related function before a call to DMLoad().
3851b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3852b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3853b859378eSBarry Smith 
3854b859378eSBarry Smith    Level: intermediate
3855b859378eSBarry Smith 
3856b859378eSBarry Smith   Notes:
385755849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3858b859378eSBarry Smith 
3859b859378eSBarry Smith   Notes for advanced users:
3860b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3861b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3862b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3863b859378eSBarry Smith   format is
3864b859378eSBarry Smith .vb
3865b859378eSBarry Smith      has not yet been determined
3866b859378eSBarry Smith .ve
3867b859378eSBarry Smith 
3868b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3869b859378eSBarry Smith @*/
3870b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3871b859378eSBarry Smith {
38729331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3873b859378eSBarry Smith   PetscErrorCode ierr;
3874b859378eSBarry Smith 
3875b859378eSBarry Smith   PetscFunctionBegin;
3876b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3877b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3878fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
387932c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
38809331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
388158cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
38829331c7a4SMatthew G. Knepley   if (isbinary) {
38839331c7a4SMatthew G. Knepley     PetscInt classid;
38849331c7a4SMatthew G. Knepley     char     type[256];
3885b859378eSBarry Smith 
3886060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
38879200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3888060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
388932c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
38909331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38919331c7a4SMatthew G. Knepley   } else if (ishdf5) {
38929331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38939331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
389458cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3895b859378eSBarry Smith   PetscFunctionReturn(0);
3896b859378eSBarry Smith }
3897b859378eSBarry Smith 
3898b2e4378dSMatthew G. Knepley /*@
3899b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3900b2e4378dSMatthew G. Knepley 
3901b2e4378dSMatthew G. Knepley   Not collective
3902b2e4378dSMatthew G. Knepley 
3903b2e4378dSMatthew G. Knepley   Input Parameter:
3904b2e4378dSMatthew G. Knepley . dm - the DM
3905b2e4378dSMatthew G. Knepley 
3906b2e4378dSMatthew G. Knepley   Output Parameters:
3907b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3908b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3909b2e4378dSMatthew G. Knepley 
3910b2e4378dSMatthew G. Knepley   Level: beginner
3911b2e4378dSMatthew G. Knepley 
3912b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3913b2e4378dSMatthew G. Knepley 
3914b2e4378dSMatthew G. Knepley 
3915b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3916b2e4378dSMatthew G. Knepley @*/
3917b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3918b2e4378dSMatthew G. Knepley {
3919b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3920b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3921b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3922b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3923b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3924b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3925b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3926b2e4378dSMatthew G. Knepley 
3927b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3928b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3929b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3930b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3931b2e4378dSMatthew G. Knepley   if (coords) {
3932b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3933b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3934b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3935b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3936b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3937b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3938b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3939b2e4378dSMatthew G. Knepley       }
3940b2e4378dSMatthew G. Knepley     }
3941b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3942b2e4378dSMatthew G. Knepley   } else {
3943b2e4378dSMatthew G. Knepley     PetscBool isda;
3944b2e4378dSMatthew G. Knepley 
3945b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3946b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3947b2e4378dSMatthew G. Knepley   }
3948b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3949b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3950b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3951b2e4378dSMatthew G. Knepley }
3952b2e4378dSMatthew G. Knepley 
3953b2e4378dSMatthew G. Knepley /*@
3954b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3955b2e4378dSMatthew G. Knepley 
3956b2e4378dSMatthew G. Knepley   Collective
3957b2e4378dSMatthew G. Knepley 
3958b2e4378dSMatthew G. Knepley   Input Parameter:
3959b2e4378dSMatthew G. Knepley . dm - the DM
3960b2e4378dSMatthew G. Knepley 
3961b2e4378dSMatthew G. Knepley   Output Parameters:
3962b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3963b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3964b2e4378dSMatthew G. Knepley 
3965b2e4378dSMatthew G. Knepley   Level: beginner
3966b2e4378dSMatthew G. Knepley 
3967b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3968b2e4378dSMatthew G. Knepley @*/
3969b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3970b2e4378dSMatthew G. Knepley {
3971b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
39726ce308c4SMatthew G. Knepley   PetscInt       cdim;
39736ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3974b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3975b2e4378dSMatthew G. Knepley 
3976b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3977b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3978b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3979b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3980b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3981b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3982b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3983b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3984b2e4378dSMatthew G. Knepley }
3985b2e4378dSMatthew G. Knepley 
39867da65231SMatthew G Knepley /******************************** FEM Support **********************************/
39877da65231SMatthew G Knepley 
3988a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3989a6dfd86eSKarl Rupp {
39901d47ebbbSSatish Balay   PetscInt       f;
39911b30c384SMatthew G Knepley   PetscErrorCode ierr;
39921b30c384SMatthew G Knepley 
39937da65231SMatthew G Knepley   PetscFunctionBegin;
399474778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
39951d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
399657622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
39977da65231SMatthew G Knepley   }
39987da65231SMatthew G Knepley   PetscFunctionReturn(0);
39997da65231SMatthew G Knepley }
40007da65231SMatthew G Knepley 
4001a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4002a6dfd86eSKarl Rupp {
40031b30c384SMatthew G Knepley   PetscInt       f, g;
40047da65231SMatthew G Knepley   PetscErrorCode ierr;
40057da65231SMatthew G Knepley 
40067da65231SMatthew G Knepley   PetscFunctionBegin;
400774778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
40081d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
400974778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
40101d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4011e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
40127da65231SMatthew G Knepley     }
401374778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
40147da65231SMatthew G Knepley   }
40157da65231SMatthew G Knepley   PetscFunctionReturn(0);
40167da65231SMatthew G Knepley }
4017e7c4fc90SDmitry Karpeev 
40186113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4019e759306cSMatthew G. Knepley {
40200c5b8624SToby Isaac   PetscInt          localSize, bs;
40210c5b8624SToby Isaac   PetscMPIInt       size;
40220c5b8624SToby Isaac   Vec               x, xglob;
40230c5b8624SToby Isaac   const PetscScalar *xarray;
4024e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4025e759306cSMatthew G. Knepley 
4026e759306cSMatthew G. Knepley   PetscFunctionBegin;
40279852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
4028e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4029e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
40306113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
40310c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
40320c5b8624SToby Isaac   if (size > 1) {
40330c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
40340c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
40350c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
40360c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
40370c5b8624SToby Isaac   } else {
40380c5b8624SToby Isaac     xglob = x;
40390c5b8624SToby Isaac   }
40400c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
40410c5b8624SToby Isaac   if (size > 1) {
40420c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
40430c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
40440c5b8624SToby Isaac   }
4045e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4046e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4047e759306cSMatthew G. Knepley }
4048e759306cSMatthew G. Knepley 
404988ed4aceSMatthew G Knepley /*@
40501bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4051061576a5SJed Brown 
4052061576a5SJed Brown   Input Parameter:
4053061576a5SJed Brown . dm - The DM
4054061576a5SJed Brown 
4055061576a5SJed Brown   Output Parameter:
4056061576a5SJed Brown . section - The PetscSection
4057061576a5SJed Brown 
4058061576a5SJed Brown   Options Database Keys:
4059061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4060061576a5SJed Brown 
4061061576a5SJed Brown   Level: advanced
4062061576a5SJed Brown 
4063061576a5SJed Brown   Notes:
4064061576a5SJed Brown   Use DMGetLocalSection() in new code.
4065061576a5SJed Brown 
4066061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4067061576a5SJed Brown 
4068061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4069061576a5SJed Brown @*/
4070061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4071061576a5SJed Brown {
4072061576a5SJed Brown   PetscErrorCode ierr;
4073061576a5SJed Brown 
4074061576a5SJed Brown   PetscFunctionBegin;
4075061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4076061576a5SJed Brown   PetscFunctionReturn(0);
4077061576a5SJed Brown }
4078061576a5SJed Brown 
4079061576a5SJed Brown /*@
4080061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
408188ed4aceSMatthew G Knepley 
408288ed4aceSMatthew G Knepley   Input Parameter:
408388ed4aceSMatthew G Knepley . dm - The DM
408488ed4aceSMatthew G Knepley 
408588ed4aceSMatthew G Knepley   Output Parameter:
408688ed4aceSMatthew G Knepley . section - The PetscSection
408788ed4aceSMatthew G Knepley 
4088e5893cccSMatthew G. Knepley   Options Database Keys:
4089e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4090e5893cccSMatthew G. Knepley 
409188ed4aceSMatthew G Knepley   Level: intermediate
409288ed4aceSMatthew G Knepley 
409388ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
409488ed4aceSMatthew G Knepley 
4095061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
409688ed4aceSMatthew G Knepley @*/
4097061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
40980adebc6cSBarry Smith {
4099fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4100fd59a867SMatthew G. Knepley 
410188ed4aceSMatthew G Knepley   PetscFunctionBegin;
410288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
410388ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41041bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4105e5e52638SMatthew G. Knepley     PetscInt d;
4106e5e52638SMatthew G. Knepley 
4107e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
41081bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
41091bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
41102f0f8703SMatthew G. Knepley   }
41111bb6d2a8SBarry Smith   *section = dm->localSection;
411288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
411388ed4aceSMatthew G Knepley }
411488ed4aceSMatthew G Knepley 
411588ed4aceSMatthew G Knepley /*@
41161bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4117061576a5SJed Brown 
4118061576a5SJed Brown   Input Parameters:
4119061576a5SJed Brown + dm - The DM
4120061576a5SJed Brown - section - The PetscSection
4121061576a5SJed Brown 
4122061576a5SJed Brown   Level: advanced
4123061576a5SJed Brown 
4124061576a5SJed Brown   Notes:
4125061576a5SJed Brown   Use DMSetLocalSection() in new code.
4126061576a5SJed Brown 
4127061576a5SJed Brown   Any existing Section will be destroyed
4128061576a5SJed Brown 
4129061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4130061576a5SJed Brown @*/
4131061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4132061576a5SJed Brown {
4133061576a5SJed Brown   PetscErrorCode ierr;
4134061576a5SJed Brown 
4135061576a5SJed Brown   PetscFunctionBegin;
4136061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4137061576a5SJed Brown   PetscFunctionReturn(0);
4138061576a5SJed Brown }
4139061576a5SJed Brown 
4140061576a5SJed Brown /*@
4141061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
414288ed4aceSMatthew G Knepley 
414388ed4aceSMatthew G Knepley   Input Parameters:
414488ed4aceSMatthew G Knepley + dm - The DM
414588ed4aceSMatthew G Knepley - section - The PetscSection
414688ed4aceSMatthew G Knepley 
414788ed4aceSMatthew G Knepley   Level: intermediate
414888ed4aceSMatthew G Knepley 
414988ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
415088ed4aceSMatthew G Knepley 
4151061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
415288ed4aceSMatthew G Knepley @*/
4153061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
41540adebc6cSBarry Smith {
4155c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4156af122d2aSMatthew G Knepley   PetscInt       f;
415788ed4aceSMatthew G Knepley   PetscErrorCode ierr;
415888ed4aceSMatthew G Knepley 
415988ed4aceSMatthew G Knepley   PetscFunctionBegin;
416088ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4161b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
41621d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41631bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
41641bb6d2a8SBarry Smith   dm->localSection = section;
41651bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4166af122d2aSMatthew G Knepley   if (numFields) {
4167af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4168af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
41690f21e855SMatthew G. Knepley       PetscObject disc;
4170af122d2aSMatthew G Knepley       const char *name;
4171af122d2aSMatthew G Knepley 
41721bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
417344a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
41740f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4175af122d2aSMatthew G Knepley     }
4176af122d2aSMatthew G Knepley   }
4177e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
41781bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
417988ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
418088ed4aceSMatthew G Knepley }
418188ed4aceSMatthew G Knepley 
41829435951eSToby Isaac /*@
4183b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
41849435951eSToby Isaac 
4185e228b242SToby Isaac   not collective
4186e228b242SToby Isaac 
41879435951eSToby Isaac   Input Parameter:
41889435951eSToby Isaac . dm - The DM
41899435951eSToby Isaac 
41909435951eSToby Isaac   Output Parameter:
41919435951eSToby 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.
41929435951eSToby 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.
41939435951eSToby Isaac 
41949435951eSToby Isaac   Level: advanced
41959435951eSToby Isaac 
41969435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
41979435951eSToby Isaac 
41989435951eSToby Isaac .seealso: DMSetDefaultConstraints()
41999435951eSToby Isaac @*/
42009435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
42019435951eSToby Isaac {
42029435951eSToby Isaac   PetscErrorCode ierr;
42039435951eSToby Isaac 
42049435951eSToby Isaac   PetscFunctionBegin;
42059435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42069435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
420745a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
420845a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
42099435951eSToby Isaac   PetscFunctionReturn(0);
42109435951eSToby Isaac }
42119435951eSToby Isaac 
42129435951eSToby Isaac /*@
4213b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
42149435951eSToby Isaac 
42159435951eSToby 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().
42169435951eSToby Isaac 
42179435951eSToby 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.
42189435951eSToby Isaac 
4219e228b242SToby Isaac   collective on dm
4220e228b242SToby Isaac 
42219435951eSToby Isaac   Input Parameters:
42229435951eSToby Isaac + dm - The DM
4223e228b242SToby 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).
4224e228b242SToby 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).
42259435951eSToby Isaac 
42269435951eSToby Isaac   Level: advanced
42279435951eSToby Isaac 
42289435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
42299435951eSToby Isaac 
42309435951eSToby Isaac .seealso: DMGetDefaultConstraints()
42319435951eSToby Isaac @*/
42329435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
42339435951eSToby Isaac {
4234e228b242SToby Isaac   PetscMPIInt result;
42359435951eSToby Isaac   PetscErrorCode ierr;
42369435951eSToby Isaac 
42379435951eSToby Isaac   PetscFunctionBegin;
42389435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4239e228b242SToby Isaac   if (section) {
4240e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4241e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4242f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4243e228b242SToby Isaac   }
4244e228b242SToby Isaac   if (mat) {
4245e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4246e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4247f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4248e228b242SToby Isaac   }
42499435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42509435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
42519435951eSToby Isaac   dm->defaultConstraintSection = section;
42529435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
42539435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
42549435951eSToby Isaac   dm->defaultConstraintMat = mat;
42559435951eSToby Isaac   PetscFunctionReturn(0);
42569435951eSToby Isaac }
42579435951eSToby Isaac 
4258497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4259507e4973SMatthew G. Knepley /*
4260507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4261507e4973SMatthew G. Knepley 
4262507e4973SMatthew G. Knepley   Input Parameters:
4263507e4973SMatthew G. Knepley + dm - The DM
4264507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4265507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4266507e4973SMatthew G. Knepley 
4267507e4973SMatthew G. Knepley   Level: intermediate
4268507e4973SMatthew G. Knepley 
42691bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4270507e4973SMatthew G. Knepley */
4271f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4272507e4973SMatthew G. Knepley {
4273507e4973SMatthew G. Knepley   MPI_Comm        comm;
4274507e4973SMatthew G. Knepley   PetscLayout     layout;
4275507e4973SMatthew G. Knepley   const PetscInt *ranges;
4276507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4277507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4278507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4279507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4280507e4973SMatthew G. Knepley 
4281507e4973SMatthew G. Knepley   PetscFunctionBegin;
4282507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4283507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4284507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4285507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4286507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4287507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4288507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4289507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4290507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4291507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4292507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4293507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4294f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4295507e4973SMatthew G. Knepley 
4296507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4297507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4298507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4299507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4300507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4301507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4302507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4303507e4973SMatthew 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;}
4304507e4973SMatthew 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;}
4305507e4973SMatthew G. Knepley     if (gdof < 0) {
4306507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4307507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4308507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4309507e4973SMatthew G. Knepley 
4310507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4311507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4312507e4973SMatthew 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;}
4313507e4973SMatthew G. Knepley       }
4314507e4973SMatthew G. Knepley     }
4315507e4973SMatthew G. Knepley   }
4316507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4317507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4318b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4319507e4973SMatthew G. Knepley   if (!gvalid) {
4320507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4321507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4322507e4973SMatthew G. Knepley   }
4323507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4324507e4973SMatthew G. Knepley }
4325f741bcd2SMatthew G. Knepley #endif
4326507e4973SMatthew G. Knepley 
432788ed4aceSMatthew G Knepley /*@
4328e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
432988ed4aceSMatthew G Knepley 
4330d083f849SBarry Smith   Collective on dm
43318b1ab98fSJed Brown 
433288ed4aceSMatthew G Knepley   Input Parameter:
433388ed4aceSMatthew G Knepley . dm - The DM
433488ed4aceSMatthew G Knepley 
433588ed4aceSMatthew G Knepley   Output Parameter:
433688ed4aceSMatthew G Knepley . section - The PetscSection
433788ed4aceSMatthew G Knepley 
433888ed4aceSMatthew G Knepley   Level: intermediate
433988ed4aceSMatthew G Knepley 
434088ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
434188ed4aceSMatthew G Knepley 
434292fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
434388ed4aceSMatthew G Knepley @*/
4344e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
43450adebc6cSBarry Smith {
434688ed4aceSMatthew G Knepley   PetscErrorCode ierr;
434788ed4aceSMatthew G Knepley 
434888ed4aceSMatthew G Knepley   PetscFunctionBegin;
434988ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
435088ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
43511bb6d2a8SBarry Smith   if (!dm->globalSection) {
4352fd59a867SMatthew G. Knepley     PetscSection s;
4353fd59a867SMatthew G. Knepley 
435492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4355fd59a867SMatthew 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");
435633907cc2SStefano 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");
43571bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4358cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
43591bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
43601bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
436188ed4aceSMatthew G Knepley   }
43621bb6d2a8SBarry Smith   *section = dm->globalSection;
436388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
436488ed4aceSMatthew G Knepley }
436588ed4aceSMatthew G Knepley 
4366b21d0597SMatthew G Knepley /*@
4367e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4368b21d0597SMatthew G Knepley 
4369b21d0597SMatthew G Knepley   Input Parameters:
4370b21d0597SMatthew G Knepley + dm - The DM
43715080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4372b21d0597SMatthew G Knepley 
4373b21d0597SMatthew G Knepley   Level: intermediate
4374b21d0597SMatthew G Knepley 
4375b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4376b21d0597SMatthew G Knepley 
437792fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4378b21d0597SMatthew G Knepley @*/
4379e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
43800adebc6cSBarry Smith {
4381b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4382b21d0597SMatthew G Knepley 
4383b21d0597SMatthew G Knepley   PetscFunctionBegin;
4384b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43855080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43861d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43871bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
43881bb6d2a8SBarry Smith   dm->globalSection = section;
4389497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
43901bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4391507e4973SMatthew G. Knepley #endif
4392b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4393b21d0597SMatthew G Knepley }
4394b21d0597SMatthew G Knepley 
439588ed4aceSMatthew G Knepley /*@
43961bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
439788ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
439888ed4aceSMatthew G Knepley 
439988ed4aceSMatthew G Knepley   Input Parameter:
440088ed4aceSMatthew G Knepley . dm - The DM
440188ed4aceSMatthew G Knepley 
440288ed4aceSMatthew G Knepley   Output Parameter:
440388ed4aceSMatthew G Knepley . sf - The PetscSF
440488ed4aceSMatthew G Knepley 
440588ed4aceSMatthew G Knepley   Level: intermediate
440688ed4aceSMatthew G Knepley 
440788ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
440888ed4aceSMatthew G Knepley 
44091bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
441088ed4aceSMatthew G Knepley @*/
44111bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
44120adebc6cSBarry Smith {
441388ed4aceSMatthew G Knepley   PetscInt       nroots;
441488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
441588ed4aceSMatthew G Knepley 
441688ed4aceSMatthew G Knepley   PetscFunctionBegin;
441788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
441888ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
44191bb6d2a8SBarry Smith   if (!dm->sectionSF) {
44201bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
442133907cc2SStefano Zampini   }
44221bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
442388ed4aceSMatthew G Knepley   if (nroots < 0) {
442488ed4aceSMatthew G Knepley     PetscSection section, gSection;
442588ed4aceSMatthew G Knepley 
442692fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
442731ea6d37SMatthew G Knepley     if (section) {
4428e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
44291bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
443031ea6d37SMatthew G Knepley     } else {
44310298fd71SBarry Smith       *sf = NULL;
443231ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
443331ea6d37SMatthew G Knepley     }
443488ed4aceSMatthew G Knepley   }
44351bb6d2a8SBarry Smith   *sf = dm->sectionSF;
443688ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
443788ed4aceSMatthew G Knepley }
443888ed4aceSMatthew G Knepley 
443988ed4aceSMatthew G Knepley /*@
44401bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
444188ed4aceSMatthew G Knepley 
444288ed4aceSMatthew G Knepley   Input Parameters:
444388ed4aceSMatthew G Knepley + dm - The DM
444488ed4aceSMatthew G Knepley - sf - The PetscSF
444588ed4aceSMatthew G Knepley 
444688ed4aceSMatthew G Knepley   Level: intermediate
444788ed4aceSMatthew G Knepley 
444888ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
444988ed4aceSMatthew G Knepley 
44501bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
445188ed4aceSMatthew G Knepley @*/
44521bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
44530adebc6cSBarry Smith {
445488ed4aceSMatthew G Knepley   PetscErrorCode ierr;
445588ed4aceSMatthew G Knepley 
445688ed4aceSMatthew G Knepley   PetscFunctionBegin;
445788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4458b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
445933907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
44601bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
44611bb6d2a8SBarry Smith   dm->sectionSF = sf;
446288ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
446388ed4aceSMatthew G Knepley }
446488ed4aceSMatthew G Knepley 
446588ed4aceSMatthew G Knepley /*@C
44661bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
446788ed4aceSMatthew G Knepley   describing the data layout.
446888ed4aceSMatthew G Knepley 
446988ed4aceSMatthew G Knepley   Input Parameters:
447088ed4aceSMatthew G Knepley + dm - The DM
447188ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
447288ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
447388ed4aceSMatthew G Knepley 
44741bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
447588ed4aceSMatthew G Knepley 
44761bb6d2a8SBarry Smith   Level: developer
44771bb6d2a8SBarry Smith 
44781bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
44791bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
44801bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
44811bb6d2a8SBarry Smith 
44821bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
448388ed4aceSMatthew G Knepley @*/
44841bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
448588ed4aceSMatthew G Knepley {
448682f516ccSBarry Smith   MPI_Comm       comm;
448788ed4aceSMatthew G Knepley   PetscLayout    layout;
448888ed4aceSMatthew G Knepley   const PetscInt *ranges;
448988ed4aceSMatthew G Knepley   PetscInt       *local;
449088ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4491ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
449288ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
449388ed4aceSMatthew G Knepley   PetscErrorCode ierr;
449488ed4aceSMatthew G Knepley 
449588ed4aceSMatthew G Knepley   PetscFunctionBegin;
449688ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4497367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
449888ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
449988ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
450088ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
450188ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
450288ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
450388ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
450488ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
450588ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
450688ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4507ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
45086636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
450988ed4aceSMatthew G Knepley 
45106636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
45116636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4512235fbf56SMatthew 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));
45136636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
451488ed4aceSMatthew G Knepley   }
4515785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4516785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
451788ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
45181f588964SMatthew G Knepley     const PetscInt *cind;
45196636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
452088ed4aceSMatthew G Knepley 
452188ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
452288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
452388ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
452488ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
452588ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
45266636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
452788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
45286636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
45296636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
45306636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4531057b4bcdSMatthew 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);
45326636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
45336636e97aSMatthew G Knepley     }
453488ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
453588ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
453688ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
453788ed4aceSMatthew G Knepley     }
453888ed4aceSMatthew G Knepley     if (gdof < 0) {
45396636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
454088ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
454188ed4aceSMatthew G Knepley 
454205376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
454331d3f06eSJed Brown         if (r < 0) r = -(r+2);
454405376888SMatthew 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);
454588ed4aceSMatthew G Knepley         remote[l].rank  = r;
454688ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
454788ed4aceSMatthew G Knepley       }
454888ed4aceSMatthew G Knepley     } else {
45496636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
455088ed4aceSMatthew G Knepley         remote[l].rank  = rank;
455188ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
455288ed4aceSMatthew G Knepley       }
455388ed4aceSMatthew G Knepley     }
455488ed4aceSMatthew G Knepley   }
45556636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
455688ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
45571bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
455888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
455988ed4aceSMatthew G Knepley }
4560af122d2aSMatthew G Knepley 
4561b21d0597SMatthew G Knepley /*@
4562b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4563b21d0597SMatthew G Knepley 
4564b21d0597SMatthew G Knepley   Input Parameter:
4565b21d0597SMatthew G Knepley . dm - The DM
4566b21d0597SMatthew G Knepley 
4567b21d0597SMatthew G Knepley   Output Parameter:
4568b21d0597SMatthew G Knepley . sf - The PetscSF
4569b21d0597SMatthew G Knepley 
4570b21d0597SMatthew G Knepley   Level: intermediate
4571b21d0597SMatthew G Knepley 
4572b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4573b21d0597SMatthew G Knepley 
45741bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4575b21d0597SMatthew G Knepley @*/
45760adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
45770adebc6cSBarry Smith {
4578b21d0597SMatthew G Knepley   PetscFunctionBegin;
4579b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4580b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4581b21d0597SMatthew G Knepley   *sf = dm->sf;
4582b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4583b21d0597SMatthew G Knepley }
4584b21d0597SMatthew G Knepley 
4585057b4bcdSMatthew G Knepley /*@
4586057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4587057b4bcdSMatthew G Knepley 
4588057b4bcdSMatthew G Knepley   Input Parameters:
4589057b4bcdSMatthew G Knepley + dm - The DM
4590057b4bcdSMatthew G Knepley - sf - The PetscSF
4591057b4bcdSMatthew G Knepley 
4592057b4bcdSMatthew G Knepley   Level: intermediate
4593057b4bcdSMatthew G Knepley 
45941bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4595057b4bcdSMatthew G Knepley @*/
45960adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
45970adebc6cSBarry Smith {
4598057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4599057b4bcdSMatthew G Knepley 
4600057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4601057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4602b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4603057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
460433907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4605057b4bcdSMatthew G Knepley   dm->sf = sf;
4606057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4607057b4bcdSMatthew G Knepley }
4608057b4bcdSMatthew G Knepley 
460934aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
461034aa8a36SMatthew G. Knepley {
461134aa8a36SMatthew G. Knepley   PetscClassId   id;
461234aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
461334aa8a36SMatthew G. Knepley 
461434aa8a36SMatthew G. Knepley   PetscFunctionBegin;
461534aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
461634aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
461734aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
461834aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
461934aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
462017c1d62eSMatthew G. Knepley   } else {
462117c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
462234aa8a36SMatthew G. Knepley   }
462334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
462434aa8a36SMatthew G. Knepley }
462534aa8a36SMatthew G. Knepley 
462644a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
462744a7f3ddSMatthew G. Knepley {
462844a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
462944a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
463044a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
463144a7f3ddSMatthew G. Knepley 
463244a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
463344a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
463444a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
463544a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
463644a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
463744a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
463844a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
463944a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
464044a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
464144a7f3ddSMatthew G. Knepley }
464244a7f3ddSMatthew G. Knepley 
464344a7f3ddSMatthew G. Knepley /*@
464444a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
464544a7f3ddSMatthew G. Knepley 
4646d083f849SBarry Smith   Logically collective on dm
464744a7f3ddSMatthew G. Knepley 
464844a7f3ddSMatthew G. Knepley   Input Parameter:
464944a7f3ddSMatthew G. Knepley . dm - The DM
465044a7f3ddSMatthew G. Knepley 
465144a7f3ddSMatthew G. Knepley   Level: intermediate
465244a7f3ddSMatthew G. Knepley 
465344a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
465444a7f3ddSMatthew G. Knepley @*/
465544a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
465644a7f3ddSMatthew G. Knepley {
465744a7f3ddSMatthew G. Knepley   PetscInt       f;
465844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
465944a7f3ddSMatthew G. Knepley 
466044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
466144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
466244a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
466344a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
466444a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
466544a7f3ddSMatthew G. Knepley   }
466644a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
466744a7f3ddSMatthew G. Knepley   dm->fields = NULL;
466844a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
466944a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
467044a7f3ddSMatthew G. Knepley }
467144a7f3ddSMatthew G. Knepley 
4672689b5837SMatthew G. Knepley /*@
4673689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4674689b5837SMatthew G. Knepley 
4675689b5837SMatthew G. Knepley   Not collective
4676689b5837SMatthew G. Knepley 
4677689b5837SMatthew G. Knepley   Input Parameter:
4678689b5837SMatthew G. Knepley . dm - The DM
4679689b5837SMatthew G. Knepley 
4680689b5837SMatthew G. Knepley   Output Parameter:
4681689b5837SMatthew G. Knepley . Nf - The number of fields
4682689b5837SMatthew G. Knepley 
4683689b5837SMatthew G. Knepley   Level: intermediate
4684689b5837SMatthew G. Knepley 
4685689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4686689b5837SMatthew G. Knepley @*/
46870f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
46880f21e855SMatthew G. Knepley {
46890f21e855SMatthew G. Knepley   PetscFunctionBegin;
46900f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4691534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
469244a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4693af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4694af122d2aSMatthew G Knepley }
4695af122d2aSMatthew G Knepley 
4696689b5837SMatthew G. Knepley /*@
4697689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4698689b5837SMatthew G. Knepley 
4699d083f849SBarry Smith   Logically collective on dm
4700689b5837SMatthew G. Knepley 
4701689b5837SMatthew G. Knepley   Input Parameters:
4702689b5837SMatthew G. Knepley + dm - The DM
4703689b5837SMatthew G. Knepley - Nf - The number of fields
4704689b5837SMatthew G. Knepley 
4705689b5837SMatthew G. Knepley   Level: intermediate
4706689b5837SMatthew G. Knepley 
4707689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4708689b5837SMatthew G. Knepley @*/
4709af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4710af122d2aSMatthew G Knepley {
47110f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4712af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4713af122d2aSMatthew G Knepley 
4714af122d2aSMatthew G Knepley   PetscFunctionBegin;
4715af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
471644a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
47170f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47180f21e855SMatthew G. Knepley     PetscContainer obj;
47190f21e855SMatthew G. Knepley 
47200f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
472144a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
47220f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4723af122d2aSMatthew G Knepley   }
4724af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4725af122d2aSMatthew G Knepley }
4726af122d2aSMatthew G Knepley 
4727c1929be8SMatthew G. Knepley /*@
4728c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4729c1929be8SMatthew G. Knepley 
4730c1929be8SMatthew G. Knepley   Not collective
4731c1929be8SMatthew G. Knepley 
4732c1929be8SMatthew G. Knepley   Input Parameters:
4733c1929be8SMatthew G. Knepley + dm - The DM
4734c1929be8SMatthew G. Knepley - f  - The field number
4735c1929be8SMatthew G. Knepley 
473644a7f3ddSMatthew G. Knepley   Output Parameters:
473744a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
473844a7f3ddSMatthew G. Knepley - field - The discretization object
4739c1929be8SMatthew G. Knepley 
474044a7f3ddSMatthew G. Knepley   Level: intermediate
4741c1929be8SMatthew G. Knepley 
474244a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4743c1929be8SMatthew G. Knepley @*/
474444a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4745af122d2aSMatthew G Knepley {
4746af122d2aSMatthew G Knepley   PetscFunctionBegin;
4747af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
474844a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
474944a7f3ddSMatthew 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);
475044a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
475144a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4752decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4753decb47aaSMatthew G. Knepley }
4754decb47aaSMatthew G. Knepley 
4755083401c6SMatthew G. Knepley /* Does not clear the DS */
4756083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4757083401c6SMatthew G. Knepley {
4758083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4759083401c6SMatthew G. Knepley 
4760083401c6SMatthew G. Knepley   PetscFunctionBegin;
4761083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4762083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4763083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4764083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4765083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4766083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4767083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4768083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4769083401c6SMatthew G. Knepley }
4770083401c6SMatthew G. Knepley 
4771c1929be8SMatthew G. Knepley /*@
4772c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4773c1929be8SMatthew G. Knepley 
4774d083f849SBarry Smith   Logically collective on dm
4775c1929be8SMatthew G. Knepley 
4776c1929be8SMatthew G. Knepley   Input Parameters:
4777c1929be8SMatthew G. Knepley + dm    - The DM
4778c1929be8SMatthew G. Knepley . f     - The field number
477944a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4780c1929be8SMatthew G. Knepley - field - The discretization object
4781c1929be8SMatthew G. Knepley 
478244a7f3ddSMatthew G. Knepley   Level: intermediate
4783c1929be8SMatthew G. Knepley 
478444a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4785c1929be8SMatthew G. Knepley @*/
478644a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4787decb47aaSMatthew G. Knepley {
4788decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4789decb47aaSMatthew G. Knepley 
4790decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4791decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4792e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
479344a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4794e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4795083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
479634aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
47972df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
479844a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
479944a7f3ddSMatthew G. Knepley }
480044a7f3ddSMatthew G. Knepley 
480144a7f3ddSMatthew G. Knepley /*@
480244a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
480344a7f3ddSMatthew G. Knepley 
4804d083f849SBarry Smith   Logically collective on dm
480544a7f3ddSMatthew G. Knepley 
480644a7f3ddSMatthew G. Knepley   Input Parameters:
480744a7f3ddSMatthew G. Knepley + dm    - The DM
480844a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
480944a7f3ddSMatthew G. Knepley - field - The discretization object
481044a7f3ddSMatthew G. Knepley 
481144a7f3ddSMatthew G. Knepley   Level: intermediate
481244a7f3ddSMatthew G. Knepley 
481344a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
481444a7f3ddSMatthew G. Knepley @*/
481544a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
481644a7f3ddSMatthew G. Knepley {
481744a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
481844a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
481944a7f3ddSMatthew G. Knepley 
482044a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
482144a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
482244a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
482344a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
482444a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
482544a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
482644a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
482744a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
482844a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
482934aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
48302df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4831af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4832af122d2aSMatthew G Knepley }
48336636e97aSMatthew G Knepley 
4834e5e52638SMatthew G. Knepley /*@
4835e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4836e5e52638SMatthew G. Knepley 
4837d083f849SBarry Smith   Collective on dm
4838e5e52638SMatthew G. Knepley 
4839e5e52638SMatthew G. Knepley   Input Parameter:
4840e5e52638SMatthew G. Knepley . dm - The DM
4841e5e52638SMatthew G. Knepley 
4842e5e52638SMatthew G. Knepley   Output Parameter:
4843e5e52638SMatthew G. Knepley . newdm - The DM
4844e5e52638SMatthew G. Knepley 
4845e5e52638SMatthew G. Knepley   Level: advanced
4846e5e52638SMatthew G. Knepley 
4847e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4848e5e52638SMatthew G. Knepley @*/
4849e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4850e5e52638SMatthew G. Knepley {
4851e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4852e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4853e5e52638SMatthew G. Knepley 
4854e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4855e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4856e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4857e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4858e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4859e5e52638SMatthew G. Knepley     DMLabel     label;
4860e5e52638SMatthew G. Knepley     PetscObject field;
486134aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4862e5e52638SMatthew G. Knepley 
4863e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4864e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
486534aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
486634aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
486734aa8a36SMatthew G. Knepley   }
486834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
486934aa8a36SMatthew G. Knepley }
487034aa8a36SMatthew G. Knepley 
487134aa8a36SMatthew G. Knepley /*@
487234aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
487334aa8a36SMatthew G. Knepley 
487434aa8a36SMatthew G. Knepley   Not collective
487534aa8a36SMatthew G. Knepley 
487634aa8a36SMatthew G. Knepley   Input Parameters:
487734aa8a36SMatthew G. Knepley + dm - The DM object
487834aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
487934aa8a36SMatthew G. Knepley 
488034aa8a36SMatthew G. Knepley   Output Parameter:
488134aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
488234aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
488334aa8a36SMatthew G. Knepley 
488434aa8a36SMatthew G. Knepley   Notes:
488534aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
488634aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
488734aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4888979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
488934aa8a36SMatthew G. Knepley 
489034aa8a36SMatthew G. Knepley   Level: developer
489134aa8a36SMatthew G. Knepley 
489234aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
489334aa8a36SMatthew G. Knepley @*/
489434aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
489534aa8a36SMatthew G. Knepley {
489634aa8a36SMatthew G. Knepley   PetscFunctionBegin;
489734aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4898534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4899534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
490034aa8a36SMatthew G. Knepley   if (f < 0) {
490134aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
490234aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
490334aa8a36SMatthew G. Knepley   } else {
490434aa8a36SMatthew G. Knepley     PetscInt       Nf;
490534aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
490634aa8a36SMatthew G. Knepley 
490734aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
490834aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
490934aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
491034aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
491134aa8a36SMatthew G. Knepley   }
491234aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
491334aa8a36SMatthew G. Knepley }
491434aa8a36SMatthew G. Knepley 
491534aa8a36SMatthew G. Knepley /*@
491634aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
491734aa8a36SMatthew G. Knepley 
491834aa8a36SMatthew G. Knepley   Not collective
491934aa8a36SMatthew G. Knepley 
492034aa8a36SMatthew G. Knepley   Input Parameters:
492134aa8a36SMatthew G. Knepley + dm         - The DM object
492234aa8a36SMatthew G. Knepley . f          - The field number
492334aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
492434aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
492534aa8a36SMatthew G. Knepley 
492634aa8a36SMatthew G. Knepley   Notes:
492734aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
492834aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
492934aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4930979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
493134aa8a36SMatthew G. Knepley 
493234aa8a36SMatthew G. Knepley   Level: developer
493334aa8a36SMatthew G. Knepley 
493434aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
493534aa8a36SMatthew G. Knepley @*/
493634aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
493734aa8a36SMatthew G. Knepley {
493834aa8a36SMatthew G. Knepley   PetscFunctionBegin;
493934aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
494034aa8a36SMatthew G. Knepley   if (f < 0) {
494134aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
494234aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
494334aa8a36SMatthew G. Knepley   } else {
494434aa8a36SMatthew G. Knepley     PetscInt       Nf;
494534aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
494634aa8a36SMatthew G. Knepley 
494734aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
494834aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
494934aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
495034aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4951e5e52638SMatthew G. Knepley   }
4952e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4953e5e52638SMatthew G. Knepley }
4954e5e52638SMatthew G. Knepley 
4955b0441da4SMatthew G. Knepley /*@
4956b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4957b0441da4SMatthew G. Knepley 
4958b0441da4SMatthew G. Knepley   Not collective
4959b0441da4SMatthew G. Knepley 
4960b0441da4SMatthew G. Knepley   Input Parameters:
4961b0441da4SMatthew G. Knepley . dm - The DM object
4962b0441da4SMatthew G. Knepley 
4963b0441da4SMatthew G. Knepley   Output Parameter:
4964b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4965b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4966b0441da4SMatthew G. Knepley 
4967b0441da4SMatthew G. Knepley   Notes:
4968b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4969b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4970b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4971b0441da4SMatthew G. Knepley 
4972b0441da4SMatthew G. Knepley   Level: developer
4973b0441da4SMatthew G. Knepley 
4974b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4975b0441da4SMatthew G. Knepley @*/
4976b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4977b0441da4SMatthew G. Knepley {
4978b0441da4SMatthew G. Knepley   PetscInt       Nf;
4979b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4980b0441da4SMatthew G. Knepley 
4981b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4982b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4983534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4984534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4985b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4986b0441da4SMatthew G. Knepley   if (!Nf) {
4987b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4988b0441da4SMatthew G. Knepley   } else {
4989b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4990b0441da4SMatthew G. Knepley   }
4991b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4992b0441da4SMatthew G. Knepley }
4993b0441da4SMatthew G. Knepley 
4994b0441da4SMatthew G. Knepley /*@
4995b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
4996b0441da4SMatthew G. Knepley 
4997b0441da4SMatthew G. Knepley   Not collective
4998b0441da4SMatthew G. Knepley 
4999b0441da4SMatthew G. Knepley   Input Parameters:
5000b0441da4SMatthew G. Knepley + dm         - The DM object
5001b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5002b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5003b0441da4SMatthew G. Knepley 
5004b0441da4SMatthew G. Knepley   Notes:
5005b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5006b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5007b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5008b0441da4SMatthew G. Knepley 
5009b0441da4SMatthew G. Knepley   Level: developer
5010b0441da4SMatthew G. Knepley 
5011b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5012b0441da4SMatthew G. Knepley @*/
5013b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5014b0441da4SMatthew G. Knepley {
5015b0441da4SMatthew G. Knepley   PetscInt       Nf;
5016b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5017b0441da4SMatthew G. Knepley 
5018b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5019b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5020b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5021b0441da4SMatthew G. Knepley   if (!Nf) {
5022b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5023b0441da4SMatthew G. Knepley   } else {
5024b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5025e5e52638SMatthew G. Knepley   }
5026e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5027e5e52638SMatthew G. Knepley }
5028e5e52638SMatthew G. Knepley 
5029783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
5030783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
5031783e2ec8SMatthew G. Knepley {
5032783e2ec8SMatthew G. Knepley   DMLabel        label;
5033783e2ec8SMatthew G. Knepley   PetscObject    obj;
5034783e2ec8SMatthew G. Knepley   PetscClassId   id;
5035783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5036783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5037783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5038783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5039783e2ec8SMatthew G. Knepley 
5040783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5041783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5042783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5043783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5044783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
5045783e2ec8SMatthew G. Knepley   if (isFE && label) {
5046783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5047783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5048783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
5049783e2ec8SMatthew G. Knepley       const char *lname;
5050783e2ec8SMatthew G. Knepley 
505156cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5052783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
5053783e2ec8SMatthew G. Knepley       if (duplicate) break;
5054783e2ec8SMatthew G. Knepley     }
5055783e2ec8SMatthew G. Knepley     if (!duplicate) {
5056783e2ec8SMatthew G. Knepley       DM plex;
5057783e2ec8SMatthew G. Knepley 
5058783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5059783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5060783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5061783e2ec8SMatthew G. Knepley     }
5062783e2ec8SMatthew G. Knepley   }
5063783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5064783e2ec8SMatthew G. Knepley }
5065783e2ec8SMatthew G. Knepley 
5066e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5067e5e52638SMatthew G. Knepley {
5068e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5069e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5070e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5071e5e52638SMatthew G. Knepley 
5072e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5073e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5074e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5075e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5076b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5077e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5078e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5079e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5080e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5081e5e52638SMatthew G. Knepley }
5082e5e52638SMatthew G. Knepley 
5083e5e52638SMatthew G. Knepley /*@
5084e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5085e5e52638SMatthew G. Knepley 
5086e5e52638SMatthew G. Knepley   Not collective
5087e5e52638SMatthew G. Knepley 
5088e5e52638SMatthew G. Knepley   Input Parameter:
5089e5e52638SMatthew G. Knepley . dm - The DM
5090e5e52638SMatthew G. Knepley 
5091e5e52638SMatthew G. Knepley   Output Parameter:
5092e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5093e5e52638SMatthew G. Knepley 
5094e5e52638SMatthew G. Knepley   Level: intermediate
5095e5e52638SMatthew G. Knepley 
5096e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5097e5e52638SMatthew G. Knepley @*/
5098e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5099e5e52638SMatthew G. Knepley {
5100e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5101e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5102534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5103e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5104e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5105e5e52638SMatthew G. Knepley }
5106e5e52638SMatthew G. Knepley 
5107e5e52638SMatthew G. Knepley /*@
5108e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5109e5e52638SMatthew G. Knepley 
5110d083f849SBarry Smith   Logically collective on dm
5111e5e52638SMatthew G. Knepley 
5112e5e52638SMatthew G. Knepley   Input Parameter:
5113e5e52638SMatthew G. Knepley . dm - The DM
5114e5e52638SMatthew G. Knepley 
5115e5e52638SMatthew G. Knepley   Level: intermediate
5116e5e52638SMatthew G. Knepley 
5117e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5118e5e52638SMatthew G. Knepley @*/
5119e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5120e5e52638SMatthew G. Knepley {
5121e5e52638SMatthew G. Knepley   PetscInt       s;
5122e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5123e5e52638SMatthew G. Knepley 
5124e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5125e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5126e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5127e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5128e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5129b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5130e5e52638SMatthew G. Knepley   }
5131e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5132e5e52638SMatthew G. Knepley   dm->probs = NULL;
5133e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5134e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5135e5e52638SMatthew G. Knepley }
5136e5e52638SMatthew G. Knepley 
5137e5e52638SMatthew G. Knepley /*@
5138e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5139e5e52638SMatthew G. Knepley 
5140e5e52638SMatthew G. Knepley   Not collective
5141e5e52638SMatthew G. Knepley 
5142e5e52638SMatthew G. Knepley   Input Parameter:
5143e5e52638SMatthew G. Knepley . dm    - The DM
5144e5e52638SMatthew G. Knepley 
5145e5e52638SMatthew G. Knepley   Output Parameter:
5146e5e52638SMatthew G. Knepley . prob - The default PetscDS
5147e5e52638SMatthew G. Knepley 
5148e5e52638SMatthew G. Knepley   Level: intermediate
5149e5e52638SMatthew G. Knepley 
5150e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5151e5e52638SMatthew G. Knepley @*/
5152e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5153e5e52638SMatthew G. Knepley {
5154b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5155b0143b4dSMatthew G. Knepley 
5156e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5157e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5158e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5159b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5160b0143b4dSMatthew G. Knepley     PetscDS ds;
5161b0143b4dSMatthew G. Knepley 
5162b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5163b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5164b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5165b0143b4dSMatthew G. Knepley   }
5166b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5167e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5168e5e52638SMatthew G. Knepley }
5169e5e52638SMatthew G. Knepley 
5170e5e52638SMatthew G. Knepley /*@
5171e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5172e5e52638SMatthew G. Knepley 
5173e5e52638SMatthew G. Knepley   Not collective
5174e5e52638SMatthew G. Knepley 
5175e5e52638SMatthew G. Knepley   Input Parameters:
5176e5e52638SMatthew G. Knepley + dm    - The DM
5177e5e52638SMatthew G. Knepley - point - Cell for the DS
5178e5e52638SMatthew G. Knepley 
5179e5e52638SMatthew G. Knepley   Output Parameter:
5180e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5181e5e52638SMatthew G. Knepley 
5182e5e52638SMatthew G. Knepley   Level: developer
5183e5e52638SMatthew G. Knepley 
5184b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5185e5e52638SMatthew G. Knepley @*/
5186e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5187e5e52638SMatthew G. Knepley {
5188e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5189e5e52638SMatthew G. Knepley   PetscInt       s;
5190e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5191e5e52638SMatthew G. Knepley 
5192e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5193e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5194e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5195360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5196e5e52638SMatthew G. Knepley   *prob = NULL;
5197e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5198e5e52638SMatthew G. Knepley     PetscInt val;
5199e5e52638SMatthew G. Knepley 
5200e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5201e5e52638SMatthew G. Knepley     else {
5202e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5203e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5204e5e52638SMatthew G. Knepley     }
5205e5e52638SMatthew G. Knepley   }
5206e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5207e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5208e5e52638SMatthew G. Knepley }
5209e5e52638SMatthew G. Knepley 
5210e5e52638SMatthew G. Knepley /*@
5211e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5212e5e52638SMatthew G. Knepley 
5213e5e52638SMatthew G. Knepley   Not collective
5214e5e52638SMatthew G. Knepley 
5215e5e52638SMatthew G. Knepley   Input Parameters:
5216e5e52638SMatthew G. Knepley + dm    - The DM
5217e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5218e5e52638SMatthew G. Knepley 
5219b3cf3223SMatthew G. Knepley   Output Parameters:
5220b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5221b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5222e5e52638SMatthew G. Knepley 
5223e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5224e5e52638SMatthew G. Knepley 
5225e5e52638SMatthew G. Knepley   Level: advanced
5226e5e52638SMatthew G. Knepley 
5227e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5228e5e52638SMatthew G. Knepley @*/
5229b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5230e5e52638SMatthew G. Knepley {
5231e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5232e5e52638SMatthew G. Knepley 
5233e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5234e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5235e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5236b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5237b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5238e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5239b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5240b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5241b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5242b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5243b3cf3223SMatthew G. Knepley     }
5244e5e52638SMatthew G. Knepley   }
52452df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5246e5e52638SMatthew G. Knepley }
5247e5e52638SMatthew G. Knepley 
5248e5e52638SMatthew G. Knepley /*@
5249083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5250083401c6SMatthew G. Knepley 
5251083401c6SMatthew G. Knepley   Collective on dm
5252083401c6SMatthew G. Knepley 
5253083401c6SMatthew G. Knepley   Input Parameters:
5254083401c6SMatthew G. Knepley + dm     - The DM
5255083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5256083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5257083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5258083401c6SMatthew G. Knepley 
5259083401c6SMatthew 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,
5260083401c6SMatthew G. Knepley   the fields argument is ignored.
5261083401c6SMatthew G. Knepley 
5262083401c6SMatthew G. Knepley   Level: advanced
5263083401c6SMatthew G. Knepley 
5264083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5265083401c6SMatthew G. Knepley @*/
5266083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5267083401c6SMatthew G. Knepley {
5268083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5269083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5270083401c6SMatthew G. Knepley 
5271083401c6SMatthew G. Knepley   PetscFunctionBegin;
5272083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5273083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5274083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5275083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5276083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5277083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5278083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5279083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5280083401c6SMatthew G. Knepley     }
5281083401c6SMatthew G. Knepley   }
5282083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5283083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5284083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5285083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5286083401c6SMatthew G. Knepley   if (!label) {
5287083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5288083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5289083401c6SMatthew G. Knepley     Nds = 0;
5290083401c6SMatthew G. Knepley   }
5291083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5292083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5293083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5294083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5295083401c6SMatthew G. Knepley }
5296083401c6SMatthew G. Knepley 
5297083401c6SMatthew G. Knepley /*@
5298e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5299e5e52638SMatthew G. Knepley 
5300e5e52638SMatthew G. Knepley   Not collective
5301e5e52638SMatthew G. Knepley 
5302e5e52638SMatthew G. Knepley   Input Parameters:
5303e5e52638SMatthew G. Knepley + dm  - The DM
5304e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5305e5e52638SMatthew G. Knepley 
5306e5e52638SMatthew G. Knepley   Output Parameters:
5307b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5308b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5309083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5310e5e52638SMatthew G. Knepley 
5311e5e52638SMatthew G. Knepley   Level: advanced
5312e5e52638SMatthew G. Knepley 
5313e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5314e5e52638SMatthew G. Knepley @*/
5315b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5316e5e52638SMatthew G. Knepley {
5317e5e52638SMatthew G. Knepley   PetscInt       Nds;
5318e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5319e5e52638SMatthew G. Knepley 
5320e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5321e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5322e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5323e5e52638SMatthew 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);
5324e5e52638SMatthew G. Knepley   if (label) {
5325e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5326e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5327e5e52638SMatthew G. Knepley   }
5328b3cf3223SMatthew G. Knepley   if (fields) {
5329b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5330b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5331b3cf3223SMatthew G. Knepley   }
5332e5e52638SMatthew G. Knepley   if (ds) {
5333b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5334e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5335e5e52638SMatthew G. Knepley   }
5336e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5337e5e52638SMatthew G. Knepley }
5338e5e52638SMatthew G. Knepley 
5339e5e52638SMatthew G. Knepley /*@
5340083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5341e5e52638SMatthew G. Knepley 
5342083401c6SMatthew G. Knepley   Not collective
5343e5e52638SMatthew G. Knepley 
5344e5e52638SMatthew G. Knepley   Input Parameters:
5345e5e52638SMatthew G. Knepley + dm     - The DM
5346083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5347083401c6SMatthew G. Knepley . label  - The region label, or NULL
5348083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5349083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5350e5e52638SMatthew G. Knepley 
5351e5e52638SMatthew G. Knepley   Level: advanced
5352e5e52638SMatthew G. Knepley 
5353083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5354e5e52638SMatthew G. Knepley @*/
5355083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5356e5e52638SMatthew G. Knepley {
5357083401c6SMatthew G. Knepley   PetscInt       Nds;
5358e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5359e5e52638SMatthew G. Knepley 
5360e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5361e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5362083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5363083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5364083401c6SMatthew 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);
5365e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5366083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5367083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5368083401c6SMatthew G. Knepley   if (fields) {
5369083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5370b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5371083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5372083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5373e5e52638SMatthew G. Knepley   }
5374083401c6SMatthew G. Knepley   if (ds) {
5375083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5376083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5377083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5378083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5379083401c6SMatthew G. Knepley   }
5380e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5381e5e52638SMatthew G. Knepley }
5382e5e52638SMatthew G. Knepley 
5383e5e52638SMatthew G. Knepley /*@
53841d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
53851d3af9e0SMatthew G. Knepley 
53861d3af9e0SMatthew G. Knepley   Not collective
53871d3af9e0SMatthew G. Knepley 
53881d3af9e0SMatthew G. Knepley   Input Parameters:
53891d3af9e0SMatthew G. Knepley + dm  - The DM
53901d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
53911d3af9e0SMatthew G. Knepley 
53921d3af9e0SMatthew G. Knepley   Output Parameter:
53931d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
53941d3af9e0SMatthew G. Knepley 
53951d3af9e0SMatthew G. Knepley   Level: advanced
53961d3af9e0SMatthew G. Knepley 
53971d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
53981d3af9e0SMatthew G. Knepley @*/
53991d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
54001d3af9e0SMatthew G. Knepley {
54011d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
54021d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
54031d3af9e0SMatthew G. Knepley 
54041d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
54051d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54061d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
54071d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
54081d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
54091d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
54101d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
54111d3af9e0SMatthew G. Knepley   else          *num = n;
54121d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
54131d3af9e0SMatthew G. Knepley }
54141d3af9e0SMatthew G. Knepley 
54151d3af9e0SMatthew G. Knepley /*@
5416e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5417e5e52638SMatthew G. Knepley 
5418d083f849SBarry Smith   Collective on dm
5419e5e52638SMatthew G. Knepley 
5420e5e52638SMatthew G. Knepley   Input Parameter:
5421e5e52638SMatthew G. Knepley . dm - The DM
5422e5e52638SMatthew G. Knepley 
5423e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5424e5e52638SMatthew G. Knepley 
5425e5e52638SMatthew G. Knepley   Level: intermediate
5426e5e52638SMatthew G. Knepley 
5427e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5428e5e52638SMatthew G. Knepley @*/
5429e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5430e5e52638SMatthew G. Knepley {
5431e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5432083401c6SMatthew G. Knepley   PetscDS        dsDef;
5433083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5434083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5435e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5436e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5437e5e52638SMatthew G. Knepley 
5438e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5439e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5440e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5441e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5442083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5443083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5444083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5445083401c6SMatthew G. Knepley   Nl   = 0;
5446083401c6SMatthew G. Knepley   Ndef = 0;
5447083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5448083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5449083401c6SMatthew G. Knepley     PetscInt l;
5450083401c6SMatthew G. Knepley 
5451083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5452083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5453083401c6SMatthew G. Knepley     if (l < Nl) continue;
5454083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5455083401c6SMatthew G. Knepley   }
5456083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5457083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5458083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5459b3cf3223SMatthew G. Knepley     IS        fields;
5460b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5461b3cf3223SMatthew G. Knepley 
5462b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5463083401c6SMatthew G. Knepley     if (nf) {
5464b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5465b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
546688f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
546788f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
546888f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
546988f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
547088f0c812SMatthew G. Knepley 
5471083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5472083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5473083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5474b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
54752df9ee95SMatthew G. Knepley     }
5476083401c6SMatthew G. Knepley   }
5477083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5478083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5479083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5480083401c6SMatthew G. Knepley   if (Ndef && Nl) {
54810122748bSMatthew G. Knepley     DM              plex;
5482083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5483083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5484083401c6SMatthew G. Knepley     PetscInt       *fields;
5485083401c6SMatthew G. Knepley     const PetscInt *cells;
5486083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
54870122748bSMatthew G. Knepley 
54880122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
54890122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5490083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5491083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5492083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5493083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5494083401c6SMatthew G. Knepley       IS      pointIS;
5495083401c6SMatthew G. Knepley 
5496083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5497083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5498083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5499083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5500083401c6SMatthew G. Knepley     }
5501083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5502083401c6SMatthew G. Knepley 
5503083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5504083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5505083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5506083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5507083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5508083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5509083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5510083401c6SMatthew G. Knepley 
5511083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5512083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5513083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5514083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5515083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5516083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5517083401c6SMatthew G. Knepley 
5518083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5519083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5520083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5521083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5522083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5523083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
55240122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5525083401c6SMatthew G. Knepley   }
5526083401c6SMatthew G. Knepley   /* Create label DSes
5527083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5528083401c6SMatthew G. Knepley   */
5529083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5530083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5531083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5532083401c6SMatthew G. Knepley     PetscDS   ds;
5533083401c6SMatthew G. Knepley     IS        fields;
5534083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5535083401c6SMatthew G. Knepley 
5536083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5537083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5538083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5539083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5540083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5541083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5542083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5543083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5544083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5545083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5546083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5547083401c6SMatthew G. Knepley     {
5548083401c6SMatthew G. Knepley       DMPolytopeType ct;
5549083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5550083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
55510122748bSMatthew G. Knepley 
5552e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5553665f567fSMatthew G. Knepley       if (lStart >= 0) {
5554412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5555412e9a14SMatthew G. Knepley         switch (ct) {
5556412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5557412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5558412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5559412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5560083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5561083401c6SMatthew G. Knepley           default: break;
5562412e9a14SMatthew G. Knepley         }
5563665f567fSMatthew G. Knepley       }
5564083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5565083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5566e5e52638SMatthew G. Knepley     }
5567083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5568e5e52638SMatthew G. Knepley   }
5569083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5570e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5571083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5572083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5573083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5574083401c6SMatthew G. Knepley     const PetscInt *fld;
5575083401c6SMatthew G. Knepley     PetscInt        nf;
5576e5e52638SMatthew G. Knepley 
5577083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5578083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5579083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5580083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5581083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5582e5e52638SMatthew G. Knepley       PetscClassId id;
5583e5e52638SMatthew G. Knepley 
5584083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5585083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5586083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5587083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5588083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5589e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5590e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5591e5e52638SMatthew G. Knepley     }
5592083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5593e5e52638SMatthew G. Knepley   }
5594e5e52638SMatthew G. Knepley   /* Setup DSes */
5595e5e52638SMatthew G. Knepley   if (doSetup) {
5596e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5597e5e52638SMatthew G. Knepley   }
5598e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5599e5e52638SMatthew G. Knepley }
5600e5e52638SMatthew G. Knepley 
5601e5e52638SMatthew G. Knepley /*@
56027f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
56037f96f943SMatthew G. Knepley 
5604f2cacb80SMatthew G. Knepley   Collective on DM
5605f2cacb80SMatthew G. Knepley 
56067f96f943SMatthew G. Knepley   Input Parameters:
56077f96f943SMatthew G. Knepley + dm   - The DM
56087f96f943SMatthew G. Knepley - time - The time
56097f96f943SMatthew G. Knepley 
56107f96f943SMatthew G. Knepley   Output Parameters:
5611f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5612f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
56137f96f943SMatthew G. Knepley 
56147f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
56157f96f943SMatthew G. Knepley 
56167f96f943SMatthew G. Knepley   Level: developer
56177f96f943SMatthew G. Knepley 
56187f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
56197f96f943SMatthew G. Knepley @*/
5620f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
56217f96f943SMatthew G. Knepley {
56227f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
56237f96f943SMatthew G. Knepley   void            **ectxs;
56247f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
56257f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
56267f96f943SMatthew G. Knepley 
56277f96f943SMatthew G. Knepley   PetscFunctionBegin;
5628f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5629f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5630f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
56317f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
56327f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
56337f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
56347f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
56357f96f943SMatthew G. Knepley     PetscDS         ds;
56367f96f943SMatthew G. Knepley     DMLabel         label;
56377f96f943SMatthew G. Knepley     IS              fieldIS;
56387f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
56397f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
56407f96f943SMatthew G. Knepley 
56417f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
56427f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
56437f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
56447f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
56457f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5646f2cacb80SMatthew G. Knepley     if (u) {
56477f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
56487f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
56497f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
56507f96f943SMatthew G. Knepley       }
56517f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
56527f96f943SMatthew G. Knepley       if (label) {
56537f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56547f96f943SMatthew G. Knepley       } else {
56557f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56567f96f943SMatthew G. Knepley       }
56577f96f943SMatthew G. Knepley     }
5658f2cacb80SMatthew G. Knepley     if (u_t) {
5659f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5660f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5661f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5662f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5663f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5664f2cacb80SMatthew G. Knepley       }
5665f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5666f2cacb80SMatthew G. Knepley       if (label) {
5667f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5668f2cacb80SMatthew G. Knepley       } else {
5669f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5670f2cacb80SMatthew G. Knepley       }
5671f2cacb80SMatthew G. Knepley     }
5672f2cacb80SMatthew G. Knepley   }
5673f2cacb80SMatthew G. Knepley   if (u) {
56747f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
56757f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5676f2cacb80SMatthew G. Knepley   }
5677f2cacb80SMatthew G. Knepley   if (u_t) {
5678f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5679f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5680f2cacb80SMatthew G. Knepley   }
56817f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
56827f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
56837f96f943SMatthew G. Knepley }
56847f96f943SMatthew G. Knepley 
56857f96f943SMatthew G. Knepley /*@
5686e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5687e5e52638SMatthew G. Knepley 
5688d083f849SBarry Smith   Collective on dm
5689e5e52638SMatthew G. Knepley 
5690e5e52638SMatthew G. Knepley   Input Parameter:
5691e5e52638SMatthew G. Knepley . dm - The DM
5692e5e52638SMatthew G. Knepley 
5693e5e52638SMatthew G. Knepley   Output Parameter:
5694e5e52638SMatthew G. Knepley . newdm - The DM
5695e5e52638SMatthew G. Knepley 
5696e5e52638SMatthew G. Knepley   Level: advanced
5697e5e52638SMatthew G. Knepley 
5698e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5699e5e52638SMatthew G. Knepley @*/
5700e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5701e5e52638SMatthew G. Knepley {
5702e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5703e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5704e5e52638SMatthew G. Knepley 
5705e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5706e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5707e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5708e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5709e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5710e5e52638SMatthew G. Knepley     DMLabel  label;
5711b3cf3223SMatthew G. Knepley     IS       fields;
5712e5e52638SMatthew G. Knepley     PetscDS  ds;
5713783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5714e5e52638SMatthew G. Knepley 
5715b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5716b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5717783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5718783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5719783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5720783e2ec8SMatthew G. Knepley       PetscInt    field;
5721783e2ec8SMatthew G. Knepley 
5722783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
572356cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5724783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5725783e2ec8SMatthew G. Knepley     }
5726e5e52638SMatthew G. Knepley   }
5727e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5728e5e52638SMatthew G. Knepley }
5729e5e52638SMatthew G. Knepley 
5730e5e52638SMatthew G. Knepley /*@
5731e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5732e5e52638SMatthew G. Knepley 
5733d083f849SBarry Smith   Collective on dm
5734e5e52638SMatthew G. Knepley 
5735e5e52638SMatthew G. Knepley   Input Parameter:
5736e5e52638SMatthew G. Knepley . dm - The DM
5737e5e52638SMatthew G. Knepley 
5738e5e52638SMatthew G. Knepley   Output Parameter:
5739e5e52638SMatthew G. Knepley . newdm - The DM
5740e5e52638SMatthew G. Knepley 
5741e5e52638SMatthew G. Knepley   Level: advanced
5742e5e52638SMatthew G. Knepley 
5743e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5744e5e52638SMatthew G. Knepley @*/
5745e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5746e5e52638SMatthew G. Knepley {
5747e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5748e5e52638SMatthew G. Knepley 
5749e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5750e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5751e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5752e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5753e5e52638SMatthew G. Knepley }
5754e5e52638SMatthew G. Knepley 
5755b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5756b64e0483SPeter Brune {
5757b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5758b64e0483SPeter Brune   PetscErrorCode ierr;
5759b64e0483SPeter Brune   Vec coords,ccoords;
57606dbf9973SLawrence Mitchell   Mat inject;
5761b64e0483SPeter Brune   PetscFunctionBegin;
5762b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5763b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5764b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5765b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5766b64e0483SPeter Brune   if (coords && !ccoords) {
5767b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
57686668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
57696dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
57702adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
57716dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5772b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5773b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5774b64e0483SPeter Brune   }
5775b64e0483SPeter Brune   PetscFunctionReturn(0);
5776b64e0483SPeter Brune }
5777b64e0483SPeter Brune 
577803dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
577903dadc2fSPeter Brune {
578003dadc2fSPeter Brune   DM dm_coord,subdm_coord;
578103dadc2fSPeter Brune   PetscErrorCode ierr;
578203dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
578303dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
578403dadc2fSPeter Brune   PetscFunctionBegin;
578503dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
578603dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
578703dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
578803dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
578903dadc2fSPeter Brune   if (coords && !ccoords) {
579003dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
57916668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
579203dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
579324640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
579403dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
579503dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
579603dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
57971ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
579803dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
579903dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
580003dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
580103dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
580203dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
580303dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
580403dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
580503dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
580603dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
580703dadc2fSPeter Brune   }
580803dadc2fSPeter Brune   PetscFunctionReturn(0);
580903dadc2fSPeter Brune }
581003dadc2fSPeter Brune 
5811c73cfb54SMatthew G. Knepley /*@
5812c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5813c73cfb54SMatthew G. Knepley 
5814c73cfb54SMatthew G. Knepley   Not collective
5815c73cfb54SMatthew G. Knepley 
5816c73cfb54SMatthew G. Knepley   Input Parameter:
5817c73cfb54SMatthew G. Knepley . dm - The DM
5818c73cfb54SMatthew G. Knepley 
5819c73cfb54SMatthew G. Knepley   Output Parameter:
5820c73cfb54SMatthew G. Knepley . dim - The topological dimension
5821c73cfb54SMatthew G. Knepley 
5822c73cfb54SMatthew G. Knepley   Level: beginner
5823c73cfb54SMatthew G. Knepley 
5824c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5825c73cfb54SMatthew G. Knepley @*/
5826c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5827c73cfb54SMatthew G. Knepley {
5828c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5829c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5830534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5831c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5832c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5833c73cfb54SMatthew G. Knepley }
5834c73cfb54SMatthew G. Knepley 
5835c73cfb54SMatthew G. Knepley /*@
5836c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5837c73cfb54SMatthew G. Knepley 
5838c73cfb54SMatthew G. Knepley   Collective on dm
5839c73cfb54SMatthew G. Knepley 
5840c73cfb54SMatthew G. Knepley   Input Parameters:
5841c73cfb54SMatthew G. Knepley + dm - The DM
5842c73cfb54SMatthew G. Knepley - dim - The topological dimension
5843c73cfb54SMatthew G. Knepley 
5844c73cfb54SMatthew G. Knepley   Level: beginner
5845c73cfb54SMatthew G. Knepley 
5846c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5847c73cfb54SMatthew G. Knepley @*/
5848c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5849c73cfb54SMatthew G. Knepley {
5850e5e52638SMatthew G. Knepley   PetscDS        ds;
5851f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5852f17e8794SMatthew G. Knepley 
5853c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5854c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5855c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5856c73cfb54SMatthew G. Knepley   dm->dim = dim;
5857e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5858e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5859c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5860c73cfb54SMatthew G. Knepley }
5861c73cfb54SMatthew G. Knepley 
5862793f3fe5SMatthew G. Knepley /*@
5863793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5864793f3fe5SMatthew G. Knepley 
5865d083f849SBarry Smith   Collective on dm
5866793f3fe5SMatthew G. Knepley 
5867793f3fe5SMatthew G. Knepley   Input Parameters:
5868793f3fe5SMatthew G. Knepley + dm - the DM
5869793f3fe5SMatthew G. Knepley - dim - the dimension
5870793f3fe5SMatthew G. Knepley 
5871793f3fe5SMatthew G. Knepley   Output Parameters:
5872793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5873aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5874793f3fe5SMatthew G. Knepley 
5875793f3fe5SMatthew G. Knepley   Note:
5876793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5877a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5878793f3fe5SMatthew G. Knepley   then the interval is empty.
5879793f3fe5SMatthew G. Knepley 
5880793f3fe5SMatthew G. Knepley   Level: intermediate
5881793f3fe5SMatthew G. Knepley 
5882793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5883793f3fe5SMatthew G. Knepley @*/
5884793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5885793f3fe5SMatthew G. Knepley {
5886793f3fe5SMatthew G. Knepley   PetscInt       d;
5887793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5888793f3fe5SMatthew G. Knepley 
5889793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5890793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5891793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5892793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5893b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5894793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5895793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5896793f3fe5SMatthew G. Knepley }
5897793f3fe5SMatthew G. Knepley 
58986636e97aSMatthew G Knepley /*@
58996636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
59006636e97aSMatthew G Knepley 
5901d083f849SBarry Smith   Collective on dm
59026636e97aSMatthew G Knepley 
59036636e97aSMatthew G Knepley   Input Parameters:
59046636e97aSMatthew G Knepley + dm - the DM
59056636e97aSMatthew G Knepley - c - coordinate vector
59066636e97aSMatthew G Knepley 
59072dd40e9bSPatrick Sanan   Notes:
59082dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
59092dd40e9bSPatrick Sanan 
59102dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59116636e97aSMatthew G Knepley 
59126636e97aSMatthew G Knepley   Level: intermediate
59136636e97aSMatthew G Knepley 
59142dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59156636e97aSMatthew G Knepley @*/
59166636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
59176636e97aSMatthew G Knepley {
59186636e97aSMatthew G Knepley   PetscErrorCode ierr;
59196636e97aSMatthew G Knepley 
59206636e97aSMatthew G Knepley   PetscFunctionBegin;
59216636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59226636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59236636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59246636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59256636e97aSMatthew G Knepley   dm->coordinates = c;
59266636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5927b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
592803dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
59296636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59306636e97aSMatthew G Knepley }
59316636e97aSMatthew G Knepley 
59326636e97aSMatthew G Knepley /*@
59336636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
59346636e97aSMatthew G Knepley 
59357058e716SVaclav Hapla   Not collective
59366636e97aSMatthew G Knepley 
59376636e97aSMatthew G Knepley    Input Parameters:
59386636e97aSMatthew G Knepley +  dm - the DM
59396636e97aSMatthew G Knepley -  c - coordinate vector
59406636e97aSMatthew G Knepley 
59412dd40e9bSPatrick Sanan   Notes:
59426636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
59436636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
59446636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
59456636e97aSMatthew G Knepley 
59462dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59472dd40e9bSPatrick Sanan 
59486636e97aSMatthew G Knepley   Level: intermediate
59496636e97aSMatthew G Knepley 
59506636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
59516636e97aSMatthew G Knepley @*/
59526636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
59536636e97aSMatthew G Knepley {
59546636e97aSMatthew G Knepley   PetscErrorCode ierr;
59556636e97aSMatthew G Knepley 
59566636e97aSMatthew G Knepley   PetscFunctionBegin;
59576636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59586636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59596636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59606636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
59618865f1eaSKarl Rupp 
59626636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
59638865f1eaSKarl Rupp 
59646636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59656636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59666636e97aSMatthew G Knepley }
59676636e97aSMatthew G Knepley 
59686636e97aSMatthew G Knepley /*@
59696636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
59706636e97aSMatthew G Knepley 
5971d083f849SBarry Smith   Collective on dm
59726636e97aSMatthew G Knepley 
59736636e97aSMatthew G Knepley   Input Parameter:
59746636e97aSMatthew G Knepley . dm - the DM
59756636e97aSMatthew G Knepley 
59766636e97aSMatthew G Knepley   Output Parameter:
59776636e97aSMatthew G Knepley . c - global coordinate vector
59786636e97aSMatthew G Knepley 
59796636e97aSMatthew G Knepley   Note:
59806636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59816636e97aSMatthew G Knepley 
59826636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
59836636e97aSMatthew G Knepley 
59846636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59856636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59866636e97aSMatthew G Knepley 
59876636e97aSMatthew G Knepley   Level: intermediate
59886636e97aSMatthew G Knepley 
59896636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59906636e97aSMatthew G Knepley @*/
59916636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
59926636e97aSMatthew G Knepley {
59936636e97aSMatthew G Knepley   PetscErrorCode ierr;
59946636e97aSMatthew G Knepley 
59956636e97aSMatthew G Knepley   PetscFunctionBegin;
59966636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59976636e97aSMatthew G Knepley   PetscValidPointer(c,2);
59981f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
59990298fd71SBarry Smith     DM        cdm = NULL;
60001970a576SMatthew G. Knepley     PetscBool localized;
60016636e97aSMatthew G Knepley 
60026636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
60036636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
60041970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60051970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
60061970a576SMatthew G. Knepley     if (localized) {
60071970a576SMatthew G. Knepley       PetscInt cdim;
60081970a576SMatthew G. Knepley 
60091970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60101970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60111970a576SMatthew G. Knepley     }
60126636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
60136636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60146636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60156636e97aSMatthew G Knepley   }
60166636e97aSMatthew G Knepley   *c = dm->coordinates;
60176636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60186636e97aSMatthew G Knepley }
60196636e97aSMatthew G Knepley 
60206636e97aSMatthew G Knepley /*@
602181e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
602281e9a530SVaclav Hapla 
6023d083f849SBarry Smith   Collective on dm
602481e9a530SVaclav Hapla 
602581e9a530SVaclav Hapla   Input Parameter:
602681e9a530SVaclav Hapla . dm - the DM
602781e9a530SVaclav Hapla 
602881e9a530SVaclav Hapla   Level: advanced
602981e9a530SVaclav Hapla 
603081e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
603181e9a530SVaclav Hapla @*/
603281e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
603381e9a530SVaclav Hapla {
603481e9a530SVaclav Hapla   PetscErrorCode ierr;
603581e9a530SVaclav Hapla 
603681e9a530SVaclav Hapla   PetscFunctionBegin;
603781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
603881e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
60391970a576SMatthew G. Knepley     DM        cdm = NULL;
60401970a576SMatthew G. Knepley     PetscBool localized;
60411970a576SMatthew G. Knepley 
604281e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
604381e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
60441970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60451970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
60461970a576SMatthew G. Knepley     if (localized) {
60471970a576SMatthew G. Knepley       PetscInt cdim;
60481970a576SMatthew G. Knepley 
60491970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60501970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60511970a576SMatthew G. Knepley     }
605281e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
605381e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
605481e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
605581e9a530SVaclav Hapla   }
605681e9a530SVaclav Hapla   PetscFunctionReturn(0);
605781e9a530SVaclav Hapla }
605881e9a530SVaclav Hapla 
605981e9a530SVaclav Hapla /*@
60606636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
60616636e97aSMatthew G Knepley 
6062d083f849SBarry Smith   Collective on dm
60636636e97aSMatthew G Knepley 
60646636e97aSMatthew G Knepley   Input Parameter:
60656636e97aSMatthew G Knepley . dm - the DM
60666636e97aSMatthew G Knepley 
60676636e97aSMatthew G Knepley   Output Parameter:
60686636e97aSMatthew G Knepley . c - coordinate vector
60696636e97aSMatthew G Knepley 
60706636e97aSMatthew G Knepley   Note:
60716636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
60726636e97aSMatthew G Knepley 
60736636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
60746636e97aSMatthew G Knepley 
60756636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60766636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60776636e97aSMatthew G Knepley 
60786636e97aSMatthew G Knepley   Level: intermediate
60796636e97aSMatthew G Knepley 
608081e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
60816636e97aSMatthew G Knepley @*/
60826636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
60836636e97aSMatthew G Knepley {
60846636e97aSMatthew G Knepley   PetscErrorCode ierr;
60856636e97aSMatthew G Knepley 
60866636e97aSMatthew G Knepley   PetscFunctionBegin;
60876636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60886636e97aSMatthew G Knepley   PetscValidPointer(c,2);
608981e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
60906636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
60916636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60926636e97aSMatthew G Knepley }
60936636e97aSMatthew G Knepley 
609481e9a530SVaclav Hapla /*@
609581e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
609681e9a530SVaclav Hapla 
609781e9a530SVaclav Hapla   Not collective
609881e9a530SVaclav Hapla 
609981e9a530SVaclav Hapla   Input Parameter:
610081e9a530SVaclav Hapla . dm - the DM
610181e9a530SVaclav Hapla 
610281e9a530SVaclav Hapla   Output Parameter:
610381e9a530SVaclav Hapla . c - coordinate vector
610481e9a530SVaclav Hapla 
610581e9a530SVaclav Hapla   Level: advanced
610681e9a530SVaclav Hapla 
610781e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
610881e9a530SVaclav Hapla @*/
610981e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
611081e9a530SVaclav Hapla {
611181e9a530SVaclav Hapla   PetscFunctionBegin;
611281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
611381e9a530SVaclav Hapla   PetscValidPointer(c,2);
611481e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
61156636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
61166636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61176636e97aSMatthew G Knepley }
61186636e97aSMatthew G Knepley 
61192db98f8dSVaclav Hapla /*@
61202db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
61212db98f8dSVaclav Hapla 
61222db98f8dSVaclav Hapla   Not collective
61232db98f8dSVaclav Hapla 
61242db98f8dSVaclav Hapla   Input Parameter:
61252db98f8dSVaclav Hapla + dm - the DM
61262db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
61272db98f8dSVaclav Hapla 
61282db98f8dSVaclav Hapla   Output Parameter:
61292db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
61302db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
61312db98f8dSVaclav Hapla 
61322db98f8dSVaclav Hapla   Note:
61332db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
61342db98f8dSVaclav Hapla 
61352db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
61362db98f8dSVaclav Hapla 
61372db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
61382db98f8dSVaclav Hapla 
61392db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61402db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61412db98f8dSVaclav Hapla 
61422db98f8dSVaclav Hapla   Level: advanced
61432db98f8dSVaclav Hapla 
61442db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
61452db98f8dSVaclav Hapla @*/
61462db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
61472db98f8dSVaclav Hapla {
61482db98f8dSVaclav Hapla   PetscSection        cs, newcs;
61492db98f8dSVaclav Hapla   Vec                 coords;
61502db98f8dSVaclav Hapla   const PetscScalar   *arr;
61512db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
61522db98f8dSVaclav Hapla   PetscInt            n;
61532db98f8dSVaclav Hapla   PetscErrorCode      ierr;
61542db98f8dSVaclav Hapla 
61552db98f8dSVaclav Hapla   PetscFunctionBegin;
61562db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61572db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
61582db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
61592db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
61602db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
61611bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
61621bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
61632db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
61642db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
61652db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
61662db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
61672db98f8dSVaclav Hapla   if (pCoord) {
61682db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
61692db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
61702db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
61712db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6172ad9ac99dSVaclav Hapla   } else {
6173ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
61742db98f8dSVaclav Hapla   }
6175ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6176ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
61772db98f8dSVaclav Hapla   PetscFunctionReturn(0);
61782db98f8dSVaclav Hapla }
61792db98f8dSVaclav Hapla 
6180f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6181f19dbd58SToby Isaac {
6182f19dbd58SToby Isaac   PetscErrorCode ierr;
6183f19dbd58SToby Isaac 
6184f19dbd58SToby Isaac   PetscFunctionBegin;
6185f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6186f19dbd58SToby Isaac   PetscValidPointer(field,2);
6187f19dbd58SToby Isaac   if (!dm->coordinateField) {
6188f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6189f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6190f19dbd58SToby Isaac     }
6191f19dbd58SToby Isaac   }
6192f19dbd58SToby Isaac   *field = dm->coordinateField;
6193f19dbd58SToby Isaac   PetscFunctionReturn(0);
6194f19dbd58SToby Isaac }
6195f19dbd58SToby Isaac 
6196f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6197f19dbd58SToby Isaac {
6198f19dbd58SToby Isaac   PetscErrorCode ierr;
6199f19dbd58SToby Isaac 
6200f19dbd58SToby Isaac   PetscFunctionBegin;
6201f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6202f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6203c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6204f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6205f19dbd58SToby Isaac   dm->coordinateField = field;
6206f19dbd58SToby Isaac   PetscFunctionReturn(0);
6207f19dbd58SToby Isaac }
6208f19dbd58SToby Isaac 
62096636e97aSMatthew G Knepley /*@
62101cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
62116636e97aSMatthew G Knepley 
6212d083f849SBarry Smith   Collective on dm
62136636e97aSMatthew G Knepley 
62146636e97aSMatthew G Knepley   Input Parameter:
62156636e97aSMatthew G Knepley . dm - the DM
62166636e97aSMatthew G Knepley 
62176636e97aSMatthew G Knepley   Output Parameter:
62186636e97aSMatthew G Knepley . cdm - coordinate DM
62196636e97aSMatthew G Knepley 
62206636e97aSMatthew G Knepley   Level: intermediate
62216636e97aSMatthew G Knepley 
62221cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62236636e97aSMatthew G Knepley @*/
62246636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
62256636e97aSMatthew G Knepley {
62266636e97aSMatthew G Knepley   PetscErrorCode ierr;
62276636e97aSMatthew G Knepley 
62286636e97aSMatthew G Knepley   PetscFunctionBegin;
62296636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62306636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
62316636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6232308f8a94SToby Isaac     DM cdm;
6233308f8a94SToby Isaac 
623482f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6235308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6236308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6237308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6238308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6239308f8a94SToby Isaac     dm->coordinateDM = cdm;
62406636e97aSMatthew G Knepley   }
62416636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
62426636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62436636e97aSMatthew G Knepley }
6244e87bb0d3SMatthew G Knepley 
62451cfe2091SMatthew G. Knepley /*@
62461cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
62471cfe2091SMatthew G. Knepley 
6248d083f849SBarry Smith   Logically Collective on dm
62491cfe2091SMatthew G. Knepley 
62501cfe2091SMatthew G. Knepley   Input Parameters:
62511cfe2091SMatthew G. Knepley + dm - the DM
62521cfe2091SMatthew G. Knepley - cdm - coordinate DM
62531cfe2091SMatthew G. Knepley 
62541cfe2091SMatthew G. Knepley   Level: intermediate
62551cfe2091SMatthew G. Knepley 
62561cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62571cfe2091SMatthew G. Knepley @*/
62581cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
62591cfe2091SMatthew G. Knepley {
62601cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
62611cfe2091SMatthew G. Knepley 
62621cfe2091SMatthew G. Knepley   PetscFunctionBegin;
62631cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62641cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6265f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
62661cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
62671cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
62681cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
62691cfe2091SMatthew G. Knepley }
62701cfe2091SMatthew G. Knepley 
627146e270d4SMatthew G. Knepley /*@
627246e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
627346e270d4SMatthew G. Knepley 
627446e270d4SMatthew G. Knepley   Not Collective
627546e270d4SMatthew G. Knepley 
627646e270d4SMatthew G. Knepley   Input Parameter:
627746e270d4SMatthew G. Knepley . dm - The DM object
627846e270d4SMatthew G. Knepley 
627946e270d4SMatthew G. Knepley   Output Parameter:
628046e270d4SMatthew G. Knepley . dim - The embedding dimension
628146e270d4SMatthew G. Knepley 
628246e270d4SMatthew G. Knepley   Level: intermediate
628346e270d4SMatthew G. Knepley 
628492fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
628546e270d4SMatthew G. Knepley @*/
628646e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
628746e270d4SMatthew G. Knepley {
628846e270d4SMatthew G. Knepley   PetscFunctionBegin;
628946e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6290534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
62919a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
62929a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
62939a9a41abSToby Isaac   }
629446e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
629546e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
629646e270d4SMatthew G. Knepley }
629746e270d4SMatthew G. Knepley 
629846e270d4SMatthew G. Knepley /*@
629946e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
630046e270d4SMatthew G. Knepley 
630146e270d4SMatthew G. Knepley   Not Collective
630246e270d4SMatthew G. Knepley 
630346e270d4SMatthew G. Knepley   Input Parameters:
630446e270d4SMatthew G. Knepley + dm  - The DM object
630546e270d4SMatthew G. Knepley - dim - The embedding dimension
630646e270d4SMatthew G. Knepley 
630746e270d4SMatthew G. Knepley   Level: intermediate
630846e270d4SMatthew G. Knepley 
630992fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
631046e270d4SMatthew G. Knepley @*/
631146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
631246e270d4SMatthew G. Knepley {
6313e5e52638SMatthew G. Knepley   PetscDS        ds;
6314f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6315f17e8794SMatthew G. Knepley 
631646e270d4SMatthew G. Knepley   PetscFunctionBegin;
631746e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
631846e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6319e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6320e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
632146e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
632246e270d4SMatthew G. Knepley }
632346e270d4SMatthew G. Knepley 
6324e8abe2deSMatthew G. Knepley /*@
6325e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6326e8abe2deSMatthew G. Knepley 
6327d083f849SBarry Smith   Collective on dm
6328e8abe2deSMatthew G. Knepley 
6329e8abe2deSMatthew G. Knepley   Input Parameter:
6330e8abe2deSMatthew G. Knepley . dm - The DM object
6331e8abe2deSMatthew G. Knepley 
6332e8abe2deSMatthew G. Knepley   Output Parameter:
6333e8abe2deSMatthew G. Knepley . section - The PetscSection object
6334e8abe2deSMatthew G. Knepley 
6335e8abe2deSMatthew G. Knepley   Level: intermediate
6336e8abe2deSMatthew G. Knepley 
633792fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6338e8abe2deSMatthew G. Knepley @*/
6339e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6340e8abe2deSMatthew G. Knepley {
6341e8abe2deSMatthew G. Knepley   DM             cdm;
6342e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6343e8abe2deSMatthew G. Knepley 
6344e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6345e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6346e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6347e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
634892fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6349e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6350e8abe2deSMatthew G. Knepley }
6351e8abe2deSMatthew G. Knepley 
6352e8abe2deSMatthew G. Knepley /*@
6353e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6354e8abe2deSMatthew G. Knepley 
6355e8abe2deSMatthew G. Knepley   Not Collective
6356e8abe2deSMatthew G. Knepley 
6357e8abe2deSMatthew G. Knepley   Input Parameters:
6358e8abe2deSMatthew G. Knepley + dm      - The DM object
635946e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6360e8abe2deSMatthew G. Knepley - section - The PetscSection object
6361e8abe2deSMatthew G. Knepley 
6362e8abe2deSMatthew G. Knepley   Level: intermediate
6363e8abe2deSMatthew G. Knepley 
636492fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6365e8abe2deSMatthew G. Knepley @*/
636646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6367e8abe2deSMatthew G. Knepley {
6368e8abe2deSMatthew G. Knepley   DM             cdm;
6369e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6370e8abe2deSMatthew G. Knepley 
6371e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6372e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
637346e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6374e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
637592fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
637646e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
63774c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
637846e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
637946e270d4SMatthew G. Knepley 
638046e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
638146e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
638246e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
638346e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
638446e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
638546e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
638646e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
638746e270d4SMatthew G. Knepley     }
6388ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
638946e270d4SMatthew G. Knepley   }
6390e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6391e8abe2deSMatthew G. Knepley }
6392e8abe2deSMatthew G. Knepley 
6393d864a3eaSLisandro Dalcin /*@
6394d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6395d864a3eaSLisandro Dalcin 
6396d864a3eaSLisandro Dalcin   Input Parameters:
6397d864a3eaSLisandro Dalcin + dm      - The DM object
6398d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6399d864a3eaSLisandro Dalcin 
6400d864a3eaSLisandro Dalcin   Level: intermediate
6401d864a3eaSLisandro Dalcin 
6402d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6403d864a3eaSLisandro Dalcin @*/
6404d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6405d864a3eaSLisandro Dalcin {
6406d864a3eaSLisandro Dalcin   PetscObject    discOld;
6407d864a3eaSLisandro Dalcin   PetscClassId   classid;
6408d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6409d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6410d864a3eaSLisandro Dalcin   Mat            matInterp;
6411d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6412d864a3eaSLisandro Dalcin 
6413d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6414d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6415d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6416d864a3eaSLisandro Dalcin 
6417d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6418d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6419d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6420d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
642129ad44c5SMatthew G. Knepley   if (classid != PETSCFE_CLASSID) {
642229ad44c5SMatthew G. Knepley     if (classid == PETSC_CONTAINER_CLASSID) {
642329ad44c5SMatthew G. Knepley       PetscFE        feLinear;
642429ad44c5SMatthew G. Knepley       DMPolytopeType ct;
642529ad44c5SMatthew G. Knepley       PetscInt       dim, dE, cStart;
642629ad44c5SMatthew G. Knepley       PetscBool      simplex;
642729ad44c5SMatthew G. Knepley 
642829ad44c5SMatthew G. Knepley       /* Assume linear vertex coordinates */
642929ad44c5SMatthew G. Knepley       ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
643029ad44c5SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
643129ad44c5SMatthew G. Knepley       ierr = DMPlexGetHeightStratum(cdmOld, 0, &cStart, NULL);CHKERRQ(ierr);
643229ad44c5SMatthew G. Knepley       ierr = DMPlexGetCellType(dm, cStart, &ct);CHKERRQ(ierr);
643329ad44c5SMatthew G. Knepley       switch (ct) {
643429ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM:
643529ad44c5SMatthew G. Knepley         case DM_POLYTOPE_TRI_PRISM_TENSOR:
643629ad44c5SMatthew G. Knepley           SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP, "Cannot autoamtically create coordinate space for prisms");
643729ad44c5SMatthew G. Knepley         default: break;
643829ad44c5SMatthew G. Knepley       }
643929ad44c5SMatthew G. Knepley       simplex = DMPolytopeTypeGetNumVertices(ct) == DMPolytopeTypeGetDim(ct)+1 ? PETSC_TRUE : PETSC_FALSE;
644029ad44c5SMatthew G. Knepley       ierr = PetscFECreateLagrange(PETSC_COMM_SELF, dim, dE, simplex, 1, -1, &feLinear);CHKERRQ(ierr);
644129ad44c5SMatthew G. Knepley       ierr = DMSetField(cdmOld, 0, NULL, (PetscObject) feLinear);CHKERRQ(ierr);
644229ad44c5SMatthew G. Knepley       ierr = PetscFEDestroy(&feLinear);CHKERRQ(ierr);
644329ad44c5SMatthew G. Knepley       ierr = DMCreateDS(cdmOld);CHKERRQ(ierr);
644429ad44c5SMatthew G. Knepley     } else {
644529ad44c5SMatthew G. Knepley       const char *discname;
644629ad44c5SMatthew G. Knepley 
644729ad44c5SMatthew G. Knepley       ierr = PetscObjectGetType(discOld, &discname);CHKERRQ(ierr);
644829ad44c5SMatthew G. Knepley       SETERRQ1(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type %s not supported", discname);
644929ad44c5SMatthew G. Knepley     }
645029ad44c5SMatthew G. Knepley   }
6451d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6452d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6453d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6454d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6455d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6456d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6457d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6458d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6459d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6460d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6461d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6462d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6463d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6464d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6465d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6466d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6467d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6468d864a3eaSLisandro Dalcin }
6469d864a3eaSLisandro Dalcin 
64705dc8c3f7SMatthew G. Knepley /*@C
647190b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
64725dc8c3f7SMatthew G. Knepley 
64735dc8c3f7SMatthew G. Knepley   Input Parameters:
647490b157c4SStefano Zampini . dm      - The DM object
647590b157c4SStefano Zampini 
647690b157c4SStefano Zampini   Output Parameters:
647790b157c4SStefano Zampini + per     - Whether the DM is periodic or not
64785dc8c3f7SMatthew 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
64795dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64805dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64815dc8c3f7SMatthew G. Knepley 
64825dc8c3f7SMatthew G. Knepley   Level: developer
64835dc8c3f7SMatthew G. Knepley 
64845dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64855dc8c3f7SMatthew G. Knepley @*/
648690b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6487c6b900c6SMatthew G. Knepley {
6488c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6489c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
649090b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6491c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6492c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
64935dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6494c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6495c6b900c6SMatthew G. Knepley }
6496c6b900c6SMatthew G. Knepley 
64975dc8c3f7SMatthew G. Knepley /*@C
64985dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
64995dc8c3f7SMatthew G. Knepley 
65005dc8c3f7SMatthew G. Knepley   Input Parameters:
65015dc8c3f7SMatthew G. Knepley + dm      - The DM object
6502db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6503db2bf62eSStefano Zampini . maxCell - Over distances greater than this, we can assume a point has crossed over to another sheet, when trying to localize cell coordinates. Pass NULL to remove such information.
65045dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
65055dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
65065dc8c3f7SMatthew G. Knepley 
6507db2bf62eSStefano Zampini   Notes: If per is PETSC_TRUE and maxCell is not provided, coordinates need to be already localized, or must be localized by hand by the user.
6508db2bf62eSStefano Zampini 
65095dc8c3f7SMatthew G. Knepley   Level: developer
65105dc8c3f7SMatthew G. Knepley 
65115dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
65125dc8c3f7SMatthew G. Knepley @*/
651390b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6514c6b900c6SMatthew G. Knepley {
6515c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6516c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6517c6b900c6SMatthew G. Knepley 
6518c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6519c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
652090b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6521412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6522412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6523412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
65245dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
652590b157c4SStefano Zampini   if (maxCell) {
6526412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6527412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6528db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6529db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6530412e9a14SMatthew G. Knepley   }
6531db2bf62eSStefano Zampini 
6532412e9a14SMatthew G. Knepley   if (L) {
6533412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6534412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6535412e9a14SMatthew G. Knepley   }
6536412e9a14SMatthew G. Knepley   if (bd) {
6537412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6538412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
653990b157c4SStefano Zampini   }
6540072d7d67SStefano Zampini   dm->periodic = per;
6541c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6542c6b900c6SMatthew G. Knepley }
6543c6b900c6SMatthew G. Knepley 
65442e17dfb7SMatthew G. Knepley /*@
65452e17dfb7SMatthew 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.
65462e17dfb7SMatthew G. Knepley 
65472e17dfb7SMatthew G. Knepley   Input Parameters:
65482e17dfb7SMatthew G. Knepley + dm     - The DM
654965da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
655065da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
65512e17dfb7SMatthew G. Knepley 
65522e17dfb7SMatthew G. Knepley   Output Parameter:
65532e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65542e17dfb7SMatthew G. Knepley 
65552e17dfb7SMatthew G. Knepley   Level: developer
65562e17dfb7SMatthew G. Knepley 
65572e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65582e17dfb7SMatthew G. Knepley @*/
655965da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
65602e17dfb7SMatthew G. Knepley {
65612e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
65622e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
65632e17dfb7SMatthew G. Knepley 
65642e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65652e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
65662e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65672e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65682e17dfb7SMatthew G. Knepley   } else {
656965da65dcSMatthew G. Knepley     if (endpoint) {
657065da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6571da3333bfSMatthew 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)) {
6572da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
657365da65dcSMatthew G. Knepley         } else {
6574da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
657565da65dcSMatthew G. Knepley         }
657665da65dcSMatthew G. Knepley       }
657765da65dcSMatthew G. Knepley     } else {
65782e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
65791118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
65802e17dfb7SMatthew G. Knepley       }
65812e17dfb7SMatthew G. Knepley     }
658265da65dcSMatthew G. Knepley   }
65832e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65842e17dfb7SMatthew G. Knepley }
65852e17dfb7SMatthew G. Knepley 
65862e17dfb7SMatthew G. Knepley /*
65872e17dfb7SMatthew 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.
65882e17dfb7SMatthew G. Knepley 
65892e17dfb7SMatthew G. Knepley   Input Parameters:
65902e17dfb7SMatthew G. Knepley + dm     - The DM
65912e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
65922e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
65932e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
65942e17dfb7SMatthew G. Knepley 
65952e17dfb7SMatthew G. Knepley   Output Parameter:
65962e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65972e17dfb7SMatthew G. Knepley 
65982e17dfb7SMatthew G. Knepley   Level: developer
65992e17dfb7SMatthew G. Knepley 
66002e17dfb7SMatthew 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
66012e17dfb7SMatthew G. Knepley 
66022e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
66032e17dfb7SMatthew G. Knepley */
66042e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66052e17dfb7SMatthew G. Knepley {
66062e17dfb7SMatthew G. Knepley   PetscInt d;
66072e17dfb7SMatthew G. Knepley 
66082e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66092e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66102e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66112e17dfb7SMatthew G. Knepley   } else {
66122e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6613908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
66142e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
66152e17dfb7SMatthew G. Knepley       } else {
66162e17dfb7SMatthew G. Knepley         out[d] = in[d];
66172e17dfb7SMatthew G. Knepley       }
66182e17dfb7SMatthew G. Knepley     }
66192e17dfb7SMatthew G. Knepley   }
66202e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66212e17dfb7SMatthew G. Knepley }
6622a5801f52SStefano Zampini 
66232e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
66242e17dfb7SMatthew G. Knepley {
66252e17dfb7SMatthew G. Knepley   PetscInt d;
66262e17dfb7SMatthew G. Knepley 
66272e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66282e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66292e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66302e17dfb7SMatthew G. Knepley   } else {
66312e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6632908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
66332e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
66342e17dfb7SMatthew G. Knepley       } else {
66352e17dfb7SMatthew G. Knepley         out[d] = in[d];
66362e17dfb7SMatthew G. Knepley       }
66372e17dfb7SMatthew G. Knepley     }
66382e17dfb7SMatthew G. Knepley   }
66392e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66402e17dfb7SMatthew G. Knepley }
66412e17dfb7SMatthew G. Knepley 
66422e17dfb7SMatthew G. Knepley /*
66432e17dfb7SMatthew 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.
66442e17dfb7SMatthew G. Knepley 
66452e17dfb7SMatthew G. Knepley   Input Parameters:
66462e17dfb7SMatthew G. Knepley + dm     - The DM
66472e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
66482e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
66492e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
66502e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
66512e17dfb7SMatthew G. Knepley 
66522e17dfb7SMatthew G. Knepley   Output Parameter:
66532e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
66542e17dfb7SMatthew G. Knepley 
66552e17dfb7SMatthew G. Knepley   Level: developer
66562e17dfb7SMatthew G. Knepley 
66572e17dfb7SMatthew 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
66582e17dfb7SMatthew G. Knepley 
66592e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
66602e17dfb7SMatthew G. Knepley */
66612e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66622e17dfb7SMatthew G. Knepley {
66632e17dfb7SMatthew G. Knepley   PetscInt d;
66642e17dfb7SMatthew G. Knepley 
66652e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66662e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66672e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
66682e17dfb7SMatthew G. Knepley   } else {
66692e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6670412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6671412e9a14SMatthew G. Knepley 
6672412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6673412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6674412e9a14SMatthew G. Knepley 
6675412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6676412e9a14SMatthew G. Knepley           SETERRQ4(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "%D-Coordinate %g more than %g away from anchor %g", d, (double) PetscRealPart(in[d]), (double) maxC, (double) PetscRealPart(anchor[d]));
6677412e9a14SMatthew G. Knepley         out[d] += newCoord;
66782e17dfb7SMatthew G. Knepley       } else {
66792e17dfb7SMatthew G. Knepley         out[d] += in[d];
66802e17dfb7SMatthew G. Knepley       }
66812e17dfb7SMatthew G. Knepley     }
66822e17dfb7SMatthew G. Knepley   }
66832e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66842e17dfb7SMatthew G. Knepley }
66852e17dfb7SMatthew G. Knepley 
668636447a5eSToby Isaac /*@
66878f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
66888f700142SStefano Zampini 
66898f700142SStefano Zampini   Not collective
669036447a5eSToby Isaac 
669136447a5eSToby Isaac   Input Parameter:
669236447a5eSToby Isaac . dm - The DM
669336447a5eSToby Isaac 
669436447a5eSToby Isaac   Output Parameter:
669536447a5eSToby Isaac   areLocalized - True if localized
669636447a5eSToby Isaac 
669736447a5eSToby Isaac   Level: developer
669836447a5eSToby Isaac 
66998f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
670036447a5eSToby Isaac @*/
67018f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
670236447a5eSToby Isaac {
670336447a5eSToby Isaac   DM             cdm;
670436447a5eSToby Isaac   PetscSection   coordSection;
670546a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
670646a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
670736447a5eSToby Isaac   PetscErrorCode ierr;
670836447a5eSToby Isaac 
670936447a5eSToby Isaac   PetscFunctionBegin;
671036447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6711534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67128b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
671346a3a80fSLisandro Dalcin 
671436447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
671536447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
671646a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
67179f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
671846a3a80fSLisandro Dalcin 
67199f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
672046a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
672136447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
672236447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
672346a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
672446a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
672536447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
672646a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
672736447a5eSToby Isaac   }
67288f700142SStefano Zampini   *areLocalized = alreadyLocalized;
672936447a5eSToby Isaac   PetscFunctionReturn(0);
673036447a5eSToby Isaac }
673136447a5eSToby Isaac 
67328f700142SStefano Zampini /*@
67338f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
67348f700142SStefano Zampini 
67358f700142SStefano Zampini   Collective on dm
67368f700142SStefano Zampini 
67378f700142SStefano Zampini   Input Parameter:
67388f700142SStefano Zampini . dm - The DM
67398f700142SStefano Zampini 
67408f700142SStefano Zampini   Output Parameter:
67418f700142SStefano Zampini   areLocalized - True if localized
67428f700142SStefano Zampini 
67438f700142SStefano Zampini   Level: developer
67448f700142SStefano Zampini 
67458f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
67468f700142SStefano Zampini @*/
67478f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
67488f700142SStefano Zampini {
67498f700142SStefano Zampini   PetscBool      localized;
67508f700142SStefano Zampini   PetscErrorCode ierr;
67518f700142SStefano Zampini 
67528f700142SStefano Zampini   PetscFunctionBegin;
67538f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6754534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67558f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
67568f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
67578f700142SStefano Zampini   PetscFunctionReturn(0);
67588f700142SStefano Zampini }
675936447a5eSToby Isaac 
67602e17dfb7SMatthew G. Knepley /*@
6761492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
67622e17dfb7SMatthew G. Knepley 
67638f700142SStefano Zampini   Collective on dm
67648f700142SStefano Zampini 
67652e17dfb7SMatthew G. Knepley   Input Parameter:
67662e17dfb7SMatthew G. Knepley . dm - The DM
67672e17dfb7SMatthew G. Knepley 
67682e17dfb7SMatthew G. Knepley   Level: developer
67692e17dfb7SMatthew G. Knepley 
67708f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
67712e17dfb7SMatthew G. Knepley @*/
67722e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
67732e17dfb7SMatthew G. Knepley {
67742e17dfb7SMatthew G. Knepley   DM             cdm;
67752e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
67762e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
67773e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
67783e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6779e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
67803e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
67813e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
67822e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67832e17dfb7SMatthew G. Knepley 
67842e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67852e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
678692c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6787f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6788f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6789f7cbd40bSStefano Zampini 
67902e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
67912e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
67922e17dfb7SMatthew G. Knepley   {
67932e17dfb7SMatthew G. Knepley     PetscBool isplex;
67942e17dfb7SMatthew G. Knepley 
67952e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
67962e17dfb7SMatthew G. Knepley     if (isplex) {
67972e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
67983e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
679969291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68003e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
68013e922f36SToby Isaac       newStart = vStart;
68023e922f36SToby Isaac       newEnd   = vEnd;
68033e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
68043e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
68053e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
68063e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
68073e922f36SToby Isaac       }
68082e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
68092e17dfb7SMatthew G. Knepley   }
68102e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
681143eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
68122e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
68133e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6814e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
68153e922f36SToby Isaac 
68162e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
68172e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
68182e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
68192e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
68203e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
68213e922f36SToby Isaac 
682269291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68233e922f36SToby Isaac   localized = &anchor[bs];
68243e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
68253e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68263e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68273e922f36SToby Isaac 
68283e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
68293e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
68303e922f36SToby Isaac       PetscInt     b;
68313e922f36SToby Isaac 
68323e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
68333e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68343e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68353e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
68363e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
68373e922f36SToby Isaac         for (b = 0; b < bs; b++) {
68383e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
68393e922f36SToby Isaac         }
68403e922f36SToby Isaac         if (b < bs) break;
68413e922f36SToby Isaac       }
68423e922f36SToby Isaac       if (d < dof/bs) {
68433e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
68443e922f36SToby Isaac           PetscInt cdof;
68453e922f36SToby Isaac 
68463e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
68473e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
68483e922f36SToby Isaac         }
68493e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
68503e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
68513e922f36SToby Isaac       }
68523e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68533e922f36SToby Isaac     }
68543e922f36SToby Isaac   }
68553e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
68563e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
685769291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68583e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
685969291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68603e922f36SToby Isaac     PetscFunctionReturn(0);
68613e922f36SToby Isaac   }
68622e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68632e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68642e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
68652e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
68662e17dfb7SMatthew G. Knepley   }
68672e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
68682e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6869c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
68702e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
68712e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
68722e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
68732e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6874c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68752e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
68762e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68772e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68782e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
68792e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
68802e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
68812e17dfb7SMatthew G. Knepley   }
68823e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68833e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68843e922f36SToby Isaac 
68852e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
68862e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
68873e922f36SToby Isaac       PetscInt     b, cdof;
68882e17dfb7SMatthew G. Knepley 
68893e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
68903e922f36SToby Isaac       if (!cdof) continue;
68912e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68922e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
68932e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68942e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
68952e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68962e17dfb7SMatthew G. Knepley     }
68973e922f36SToby Isaac   }
689869291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
689969291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6900c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
69012e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
69022e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
69032e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
69042e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
69052e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
69062e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
69072e17dfb7SMatthew G. Knepley }
69082e17dfb7SMatthew G. Knepley 
6909e87bb0d3SMatthew G Knepley /*@
69103a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6911e87bb0d3SMatthew G Knepley 
6912d083f849SBarry Smith   Collective on v (see explanation below)
6913e87bb0d3SMatthew G Knepley 
6914e87bb0d3SMatthew G Knepley   Input Parameters:
6915e87bb0d3SMatthew G Knepley + dm - The DM
69163a93e3b7SToby Isaac . v - The Vec of points
691762a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
69183a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6919e87bb0d3SMatthew G Knepley 
692061e3bb9bSMatthew G Knepley   Output Parameter:
692162a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
692262a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
69233a93e3b7SToby Isaac 
6924e87bb0d3SMatthew G Knepley 
6925e87bb0d3SMatthew G Knepley   Level: developer
692661e3bb9bSMatthew G Knepley 
692762a38674SMatthew G. Knepley   Notes:
69283a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
692962a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
69303a93e3b7SToby Isaac 
69313a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
693262a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
69333a93e3b7SToby Isaac 
69343a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
69353a93e3b7SToby Isaac 
693662a38674SMatthew G. Knepley $    const PetscSFNode *cells;
693762a38674SMatthew G. Knepley $    PetscInt           nFound;
6938a6216909SToby Isaac $    const PetscInt    *found;
693962a38674SMatthew G. Knepley $
6940a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
69413a93e3b7SToby Isaac 
69423a93e3b7SToby 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
69433a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
69443a93e3b7SToby Isaac 
694562a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
694661e3bb9bSMatthew G Knepley @*/
694762a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6948e87bb0d3SMatthew G Knepley {
6949735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6950735aa83eSMatthew G Knepley 
6951e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6952e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6953e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6954e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
69553a93e3b7SToby Isaac   if (*cellSF) {
69563a93e3b7SToby Isaac     PetscMPIInt result;
69573a93e3b7SToby Isaac 
6958e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6959a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
69603a93e3b7SToby 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");
6961e0fc9d1bSMatthew G. Knepley   } else {
69623a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
69633a93e3b7SToby Isaac   }
6964b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
696547a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
696662a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
696747a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6968e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6969e87bb0d3SMatthew G Knepley }
697014f150ffSMatthew G. Knepley 
6971f4d763aaSMatthew G. Knepley /*@
6972f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6973f4d763aaSMatthew G. Knepley 
69748f700142SStefano Zampini   Collective on dm
69758f700142SStefano Zampini 
6976f4d763aaSMatthew G. Knepley   Input Parameter:
6977f4d763aaSMatthew G. Knepley . dm - The original DM
6978f4d763aaSMatthew G. Knepley 
6979f4d763aaSMatthew G. Knepley   Output Parameter:
6980f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6981f4d763aaSMatthew G. Knepley 
6982f4d763aaSMatthew G. Knepley   Level: intermediate
6983f4d763aaSMatthew G. Knepley 
6984e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6985f4d763aaSMatthew G. Knepley @*/
698614f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
698714f150ffSMatthew G. Knepley {
6988c26acbdeSMatthew G. Knepley   PetscSection   section;
69892d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
699014f150ffSMatthew G. Knepley   PetscErrorCode ierr;
699114f150ffSMatthew G. Knepley 
699214f150ffSMatthew G. Knepley   PetscFunctionBegin;
699314f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
699414f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
699592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6996c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6997127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
69982d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6999c26acbdeSMatthew G. Knepley     *odm = dm;
7000c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
7001c26acbdeSMatthew G. Knepley   }
700214f150ffSMatthew G. Knepley   if (!dm->dmBC) {
7003c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
700414f150ffSMatthew G. Knepley     PetscSF      sf;
700514f150ffSMatthew G. Knepley 
700614f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
7007e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
700814f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
700992fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
701014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
701114f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
701215b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
7013e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
701414f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
701514f150ffSMatthew G. Knepley   }
701614f150ffSMatthew G. Knepley   *odm = dm->dmBC;
701714f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
701814f150ffSMatthew G. Knepley }
7019f4d763aaSMatthew G. Knepley 
7020f4d763aaSMatthew G. Knepley /*@
7021cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
7022f4d763aaSMatthew G. Knepley 
7023f4d763aaSMatthew G. Knepley   Input Parameter:
7024f4d763aaSMatthew G. Knepley . dm - The original DM
7025f4d763aaSMatthew G. Knepley 
7026cdb7a50dSMatthew G. Knepley   Output Parameters:
7027cdb7a50dSMatthew G. Knepley + num - The output sequence number
7028cdb7a50dSMatthew G. Knepley - val - The output sequence value
7029f4d763aaSMatthew G. Knepley 
7030f4d763aaSMatthew G. Knepley   Level: intermediate
7031f4d763aaSMatthew G. Knepley 
7032f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7033f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7034f4d763aaSMatthew G. Knepley 
7035f4d763aaSMatthew G. Knepley .seealso: VecView()
7036f4d763aaSMatthew G. Knepley @*/
7037cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7038f4d763aaSMatthew G. Knepley {
7039f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7040f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7041534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7042534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7043f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7044f4d763aaSMatthew G. Knepley }
7045f4d763aaSMatthew G. Knepley 
7046f4d763aaSMatthew G. Knepley /*@
7047cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7048f4d763aaSMatthew G. Knepley 
7049f4d763aaSMatthew G. Knepley   Input Parameters:
7050f4d763aaSMatthew G. Knepley + dm - The original DM
7051cdb7a50dSMatthew G. Knepley . num - The output sequence number
7052cdb7a50dSMatthew G. Knepley - val - The output sequence value
7053f4d763aaSMatthew G. Knepley 
7054f4d763aaSMatthew G. Knepley   Level: intermediate
7055f4d763aaSMatthew G. Knepley 
7056f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7057f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7058f4d763aaSMatthew G. Knepley 
7059f4d763aaSMatthew G. Knepley .seealso: VecView()
7060f4d763aaSMatthew G. Knepley @*/
7061cdb7a50dSMatthew G. Knepley PetscErrorCode DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7062f4d763aaSMatthew G. Knepley {
7063f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7064f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7065f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7066cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7067cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7068cdb7a50dSMatthew G. Knepley }
7069cdb7a50dSMatthew G. Knepley 
7070cdb7a50dSMatthew G. Knepley /*@C
7071cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7072cdb7a50dSMatthew G. Knepley 
7073cdb7a50dSMatthew G. Knepley   Input Parameters:
7074cdb7a50dSMatthew G. Knepley + dm   - The original DM
7075cdb7a50dSMatthew G. Knepley . name - The sequence name
7076cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7077cdb7a50dSMatthew G. Knepley 
7078cdb7a50dSMatthew G. Knepley   Output Parameter:
7079cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7080cdb7a50dSMatthew G. Knepley 
7081cdb7a50dSMatthew G. Knepley   Level: intermediate
7082cdb7a50dSMatthew G. Knepley 
7083cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7084cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7085cdb7a50dSMatthew G. Knepley 
7086cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7087cdb7a50dSMatthew G. Knepley @*/
7088cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7089cdb7a50dSMatthew G. Knepley {
7090cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7091cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7092cdb7a50dSMatthew G. Knepley 
7093cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7094cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7095cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7096534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
7097cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7098cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7099cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7100cdb7a50dSMatthew G. Knepley     PetscScalar value;
7101cdb7a50dSMatthew G. Knepley 
710239d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
71034aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7104cdb7a50dSMatthew G. Knepley #endif
7105cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7106f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7107f4d763aaSMatthew G. Knepley }
71088e4ac7eaSMatthew G. Knepley 
71098e4ac7eaSMatthew G. Knepley /*@
71108e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
71118e4ac7eaSMatthew G. Knepley 
71128e4ac7eaSMatthew G. Knepley   Not collective
71138e4ac7eaSMatthew G. Knepley 
71148e4ac7eaSMatthew G. Knepley   Input Parameter:
71158e4ac7eaSMatthew G. Knepley . dm - The DM
71168e4ac7eaSMatthew G. Knepley 
71178e4ac7eaSMatthew G. Knepley   Output Parameter:
71188e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
71198e4ac7eaSMatthew G. Knepley 
71208e4ac7eaSMatthew G. Knepley   Level: beginner
71218e4ac7eaSMatthew G. Knepley 
71228e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
71238e4ac7eaSMatthew G. Knepley @*/
71248e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
71258e4ac7eaSMatthew G. Knepley {
71268e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71278e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7128534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
71298e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
71308e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71318e4ac7eaSMatthew G. Knepley }
71328e4ac7eaSMatthew G. Knepley 
71338e4ac7eaSMatthew G. Knepley /*@
71345d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
71358e4ac7eaSMatthew G. Knepley 
71368e4ac7eaSMatthew G. Knepley   Collective on dm
71378e4ac7eaSMatthew G. Knepley 
71388e4ac7eaSMatthew G. Knepley   Input Parameters:
71398e4ac7eaSMatthew G. Knepley + dm - The DM
71408e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
71418e4ac7eaSMatthew G. Knepley 
71425d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
71435d3b26e6SMatthew G. Knepley 
71448e4ac7eaSMatthew G. Knepley   Level: beginner
71458e4ac7eaSMatthew G. Knepley 
71465d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
71478e4ac7eaSMatthew G. Knepley @*/
71488e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
71498e4ac7eaSMatthew G. Knepley {
71508e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71518e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71528833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
71538e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
71548e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71558e4ac7eaSMatthew G. Knepley }
7156c58f1c22SToby Isaac 
7157c58f1c22SToby Isaac 
7158c58f1c22SToby Isaac /*@C
7159c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7160c58f1c22SToby Isaac 
7161c58f1c22SToby Isaac   Not Collective
7162c58f1c22SToby Isaac 
7163c58f1c22SToby Isaac   Input Parameters:
7164c58f1c22SToby Isaac + dm   - The DM object
7165c58f1c22SToby Isaac - name - The label name
7166c58f1c22SToby Isaac 
7167c58f1c22SToby Isaac   Level: intermediate
7168c58f1c22SToby Isaac 
7169c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7170c58f1c22SToby Isaac @*/
7171c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7172c58f1c22SToby Isaac {
71735d80c0bfSVaclav Hapla   PetscBool      flg;
71745d80c0bfSVaclav Hapla   DMLabel        label;
7175c58f1c22SToby Isaac   PetscErrorCode ierr;
7176c58f1c22SToby Isaac 
7177c58f1c22SToby Isaac   PetscFunctionBegin;
7178c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7179c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
71805d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7181c58f1c22SToby Isaac   if (!flg) {
71825d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
71835d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
71845d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7185c58f1c22SToby Isaac   }
7186c58f1c22SToby Isaac   PetscFunctionReturn(0);
7187c58f1c22SToby Isaac }
7188c58f1c22SToby Isaac 
7189c58f1c22SToby Isaac /*@C
7190c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7191c58f1c22SToby Isaac 
7192c58f1c22SToby Isaac   Not Collective
7193c58f1c22SToby Isaac 
7194c58f1c22SToby Isaac   Input Parameters:
7195c58f1c22SToby Isaac + dm   - The DM object
7196c58f1c22SToby Isaac . name - The label name
7197c58f1c22SToby Isaac - point - The mesh point
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac   Output Parameter:
7200c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7201c58f1c22SToby Isaac 
7202c58f1c22SToby Isaac   Level: beginner
7203c58f1c22SToby Isaac 
7204c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7205c58f1c22SToby Isaac @*/
7206c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7207c58f1c22SToby Isaac {
7208c58f1c22SToby Isaac   DMLabel        label;
7209c58f1c22SToby Isaac   PetscErrorCode ierr;
7210c58f1c22SToby Isaac 
7211c58f1c22SToby Isaac   PetscFunctionBegin;
7212c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7213c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7214c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
721513903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7216c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7217c58f1c22SToby Isaac   PetscFunctionReturn(0);
7218c58f1c22SToby Isaac }
7219c58f1c22SToby Isaac 
7220c58f1c22SToby Isaac /*@C
7221c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7222c58f1c22SToby Isaac 
7223c58f1c22SToby Isaac   Not Collective
7224c58f1c22SToby Isaac 
7225c58f1c22SToby Isaac   Input Parameters:
7226c58f1c22SToby Isaac + dm   - The DM object
7227c58f1c22SToby Isaac . name - The label name
7228c58f1c22SToby Isaac . point - The mesh point
7229c58f1c22SToby Isaac - value - The label value for this point
7230c58f1c22SToby Isaac 
7231c58f1c22SToby Isaac   Output Parameter:
7232c58f1c22SToby Isaac 
7233c58f1c22SToby Isaac   Level: beginner
7234c58f1c22SToby Isaac 
7235c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7236c58f1c22SToby Isaac @*/
7237c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7238c58f1c22SToby Isaac {
7239c58f1c22SToby Isaac   DMLabel        label;
7240c58f1c22SToby Isaac   PetscErrorCode ierr;
7241c58f1c22SToby Isaac 
7242c58f1c22SToby Isaac   PetscFunctionBegin;
7243c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7244c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7245c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7246c58f1c22SToby Isaac   if (!label) {
7247c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7248c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7249c58f1c22SToby Isaac   }
7250c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7251c58f1c22SToby Isaac   PetscFunctionReturn(0);
7252c58f1c22SToby Isaac }
7253c58f1c22SToby Isaac 
7254c58f1c22SToby Isaac /*@C
7255c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7256c58f1c22SToby Isaac 
7257c58f1c22SToby Isaac   Not Collective
7258c58f1c22SToby Isaac 
7259c58f1c22SToby Isaac   Input Parameters:
7260c58f1c22SToby Isaac + dm   - The DM object
7261c58f1c22SToby Isaac . name - The label name
7262c58f1c22SToby Isaac . point - The mesh point
7263c58f1c22SToby Isaac - value - The label value for this point
7264c58f1c22SToby Isaac 
7265c58f1c22SToby Isaac   Output Parameter:
7266c58f1c22SToby Isaac 
7267c58f1c22SToby Isaac   Level: beginner
7268c58f1c22SToby Isaac 
7269c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7270c58f1c22SToby Isaac @*/
7271c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7272c58f1c22SToby Isaac {
7273c58f1c22SToby Isaac   DMLabel        label;
7274c58f1c22SToby Isaac   PetscErrorCode ierr;
7275c58f1c22SToby Isaac 
7276c58f1c22SToby Isaac   PetscFunctionBegin;
7277c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7278c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7279c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7280c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7281c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7282c58f1c22SToby Isaac   PetscFunctionReturn(0);
7283c58f1c22SToby Isaac }
7284c58f1c22SToby Isaac 
7285c58f1c22SToby Isaac /*@C
7286c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7287c58f1c22SToby Isaac 
7288c58f1c22SToby Isaac   Not Collective
7289c58f1c22SToby Isaac 
7290c58f1c22SToby Isaac   Input Parameters:
7291c58f1c22SToby Isaac + dm   - The DM object
7292c58f1c22SToby Isaac - name - The label name
7293c58f1c22SToby Isaac 
7294c58f1c22SToby Isaac   Output Parameter:
7295c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7296c58f1c22SToby Isaac 
7297c58f1c22SToby Isaac   Level: beginner
7298c58f1c22SToby Isaac 
7299df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7300c58f1c22SToby Isaac @*/
7301c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7302c58f1c22SToby Isaac {
7303c58f1c22SToby Isaac   DMLabel        label;
7304c58f1c22SToby Isaac   PetscErrorCode ierr;
7305c58f1c22SToby Isaac 
7306c58f1c22SToby Isaac   PetscFunctionBegin;
7307c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7308c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7309534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7310c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7311c58f1c22SToby Isaac   *size = 0;
7312c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7313c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7314c58f1c22SToby Isaac   PetscFunctionReturn(0);
7315c58f1c22SToby Isaac }
7316c58f1c22SToby Isaac 
7317c58f1c22SToby Isaac /*@C
7318c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7319c58f1c22SToby Isaac 
7320c58f1c22SToby Isaac   Not Collective
7321c58f1c22SToby Isaac 
7322c58f1c22SToby Isaac   Input Parameters:
7323c58f1c22SToby Isaac + mesh - The DM object
7324c58f1c22SToby Isaac - name - The label name
7325c58f1c22SToby Isaac 
7326c58f1c22SToby Isaac   Output Parameter:
7327c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7328c58f1c22SToby Isaac 
7329c58f1c22SToby Isaac   Level: beginner
7330c58f1c22SToby Isaac 
7331c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7332c58f1c22SToby Isaac @*/
7333c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7334c58f1c22SToby Isaac {
7335c58f1c22SToby Isaac   DMLabel        label;
7336c58f1c22SToby Isaac   PetscErrorCode ierr;
7337c58f1c22SToby Isaac 
7338c58f1c22SToby Isaac   PetscFunctionBegin;
7339c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7340c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7341c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7342c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7343c58f1c22SToby Isaac   *ids = NULL;
7344dab2e251SBlaise Bourdin  if (label) {
7345c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7346dab2e251SBlaise Bourdin   } else {
7347dab2e251SBlaise Bourdin     /* returning an empty IS */
7348dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7349dab2e251SBlaise Bourdin   }
7350c58f1c22SToby Isaac   PetscFunctionReturn(0);
7351c58f1c22SToby Isaac }
7352c58f1c22SToby Isaac 
7353c58f1c22SToby Isaac /*@C
7354c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7355c58f1c22SToby Isaac 
7356c58f1c22SToby Isaac   Not Collective
7357c58f1c22SToby Isaac 
7358c58f1c22SToby Isaac   Input Parameters:
7359c58f1c22SToby Isaac + dm - The DM object
7360c58f1c22SToby Isaac . name - The label name
7361c58f1c22SToby Isaac - value - The stratum value
7362c58f1c22SToby Isaac 
7363c58f1c22SToby Isaac   Output Parameter:
7364c58f1c22SToby Isaac . size - The stratum size
7365c58f1c22SToby Isaac 
7366c58f1c22SToby Isaac   Level: beginner
7367c58f1c22SToby Isaac 
7368c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7369c58f1c22SToby Isaac @*/
7370c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7371c58f1c22SToby Isaac {
7372c58f1c22SToby Isaac   DMLabel        label;
7373c58f1c22SToby Isaac   PetscErrorCode ierr;
7374c58f1c22SToby Isaac 
7375c58f1c22SToby Isaac   PetscFunctionBegin;
7376c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7377c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7378534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7379c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7380c58f1c22SToby Isaac   *size = 0;
7381c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7382c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7383c58f1c22SToby Isaac   PetscFunctionReturn(0);
7384c58f1c22SToby Isaac }
7385c58f1c22SToby Isaac 
7386c58f1c22SToby Isaac /*@C
7387c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7388c58f1c22SToby Isaac 
7389c58f1c22SToby Isaac   Not Collective
7390c58f1c22SToby Isaac 
7391c58f1c22SToby Isaac   Input Parameters:
7392c58f1c22SToby Isaac + dm - The DM object
7393c58f1c22SToby Isaac . name - The label name
7394c58f1c22SToby Isaac - value - The stratum value
7395c58f1c22SToby Isaac 
7396c58f1c22SToby Isaac   Output Parameter:
7397c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7398c58f1c22SToby Isaac 
7399c58f1c22SToby Isaac   Level: beginner
7400c58f1c22SToby Isaac 
7401c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7402c58f1c22SToby Isaac @*/
7403c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7404c58f1c22SToby Isaac {
7405c58f1c22SToby Isaac   DMLabel        label;
7406c58f1c22SToby Isaac   PetscErrorCode ierr;
7407c58f1c22SToby Isaac 
7408c58f1c22SToby Isaac   PetscFunctionBegin;
7409c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7410c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7411c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7412c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7413c58f1c22SToby Isaac   *points = NULL;
7414c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7415c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7416c58f1c22SToby Isaac   PetscFunctionReturn(0);
7417c58f1c22SToby Isaac }
7418c58f1c22SToby Isaac 
74194de306b1SToby Isaac /*@C
74209044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
74214de306b1SToby Isaac 
74224de306b1SToby Isaac   Not Collective
74234de306b1SToby Isaac 
74244de306b1SToby Isaac   Input Parameters:
74254de306b1SToby Isaac + dm - The DM object
74264de306b1SToby Isaac . name - The label name
74274de306b1SToby Isaac . value - The stratum value
74284de306b1SToby Isaac - points - The stratum points
74294de306b1SToby Isaac 
74304de306b1SToby Isaac   Level: beginner
74314de306b1SToby Isaac 
74324de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
74334de306b1SToby Isaac @*/
74344de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
74354de306b1SToby Isaac {
74364de306b1SToby Isaac   DMLabel        label;
74374de306b1SToby Isaac   PetscErrorCode ierr;
74384de306b1SToby Isaac 
74394de306b1SToby Isaac   PetscFunctionBegin;
74404de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74414de306b1SToby Isaac   PetscValidCharPointer(name, 2);
74424de306b1SToby Isaac   PetscValidPointer(points, 4);
74434de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
74444de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
74454de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
74464de306b1SToby Isaac   PetscFunctionReturn(0);
74474de306b1SToby Isaac }
74484de306b1SToby Isaac 
7449c58f1c22SToby Isaac /*@C
7450c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7451c58f1c22SToby Isaac 
7452c58f1c22SToby Isaac   Not Collective
7453c58f1c22SToby Isaac 
7454c58f1c22SToby Isaac   Input Parameters:
7455c58f1c22SToby Isaac + dm   - The DM object
7456c58f1c22SToby Isaac . name - The label name
7457c58f1c22SToby Isaac - value - The label value for this point
7458c58f1c22SToby Isaac 
7459c58f1c22SToby Isaac   Output Parameter:
7460c58f1c22SToby Isaac 
7461c58f1c22SToby Isaac   Level: beginner
7462c58f1c22SToby Isaac 
7463c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7464c58f1c22SToby Isaac @*/
7465c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7466c58f1c22SToby Isaac {
7467c58f1c22SToby Isaac   DMLabel        label;
7468c58f1c22SToby Isaac   PetscErrorCode ierr;
7469c58f1c22SToby Isaac 
7470c58f1c22SToby Isaac   PetscFunctionBegin;
7471c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7472c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7473c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7474c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7475c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7476c58f1c22SToby Isaac   PetscFunctionReturn(0);
7477c58f1c22SToby Isaac }
7478c58f1c22SToby Isaac 
7479c58f1c22SToby Isaac /*@
7480c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7481c58f1c22SToby Isaac 
7482c58f1c22SToby Isaac   Not Collective
7483c58f1c22SToby Isaac 
7484c58f1c22SToby Isaac   Input Parameter:
7485c58f1c22SToby Isaac . dm   - The DM object
7486c58f1c22SToby Isaac 
7487c58f1c22SToby Isaac   Output Parameter:
7488c58f1c22SToby Isaac . numLabels - the number of Labels
7489c58f1c22SToby Isaac 
7490c58f1c22SToby Isaac   Level: intermediate
7491c58f1c22SToby Isaac 
7492c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7493c58f1c22SToby Isaac @*/
7494c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7495c58f1c22SToby Isaac {
74965d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7497c58f1c22SToby Isaac   PetscInt  n    = 0;
7498c58f1c22SToby Isaac 
7499c58f1c22SToby Isaac   PetscFunctionBegin;
7500c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7501534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7502c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7503c58f1c22SToby Isaac   *numLabels = n;
7504c58f1c22SToby Isaac   PetscFunctionReturn(0);
7505c58f1c22SToby Isaac }
7506c58f1c22SToby Isaac 
7507c58f1c22SToby Isaac /*@C
7508c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7509c58f1c22SToby Isaac 
7510c58f1c22SToby Isaac   Not Collective
7511c58f1c22SToby Isaac 
7512c58f1c22SToby Isaac   Input Parameters:
7513c58f1c22SToby Isaac + dm - The DM object
7514c58f1c22SToby Isaac - n  - the label number
7515c58f1c22SToby Isaac 
7516c58f1c22SToby Isaac   Output Parameter:
7517c58f1c22SToby Isaac . name - the label name
7518c58f1c22SToby Isaac 
7519c58f1c22SToby Isaac   Level: intermediate
7520c58f1c22SToby Isaac 
7521c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7522c58f1c22SToby Isaac @*/
7523c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7524c58f1c22SToby Isaac {
75255d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7526c58f1c22SToby Isaac   PetscInt       l    = 0;
7527d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7528c58f1c22SToby Isaac 
7529c58f1c22SToby Isaac   PetscFunctionBegin;
7530c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7531c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7532c58f1c22SToby Isaac   while (next) {
7533c58f1c22SToby Isaac     if (l == n) {
7534d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7535c58f1c22SToby Isaac       PetscFunctionReturn(0);
7536c58f1c22SToby Isaac     }
7537c58f1c22SToby Isaac     ++l;
7538c58f1c22SToby Isaac     next = next->next;
7539c58f1c22SToby Isaac   }
7540c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7541c58f1c22SToby Isaac }
7542c58f1c22SToby Isaac 
7543c58f1c22SToby Isaac /*@C
7544c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7545c58f1c22SToby Isaac 
7546c58f1c22SToby Isaac   Not Collective
7547c58f1c22SToby Isaac 
7548c58f1c22SToby Isaac   Input Parameters:
7549c58f1c22SToby Isaac + dm   - The DM object
7550c58f1c22SToby Isaac - name - The label name
7551c58f1c22SToby Isaac 
7552c58f1c22SToby Isaac   Output Parameter:
7553c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7554c58f1c22SToby Isaac 
7555c58f1c22SToby Isaac   Level: intermediate
7556c58f1c22SToby Isaac 
7557c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7558c58f1c22SToby Isaac @*/
7559c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7560c58f1c22SToby Isaac {
75615d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7562d67d17b1SMatthew G. Knepley   const char    *lname;
7563c58f1c22SToby Isaac   PetscErrorCode ierr;
7564c58f1c22SToby Isaac 
7565c58f1c22SToby Isaac   PetscFunctionBegin;
7566c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7567c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7568534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7569c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7570c58f1c22SToby Isaac   while (next) {
7571d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7572d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7573c58f1c22SToby Isaac     if (*hasLabel) break;
7574c58f1c22SToby Isaac     next = next->next;
7575c58f1c22SToby Isaac   }
7576c58f1c22SToby Isaac   PetscFunctionReturn(0);
7577c58f1c22SToby Isaac }
7578c58f1c22SToby Isaac 
7579c58f1c22SToby Isaac /*@C
7580c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7581c58f1c22SToby Isaac 
7582c58f1c22SToby Isaac   Not Collective
7583c58f1c22SToby Isaac 
7584c58f1c22SToby Isaac   Input Parameters:
7585c58f1c22SToby Isaac + dm   - The DM object
7586c58f1c22SToby Isaac - name - The label name
7587c58f1c22SToby Isaac 
7588c58f1c22SToby Isaac   Output Parameter:
7589c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7590c58f1c22SToby Isaac 
7591c58f1c22SToby Isaac   Level: intermediate
7592c58f1c22SToby Isaac 
7593c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7594c58f1c22SToby Isaac @*/
7595c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7596c58f1c22SToby Isaac {
75975d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7598c58f1c22SToby Isaac   PetscBool      hasLabel;
7599d67d17b1SMatthew G. Knepley   const char    *lname;
7600c58f1c22SToby Isaac   PetscErrorCode ierr;
7601c58f1c22SToby Isaac 
7602c58f1c22SToby Isaac   PetscFunctionBegin;
7603c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7604c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7605c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7606c58f1c22SToby Isaac   *label = NULL;
7607c58f1c22SToby Isaac   while (next) {
7608d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7609d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7610c58f1c22SToby Isaac     if (hasLabel) {
7611c58f1c22SToby Isaac       *label = next->label;
7612c58f1c22SToby Isaac       break;
7613c58f1c22SToby Isaac     }
7614c58f1c22SToby Isaac     next = next->next;
7615c58f1c22SToby Isaac   }
7616c58f1c22SToby Isaac   PetscFunctionReturn(0);
7617c58f1c22SToby Isaac }
7618c58f1c22SToby Isaac 
7619c58f1c22SToby Isaac /*@C
7620c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7621c58f1c22SToby Isaac 
7622c58f1c22SToby Isaac   Not Collective
7623c58f1c22SToby Isaac 
7624c58f1c22SToby Isaac   Input Parameters:
7625c58f1c22SToby Isaac + dm - The DM object
7626c58f1c22SToby Isaac - n  - the label number
7627c58f1c22SToby Isaac 
7628c58f1c22SToby Isaac   Output Parameter:
7629c58f1c22SToby Isaac . label - the label
7630c58f1c22SToby Isaac 
7631c58f1c22SToby Isaac   Level: intermediate
7632c58f1c22SToby Isaac 
7633c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7634c58f1c22SToby Isaac @*/
7635c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7636c58f1c22SToby Isaac {
76375d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7638c58f1c22SToby Isaac   PetscInt    l    = 0;
7639c58f1c22SToby Isaac 
7640c58f1c22SToby Isaac   PetscFunctionBegin;
7641c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7642c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7643c58f1c22SToby Isaac   while (next) {
7644c58f1c22SToby Isaac     if (l == n) {
7645c58f1c22SToby Isaac       *label = next->label;
7646c58f1c22SToby Isaac       PetscFunctionReturn(0);
7647c58f1c22SToby Isaac     }
7648c58f1c22SToby Isaac     ++l;
7649c58f1c22SToby Isaac     next = next->next;
7650c58f1c22SToby Isaac   }
7651c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7652c58f1c22SToby Isaac }
7653c58f1c22SToby Isaac 
7654c58f1c22SToby Isaac /*@C
7655c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7656c58f1c22SToby Isaac 
7657c58f1c22SToby Isaac   Not Collective
7658c58f1c22SToby Isaac 
7659c58f1c22SToby Isaac   Input Parameters:
7660c58f1c22SToby Isaac + dm   - The DM object
7661c58f1c22SToby Isaac - label - The DMLabel
7662c58f1c22SToby Isaac 
7663c58f1c22SToby Isaac   Level: developer
7664c58f1c22SToby Isaac 
7665c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7666c58f1c22SToby Isaac @*/
7667c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7668c58f1c22SToby Isaac {
76695d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7670c58f1c22SToby Isaac   PetscBool      hasLabel;
7671d67d17b1SMatthew G. Knepley   const char    *lname;
76725d80c0bfSVaclav Hapla   PetscBool      flg;
7673c58f1c22SToby Isaac   PetscErrorCode ierr;
7674c58f1c22SToby Isaac 
7675c58f1c22SToby Isaac   PetscFunctionBegin;
7676c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7677d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7678d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7679d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7680c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7681c58f1c22SToby Isaac   tmpLabel->label  = label;
7682c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
76835d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
76845d80c0bfSVaclav Hapla   *p = tmpLabel;
768508f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
76865d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
76875d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7688ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7689ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7690c58f1c22SToby Isaac   PetscFunctionReturn(0);
7691c58f1c22SToby Isaac }
7692c58f1c22SToby Isaac 
7693c58f1c22SToby Isaac /*@C
7694e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7695c58f1c22SToby Isaac 
7696c58f1c22SToby Isaac   Not Collective
7697c58f1c22SToby Isaac 
7698c58f1c22SToby Isaac   Input Parameters:
7699c58f1c22SToby Isaac + dm   - The DM object
7700c58f1c22SToby Isaac - name - The label name
7701c58f1c22SToby Isaac 
7702c58f1c22SToby Isaac   Output Parameter:
7703c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7704c58f1c22SToby Isaac 
7705c58f1c22SToby Isaac   Level: developer
7706c58f1c22SToby Isaac 
7707e5472504SVaclav Hapla   Notes:
7708e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7709e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7710e5472504SVaclav Hapla 
7711e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7712e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7713e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7714e5472504SVaclav Hapla 
7715e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7716c58f1c22SToby Isaac @*/
7717c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7718c58f1c22SToby Isaac {
771995d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7720c58f1c22SToby Isaac   PetscBool      hasLabel;
7721d67d17b1SMatthew G. Knepley   const char    *lname;
7722c58f1c22SToby Isaac   PetscErrorCode ierr;
7723c58f1c22SToby Isaac 
7724c58f1c22SToby Isaac   PetscFunctionBegin;
7725c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7726e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7727e5472504SVaclav Hapla   if (label) {
7728e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7729c58f1c22SToby Isaac     *label = NULL;
7730e5472504SVaclav Hapla   }
77315d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
773295d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7733d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7734c58f1c22SToby Isaac     if (hasLabel) {
773595d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7736c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
773795d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7738ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7739ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
774095d578d6SVaclav Hapla       if (label) *label = link->label;
774195d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
774295d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7743c58f1c22SToby Isaac       break;
7744c58f1c22SToby Isaac     }
7745c58f1c22SToby Isaac   }
7746c58f1c22SToby Isaac   PetscFunctionReturn(0);
7747c58f1c22SToby Isaac }
7748c58f1c22SToby Isaac 
7749306894acSVaclav Hapla /*@
7750306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7751306894acSVaclav Hapla 
7752306894acSVaclav Hapla   Not Collective
7753306894acSVaclav Hapla 
7754306894acSVaclav Hapla   Input Parameters:
7755306894acSVaclav Hapla + dm   - The DM object
7756306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7757306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7758306894acSVaclav Hapla 
7759306894acSVaclav Hapla   Level: developer
7760306894acSVaclav Hapla 
7761306894acSVaclav Hapla   Notes:
7762306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7763306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7764306894acSVaclav Hapla   *label nullified.
7765306894acSVaclav Hapla 
7766306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7767306894acSVaclav Hapla @*/
7768306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7769306894acSVaclav Hapla {
777043e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7771306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7772306894acSVaclav Hapla   PetscErrorCode ierr;
7773306894acSVaclav Hapla 
7774306894acSVaclav Hapla   PetscFunctionBegin;
7775306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7776306894acSVaclav Hapla   PetscValidPointer(label, 2);
7777f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7778306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7779306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
77805d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
778143e45a93SVaclav Hapla     if (*label == link->label) {
7782306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
778343e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7784306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7785ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
778643e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
778743e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
778843e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7789306894acSVaclav Hapla       break;
7790306894acSVaclav Hapla     }
7791306894acSVaclav Hapla   }
7792306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7793306894acSVaclav Hapla   PetscFunctionReturn(0);
7794306894acSVaclav Hapla }
7795306894acSVaclav Hapla 
7796c58f1c22SToby Isaac /*@C
7797c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7798c58f1c22SToby Isaac 
7799c58f1c22SToby Isaac   Not Collective
7800c58f1c22SToby Isaac 
7801c58f1c22SToby Isaac   Input Parameters:
7802c58f1c22SToby Isaac + dm   - The DM object
7803c58f1c22SToby Isaac - name - The label name
7804c58f1c22SToby Isaac 
7805c58f1c22SToby Isaac   Output Parameter:
7806c58f1c22SToby Isaac . output - The flag for output
7807c58f1c22SToby Isaac 
7808c58f1c22SToby Isaac   Level: developer
7809c58f1c22SToby Isaac 
7810c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7811c58f1c22SToby Isaac @*/
7812c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7813c58f1c22SToby Isaac {
78145d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7815d67d17b1SMatthew G. Knepley   const char    *lname;
7816c58f1c22SToby Isaac   PetscErrorCode ierr;
7817c58f1c22SToby Isaac 
7818c58f1c22SToby Isaac   PetscFunctionBegin;
7819c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7820c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7821c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7822c58f1c22SToby Isaac   while (next) {
7823c58f1c22SToby Isaac     PetscBool flg;
7824c58f1c22SToby Isaac 
7825d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7826d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7827c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7828c58f1c22SToby Isaac     next = next->next;
7829c58f1c22SToby Isaac   }
7830c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7831c58f1c22SToby Isaac }
7832c58f1c22SToby Isaac 
7833c58f1c22SToby Isaac /*@C
7834c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7835c58f1c22SToby Isaac 
7836c58f1c22SToby Isaac   Not Collective
7837c58f1c22SToby Isaac 
7838c58f1c22SToby Isaac   Input Parameters:
7839c58f1c22SToby Isaac + dm     - The DM object
7840c58f1c22SToby Isaac . name   - The label name
7841c58f1c22SToby Isaac - output - The flag for output
7842c58f1c22SToby Isaac 
7843c58f1c22SToby Isaac   Level: developer
7844c58f1c22SToby Isaac 
7845c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7846c58f1c22SToby Isaac @*/
7847c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7848c58f1c22SToby Isaac {
78495d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7850d67d17b1SMatthew G. Knepley   const char    *lname;
7851c58f1c22SToby Isaac   PetscErrorCode ierr;
7852c58f1c22SToby Isaac 
7853c58f1c22SToby Isaac   PetscFunctionBegin;
7854c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7855534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7856c58f1c22SToby Isaac   while (next) {
7857c58f1c22SToby Isaac     PetscBool flg;
7858c58f1c22SToby Isaac 
7859d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7860d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7861c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7862c58f1c22SToby Isaac     next = next->next;
7863c58f1c22SToby Isaac   }
7864c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7865c58f1c22SToby Isaac }
7866c58f1c22SToby Isaac 
7867c58f1c22SToby Isaac /*@
7868c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7869c58f1c22SToby Isaac 
7870d083f849SBarry Smith   Collective on dmA
7871c58f1c22SToby Isaac 
7872c58f1c22SToby Isaac   Input Parameter:
78735d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
78742e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
78755d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7876ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7877c58f1c22SToby Isaac 
7878c58f1c22SToby Isaac   Level: intermediate
7879c58f1c22SToby Isaac 
7880c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7881c58f1c22SToby Isaac 
78825d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7883c58f1c22SToby Isaac @*/
78845d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7885c58f1c22SToby Isaac {
7886c58f1c22SToby Isaac   DMLabel        label, labelNew;
7887c58f1c22SToby Isaac   const char    *name;
7888c58f1c22SToby Isaac   PetscBool      flg;
78895d80c0bfSVaclav Hapla   DMLabelLink    link;
78905d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7891c58f1c22SToby Isaac 
78925d80c0bfSVaclav Hapla   PetscFunctionBegin;
78935d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
78945d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
78955d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
78965d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
78975d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
78985d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
78995d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
79005d80c0bfSVaclav Hapla     label=link->label;
79015d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
79025d80c0bfSVaclav Hapla     if (!all) {
7903c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7904c58f1c22SToby Isaac       if (flg) continue;
79057d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
79067d5acc75SStefano Zampini       if (flg) continue;
7907ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7908ba2698f1SMatthew G. Knepley       if (flg) continue;
79095d80c0bfSVaclav Hapla     }
79105d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7911c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
79125d80c0bfSVaclav Hapla     } else {
79135d80c0bfSVaclav Hapla       labelNew = label;
79145d80c0bfSVaclav Hapla     }
7915c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
79165d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7917c58f1c22SToby Isaac   }
7918c58f1c22SToby Isaac   PetscFunctionReturn(0);
7919c58f1c22SToby Isaac }
7920a8fb8f29SToby Isaac 
7921a8fb8f29SToby Isaac /*@
7922a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7923a8fb8f29SToby Isaac 
7924a8fb8f29SToby Isaac   Input Parameter:
7925a8fb8f29SToby Isaac . dm - The DM object
7926a8fb8f29SToby Isaac 
7927a8fb8f29SToby Isaac   Output Parameter:
7928a8fb8f29SToby Isaac . cdm - The coarse DM
7929a8fb8f29SToby Isaac 
7930a8fb8f29SToby Isaac   Level: intermediate
7931a8fb8f29SToby Isaac 
7932a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7933a8fb8f29SToby Isaac @*/
7934a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7935a8fb8f29SToby Isaac {
7936a8fb8f29SToby Isaac   PetscFunctionBegin;
7937a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7938a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7939a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7940a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7941a8fb8f29SToby Isaac }
7942a8fb8f29SToby Isaac 
7943a8fb8f29SToby Isaac /*@
7944a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7945a8fb8f29SToby Isaac 
7946a8fb8f29SToby Isaac   Input Parameters:
7947a8fb8f29SToby Isaac + dm - The DM object
7948a8fb8f29SToby Isaac - cdm - The coarse DM
7949a8fb8f29SToby Isaac 
7950a8fb8f29SToby Isaac   Level: intermediate
7951a8fb8f29SToby Isaac 
7952a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7953a8fb8f29SToby Isaac @*/
7954a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7955a8fb8f29SToby Isaac {
7956a8fb8f29SToby Isaac   PetscErrorCode ierr;
7957a8fb8f29SToby Isaac 
7958a8fb8f29SToby Isaac   PetscFunctionBegin;
7959a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7960a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7961a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7962a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7963a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7964a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7965a8fb8f29SToby Isaac }
7966a8fb8f29SToby Isaac 
796788bdff64SToby Isaac /*@
796888bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
796988bdff64SToby Isaac 
797088bdff64SToby Isaac   Input Parameter:
797188bdff64SToby Isaac . dm - The DM object
797288bdff64SToby Isaac 
797388bdff64SToby Isaac   Output Parameter:
797488bdff64SToby Isaac . fdm - The fine DM
797588bdff64SToby Isaac 
797688bdff64SToby Isaac   Level: intermediate
797788bdff64SToby Isaac 
797888bdff64SToby Isaac .seealso: DMSetFineDM()
797988bdff64SToby Isaac @*/
798088bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
798188bdff64SToby Isaac {
798288bdff64SToby Isaac   PetscFunctionBegin;
798388bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
798488bdff64SToby Isaac   PetscValidPointer(fdm, 2);
798588bdff64SToby Isaac   *fdm = dm->fineMesh;
798688bdff64SToby Isaac   PetscFunctionReturn(0);
798788bdff64SToby Isaac }
798888bdff64SToby Isaac 
798988bdff64SToby Isaac /*@
799088bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
799188bdff64SToby Isaac 
799288bdff64SToby Isaac   Input Parameters:
799388bdff64SToby Isaac + dm - The DM object
799488bdff64SToby Isaac - fdm - The fine DM
799588bdff64SToby Isaac 
799688bdff64SToby Isaac   Level: intermediate
799788bdff64SToby Isaac 
799888bdff64SToby Isaac .seealso: DMGetFineDM()
799988bdff64SToby Isaac @*/
800088bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
800188bdff64SToby Isaac {
800288bdff64SToby Isaac   PetscErrorCode ierr;
800388bdff64SToby Isaac 
800488bdff64SToby Isaac   PetscFunctionBegin;
800588bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
800688bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
800788bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
800888bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
800988bdff64SToby Isaac   dm->fineMesh = fdm;
801088bdff64SToby Isaac   PetscFunctionReturn(0);
801188bdff64SToby Isaac }
801288bdff64SToby Isaac 
8013a6ba4734SToby Isaac /*=== DMBoundary code ===*/
8014a6ba4734SToby Isaac 
8015a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
8016a6ba4734SToby Isaac {
8017e5e52638SMatthew G. Knepley   PetscInt       d;
8018a6ba4734SToby Isaac   PetscErrorCode ierr;
8019a6ba4734SToby Isaac 
8020a6ba4734SToby Isaac   PetscFunctionBegin;
8021e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
8022e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
8023e5e52638SMatthew G. Knepley   }
8024a6ba4734SToby Isaac   PetscFunctionReturn(0);
8025a6ba4734SToby Isaac }
8026a6ba4734SToby Isaac 
8027a6ba4734SToby Isaac /*@C
8028a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8029a6ba4734SToby Isaac 
8030783e2ec8SMatthew G. Knepley   Collective on dm
8031783e2ec8SMatthew G. Knepley 
8032a6ba4734SToby Isaac   Input Parameters:
80334c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
8034f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8035a6ba4734SToby Isaac . name        - The BC name
8036a6ba4734SToby Isaac . labelname   - The label defining constrained points
8037a6ba4734SToby Isaac . field       - The field to constrain
8038e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
8039a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8040a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
804156cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time deriative of the boundary values, or NULL
8042a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8043a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8044a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8045a6ba4734SToby Isaac 
8046a6ba4734SToby Isaac   Options Database Keys:
8047a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8048a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8049a6ba4734SToby Isaac 
805056cf3b9cSMatthew G. Knepley   Note:
805156cf3b9cSMatthew G. Knepley   Both bcFunc abd bcFunc_t will depend on the boundary condition type. If the type if DM_BC_ESSENTIAL, Then the calling sequence is:
805256cf3b9cSMatthew G. Knepley 
805356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
805456cf3b9cSMatthew G. Knepley 
805556cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
805656cf3b9cSMatthew G. Knepley 
805756cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
805856cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
805956cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
806056cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
806156cf3b9cSMatthew G. Knepley 
806256cf3b9cSMatthew G. Knepley + dim - the spatial dimension
806356cf3b9cSMatthew G. Knepley . Nf - the number of fields
806456cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
806556cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
806656cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
806756cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
806856cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
806956cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
807056cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
807156cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
807256cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
807356cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
807456cf3b9cSMatthew G. Knepley . t - current time
807556cf3b9cSMatthew G. Knepley . x - coordinates of the current point
807656cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
807756cf3b9cSMatthew G. Knepley . constants - constant parameters
807856cf3b9cSMatthew G. Knepley - bcval - output values at the current point
807956cf3b9cSMatthew G. Knepley 
8080a6ba4734SToby Isaac   Level: developer
8081a6ba4734SToby Isaac 
808256cf3b9cSMatthew G. Knepley .seealso: DMGetBoundary(), PetscDSAddBoundary()
8083a6ba4734SToby Isaac @*/
808456cf3b9cSMatthew G. Knepley PetscErrorCode DMAddBoundary(DM dm, DMBoundaryConditionType type, const char name[], const char labelname[], PetscInt field, PetscInt numcomps, const PetscInt *comps, void (*bcFunc)(void), void (*bcFunc_t)(void), PetscInt numids, const PetscInt *ids, void *ctx)
8085a6ba4734SToby Isaac {
8086e5e52638SMatthew G. Knepley   PetscDS        ds;
8087a6ba4734SToby Isaac   PetscErrorCode ierr;
8088a6ba4734SToby Isaac 
8089a6ba4734SToby Isaac   PetscFunctionBegin;
8090a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8091783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
8092783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
8093783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
8094783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
8095e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8096783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
809756cf3b9cSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, bcFunc_t, numids, ids, ctx);CHKERRQ(ierr);
8098a6ba4734SToby Isaac   PetscFunctionReturn(0);
8099a6ba4734SToby Isaac }
8100a6ba4734SToby Isaac 
8101a6ba4734SToby Isaac /*@
8102a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
8103a6ba4734SToby Isaac 
8104a6ba4734SToby Isaac   Input Parameters:
8105a6ba4734SToby Isaac . dm - The mesh object
8106a6ba4734SToby Isaac 
8107a6ba4734SToby Isaac   Output Parameters:
8108a6ba4734SToby Isaac . numBd - The number of BC
8109a6ba4734SToby Isaac 
8110a6ba4734SToby Isaac   Level: intermediate
8111a6ba4734SToby Isaac 
8112a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
8113a6ba4734SToby Isaac @*/
8114a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
8115a6ba4734SToby Isaac {
8116e5e52638SMatthew G. Knepley   PetscDS        ds;
811758ebd649SToby Isaac   PetscErrorCode ierr;
8118a6ba4734SToby Isaac 
8119a6ba4734SToby Isaac   PetscFunctionBegin;
8120a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8121e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8122e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
8123a6ba4734SToby Isaac   PetscFunctionReturn(0);
8124a6ba4734SToby Isaac }
8125a6ba4734SToby Isaac 
8126a6ba4734SToby Isaac /*@C
81271c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
8128a6ba4734SToby Isaac 
8129a6ba4734SToby Isaac   Input Parameters:
8130a6ba4734SToby Isaac + dm          - The mesh object
8131a6ba4734SToby Isaac - bd          - The BC number
8132a6ba4734SToby Isaac 
8133a6ba4734SToby Isaac   Output Parameters:
8134f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8135a6ba4734SToby Isaac . name        - The BC name
8136a6ba4734SToby Isaac . labelname   - The label defining constrained points
8137a6ba4734SToby Isaac . field       - The field to constrain
8138a6ba4734SToby Isaac . numcomps    - The number of constrained field components
8139a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8140a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
814156cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time derviative of the boundary values
8142a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8143a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8144a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8145a6ba4734SToby Isaac 
8146a6ba4734SToby Isaac   Options Database Keys:
8147a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8148a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8149a6ba4734SToby Isaac 
8150a6ba4734SToby Isaac   Level: developer
8151a6ba4734SToby Isaac 
8152a6ba4734SToby Isaac .seealso: DMAddBoundary()
8153a6ba4734SToby Isaac @*/
815456cf3b9cSMatthew G. Knepley PetscErrorCode DMGetBoundary(DM dm, PetscInt bd, DMBoundaryConditionType *type, const char **name, const char **labelname, PetscInt *field, PetscInt *numcomps, const PetscInt **comps, void (**func)(void), void (**func_t)(void), PetscInt *numids, const PetscInt **ids, void **ctx)
8155a6ba4734SToby Isaac {
8156e5e52638SMatthew G. Knepley   PetscDS        ds;
815758ebd649SToby Isaac   PetscErrorCode ierr;
8158a6ba4734SToby Isaac 
8159a6ba4734SToby Isaac   PetscFunctionBegin;
8160a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8161e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
816256cf3b9cSMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, func_t, numids, ids, ctx);CHKERRQ(ierr);
8163a6ba4734SToby Isaac   PetscFunctionReturn(0);
8164a6ba4734SToby Isaac }
8165a6ba4734SToby Isaac 
8166e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8167e6f8dbb6SToby Isaac {
8168e5e52638SMatthew G. Knepley   PetscDS        ds;
8169dff059c6SToby Isaac   DMBoundary    *lastnext;
8170e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8171e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8172e6f8dbb6SToby Isaac 
8173e6f8dbb6SToby Isaac   PetscFunctionBegin;
8174e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8175e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
817647a1f5adSToby Isaac   if (dm->boundary) {
817747a1f5adSToby Isaac     DMBoundary next = dm->boundary;
817847a1f5adSToby Isaac 
817947a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
818047a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
818147a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
818247a1f5adSToby Isaac     while (next) {
818347a1f5adSToby Isaac       DMBoundary b = next;
818447a1f5adSToby Isaac 
818547a1f5adSToby Isaac       next = b->next;
818647a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8187a6ba4734SToby Isaac     }
818847a1f5adSToby Isaac     dm->boundary = NULL;
8189a6ba4734SToby Isaac   }
819047a1f5adSToby Isaac 
8191dff059c6SToby Isaac   lastnext = &(dm->boundary);
8192e6f8dbb6SToby Isaac   while (dsbound) {
8193e6f8dbb6SToby Isaac     DMBoundary dmbound;
8194e6f8dbb6SToby Isaac 
8195e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8196e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
8197e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
8198994fe344SLisandro 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);}
819947a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8200dff059c6SToby Isaac     *lastnext = dmbound;
8201dff059c6SToby Isaac     lastnext = &(dmbound->next);
8202dff059c6SToby Isaac     dsbound = dsbound->next;
8203a6ba4734SToby Isaac   }
8204a6ba4734SToby Isaac   PetscFunctionReturn(0);
8205a6ba4734SToby Isaac }
8206a6ba4734SToby Isaac 
8207a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8208a6ba4734SToby Isaac {
8209b95f2879SToby Isaac   DMBoundary     b;
8210a6ba4734SToby Isaac   PetscErrorCode ierr;
8211a6ba4734SToby Isaac 
8212a6ba4734SToby Isaac   PetscFunctionBegin;
8213a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8214534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8215a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8216e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8217b95f2879SToby Isaac   b = dm->boundary;
8218a6ba4734SToby Isaac   while (b && !(*isBd)) {
8219e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8220e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
82213424c85cSToby Isaac 
82223424c85cSToby Isaac     if (label) {
8223a6ba4734SToby Isaac       PetscInt i;
8224a6ba4734SToby Isaac 
8225e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
8226e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
8227a6ba4734SToby Isaac       }
8228a6ba4734SToby Isaac     }
8229a6ba4734SToby Isaac     b = b->next;
8230a6ba4734SToby Isaac   }
8231a6ba4734SToby Isaac   PetscFunctionReturn(0);
8232a6ba4734SToby Isaac }
82334d6f44ffSToby Isaac 
82344d6f44ffSToby Isaac /*@C
8235a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8236a6e0b375SMatthew G. Knepley 
8237a6e0b375SMatthew G. Knepley   Collective on DM
82384d6f44ffSToby Isaac 
82394d6f44ffSToby Isaac   Input Parameters:
82404d6f44ffSToby Isaac + dm      - The DM
82410709b2feSToby Isaac . time    - The time
82424d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
82434d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
82444d6f44ffSToby Isaac - mode    - The insertion mode for values
82454d6f44ffSToby Isaac 
82464d6f44ffSToby Isaac   Output Parameter:
82474d6f44ffSToby Isaac . X - vector
82484d6f44ffSToby Isaac 
82494d6f44ffSToby Isaac    Calling sequence of func:
82500709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
82514d6f44ffSToby Isaac 
82524d6f44ffSToby Isaac +  dim - The spatial dimension
82538ec8862eSJed Brown .  time - The time at which to sample
82544d6f44ffSToby Isaac .  x   - The coordinates
82554d6f44ffSToby Isaac .  Nf  - The number of fields
82564d6f44ffSToby Isaac .  u   - The output field values
82574d6f44ffSToby Isaac -  ctx - optional user-defined function context
82584d6f44ffSToby Isaac 
82594d6f44ffSToby Isaac   Level: developer
82604d6f44ffSToby Isaac 
8261a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
82624d6f44ffSToby Isaac @*/
82630709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
82644d6f44ffSToby Isaac {
82654d6f44ffSToby Isaac   Vec            localX;
82664d6f44ffSToby Isaac   PetscErrorCode ierr;
82674d6f44ffSToby Isaac 
82684d6f44ffSToby Isaac   PetscFunctionBegin;
82694d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82704d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
82710709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82724d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
82734d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
82744d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
82754d6f44ffSToby Isaac   PetscFunctionReturn(0);
82764d6f44ffSToby Isaac }
82774d6f44ffSToby Isaac 
8278a6e0b375SMatthew G. Knepley /*@C
8279a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8280a6e0b375SMatthew G. Knepley 
8281a6e0b375SMatthew G. Knepley   Not collective
8282a6e0b375SMatthew G. Knepley 
8283a6e0b375SMatthew G. Knepley   Input Parameters:
8284a6e0b375SMatthew G. Knepley + dm      - The DM
8285a6e0b375SMatthew G. Knepley . time    - The time
8286a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8287a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8288a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8289a6e0b375SMatthew G. Knepley 
8290a6e0b375SMatthew G. Knepley   Output Parameter:
8291a6e0b375SMatthew G. Knepley . localX - vector
8292a6e0b375SMatthew G. Knepley 
8293a6e0b375SMatthew G. Knepley    Calling sequence of func:
8294a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8295a6e0b375SMatthew G. Knepley 
8296a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8297a6e0b375SMatthew G. Knepley .  x   - The coordinates
8298a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8299a6e0b375SMatthew G. Knepley .  u   - The output field values
8300a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8301a6e0b375SMatthew G. Knepley 
8302a6e0b375SMatthew G. Knepley   Level: developer
8303a6e0b375SMatthew G. Knepley 
8304a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8305a6e0b375SMatthew G. Knepley @*/
83060709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
83074d6f44ffSToby Isaac {
83084d6f44ffSToby Isaac   PetscErrorCode ierr;
83094d6f44ffSToby Isaac 
83104d6f44ffSToby Isaac   PetscFunctionBegin;
83114d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83124d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
83130918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
83140709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83154d6f44ffSToby Isaac   PetscFunctionReturn(0);
83164d6f44ffSToby Isaac }
83174d6f44ffSToby Isaac 
8318a6e0b375SMatthew G. Knepley /*@C
8319a6e0b375SMatthew G. Knepley   DMProjectFunctionLabel - This projects the given function into the function space provided, putting the coefficients in a global vector, setting values only for points in the given label.
8320a6e0b375SMatthew G. Knepley 
8321a6e0b375SMatthew G. Knepley   Collective on DM
8322a6e0b375SMatthew G. Knepley 
8323a6e0b375SMatthew G. Knepley   Input Parameters:
8324a6e0b375SMatthew G. Knepley + dm      - The DM
8325a6e0b375SMatthew G. Knepley . time    - The time
8326a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8327a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8328a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8329a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8330a6e0b375SMatthew G. Knepley 
8331a6e0b375SMatthew G. Knepley   Output Parameter:
8332a6e0b375SMatthew G. Knepley . X - vector
8333a6e0b375SMatthew G. Knepley 
8334a6e0b375SMatthew G. Knepley    Calling sequence of func:
8335a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8336a6e0b375SMatthew G. Knepley 
8337a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8338a6e0b375SMatthew G. Knepley .  x   - The coordinates
8339a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8340a6e0b375SMatthew G. Knepley .  u   - The output field values
8341a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8342a6e0b375SMatthew G. Knepley 
8343a6e0b375SMatthew G. Knepley   Level: developer
8344a6e0b375SMatthew G. Knepley 
8345a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8346a6e0b375SMatthew G. Knepley @*/
83472c53366bSMatthew 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)
83482c53366bSMatthew G. Knepley {
83492c53366bSMatthew G. Knepley   Vec            localX;
83502c53366bSMatthew G. Knepley   PetscErrorCode ierr;
83512c53366bSMatthew G. Knepley 
83522c53366bSMatthew G. Knepley   PetscFunctionBegin;
83532c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83542c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
83552c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83562c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
83572c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
83582c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
83592c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
83602c53366bSMatthew G. Knepley }
83612c53366bSMatthew G. Knepley 
8362a6e0b375SMatthew G. Knepley /*@C
8363a6e0b375SMatthew G. Knepley   DMProjectFunctionLabelLocal - This projects the given function into the function space provided, putting the coefficients in a local vector, setting values only for points in the given label.
8364a6e0b375SMatthew G. Knepley 
8365a6e0b375SMatthew G. Knepley   Not collective
8366a6e0b375SMatthew G. Knepley 
8367a6e0b375SMatthew G. Knepley   Input Parameters:
8368a6e0b375SMatthew G. Knepley + dm      - The DM
8369a6e0b375SMatthew G. Knepley . time    - The time
8370a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8371a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8372a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8373a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8374a6e0b375SMatthew G. Knepley 
8375a6e0b375SMatthew G. Knepley   Output Parameter:
8376a6e0b375SMatthew G. Knepley . localX - vector
8377a6e0b375SMatthew G. Knepley 
8378a6e0b375SMatthew G. Knepley    Calling sequence of func:
8379a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8380a6e0b375SMatthew G. Knepley 
8381a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8382a6e0b375SMatthew G. Knepley .  x   - The coordinates
8383a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8384a6e0b375SMatthew G. Knepley .  u   - The output field values
8385a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8386a6e0b375SMatthew G. Knepley 
8387a6e0b375SMatthew G. Knepley   Level: developer
8388a6e0b375SMatthew G. Knepley 
8389a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8390a6e0b375SMatthew G. Knepley @*/
83911c531cf8SMatthew 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)
83924d6f44ffSToby Isaac {
83934d6f44ffSToby Isaac   PetscErrorCode ierr;
83944d6f44ffSToby Isaac 
83954d6f44ffSToby Isaac   PetscFunctionBegin;
83964d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83974d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
83980918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
83991c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
84004d6f44ffSToby Isaac   PetscFunctionReturn(0);
84014d6f44ffSToby Isaac }
84022716604bSToby Isaac 
8403a6e0b375SMatthew G. Knepley /*@C
8404a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8405a6e0b375SMatthew G. Knepley 
8406a6e0b375SMatthew G. Knepley   Not collective
8407a6e0b375SMatthew G. Knepley 
8408a6e0b375SMatthew G. Knepley   Input Parameters:
8409a6e0b375SMatthew G. Knepley + dm      - The DM
8410a6e0b375SMatthew G. Knepley . time    - The time
8411a6e0b375SMatthew G. Knepley . localU  - The input field vector
8412a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8413a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8414a6e0b375SMatthew G. Knepley 
8415a6e0b375SMatthew G. Knepley   Output Parameter:
8416a6e0b375SMatthew G. Knepley . localX  - The output vector
8417a6e0b375SMatthew G. Knepley 
8418a6e0b375SMatthew G. Knepley    Calling sequence of func:
8419a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8420a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8421a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8422a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8423a6e0b375SMatthew G. Knepley 
8424a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8425a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8426a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8427a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8428a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8429a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8430a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8431a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8432a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8433a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8434a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8435a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8436a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8437a6e0b375SMatthew G. Knepley .  t            - The current time
8438a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8439a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8440a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8441a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8442a6e0b375SMatthew G. Knepley 
8443a6e0b375SMatthew G. Knepley   Note: There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
8444a6e0b375SMatthew G. Knepley   The input DM, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or
8445a6e0b375SMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
8446a6e0b375SMatthew G. Knepley   auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations.
8447a6e0b375SMatthew G. Knepley 
8448a6e0b375SMatthew G. Knepley   Level: intermediate
8449a6e0b375SMatthew G. Knepley 
8450a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8451a6e0b375SMatthew G. Knepley @*/
84528c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
84538c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
84548c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
84558c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8456191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
84578c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
84588c6c5593SMatthew G. Knepley {
84598c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
84608c6c5593SMatthew G. Knepley 
84618c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84628c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84638c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
84648c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
84650918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
84668c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
84678c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
84688c6c5593SMatthew G. Knepley }
84698c6c5593SMatthew G. Knepley 
8470a6e0b375SMatthew G. Knepley /*@C
8471a6e0b375SMatthew G. Knepley   DMProjectFieldLabelLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector, calculating only over the portion of the domain specified by the label.
8472a6e0b375SMatthew G. Knepley 
8473a6e0b375SMatthew G. Knepley   Not collective
8474a6e0b375SMatthew G. Knepley 
8475a6e0b375SMatthew G. Knepley   Input Parameters:
8476a6e0b375SMatthew G. Knepley + dm      - The DM
8477a6e0b375SMatthew G. Knepley . time    - The time
8478a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8479a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8480a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8481a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8482a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8483a6e0b375SMatthew G. Knepley . localU  - The input field vector
8484a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8485a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8486a6e0b375SMatthew G. Knepley 
8487a6e0b375SMatthew G. Knepley   Output Parameter:
8488a6e0b375SMatthew G. Knepley . localX  - The output vector
8489a6e0b375SMatthew G. Knepley 
8490a6e0b375SMatthew G. Knepley    Calling sequence of func:
8491a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8492a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8493a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8494a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8495a6e0b375SMatthew G. Knepley 
8496a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8497a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8498a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8499a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8500a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8501a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8502a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8503a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8504a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8505a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8506a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8507a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8508a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8509a6e0b375SMatthew G. Knepley .  t            - The current time
8510a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8511a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8512a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8513a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8514a6e0b375SMatthew G. Knepley 
8515a6e0b375SMatthew G. Knepley   Note: There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
8516a6e0b375SMatthew G. Knepley   The input DM, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or
8517a6e0b375SMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
8518a6e0b375SMatthew G. Knepley   auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations.
8519a6e0b375SMatthew G. Knepley 
8520a6e0b375SMatthew G. Knepley   Level: intermediate
8521a6e0b375SMatthew G. Knepley 
8522a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8523a6e0b375SMatthew G. Knepley @*/
85241c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
85258c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
85268c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
85278c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8528191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
85298c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
85308c6c5593SMatthew G. Knepley {
85318c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
85328c6c5593SMatthew G. Knepley 
85338c6c5593SMatthew G. Knepley   PetscFunctionBegin;
85348c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85358c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
85368c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8537ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
85381c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
85398c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
85408c6c5593SMatthew G. Knepley }
85418c6c5593SMatthew G. Knepley 
85422716604bSToby Isaac /*@C
8543ece3a9fcSMatthew G. Knepley   DMProjectBdFieldLabelLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector, calculating only over the portion of the domain boundary specified by the label.
8544ece3a9fcSMatthew G. Knepley 
8545ece3a9fcSMatthew G. Knepley   Not collective
8546ece3a9fcSMatthew G. Knepley 
8547ece3a9fcSMatthew G. Knepley   Input Parameters:
8548ece3a9fcSMatthew G. Knepley + dm      - The DM
8549ece3a9fcSMatthew G. Knepley . time    - The time
8550ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8551ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8552ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8553ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8554ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8555ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8556ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8557ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8558ece3a9fcSMatthew G. Knepley 
8559ece3a9fcSMatthew G. Knepley   Output Parameter:
8560ece3a9fcSMatthew G. Knepley . localX  - The output vector
8561ece3a9fcSMatthew G. Knepley 
8562ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8563ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8564ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8565ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8566ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8567ece3a9fcSMatthew G. Knepley 
8568ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8569ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8570ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8571ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8572ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8573ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8574ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8575ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8576ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8577ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8578ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8579ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8580ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8581ece3a9fcSMatthew G. Knepley .  t            - The current time
8582ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8583ece3a9fcSMatthew G. Knepley .  n            - The face normal
8584ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8585ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8586ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8587ece3a9fcSMatthew G. Knepley 
8588ece3a9fcSMatthew G. Knepley   Note:
8589ece3a9fcSMatthew G. Knepley   There are three different DMs that potentially interact in this function. The output DM, dm, specifies the layout of the values calculates by funcs.
8590ece3a9fcSMatthew G. Knepley   The input DM, attached to U, may be different. For example, you can input the solution over the full domain, but output over a piece of the boundary, or
8591ece3a9fcSMatthew G. Knepley   a subdomain. You can also output a different number of fields than the input, with different discretizations. Last the auxiliary DM, attached to the
8592ece3a9fcSMatthew G. Knepley   auxiliary field vector, which is attached to dm, can also be different. It can have a different topology, number of fields, and discretizations.
8593ece3a9fcSMatthew G. Knepley 
8594ece3a9fcSMatthew G. Knepley   Level: intermediate
8595ece3a9fcSMatthew G. Knepley 
8596ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8597ece3a9fcSMatthew G. Knepley @*/
8598ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8599ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8600ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8601ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8602ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8603ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8604ece3a9fcSMatthew G. Knepley {
8605ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8606ece3a9fcSMatthew G. Knepley 
8607ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8608ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8609ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8610ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8611ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8612ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8613ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8614ece3a9fcSMatthew G. Knepley }
8615ece3a9fcSMatthew G. Knepley 
8616ece3a9fcSMatthew G. Knepley /*@C
86172716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
86182716604bSToby Isaac 
86192716604bSToby Isaac   Input Parameters:
86202716604bSToby Isaac + dm    - The DM
86210709b2feSToby Isaac . time  - The time
86222716604bSToby Isaac . funcs - The functions to evaluate for each field component
86232716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8624574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86252716604bSToby Isaac 
86262716604bSToby Isaac   Output Parameter:
86272716604bSToby Isaac . diff - The diff ||u - u_h||_2
86282716604bSToby Isaac 
86292716604bSToby Isaac   Level: developer
86302716604bSToby Isaac 
86311189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86322716604bSToby Isaac @*/
86330709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
86342716604bSToby Isaac {
86352716604bSToby Isaac   PetscErrorCode ierr;
86362716604bSToby Isaac 
86372716604bSToby Isaac   PetscFunctionBegin;
86382716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8639b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
86400918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
86410709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
86422716604bSToby Isaac   PetscFunctionReturn(0);
86432716604bSToby Isaac }
8644b698f381SToby Isaac 
8645b698f381SToby Isaac /*@C
8646b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8647b698f381SToby Isaac 
8648d083f849SBarry Smith   Collective on dm
8649d083f849SBarry Smith 
8650b698f381SToby Isaac   Input Parameters:
8651b698f381SToby Isaac + dm    - The DM
8652b698f381SToby Isaac , time  - The time
8653b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8654b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8655574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8656b698f381SToby Isaac - n     - The vector to project along
8657b698f381SToby Isaac 
8658b698f381SToby Isaac   Output Parameter:
8659b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8660b698f381SToby Isaac 
8661b698f381SToby Isaac   Level: developer
8662b698f381SToby Isaac 
8663b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8664b698f381SToby Isaac @*/
8665b698f381SToby 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)
8666b698f381SToby Isaac {
8667b698f381SToby Isaac   PetscErrorCode ierr;
8668b698f381SToby Isaac 
8669b698f381SToby Isaac   PetscFunctionBegin;
8670b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8671b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8672b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8673b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8674b698f381SToby Isaac   PetscFunctionReturn(0);
8675b698f381SToby Isaac }
8676b698f381SToby Isaac 
86772a16baeaSToby Isaac /*@C
86782a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86792a16baeaSToby Isaac 
8680d083f849SBarry Smith   Collective on dm
8681d083f849SBarry Smith 
86822a16baeaSToby Isaac   Input Parameters:
86832a16baeaSToby Isaac + dm    - The DM
86842a16baeaSToby Isaac . time  - The time
86852a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86862a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8687574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86882a16baeaSToby Isaac 
86892a16baeaSToby Isaac   Output Parameter:
86902a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86912a16baeaSToby Isaac 
86922a16baeaSToby Isaac   Level: developer
86932a16baeaSToby Isaac 
86941189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86952a16baeaSToby Isaac @*/
86961189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
86972a16baeaSToby Isaac {
86982a16baeaSToby Isaac   PetscErrorCode ierr;
86992a16baeaSToby Isaac 
87002a16baeaSToby Isaac   PetscFunctionBegin;
87012a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87022a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
87030918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
87042a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
87052a16baeaSToby Isaac   PetscFunctionReturn(0);
87062a16baeaSToby Isaac }
87072a16baeaSToby Isaac 
8708df0b854cSToby Isaac /*@C
8709df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8710cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8711df0b854cSToby Isaac 
8712df0b854cSToby Isaac   Collective on dm
8713df0b854cSToby Isaac 
8714df0b854cSToby Isaac   Input parameters:
8715df0b854cSToby Isaac + dm - the pre-adaptation DM object
8716a1b0c543SToby Isaac - label - label with the flags
8717df0b854cSToby Isaac 
8718df0b854cSToby Isaac   Output parameters:
87190d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8720df0b854cSToby Isaac 
8721df0b854cSToby Isaac   Level: intermediate
87220d1cd5e0SMatthew G. Knepley 
87230d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8724df0b854cSToby Isaac @*/
87250d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8726df0b854cSToby Isaac {
8727df0b854cSToby Isaac   PetscErrorCode ierr;
8728df0b854cSToby Isaac 
8729df0b854cSToby Isaac   PetscFunctionBegin;
8730df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8731a1b0c543SToby Isaac   PetscValidPointer(label,2);
87320d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
87330d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
87346f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
87350d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
87360d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
87370d1cd5e0SMatthew G. Knepley }
87380d1cd5e0SMatthew G. Knepley 
87390d1cd5e0SMatthew G. Knepley /*@C
87400d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
87410d1cd5e0SMatthew G. Knepley 
87420d1cd5e0SMatthew G. Knepley   Input Parameters:
87430d1cd5e0SMatthew G. Knepley + dm - The DM object
87440d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
87456f25b0d8SLisandro 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_".
87460d1cd5e0SMatthew G. Knepley 
87470d1cd5e0SMatthew G. Knepley   Output Parameter:
87480d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
87490d1cd5e0SMatthew G. Knepley 
87500d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
87510d1cd5e0SMatthew G. Knepley 
87520d1cd5e0SMatthew G. Knepley   Level: advanced
87530d1cd5e0SMatthew G. Knepley 
87540d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
87550d1cd5e0SMatthew G. Knepley @*/
87560d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
87570d1cd5e0SMatthew G. Knepley {
87580d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
87590d1cd5e0SMatthew G. Knepley 
87600d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
87610d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87620d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
87630d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
87640d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
87656f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
87666f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
87670d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8768df0b854cSToby Isaac   PetscFunctionReturn(0);
8769df0b854cSToby Isaac }
8770c4088d22SMatthew G. Knepley 
8771502a2867SDave May /*@C
8772502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8773502a2867SDave May 
8774502a2867SDave May  Not Collective
8775502a2867SDave May 
8776502a2867SDave May  Input Parameter:
8777502a2867SDave May .  dm    - The DM
8778502a2867SDave May 
87790a19bb7dSprj-  Output Parameters:
87800a19bb7dSprj- +  nranks - the number of neighbours
87810a19bb7dSprj- -  ranks - the neighbors ranks
8782502a2867SDave May 
8783502a2867SDave May  Notes:
8784502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8785502a2867SDave May 
8786502a2867SDave May  Level: beginner
8787502a2867SDave May 
8788dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8789502a2867SDave May @*/
8790502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8791502a2867SDave May {
8792502a2867SDave May   PetscErrorCode ierr;
8793502a2867SDave May 
8794502a2867SDave May   PetscFunctionBegin;
8795502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87960918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8797502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8798502a2867SDave May   PetscFunctionReturn(0);
8799502a2867SDave May }
8800502a2867SDave May 
8801531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8802531c7667SBarry Smith 
8803531c7667SBarry Smith /*
8804531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8805531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8806531c7667SBarry Smith */
8807531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8808531c7667SBarry Smith {
8809531c7667SBarry Smith   PetscErrorCode ierr;
8810531c7667SBarry Smith 
8811531c7667SBarry Smith   PetscFunctionBegin;
8812531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8813531c7667SBarry Smith     Vec x1local;
8814531c7667SBarry Smith     DM  dm;
8815531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8816531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8817531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8818531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8819531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8820531c7667SBarry Smith     x1   = x1local;
8821531c7667SBarry Smith   }
8822531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8823531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8824531c7667SBarry Smith     DM  dm;
8825531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8826531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8827531c7667SBarry Smith   }
8828531c7667SBarry Smith   PetscFunctionReturn(0);
8829531c7667SBarry Smith }
8830531c7667SBarry Smith 
8831531c7667SBarry Smith /*@
8832531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8833531c7667SBarry Smith 
8834531c7667SBarry Smith     Input Parameter:
8835531c7667SBarry Smith .    coloring - the MatFDColoring object
8836531c7667SBarry Smith 
883795452b02SPatrick Sanan     Developer Notes:
883895452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8839531c7667SBarry Smith 
88401b266c99SBarry Smith     Level: advanced
88411b266c99SBarry Smith 
8842531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8843531c7667SBarry Smith @*/
8844531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8845531c7667SBarry Smith {
8846531c7667SBarry Smith   PetscFunctionBegin;
8847531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8848531c7667SBarry Smith   PetscFunctionReturn(0);
8849531c7667SBarry Smith }
88508320bc6fSPatrick Sanan 
88518320bc6fSPatrick Sanan /*@
88528320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
88538320bc6fSPatrick Sanan 
88548320bc6fSPatrick Sanan     Collective
88558320bc6fSPatrick Sanan 
88568320bc6fSPatrick Sanan     Input Parameters:
8857a5bc1bf3SBarry Smith +    dm1 - the first DM
88588320bc6fSPatrick Sanan -    dm2 - the second DM
88598320bc6fSPatrick Sanan 
88608320bc6fSPatrick Sanan     Output Parameters:
88618320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
88628320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
88638320bc6fSPatrick Sanan 
88648320bc6fSPatrick Sanan     Notes:
88658320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
88663d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
88678320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
88683d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
88693d862458SPatrick Sanan     decomposition, but hold different data.
88708320bc6fSPatrick Sanan 
88718320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
88728320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
88738320bc6fSPatrick Sanan 
88748320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
88758320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
88768320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
88778320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
88788320bc6fSPatrick Sanan 
88798320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
88808320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
88818320bc6fSPatrick Sanan .vb
88828320bc6fSPatrick Sanan   ...
88838320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
88848320bc6fSPatrick Sanan   if (set && compatible)  {
88858320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88868320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88873d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
88888320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
88898320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
88908320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88918320bc6fSPatrick Sanan       }
88928320bc6fSPatrick Sanan     }
88938320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88948320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88958320bc6fSPatrick Sanan   } else {
88968320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88978320bc6fSPatrick Sanan   }
88988320bc6fSPatrick Sanan   ...
88998320bc6fSPatrick Sanan .ve
89008320bc6fSPatrick Sanan 
89018320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
89028320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
89038320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
89048320bc6fSPatrick Sanan     always check the "set" output parameter.
89058320bc6fSPatrick Sanan 
89068320bc6fSPatrick Sanan     A DM is always compatible with itself.
89078320bc6fSPatrick Sanan 
89088320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
89098320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
89108320bc6fSPatrick Sanan     incompatible.
89118320bc6fSPatrick Sanan 
89128320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
89138320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
89148320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
89158320bc6fSPatrick Sanan 
89168320bc6fSPatrick Sanan     Developer Notes:
89173d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
89183d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8919a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
89208320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
89218320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
89223d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
89233d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
89248320bc6fSPatrick Sanan 
89258320bc6fSPatrick Sanan     Level: advanced
89268320bc6fSPatrick Sanan 
89273d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
89288320bc6fSPatrick Sanan @*/
89298320bc6fSPatrick Sanan 
8930a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
89318320bc6fSPatrick Sanan {
89328320bc6fSPatrick Sanan   PetscErrorCode ierr;
89338320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
89348320bc6fSPatrick Sanan   DMType         type,type2;
89358320bc6fSPatrick Sanan   PetscBool      sameType;
89368320bc6fSPatrick Sanan 
89378320bc6fSPatrick Sanan   PetscFunctionBegin;
8938a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
89398320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
89408320bc6fSPatrick Sanan 
89418320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8942a5bc1bf3SBarry Smith   if (dm1 == dm2) {
89438320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89448320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
89458320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89468320bc6fSPatrick Sanan   }
89478320bc6fSPatrick Sanan 
89488320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
89498320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
89508320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
89518320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8952a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
89538320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
89548320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89558320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
89568320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89578320bc6fSPatrick Sanan   }
89588320bc6fSPatrick Sanan 
89598320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8960a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8961a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8962b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
89638320bc6fSPatrick Sanan   }
89648320bc6fSPatrick Sanan 
8965a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
89668320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8967a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
89688320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
89698320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
89708320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8971a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
89728320bc6fSPatrick Sanan   } else {
89738320bc6fSPatrick Sanan     *set = PETSC_FALSE;
89748320bc6fSPatrick Sanan   }
89758320bc6fSPatrick Sanan   PetscFunctionReturn(0);
89768320bc6fSPatrick Sanan }
8977c0f0dcc3SMatthew G. Knepley 
8978c0f0dcc3SMatthew G. Knepley /*@C
8979c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8980c0f0dcc3SMatthew G. Knepley 
8981c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8982c0f0dcc3SMatthew G. Knepley 
8983c0f0dcc3SMatthew G. Knepley   Input Parameters:
8984c0f0dcc3SMatthew G. Knepley + DM - the DM
8985c0f0dcc3SMatthew G. Knepley . f - the monitor function
8986c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8987c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8988c0f0dcc3SMatthew G. Knepley 
8989c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8990c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8991c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8992c0f0dcc3SMatthew G. Knepley 
8993c0f0dcc3SMatthew G. Knepley   Notes:
8994c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8995c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8996c0f0dcc3SMatthew G. Knepley   order in which they were set.
8997c0f0dcc3SMatthew G. Knepley 
8998c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8999c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
9000c0f0dcc3SMatthew G. Knepley 
9001c0f0dcc3SMatthew G. Knepley   Level: intermediate
9002c0f0dcc3SMatthew G. Knepley 
9003c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
9004c0f0dcc3SMatthew G. Knepley @*/
9005c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
9006c0f0dcc3SMatthew G. Knepley {
9007c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9008c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9009c0f0dcc3SMatthew G. Knepley 
9010c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9011c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9012c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9013c0f0dcc3SMatthew G. Knepley     PetscBool identical;
9014c0f0dcc3SMatthew G. Knepley 
9015c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
9016c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
9017c0f0dcc3SMatthew G. Knepley   }
9018c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
9019c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
9020c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
9021c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
9022c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9023c0f0dcc3SMatthew G. Knepley }
9024c0f0dcc3SMatthew G. Knepley 
9025c0f0dcc3SMatthew G. Knepley /*@
9026c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9027c0f0dcc3SMatthew G. Knepley 
9028c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9029c0f0dcc3SMatthew G. Knepley 
9030c0f0dcc3SMatthew G. Knepley   Input Parameter:
9031c0f0dcc3SMatthew G. Knepley . dm - the DM
9032c0f0dcc3SMatthew G. Knepley 
9033c0f0dcc3SMatthew G. Knepley   Options Database Key:
9034c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9035c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9036c0f0dcc3SMatthew G. Knepley   set via the options database
9037c0f0dcc3SMatthew G. Knepley 
9038c0f0dcc3SMatthew G. Knepley   Notes:
9039c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9040c0f0dcc3SMatthew G. Knepley 
9041c0f0dcc3SMatthew G. Knepley   Level: intermediate
9042c0f0dcc3SMatthew G. Knepley 
9043c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9044c0f0dcc3SMatthew G. Knepley @*/
9045c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9046c0f0dcc3SMatthew G. Knepley {
9047c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9048c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9049c0f0dcc3SMatthew G. Knepley 
9050c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9051c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9052c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9053c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9054c0f0dcc3SMatthew G. Knepley   }
9055c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9056c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9057c0f0dcc3SMatthew G. Knepley }
9058c0f0dcc3SMatthew G. Knepley 
9059c0f0dcc3SMatthew G. Knepley /*@C
9060c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9061c0f0dcc3SMatthew G. Knepley 
9062c0f0dcc3SMatthew G. Knepley   Collective on DM
9063c0f0dcc3SMatthew G. Knepley 
9064c0f0dcc3SMatthew G. Knepley   Input Parameters:
9065c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9066c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9067c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9068c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9069c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9070c0f0dcc3SMatthew G. Knepley - monitorsetup - a function that is called once ONLY if the user selected this monitor that may set additional features of the DM or PetscViewer objects
9071c0f0dcc3SMatthew G. Knepley 
9072c0f0dcc3SMatthew G. Knepley   Output Parameter:
9073c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9074c0f0dcc3SMatthew G. Knepley 
9075c0f0dcc3SMatthew G. Knepley   Level: developer
9076c0f0dcc3SMatthew G. Knepley 
9077c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9078c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9079c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9080c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9081c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9082c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9083c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9084c0f0dcc3SMatthew G. Knepley @*/
9085c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSetFromOptions(DM dm, const char name[], const char help[], const char manual[], PetscErrorCode (*monitor)(DM, void *), PetscErrorCode (*monitorsetup)(DM, PetscViewerAndFormat *), PetscBool *flg)
9086c0f0dcc3SMatthew G. Knepley {
9087c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9088c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9089c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9090c0f0dcc3SMatthew G. Knepley 
9091c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9092c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9093c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9094c0f0dcc3SMatthew G. Knepley   if (*flg) {
9095c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9096c0f0dcc3SMatthew G. Knepley 
9097c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9098c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9099c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9100c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9101c0f0dcc3SMatthew G. Knepley   }
9102c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9103c0f0dcc3SMatthew G. Knepley }
9104c0f0dcc3SMatthew G. Knepley 
9105c0f0dcc3SMatthew G. Knepley /*@
9106c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9107c0f0dcc3SMatthew G. Knepley 
9108c0f0dcc3SMatthew G. Knepley    Collective on DM
9109c0f0dcc3SMatthew G. Knepley 
9110c0f0dcc3SMatthew G. Knepley    Input Parameters:
9111c0f0dcc3SMatthew G. Knepley .  dm - The DM
9112c0f0dcc3SMatthew G. Knepley 
9113c0f0dcc3SMatthew G. Knepley    Level: developer
9114c0f0dcc3SMatthew G. Knepley 
9115c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9116c0f0dcc3SMatthew G. Knepley @*/
9117c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9118c0f0dcc3SMatthew G. Knepley {
9119c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9120c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9121c0f0dcc3SMatthew G. Knepley 
9122c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9123c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9124c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9125c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9126c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9127c0f0dcc3SMatthew G. Knepley   }
9128c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9129c0f0dcc3SMatthew G. Knepley }
9130