xref: /petsc/src/dm/interface/dm.c (revision d0295fc027abbea29f13fa82c19cf92da8e9ba99)
1*d0295fc0SJunchao Zhang #include <petscvec.h>
2af0996ceSBarry Smith #include <petsc/private/dmimpl.h>           /*I      "petscdm.h"          I*/
3c58f1c22SToby Isaac #include <petsc/private/dmlabelimpl.h>      /*I      "petscdmlabel.h"     I*/
4e6f8dbb6SToby Isaac #include <petsc/private/petscdsimpl.h>      /*I      "petscds.h"     I*/
53e922f36SToby Isaac #include <petscdmplex.h>
6f19dbd58SToby Isaac #include <petscdmfield.h>
70c312b8eSJed Brown #include <petscsf.h>
82764a2aaSMatthew G. Knepley #include <petscds.h>
947c6ae99SBarry Smith 
1000d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
1100d952a4SJed Brown #  include <valgrind/memcheck.h>
1200d952a4SJed Brown #endif
1300d952a4SJed Brown 
14732e2eb9SMatthew G Knepley PetscClassId  DM_CLASSID;
15d67d17b1SMatthew G. Knepley PetscClassId  DMLABEL_CLASSID;
16557cf195SMatthew 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;
1767a56275SMatthew G Knepley 
18ea78f98cSLisandro Dalcin const char *const DMBoundaryTypes[] = {"NONE","GHOSTED","MIRROR","PERIODIC","TWIST","DMBoundaryType","DM_BOUNDARY_", NULL};
19ea78f98cSLisandro Dalcin const char *const DMBoundaryConditionTypes[] = {"INVALID","ESSENTIAL","NATURAL","INVALID","INVALID","ESSENTIAL_FIELD","NATURAL_FIELD","INVALID","INVALID","INVALID","NATURAL_RIEMANN","DMBoundaryConditionType","DM_BC_", NULL};
20ea78f98cSLisandro Dalcin const char *const DMPolytopeTypes[] = {"vertex", "segment", "tensor_segment", "triangle", "quadrilateral", "tensor_quad", "tetrahedron", "hexahedron", "triangular_prism", "tensor_triangular_prism", "tensor_quadrilateral_prism", "FV_ghost_cell", "interior_ghost_cell", "unknown", "invalid", "DMPolytopeType", "DM_POLYTOPE_", NULL};
21a4121054SBarry Smith /*@
22de043629SMatthew G Knepley   DMCreate - Creates an empty DM object. The type can then be set with DMSetType().
23a4121054SBarry Smith 
24a4121054SBarry Smith    If you never  call DMSetType()  it will generate an
25a4121054SBarry Smith    error when you try to use the vector.
26a4121054SBarry Smith 
27d083f849SBarry Smith   Collective
28a4121054SBarry Smith 
29a4121054SBarry Smith   Input Parameter:
30a4121054SBarry Smith . comm - The communicator for the DM object
31a4121054SBarry Smith 
32a4121054SBarry Smith   Output Parameter:
33a4121054SBarry Smith . dm - The DM object
34a4121054SBarry Smith 
35a4121054SBarry Smith   Level: beginner
36a4121054SBarry Smith 
378472ad0fSDave May .seealso: DMSetType(), DMDA, DMSLICED, DMCOMPOSITE, DMPLEX, DMMOAB, DMNETWORK
38a4121054SBarry Smith @*/
397087cfbeSBarry Smith PetscErrorCode  DMCreate(MPI_Comm comm,DM *dm)
40a4121054SBarry Smith {
41a4121054SBarry Smith   DM             v;
42e5e52638SMatthew G. Knepley   PetscDS        ds;
43a4121054SBarry Smith   PetscErrorCode ierr;
44a4121054SBarry Smith 
45a4121054SBarry Smith   PetscFunctionBegin;
461411c6eeSJed Brown   PetscValidPointer(dm,2);
470298fd71SBarry Smith   *dm = NULL;
48607a6623SBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
49a4121054SBarry Smith 
5073107ff1SLisandro Dalcin   ierr = PetscHeaderCreate(v, DM_CLASSID, "DM", "Distribution Manager", "DM", comm, DMDestroy, DMView);CHKERRQ(ierr);
51e7c4fc90SDmitry Karpeev 
5249be4549SMatthew G. Knepley   v->setupcalled              = PETSC_FALSE;
5349be4549SMatthew G. Knepley   v->setfromoptionscalled     = PETSC_FALSE;
540298fd71SBarry Smith   v->ltogmap                  = NULL;
551411c6eeSJed Brown   v->bs                       = 1;
56171400e9SBarry Smith   v->coloringtype             = IS_COLORING_GLOBAL;
5788ed4aceSMatthew G Knepley   ierr                        = PetscSFCreate(comm, &v->sf);CHKERRQ(ierr);
581bb6d2a8SBarry Smith   ierr                        = PetscSFCreate(comm, &v->sectionSF);CHKERRQ(ierr);
59c58f1c22SToby Isaac   v->labels                   = NULL;
6034aa8a36SMatthew G. Knepley   v->adjacency[0]             = PETSC_FALSE;
6134aa8a36SMatthew G. Knepley   v->adjacency[1]             = PETSC_TRUE;
62c58f1c22SToby Isaac   v->depthLabel               = NULL;
63ba2698f1SMatthew G. Knepley   v->celltypeLabel            = NULL;
641bb6d2a8SBarry Smith   v->localSection             = NULL;
651bb6d2a8SBarry Smith   v->globalSection            = NULL;
66fba222abSToby Isaac   v->defaultConstraintSection = NULL;
67fba222abSToby Isaac   v->defaultConstraintMat     = NULL;
68c6b900c6SMatthew G. Knepley   v->L                        = NULL;
69c6b900c6SMatthew G. Knepley   v->maxCell                  = NULL;
705dc8c3f7SMatthew G. Knepley   v->bdtype                   = NULL;
719a9a41abSToby Isaac   v->dimEmbed                 = PETSC_DEFAULT;
7296173672SStefano Zampini   v->dim                      = PETSC_DETERMINE;
73435a35e8SMatthew G Knepley   {
74435a35e8SMatthew G Knepley     PetscInt i;
75435a35e8SMatthew G Knepley     for (i = 0; i < 10; ++i) {
760298fd71SBarry Smith       v->nullspaceConstructors[i] = NULL;
77f9d4088aSMatthew G. Knepley       v->nearnullspaceConstructors[i] = NULL;
78435a35e8SMatthew G Knepley     }
79435a35e8SMatthew G Knepley   }
80e5e52638SMatthew G. Knepley   ierr = PetscDSCreate(PetscObjectComm((PetscObject) v), &ds);CHKERRQ(ierr);
81b3cf3223SMatthew G. Knepley   ierr = DMSetRegionDS(v, NULL, NULL, ds);CHKERRQ(ierr);
82e5e52638SMatthew G. Knepley   ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
8314f150ffSMatthew G. Knepley   v->dmBC = NULL;
84a8fb8f29SToby Isaac   v->coarseMesh = NULL;
85f4d763aaSMatthew G. Knepley   v->outputSequenceNum = -1;
86cdb7a50dSMatthew G. Knepley   v->outputSequenceVal = 0.0;
87c0dedaeaSBarry Smith   ierr = DMSetVecType(v,VECSTANDARD);CHKERRQ(ierr);
88b412c318SBarry Smith   ierr = DMSetMatType(v,MATAIJ);CHKERRQ(ierr);
894a7a4c06SLawrence Mitchell 
901411c6eeSJed Brown   *dm = v;
91a4121054SBarry Smith   PetscFunctionReturn(0);
92a4121054SBarry Smith }
93a4121054SBarry Smith 
9438221697SMatthew G. Knepley /*@
9538221697SMatthew G. Knepley   DMClone - Creates a DM object with the same topology as the original.
9638221697SMatthew G. Knepley 
97d083f849SBarry Smith   Collective
9838221697SMatthew G. Knepley 
9938221697SMatthew G. Knepley   Input Parameter:
10038221697SMatthew G. Knepley . dm - The original DM object
10138221697SMatthew G. Knepley 
10238221697SMatthew G. Knepley   Output Parameter:
10338221697SMatthew G. Knepley . newdm  - The new DM object
10438221697SMatthew G. Knepley 
10538221697SMatthew G. Knepley   Level: beginner
10638221697SMatthew G. Knepley 
1071cb8cacdSPatrick Sanan   Notes:
1081cb8cacdSPatrick Sanan   For some DM implementations this is a shallow clone, the result of which may share (referent counted) information with its parent. For example,
1091cb8cacdSPatrick Sanan   DMClone() applied to a DMPLEX object will result in a new DMPLEX that shares the topology with the original DMPLEX. It does not
1101cb8cacdSPatrick Sanan   share the PetscSection of the original DM.
1111bb6d2a8SBarry Smith 
11289706ed2SPatrick Sanan   The clone is considered set up iff the original is.
11389706ed2SPatrick Sanan 
11492cfd99aSMartin Diehl .seealso: DMDestroy(), DMCreate(), DMSetType(), DMSetLocalSection(), DMSetGlobalSection()
1151bb6d2a8SBarry Smith 
11638221697SMatthew G. Knepley @*/
11738221697SMatthew G. Knepley PetscErrorCode DMClone(DM dm, DM *newdm)
11838221697SMatthew G. Knepley {
11938221697SMatthew G. Knepley   PetscSF        sf;
12038221697SMatthew G. Knepley   Vec            coords;
12138221697SMatthew G. Knepley   void          *ctx;
122a3219837SMatthew G. Knepley   PetscInt       dim, cdim;
12338221697SMatthew G. Knepley   PetscErrorCode ierr;
12438221697SMatthew G. Knepley 
12538221697SMatthew G. Knepley   PetscFunctionBegin;
12638221697SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
12738221697SMatthew G. Knepley   PetscValidPointer(newdm,2);
12838221697SMatthew G. Knepley   ierr = DMCreate(PetscObjectComm((PetscObject) dm), newdm);CHKERRQ(ierr);
1295d80c0bfSVaclav Hapla   ierr = DMCopyLabels(dm, *newdm, PETSC_COPY_VALUES, PETSC_TRUE);CHKERRQ(ierr);
130ddf8437dSMatthew G. Knepley   (*newdm)->leveldown  = dm->leveldown;
131ddf8437dSMatthew G. Knepley   (*newdm)->levelup    = dm->levelup;
1321de53e9aSMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
1331de53e9aSMatthew G. Knepley   ierr = DMSetDimension(*newdm, dim);CHKERRQ(ierr);
13438221697SMatthew G. Knepley   if (dm->ops->clone) {
13538221697SMatthew G. Knepley     ierr = (*dm->ops->clone)(dm, newdm);CHKERRQ(ierr);
13638221697SMatthew G. Knepley   }
1373f22bcbcSToby Isaac   (*newdm)->setupcalled = dm->setupcalled;
13838221697SMatthew G. Knepley   ierr = DMGetPointSF(dm, &sf);CHKERRQ(ierr);
13938221697SMatthew G. Knepley   ierr = DMSetPointSF(*newdm, sf);CHKERRQ(ierr);
14038221697SMatthew G. Knepley   ierr = DMGetApplicationContext(dm, &ctx);CHKERRQ(ierr);
14138221697SMatthew G. Knepley   ierr = DMSetApplicationContext(*newdm, ctx);CHKERRQ(ierr);
142be4c1c3eSMatthew G. Knepley   if (dm->coordinateDM) {
143be4c1c3eSMatthew G. Knepley     DM           ncdm;
144be4c1c3eSMatthew G. Knepley     PetscSection cs;
1455a0206caSToby Isaac     PetscInt     pEnd = -1, pEndMax = -1;
146be4c1c3eSMatthew G. Knepley 
14792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm->coordinateDM, &cs);CHKERRQ(ierr);
148be4c1c3eSMatthew G. Knepley     if (cs) {ierr = PetscSectionGetChart(cs, NULL, &pEnd);CHKERRQ(ierr);}
1495a0206caSToby Isaac     ierr = MPI_Allreduce(&pEnd,&pEndMax,1,MPIU_INT,MPI_MAX,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
1505a0206caSToby Isaac     if (pEndMax >= 0) {
151be4c1c3eSMatthew G. Knepley       ierr = DMClone(dm->coordinateDM, &ncdm);CHKERRQ(ierr);
15283bfc06fSMatthew Knepley       ierr = DMCopyDisc(dm->coordinateDM, ncdm);CHKERRQ(ierr);
15392fd8e1eSJed Brown       ierr = DMSetLocalSection(ncdm, cs);CHKERRQ(ierr);
154a61e840bSMatthew G. Knepley       ierr = DMSetCoordinateDM(*newdm, ncdm);CHKERRQ(ierr);
155be4c1c3eSMatthew G. Knepley       ierr = DMDestroy(&ncdm);CHKERRQ(ierr);
156be4c1c3eSMatthew G. Knepley     }
157be4c1c3eSMatthew G. Knepley   }
158a3219837SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
159a3219837SMatthew G. Knepley   ierr = DMSetCoordinateDim(*newdm, cdim);CHKERRQ(ierr);
16038221697SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coords);CHKERRQ(ierr);
16138221697SMatthew G. Knepley   if (coords) {
16238221697SMatthew G. Knepley     ierr = DMSetCoordinatesLocal(*newdm, coords);CHKERRQ(ierr);
16338221697SMatthew G. Knepley   } else {
16438221697SMatthew G. Knepley     ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
16538221697SMatthew G. Knepley     if (coords) {ierr = DMSetCoordinates(*newdm, coords);CHKERRQ(ierr);}
16638221697SMatthew G. Knepley   }
16790b157c4SStefano Zampini   {
16890b157c4SStefano Zampini     PetscBool             isper;
169c6b900c6SMatthew G. Knepley     const PetscReal      *maxCell, *L;
1705dc8c3f7SMatthew G. Knepley     const DMBoundaryType *bd;
17190b157c4SStefano Zampini     ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
17290b157c4SStefano Zampini     ierr = DMSetPeriodicity(*newdm, isper, maxCell,  L,  bd);CHKERRQ(ierr);
173c6b900c6SMatthew G. Knepley   }
17434aa8a36SMatthew G. Knepley   {
17534aa8a36SMatthew G. Knepley     PetscBool useCone, useClosure;
17634aa8a36SMatthew G. Knepley 
17734aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, &useCone, &useClosure);CHKERRQ(ierr);
17834aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(*newdm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
17934aa8a36SMatthew G. Knepley   }
18038221697SMatthew G. Knepley   PetscFunctionReturn(0);
18138221697SMatthew G. Knepley }
18238221697SMatthew G. Knepley 
1839a42bb27SBarry Smith /*@C
184564755cdSBarry Smith        DMSetVecType - Sets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
1859a42bb27SBarry Smith 
186d083f849SBarry Smith    Logically Collective on da
1879a42bb27SBarry Smith 
1889a42bb27SBarry Smith    Input Parameter:
1899a42bb27SBarry Smith +  da - initial distributed array
190e9e886b6SKarl Rupp .  ctype - the vector type, currently either VECSTANDARD, VECCUDA, or VECVIENNACL
1919a42bb27SBarry Smith 
1929a42bb27SBarry Smith    Options Database:
193dd85299cSBarry Smith .   -dm_vec_type ctype
1949a42bb27SBarry Smith 
1959a42bb27SBarry Smith    Level: intermediate
1969a42bb27SBarry Smith 
197a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMGetVecType(), DMSetMatType(), DMGetMatType()
1989a42bb27SBarry Smith @*/
19919fd82e9SBarry Smith PetscErrorCode  DMSetVecType(DM da,VecType ctype)
2009a42bb27SBarry Smith {
2019a42bb27SBarry Smith   PetscErrorCode ierr;
2029a42bb27SBarry Smith 
2039a42bb27SBarry Smith   PetscFunctionBegin;
2049a42bb27SBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
2059a42bb27SBarry Smith   ierr = PetscFree(da->vectype);CHKERRQ(ierr);
20619fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&da->vectype);CHKERRQ(ierr);
2079a42bb27SBarry Smith   PetscFunctionReturn(0);
2089a42bb27SBarry Smith }
2099a42bb27SBarry Smith 
210c0dedaeaSBarry Smith /*@C
211c0dedaeaSBarry Smith        DMGetVecType - Gets the type of vector created with DMCreateLocalVector() and DMCreateGlobalVector()
212c0dedaeaSBarry Smith 
213d083f849SBarry Smith    Logically Collective on da
214c0dedaeaSBarry Smith 
215c0dedaeaSBarry Smith    Input Parameter:
216c0dedaeaSBarry Smith .  da - initial distributed array
217c0dedaeaSBarry Smith 
218c0dedaeaSBarry Smith    Output Parameter:
219c0dedaeaSBarry Smith .  ctype - the vector type
220c0dedaeaSBarry Smith 
221c0dedaeaSBarry Smith    Level: intermediate
222c0dedaeaSBarry Smith 
223a2a9ebe5SBarry Smith .seealso: DMCreate(), DMDestroy(), DM, DMDAInterpolationType, VecType, DMSetMatType(), DMGetMatType(), DMSetVecType()
224c0dedaeaSBarry Smith @*/
225c0dedaeaSBarry Smith PetscErrorCode  DMGetVecType(DM da,VecType *ctype)
226c0dedaeaSBarry Smith {
227c0dedaeaSBarry Smith   PetscFunctionBegin;
228c0dedaeaSBarry Smith   PetscValidHeaderSpecific(da,DM_CLASSID,1);
229c0dedaeaSBarry Smith   *ctype = da->vectype;
230c0dedaeaSBarry Smith   PetscFunctionReturn(0);
231c0dedaeaSBarry Smith }
232c0dedaeaSBarry Smith 
2335f1ad066SMatthew G Knepley /*@
23434f98d34SBarry Smith   VecGetDM - Gets the DM defining the data layout of the vector
2355f1ad066SMatthew G Knepley 
2365f1ad066SMatthew G Knepley   Not collective
2375f1ad066SMatthew G Knepley 
2385f1ad066SMatthew G Knepley   Input Parameter:
2395f1ad066SMatthew G Knepley . v - The Vec
2405f1ad066SMatthew G Knepley 
2415f1ad066SMatthew G Knepley   Output Parameter:
2425f1ad066SMatthew G Knepley . dm - The DM
2435f1ad066SMatthew G Knepley 
2445f1ad066SMatthew G Knepley   Level: intermediate
2455f1ad066SMatthew G Knepley 
2465f1ad066SMatthew G Knepley .seealso: VecSetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2475f1ad066SMatthew G Knepley @*/
2485f1ad066SMatthew G Knepley PetscErrorCode VecGetDM(Vec v, DM *dm)
2495f1ad066SMatthew G Knepley {
2505f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2515f1ad066SMatthew G Knepley 
2525f1ad066SMatthew G Knepley   PetscFunctionBegin;
2535f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
2545f1ad066SMatthew G Knepley   PetscValidPointer(dm,2);
2555f1ad066SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) v, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
2565f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2575f1ad066SMatthew G Knepley }
2585f1ad066SMatthew G Knepley 
2595f1ad066SMatthew G Knepley /*@
260d9805387SMatthew G. Knepley   VecSetDM - Sets the DM defining the data layout of the vector.
2615f1ad066SMatthew G Knepley 
2625f1ad066SMatthew G Knepley   Not collective
2635f1ad066SMatthew G Knepley 
2645f1ad066SMatthew G Knepley   Input Parameters:
2655f1ad066SMatthew G Knepley + v - The Vec
2665f1ad066SMatthew G Knepley - dm - The DM
2675f1ad066SMatthew G Knepley 
268d9805387SMatthew 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.
269d9805387SMatthew G. Knepley 
2705f1ad066SMatthew G Knepley   Level: intermediate
2715f1ad066SMatthew G Knepley 
2725f1ad066SMatthew G Knepley .seealso: VecGetDM(), DMGetLocalVector(), DMGetGlobalVector(), DMSetVecType()
2735f1ad066SMatthew G Knepley @*/
2745f1ad066SMatthew G Knepley PetscErrorCode VecSetDM(Vec v, DM dm)
2755f1ad066SMatthew G Knepley {
2765f1ad066SMatthew G Knepley   PetscErrorCode ierr;
2775f1ad066SMatthew G Knepley 
2785f1ad066SMatthew G Knepley   PetscFunctionBegin;
2795f1ad066SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,1);
280d7f50e27SLisandro Dalcin   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
2815f1ad066SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) v, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
2825f1ad066SMatthew G Knepley   PetscFunctionReturn(0);
2835f1ad066SMatthew G Knepley }
2845f1ad066SMatthew G Knepley 
285521d9a4cSLisandro Dalcin /*@C
2868f1509bcSBarry Smith        DMSetISColoringType - Sets the type of coloring, global or local, that is created by the DM
2878f1509bcSBarry Smith 
288d083f849SBarry Smith    Logically Collective on dm
2898f1509bcSBarry Smith 
2908f1509bcSBarry Smith    Input Parameters:
2918f1509bcSBarry Smith +  dm - the DM context
2928f1509bcSBarry Smith -  ctype - the matrix type
2938f1509bcSBarry Smith 
2948f1509bcSBarry Smith    Options Database:
2958f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
2968f1509bcSBarry Smith 
2978f1509bcSBarry Smith    Level: intermediate
2988f1509bcSBarry Smith 
2998f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3008f1509bcSBarry Smith           DMGetISColoringType()
3018f1509bcSBarry Smith @*/
3028f1509bcSBarry Smith PetscErrorCode  DMSetISColoringType(DM dm,ISColoringType ctype)
3038f1509bcSBarry Smith {
3048f1509bcSBarry Smith   PetscFunctionBegin;
3058f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3068f1509bcSBarry Smith   dm->coloringtype = ctype;
3078f1509bcSBarry Smith   PetscFunctionReturn(0);
3088f1509bcSBarry Smith }
3098f1509bcSBarry Smith 
3108f1509bcSBarry Smith /*@C
3118f1509bcSBarry Smith        DMGetISColoringType - Gets the type of coloring, global or local, that is created by the DM
312521d9a4cSLisandro Dalcin 
313d083f849SBarry Smith    Logically Collective on dm
314521d9a4cSLisandro Dalcin 
315521d9a4cSLisandro Dalcin    Input Parameter:
3168f1509bcSBarry Smith .  dm - the DM context
3178f1509bcSBarry Smith 
3188f1509bcSBarry Smith    Output Parameter:
3198f1509bcSBarry Smith .  ctype - the matrix type
3208f1509bcSBarry Smith 
3218f1509bcSBarry Smith    Options Database:
3228f1509bcSBarry Smith .   -dm_is_coloring_type - global or local
3238f1509bcSBarry Smith 
3248f1509bcSBarry Smith    Level: intermediate
3258f1509bcSBarry Smith 
3268f1509bcSBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(),
3278f1509bcSBarry Smith           DMGetISColoringType()
3288f1509bcSBarry Smith @*/
3298f1509bcSBarry Smith PetscErrorCode  DMGetISColoringType(DM dm,ISColoringType *ctype)
3308f1509bcSBarry Smith {
3318f1509bcSBarry Smith   PetscFunctionBegin;
3328f1509bcSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3338f1509bcSBarry Smith   *ctype = dm->coloringtype;
3348f1509bcSBarry Smith   PetscFunctionReturn(0);
3358f1509bcSBarry Smith }
3368f1509bcSBarry Smith 
3378f1509bcSBarry Smith /*@C
3388f1509bcSBarry Smith        DMSetMatType - Sets the type of matrix created with DMCreateMatrix()
3398f1509bcSBarry Smith 
340d083f849SBarry Smith    Logically Collective on dm
3418f1509bcSBarry Smith 
3428f1509bcSBarry Smith    Input Parameters:
343521d9a4cSLisandro Dalcin +  dm - the DM context
344a2b5a043SBarry Smith -  ctype - the matrix type
345521d9a4cSLisandro Dalcin 
346521d9a4cSLisandro Dalcin    Options Database:
347521d9a4cSLisandro Dalcin .   -dm_mat_type ctype
348521d9a4cSLisandro Dalcin 
349521d9a4cSLisandro Dalcin    Level: intermediate
350521d9a4cSLisandro Dalcin 
351a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMGetMatType(), DMSetMatType(), DMGetMatType()
352521d9a4cSLisandro Dalcin @*/
35319fd82e9SBarry Smith PetscErrorCode  DMSetMatType(DM dm,MatType ctype)
354521d9a4cSLisandro Dalcin {
355521d9a4cSLisandro Dalcin   PetscErrorCode ierr;
35688f0584fSBarry Smith 
357521d9a4cSLisandro Dalcin   PetscFunctionBegin;
358521d9a4cSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
359521d9a4cSLisandro Dalcin   ierr = PetscFree(dm->mattype);CHKERRQ(ierr);
36019fd82e9SBarry Smith   ierr = PetscStrallocpy(ctype,(char**)&dm->mattype);CHKERRQ(ierr);
361521d9a4cSLisandro Dalcin   PetscFunctionReturn(0);
362521d9a4cSLisandro Dalcin }
363521d9a4cSLisandro Dalcin 
364c0dedaeaSBarry Smith /*@C
365c0dedaeaSBarry Smith        DMGetMatType - Gets the type of matrix created with DMCreateMatrix()
366c0dedaeaSBarry Smith 
367d083f849SBarry Smith    Logically Collective on dm
368c0dedaeaSBarry Smith 
369c0dedaeaSBarry Smith    Input Parameter:
370c0dedaeaSBarry Smith .  dm - the DM context
371c0dedaeaSBarry Smith 
372c0dedaeaSBarry Smith    Output Parameter:
373c0dedaeaSBarry Smith .  ctype - the matrix type
374c0dedaeaSBarry Smith 
375c0dedaeaSBarry Smith    Options Database:
376c0dedaeaSBarry Smith .   -dm_mat_type ctype
377c0dedaeaSBarry Smith 
378c0dedaeaSBarry Smith    Level: intermediate
379c0dedaeaSBarry Smith 
380a2a9ebe5SBarry Smith .seealso: DMDACreate1d(), DMDACreate2d(), DMDACreate3d(), DMCreateMatrix(), DMSetMatrixPreallocateOnly(), MatType, DMSetMatType(), DMSetMatType(), DMGetMatType()
381c0dedaeaSBarry Smith @*/
382c0dedaeaSBarry Smith PetscErrorCode  DMGetMatType(DM dm,MatType *ctype)
383c0dedaeaSBarry Smith {
384c0dedaeaSBarry Smith   PetscFunctionBegin;
385c0dedaeaSBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
386c0dedaeaSBarry Smith   *ctype = dm->mattype;
387c0dedaeaSBarry Smith   PetscFunctionReturn(0);
388c0dedaeaSBarry Smith }
389c0dedaeaSBarry Smith 
390c688c046SMatthew G Knepley /*@
39134f98d34SBarry Smith   MatGetDM - Gets the DM defining the data layout of the matrix
392c688c046SMatthew G Knepley 
393c688c046SMatthew G Knepley   Not collective
394c688c046SMatthew G Knepley 
395c688c046SMatthew G Knepley   Input Parameter:
396c688c046SMatthew G Knepley . A - The Mat
397c688c046SMatthew G Knepley 
398c688c046SMatthew G Knepley   Output Parameter:
399c688c046SMatthew G Knepley . dm - The DM
400c688c046SMatthew G Knepley 
401c688c046SMatthew G Knepley   Level: intermediate
402c688c046SMatthew G Knepley 
4038f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4048f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4058f1509bcSBarry Smith 
406c688c046SMatthew G Knepley .seealso: MatSetDM(), DMCreateMatrix(), DMSetMatType()
407c688c046SMatthew G Knepley @*/
408c688c046SMatthew G Knepley PetscErrorCode MatGetDM(Mat A, DM *dm)
409c688c046SMatthew G Knepley {
410c688c046SMatthew G Knepley   PetscErrorCode ierr;
411c688c046SMatthew G Knepley 
412c688c046SMatthew G Knepley   PetscFunctionBegin;
413c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
414c688c046SMatthew G Knepley   PetscValidPointer(dm,2);
415c688c046SMatthew G Knepley   ierr = PetscObjectQuery((PetscObject) A, "__PETSc_dm", (PetscObject*) dm);CHKERRQ(ierr);
416c688c046SMatthew G Knepley   PetscFunctionReturn(0);
417c688c046SMatthew G Knepley }
418c688c046SMatthew G Knepley 
419c688c046SMatthew G Knepley /*@
420c688c046SMatthew G Knepley   MatSetDM - Sets the DM defining the data layout of the matrix
421c688c046SMatthew G Knepley 
422c688c046SMatthew G Knepley   Not collective
423c688c046SMatthew G Knepley 
424c688c046SMatthew G Knepley   Input Parameters:
425c688c046SMatthew G Knepley + A - The Mat
426c688c046SMatthew G Knepley - dm - The DM
427c688c046SMatthew G Knepley 
428c688c046SMatthew G Knepley   Level: intermediate
429c688c046SMatthew G Knepley 
4308f1509bcSBarry Smith   Developer Note: Since the Mat class doesn't know about the DM class the DM object is associated with
4318f1509bcSBarry Smith                   the Mat through a PetscObjectCompose() operation
4328f1509bcSBarry Smith 
4338f1509bcSBarry Smith 
434c688c046SMatthew G Knepley .seealso: MatGetDM(), DMCreateMatrix(), DMSetMatType()
435c688c046SMatthew G Knepley @*/
436c688c046SMatthew G Knepley PetscErrorCode MatSetDM(Mat A, DM dm)
437c688c046SMatthew G Knepley {
438c688c046SMatthew G Knepley   PetscErrorCode ierr;
439c688c046SMatthew G Knepley 
440c688c046SMatthew G Knepley   PetscFunctionBegin;
441c688c046SMatthew G Knepley   PetscValidHeaderSpecific(A,MAT_CLASSID,1);
4428865f1eaSKarl Rupp   if (dm) PetscValidHeaderSpecific(dm,DM_CLASSID,2);
443c688c046SMatthew G Knepley   ierr = PetscObjectCompose((PetscObject) A, "__PETSc_dm", (PetscObject) dm);CHKERRQ(ierr);
444c688c046SMatthew G Knepley   PetscFunctionReturn(0);
445c688c046SMatthew G Knepley }
446c688c046SMatthew G Knepley 
4479a42bb27SBarry Smith /*@C
4489a42bb27SBarry Smith    DMSetOptionsPrefix - Sets the prefix used for searching for all
4496757b960SDave May    DM options in the database.
4509a42bb27SBarry Smith 
451d083f849SBarry Smith    Logically Collective on dm
4529a42bb27SBarry Smith 
4539a42bb27SBarry Smith    Input Parameter:
4548353ddbbSDave May +  da - the DM context
4559a42bb27SBarry Smith -  prefix - the prefix to prepend to all option names
4569a42bb27SBarry Smith 
4579a42bb27SBarry Smith    Notes:
4589a42bb27SBarry Smith    A hyphen (-) must NOT be given at the beginning of the prefix name.
4599a42bb27SBarry Smith    The first character of all runtime options is AUTOMATICALLY the hyphen.
4609a42bb27SBarry Smith 
4619a42bb27SBarry Smith    Level: advanced
4629a42bb27SBarry Smith 
4639a42bb27SBarry Smith .seealso: DMSetFromOptions()
4649a42bb27SBarry Smith @*/
4657087cfbeSBarry Smith PetscErrorCode  DMSetOptionsPrefix(DM dm,const char prefix[])
4669a42bb27SBarry Smith {
4679a42bb27SBarry Smith   PetscErrorCode ierr;
4689a42bb27SBarry Smith 
4699a42bb27SBarry Smith   PetscFunctionBegin;
4709a42bb27SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
4719a42bb27SBarry Smith   ierr = PetscObjectSetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
472691be533SLawrence Mitchell   if (dm->sf) {
473691be533SLawrence Mitchell     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sf,prefix);CHKERRQ(ierr);
474691be533SLawrence Mitchell   }
4751bb6d2a8SBarry Smith   if (dm->sectionSF) {
4761bb6d2a8SBarry Smith     ierr = PetscObjectSetOptionsPrefix((PetscObject)dm->sectionSF,prefix);CHKERRQ(ierr);
477691be533SLawrence Mitchell   }
4789a42bb27SBarry Smith   PetscFunctionReturn(0);
4799a42bb27SBarry Smith }
4809a42bb27SBarry Smith 
48131697293SDave May /*@C
48231697293SDave May    DMAppendOptionsPrefix - Appends to the prefix used for searching for all
48331697293SDave May    DM options in the database.
48431697293SDave May 
485d083f849SBarry Smith    Logically Collective on dm
48631697293SDave May 
48731697293SDave May    Input Parameters:
48831697293SDave May +  dm - the DM context
48931697293SDave May -  prefix - the prefix string to prepend to all DM option requests
49031697293SDave May 
49131697293SDave May    Notes:
49231697293SDave May    A hyphen (-) must NOT be given at the beginning of the prefix name.
49331697293SDave May    The first character of all runtime options is AUTOMATICALLY the hyphen.
49431697293SDave May 
49531697293SDave May    Level: advanced
49631697293SDave May 
49731697293SDave May .seealso: DMSetOptionsPrefix(), DMGetOptionsPrefix()
49831697293SDave May @*/
49931697293SDave May PetscErrorCode  DMAppendOptionsPrefix(DM dm,const char prefix[])
50031697293SDave May {
50131697293SDave May   PetscErrorCode ierr;
50231697293SDave May 
50331697293SDave May   PetscFunctionBegin;
50431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
50531697293SDave May   ierr = PetscObjectAppendOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
50631697293SDave May   PetscFunctionReturn(0);
50731697293SDave May }
50831697293SDave May 
50931697293SDave May /*@C
51031697293SDave May    DMGetOptionsPrefix - Gets the prefix used for searching for all
51131697293SDave May    DM options in the database.
51231697293SDave May 
51331697293SDave May    Not Collective
51431697293SDave May 
51531697293SDave May    Input Parameters:
51631697293SDave May .  dm - the DM context
51731697293SDave May 
51831697293SDave May    Output Parameters:
51931697293SDave May .  prefix - pointer to the prefix string used is returned
52031697293SDave May 
52195452b02SPatrick Sanan    Notes:
52295452b02SPatrick Sanan     On the fortran side, the user should pass in a string 'prefix' of
52331697293SDave May    sufficient length to hold the prefix.
52431697293SDave May 
52531697293SDave May    Level: advanced
52631697293SDave May 
52731697293SDave May .seealso: DMSetOptionsPrefix(), DMAppendOptionsPrefix()
52831697293SDave May @*/
52931697293SDave May PetscErrorCode  DMGetOptionsPrefix(DM dm,const char *prefix[])
53031697293SDave May {
53131697293SDave May   PetscErrorCode ierr;
53231697293SDave May 
53331697293SDave May   PetscFunctionBegin;
53431697293SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
53531697293SDave May   ierr = PetscObjectGetOptionsPrefix((PetscObject)dm,prefix);CHKERRQ(ierr);
53631697293SDave May   PetscFunctionReturn(0);
53731697293SDave May }
53831697293SDave May 
53988bdff64SToby Isaac static PetscErrorCode DMCountNonCyclicReferences(DM dm, PetscBool recurseCoarse, PetscBool recurseFine, PetscInt *ncrefct)
54088bdff64SToby Isaac {
5416eb26441SStefano Zampini   PetscInt       refct = ((PetscObject) dm)->refct;
54288bdff64SToby Isaac   PetscErrorCode ierr;
54388bdff64SToby Isaac 
54488bdff64SToby Isaac   PetscFunctionBegin;
545aab5bcd8SJed Brown   *ncrefct = 0;
54688bdff64SToby Isaac   if (dm->coarseMesh && dm->coarseMesh->fineMesh == dm) {
54788bdff64SToby Isaac     refct--;
54888bdff64SToby Isaac     if (recurseCoarse) {
54988bdff64SToby Isaac       PetscInt coarseCount;
55088bdff64SToby Isaac 
55188bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->coarseMesh, PETSC_TRUE, PETSC_FALSE,&coarseCount);CHKERRQ(ierr);
55288bdff64SToby Isaac       refct += coarseCount;
55388bdff64SToby Isaac     }
55488bdff64SToby Isaac   }
55588bdff64SToby Isaac   if (dm->fineMesh && dm->fineMesh->coarseMesh == dm) {
55688bdff64SToby Isaac     refct--;
55788bdff64SToby Isaac     if (recurseFine) {
55888bdff64SToby Isaac       PetscInt fineCount;
55988bdff64SToby Isaac 
56088bdff64SToby Isaac       ierr = DMCountNonCyclicReferences(dm->fineMesh, PETSC_FALSE, PETSC_TRUE,&fineCount);CHKERRQ(ierr);
56188bdff64SToby Isaac       refct += fineCount;
56288bdff64SToby Isaac     }
56388bdff64SToby Isaac   }
56488bdff64SToby Isaac   *ncrefct = refct;
56588bdff64SToby Isaac   PetscFunctionReturn(0);
56688bdff64SToby Isaac }
56788bdff64SToby Isaac 
568f4cdcedcSVaclav Hapla PetscErrorCode DMDestroyLabelLinkList_Internal(DM dm)
569354557abSToby Isaac {
5705d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
571354557abSToby Isaac   PetscErrorCode ierr;
572354557abSToby Isaac 
573354557abSToby Isaac   PetscFunctionBegin;
574354557abSToby Isaac   /* destroy the labels */
575354557abSToby Isaac   while (next) {
576354557abSToby Isaac     DMLabelLink tmp = next->next;
577354557abSToby Isaac 
5785d80c0bfSVaclav Hapla     if (next->label == dm->depthLabel)    dm->depthLabel    = NULL;
579ba2698f1SMatthew G. Knepley     if (next->label == dm->celltypeLabel) dm->celltypeLabel = NULL;
580354557abSToby Isaac     ierr = DMLabelDestroy(&next->label);CHKERRQ(ierr);
581354557abSToby Isaac     ierr = PetscFree(next);CHKERRQ(ierr);
582354557abSToby Isaac     next = tmp;
583354557abSToby Isaac   }
5845d80c0bfSVaclav Hapla   dm->labels = NULL;
585354557abSToby Isaac   PetscFunctionReturn(0);
586354557abSToby Isaac }
587354557abSToby Isaac 
58847c6ae99SBarry Smith /*@
5898472ad0fSDave May     DMDestroy - Destroys a vector packer or DM.
59047c6ae99SBarry Smith 
591d083f849SBarry Smith     Collective on dm
59247c6ae99SBarry Smith 
59347c6ae99SBarry Smith     Input Parameter:
59447c6ae99SBarry Smith .   dm - the DM object to destroy
59547c6ae99SBarry Smith 
59647c6ae99SBarry Smith     Level: developer
59747c6ae99SBarry Smith 
598e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
59947c6ae99SBarry Smith 
60047c6ae99SBarry Smith @*/
601fcfd50ebSBarry Smith PetscErrorCode  DMDestroy(DM *dm)
60247c6ae99SBarry Smith {
6036eb26441SStefano Zampini   PetscInt       cnt;
604dfe15315SJed Brown   DMNamedVecLink nlink,nnext;
60547c6ae99SBarry Smith   PetscErrorCode ierr;
60647c6ae99SBarry Smith 
60747c6ae99SBarry Smith   PetscFunctionBegin;
6086bf464f9SBarry Smith   if (!*dm) PetscFunctionReturn(0);
6096bf464f9SBarry Smith   PetscValidHeaderSpecific((*dm),DM_CLASSID,1);
61087e657c6SBarry Smith 
61188bdff64SToby Isaac   /* count all non-cyclic references in the doubly-linked list of coarse<->fine meshes */
61288bdff64SToby Isaac   ierr = DMCountNonCyclicReferences(*dm,PETSC_TRUE,PETSC_TRUE,&cnt);CHKERRQ(ierr);
61388bdff64SToby Isaac   --((PetscObject)(*dm))->refct;
614ea78f98cSLisandro Dalcin   if (--cnt > 0) {*dm = NULL; PetscFunctionReturn(0);}
6156bf464f9SBarry Smith   if (((PetscObject)(*dm))->refct < 0) PetscFunctionReturn(0);
6166bf464f9SBarry Smith   ((PetscObject)(*dm))->refct = 0;
6176eb26441SStefano Zampini 
6186eb26441SStefano Zampini   ierr = DMClearGlobalVectors(*dm);CHKERRQ(ierr);
6196eb26441SStefano Zampini   ierr = DMClearLocalVectors(*dm);CHKERRQ(ierr);
6206eb26441SStefano Zampini 
621f490541aSPeter Brune   nnext=(*dm)->namedglobal;
6220298fd71SBarry Smith   (*dm)->namedglobal = NULL;
623f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named vectors */
6242348bcf4SPeter Brune     nnext = nlink->next;
6252348bcf4SPeter 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);
6262348bcf4SPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
6272348bcf4SPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
6282348bcf4SPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
6292348bcf4SPeter Brune   }
630f490541aSPeter Brune   nnext=(*dm)->namedlocal;
6310298fd71SBarry Smith   (*dm)->namedlocal = NULL;
632f490541aSPeter Brune   for (nlink=nnext; nlink; nlink=nnext) { /* Destroy the named local vectors */
633f490541aSPeter Brune     nnext = nlink->next;
634f490541aSPeter 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);
635f490541aSPeter Brune     ierr = PetscFree(nlink->name);CHKERRQ(ierr);
636f490541aSPeter Brune     ierr = VecDestroy(&nlink->X);CHKERRQ(ierr);
637f490541aSPeter Brune     ierr = PetscFree(nlink);CHKERRQ(ierr);
638f490541aSPeter Brune   }
6392348bcf4SPeter Brune 
640b17ce1afSJed Brown   /* Destroy the list of hooks */
641c833c3b5SJed Brown   {
642c833c3b5SJed Brown     DMCoarsenHookLink link,next;
643b17ce1afSJed Brown     for (link=(*dm)->coarsenhook; link; link=next) {
644b17ce1afSJed Brown       next = link->next;
645b17ce1afSJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
646b17ce1afSJed Brown     }
6470298fd71SBarry Smith     (*dm)->coarsenhook = NULL;
648c833c3b5SJed Brown   }
649c833c3b5SJed Brown   {
650c833c3b5SJed Brown     DMRefineHookLink link,next;
651c833c3b5SJed Brown     for (link=(*dm)->refinehook; link; link=next) {
652c833c3b5SJed Brown       next = link->next;
653c833c3b5SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
654c833c3b5SJed Brown     }
6550298fd71SBarry Smith     (*dm)->refinehook = NULL;
656c833c3b5SJed Brown   }
657be081cd6SPeter Brune   {
658be081cd6SPeter Brune     DMSubDomainHookLink link,next;
659be081cd6SPeter Brune     for (link=(*dm)->subdomainhook; link; link=next) {
660be081cd6SPeter Brune       next = link->next;
661be081cd6SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
662be081cd6SPeter Brune     }
6630298fd71SBarry Smith     (*dm)->subdomainhook = NULL;
664be081cd6SPeter Brune   }
665baf369e7SPeter Brune   {
666baf369e7SPeter Brune     DMGlobalToLocalHookLink link,next;
667baf369e7SPeter Brune     for (link=(*dm)->gtolhook; link; link=next) {
668baf369e7SPeter Brune       next = link->next;
669baf369e7SPeter Brune       ierr = PetscFree(link);CHKERRQ(ierr);
670baf369e7SPeter Brune     }
6710298fd71SBarry Smith     (*dm)->gtolhook = NULL;
672baf369e7SPeter Brune   }
673d4d07f1eSToby Isaac   {
674d4d07f1eSToby Isaac     DMLocalToGlobalHookLink link,next;
675d4d07f1eSToby Isaac     for (link=(*dm)->ltoghook; link; link=next) {
676d4d07f1eSToby Isaac       next = link->next;
677d4d07f1eSToby Isaac       ierr = PetscFree(link);CHKERRQ(ierr);
678d4d07f1eSToby Isaac     }
679d4d07f1eSToby Isaac     (*dm)->ltoghook = NULL;
680d4d07f1eSToby Isaac   }
681aa1993deSMatthew G Knepley   /* Destroy the work arrays */
682aa1993deSMatthew G Knepley   {
683aa1993deSMatthew G Knepley     DMWorkLink link,next;
684aa1993deSMatthew G Knepley     if ((*dm)->workout) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Work array still checked out");
685aa1993deSMatthew G Knepley     for (link=(*dm)->workin; link; link=next) {
686aa1993deSMatthew G Knepley       next = link->next;
687aa1993deSMatthew G Knepley       ierr = PetscFree(link->mem);CHKERRQ(ierr);
688aa1993deSMatthew G Knepley       ierr = PetscFree(link);CHKERRQ(ierr);
689aa1993deSMatthew G Knepley     }
6900298fd71SBarry Smith     (*dm)->workin = NULL;
691aa1993deSMatthew G Knepley   }
692c58f1c22SToby Isaac   /* destroy the labels */
693f4cdcedcSVaclav Hapla   ierr = DMDestroyLabelLinkList_Internal(*dm);CHKERRQ(ierr);
694f4cdcedcSVaclav Hapla   /* destroy the fields */
695e5e52638SMatthew G. Knepley   ierr = DMClearFields(*dm);CHKERRQ(ierr);
696f4cdcedcSVaclav Hapla   /* destroy the boundaries */
697e6f8dbb6SToby Isaac   {
698e6f8dbb6SToby Isaac     DMBoundary next = (*dm)->boundary;
699e6f8dbb6SToby Isaac     while (next) {
700e6f8dbb6SToby Isaac       DMBoundary b = next;
701e6f8dbb6SToby Isaac 
702e6f8dbb6SToby Isaac       next = b->next;
703e6f8dbb6SToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
704e6f8dbb6SToby Isaac     }
705e6f8dbb6SToby Isaac   }
706b17ce1afSJed Brown 
70752536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmksp);CHKERRQ(ierr);
70852536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmsnes);CHKERRQ(ierr);
70952536dc3SBarry Smith   ierr = PetscObjectDestroy(&(*dm)->dmts);CHKERRQ(ierr);
71052536dc3SBarry Smith 
7111a266240SBarry Smith   if ((*dm)->ctx && (*dm)->ctxdestroy) {
7121a266240SBarry Smith     ierr = (*(*dm)->ctxdestroy)(&(*dm)->ctx);CHKERRQ(ierr);
7131a266240SBarry Smith   }
71471cd77b2SBarry Smith   ierr = MatFDColoringDestroy(&(*dm)->fd);CHKERRQ(ierr);
7156bf464f9SBarry Smith   ierr = ISLocalToGlobalMappingDestroy(&(*dm)->ltogmap);CHKERRQ(ierr);
7166bf464f9SBarry Smith   ierr = PetscFree((*dm)->vectype);CHKERRQ(ierr);
717073dac72SJed Brown   ierr = PetscFree((*dm)->mattype);CHKERRQ(ierr);
71888ed4aceSMatthew G Knepley 
7191bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->localSection);CHKERRQ(ierr);
7201bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&(*dm)->globalSection);CHKERRQ(ierr);
7218b1ab98fSJed Brown   ierr = PetscLayoutDestroy(&(*dm)->map);CHKERRQ(ierr);
722fba222abSToby Isaac   ierr = PetscSectionDestroy(&(*dm)->defaultConstraintSection);CHKERRQ(ierr);
723fba222abSToby Isaac   ierr = MatDestroy(&(*dm)->defaultConstraintMat);CHKERRQ(ierr);
72488ed4aceSMatthew G Knepley   ierr = PetscSFDestroy(&(*dm)->sf);CHKERRQ(ierr);
7251bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&(*dm)->sectionSF);CHKERRQ(ierr);
726736995cdSBlaise Bourdin   if ((*dm)->useNatural) {
727736995cdSBlaise Bourdin     if ((*dm)->sfNatural) {
7288e4ac7eaSMatthew G. Knepley       ierr = PetscSFDestroy(&(*dm)->sfNatural);CHKERRQ(ierr);
729736995cdSBlaise Bourdin     }
730736995cdSBlaise Bourdin     ierr = PetscObjectDereference((PetscObject) (*dm)->sfMigration);CHKERRQ(ierr);
731736995cdSBlaise Bourdin   }
73288bdff64SToby Isaac   if ((*dm)->coarseMesh && (*dm)->coarseMesh->fineMesh == *dm) {
73388bdff64SToby Isaac     ierr = DMSetFineDM((*dm)->coarseMesh,NULL);CHKERRQ(ierr);
73488bdff64SToby Isaac   }
7356eb26441SStefano Zampini 
736a8fb8f29SToby Isaac   ierr = DMDestroy(&(*dm)->coarseMesh);CHKERRQ(ierr);
73788bdff64SToby Isaac   if ((*dm)->fineMesh && (*dm)->fineMesh->coarseMesh == *dm) {
73888bdff64SToby Isaac     ierr = DMSetCoarseDM((*dm)->fineMesh,NULL);CHKERRQ(ierr);
73988bdff64SToby Isaac   }
74088bdff64SToby Isaac   ierr = DMDestroy(&(*dm)->fineMesh);CHKERRQ(ierr);
741f19dbd58SToby Isaac   ierr = DMFieldDestroy(&(*dm)->coordinateField);CHKERRQ(ierr);
7426636e97aSMatthew G Knepley   ierr = DMDestroy(&(*dm)->coordinateDM);CHKERRQ(ierr);
7436636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinates);CHKERRQ(ierr);
7446636e97aSMatthew G Knepley   ierr = VecDestroy(&(*dm)->coordinatesLocal);CHKERRQ(ierr);
745412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->L);CHKERRQ(ierr);
746412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->maxCell);CHKERRQ(ierr);
747412e9a14SMatthew G. Knepley   ierr = PetscFree((*dm)->bdtype);CHKERRQ(ierr);
748ca3d3a14SMatthew G. Knepley   if ((*dm)->transformDestroy) {ierr = (*(*dm)->transformDestroy)(*dm, (*dm)->transformCtx);CHKERRQ(ierr);}
749ca3d3a14SMatthew G. Knepley   ierr = DMDestroy(&(*dm)->transformDM);CHKERRQ(ierr);
750ca3d3a14SMatthew G. Knepley   ierr = VecDestroy(&(*dm)->transform);CHKERRQ(ierr);
7516636e97aSMatthew G Knepley 
752e5e52638SMatthew G. Knepley   ierr = DMClearDS(*dm);CHKERRQ(ierr);
75314f150ffSMatthew G. Knepley   ierr = DMDestroy(&(*dm)->dmBC);CHKERRQ(ierr);
754e04113cfSBarry Smith   /* if memory was published with SAWs then destroy it */
755e04113cfSBarry Smith   ierr = PetscObjectSAWsViewOff((PetscObject)*dm);CHKERRQ(ierr);
756732e2eb9SMatthew G Knepley 
757ed3c66a1SDave May   if ((*dm)->ops->destroy) {
7586bf464f9SBarry Smith     ierr = (*(*dm)->ops->destroy)(*dm);CHKERRQ(ierr);
759ed3c66a1SDave May   }
760c0f0dcc3SMatthew G. Knepley   ierr = DMMonitorCancel(*dm);CHKERRQ(ierr);
761435a35e8SMatthew G Knepley   /* We do not destroy (*dm)->data here so that we can reference count backend objects */
762732e2eb9SMatthew G Knepley   ierr = PetscHeaderDestroy(dm);CHKERRQ(ierr);
76347c6ae99SBarry Smith   PetscFunctionReturn(0);
76447c6ae99SBarry Smith }
76547c6ae99SBarry Smith 
766d7bf68aeSBarry Smith /*@
767d7bf68aeSBarry Smith     DMSetUp - sets up the data structures inside a DM object
768d7bf68aeSBarry Smith 
769d083f849SBarry Smith     Collective on dm
770d7bf68aeSBarry Smith 
771d7bf68aeSBarry Smith     Input Parameter:
772d7bf68aeSBarry Smith .   dm - the DM object to setup
773d7bf68aeSBarry Smith 
774d7bf68aeSBarry Smith     Level: developer
775d7bf68aeSBarry Smith 
776e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
777d7bf68aeSBarry Smith 
778d7bf68aeSBarry Smith @*/
7797087cfbeSBarry Smith PetscErrorCode  DMSetUp(DM dm)
780d7bf68aeSBarry Smith {
781d7bf68aeSBarry Smith   PetscErrorCode ierr;
782d7bf68aeSBarry Smith 
783d7bf68aeSBarry Smith   PetscFunctionBegin;
784171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7858387afaaSJed Brown   if (dm->setupcalled) PetscFunctionReturn(0);
786d7bf68aeSBarry Smith   if (dm->ops->setup) {
787d7bf68aeSBarry Smith     ierr = (*dm->ops->setup)(dm);CHKERRQ(ierr);
788d7bf68aeSBarry Smith   }
7898387afaaSJed Brown   dm->setupcalled = PETSC_TRUE;
790d7bf68aeSBarry Smith   PetscFunctionReturn(0);
791d7bf68aeSBarry Smith }
792d7bf68aeSBarry Smith 
793d7bf68aeSBarry Smith /*@
794d7bf68aeSBarry Smith     DMSetFromOptions - sets parameters in a DM from the options database
795d7bf68aeSBarry Smith 
796d083f849SBarry Smith     Collective on dm
797d7bf68aeSBarry Smith 
798d7bf68aeSBarry Smith     Input Parameter:
799d7bf68aeSBarry Smith .   dm - the DM object to set options for
800d7bf68aeSBarry Smith 
801732e2eb9SMatthew G Knepley     Options Database:
8024164ae61SDominic Meiser +   -dm_preallocate_only - Only preallocate the matrix for DMCreateMatrix(), but do not fill it with zeros
8034164ae61SDominic Meiser .   -dm_vec_type <type>  - type of vector to create inside DM
8044164ae61SDominic Meiser .   -dm_mat_type <type>  - type of matrix to create inside DM
8058f1509bcSBarry Smith -   -dm_is_coloring_type - <global or local>
806732e2eb9SMatthew G Knepley 
807384a6580SVaclav Hapla     DMPLEX Specific Checks
808384a6580SVaclav Hapla +   -dm_plex_check_symmetry        - Check that the adjacency information in the mesh is symmetric - DMPlexCheckSymmetry()
809384a6580SVaclav Hapla .   -dm_plex_check_skeleton        - Check that each cell has the correct number of vertices (only for homogeneous simplex or tensor meshes) - DMPlexCheckSkeleton()
810384a6580SVaclav 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()
811384a6580SVaclav Hapla .   -dm_plex_check_geometry        - Check that cells have positive volume - DMPlexCheckGeometry()
812384a6580SVaclav Hapla .   -dm_plex_check_pointsf         - Check some necessary conditions for PointSF - DMPlexCheckPointSF()
813384a6580SVaclav Hapla .   -dm_plex_check_interface_cones - Check points on inter-partition interfaces have conforming order of cone points - DMPlexCheckInterfaceCones()
814384a6580SVaclav Hapla -   -dm_plex_check_all             - Perform all the checks above
815d7bf68aeSBarry Smith 
81695eb5ee5SVaclav Hapla     Level: intermediate
81795eb5ee5SVaclav Hapla 
81895eb5ee5SVaclav Hapla .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(),
81995eb5ee5SVaclav Hapla     DMPlexCheckSymmetry(), DMPlexCheckSkeleton(), DMPlexCheckFaces(), DMPlexCheckGeometry(), DMPlexCheckPointSF(), DMPlexCheckInterfaceCones()
820d7bf68aeSBarry Smith 
821d7bf68aeSBarry Smith @*/
8227087cfbeSBarry Smith PetscErrorCode DMSetFromOptions(DM dm)
823d7bf68aeSBarry Smith {
8247781c08eSBarry Smith   char           typeName[256];
825ca266f36SBarry Smith   PetscBool      flg;
826d7bf68aeSBarry Smith   PetscErrorCode ierr;
827d7bf68aeSBarry Smith 
828d7bf68aeSBarry Smith   PetscFunctionBegin;
829171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83049be4549SMatthew G. Knepley   dm->setfromoptionscalled = PETSC_TRUE;
83149be4549SMatthew G. Knepley   if (dm->sf) {ierr = PetscSFSetFromOptions(dm->sf);CHKERRQ(ierr);}
8321bb6d2a8SBarry Smith   if (dm->sectionSF) {ierr = PetscSFSetFromOptions(dm->sectionSF);CHKERRQ(ierr);}
8333194b578SJed Brown   ierr = PetscObjectOptionsBegin((PetscObject)dm);CHKERRQ(ierr);
8340298fd71SBarry 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);
835a264d7a6SBarry Smith   ierr = PetscOptionsFList("-dm_vec_type","Vector type used for created vectors","DMSetVecType",VecList,dm->vectype,typeName,256,&flg);CHKERRQ(ierr);
836f9ba7244SBarry Smith   if (flg) {
837f9ba7244SBarry Smith     ierr = DMSetVecType(dm,typeName);CHKERRQ(ierr);
838f9ba7244SBarry Smith   }
839a264d7a6SBarry 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);
840073dac72SJed Brown   if (flg) {
841521d9a4cSLisandro Dalcin     ierr = DMSetMatType(dm,typeName);CHKERRQ(ierr);
842073dac72SJed Brown   }
8438f1509bcSBarry Smith   ierr = PetscOptionsEnum("-dm_is_coloring_type","Global or local coloring of Jacobian","DMSetISColoringType",ISColoringTypes,(PetscEnum)dm->coloringtype,(PetscEnum*)&dm->coloringtype,NULL);CHKERRQ(ierr);
844f9ba7244SBarry Smith   if (dm->ops->setfromoptions) {
845e55864a3SBarry Smith     ierr = (*dm->ops->setfromoptions)(PetscOptionsObject,dm);CHKERRQ(ierr);
846f9ba7244SBarry Smith   }
847f9ba7244SBarry Smith   /* process any options handlers added with PetscObjectAddOptionsHandler() */
8480633abcbSJed Brown   ierr = PetscObjectProcessOptionsHandlers(PetscOptionsObject,(PetscObject) dm);CHKERRQ(ierr);
84982fcb398SMatthew G Knepley   ierr = PetscOptionsEnd();CHKERRQ(ierr);
850d7bf68aeSBarry Smith   PetscFunctionReturn(0);
851d7bf68aeSBarry Smith }
852d7bf68aeSBarry Smith 
853fc9bc008SSatish Balay /*@C
854fe2efc57SMark    DMViewFromOptions - View from Options
855fe2efc57SMark 
856fe2efc57SMark    Collective on DM
857fe2efc57SMark 
858fe2efc57SMark    Input Parameters:
859fe2efc57SMark +  dm - the DM object
860736c3998SJose E. Roman .  obj - Optional object
861736c3998SJose E. Roman -  name - command line option
862fe2efc57SMark 
863fe2efc57SMark    Level: intermediate
864fe2efc57SMark .seealso:  DM, DMView, PetscObjectViewFromOptions(), DMCreate()
865fe2efc57SMark @*/
866fe2efc57SMark PetscErrorCode  DMViewFromOptions(DM dm,PetscObject obj,const char name[])
867fe2efc57SMark {
868fe2efc57SMark   PetscErrorCode ierr;
869fe2efc57SMark 
870fe2efc57SMark   PetscFunctionBegin;
871fe2efc57SMark   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
872fe2efc57SMark   ierr = PetscObjectViewFromOptions((PetscObject)dm,obj,name);CHKERRQ(ierr);
873fe2efc57SMark   PetscFunctionReturn(0);
874fe2efc57SMark }
875fe2efc57SMark 
876fe2efc57SMark /*@C
877224748a4SBarry Smith     DMView - Views a DM
87847c6ae99SBarry Smith 
879d083f849SBarry Smith     Collective on dm
88047c6ae99SBarry Smith 
88147c6ae99SBarry Smith     Input Parameter:
88247c6ae99SBarry Smith +   dm - the DM object to view
88347c6ae99SBarry Smith -   v - the viewer
88447c6ae99SBarry Smith 
885224748a4SBarry Smith     Level: beginner
88647c6ae99SBarry Smith 
887e727c939SJed Brown .seealso DMDestroy(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
88847c6ae99SBarry Smith 
88947c6ae99SBarry Smith @*/
8907087cfbeSBarry Smith PetscErrorCode  DMView(DM dm,PetscViewer v)
89147c6ae99SBarry Smith {
89247c6ae99SBarry Smith   PetscErrorCode    ierr;
89332c0f0efSBarry Smith   PetscBool         isbinary;
89476a8abe0SBarry Smith   PetscMPIInt       size;
89576a8abe0SBarry Smith   PetscViewerFormat format;
89647c6ae99SBarry Smith 
89747c6ae99SBarry Smith   PetscFunctionBegin;
898171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8993014e516SBarry Smith   if (!v) {
900ce94432eSBarry Smith     ierr = PetscViewerASCIIGetStdout(PetscObjectComm((PetscObject)dm),&v);CHKERRQ(ierr);
9013014e516SBarry Smith   }
902b1b135c8SBarry Smith   PetscValidHeaderSpecific(v,PETSC_VIEWER_CLASSID,2);
90374903a4fSStefano Zampini   /* Ideally, we would like to have this test on.
90474903a4fSStefano Zampini      However, it currently breaks socket viz via GLVis.
90574903a4fSStefano Zampini      During DMView(parallel_mesh,glvis_viewer), each
90674903a4fSStefano Zampini      process opens a sequential ASCII socket to visualize
90774903a4fSStefano Zampini      the local mesh, and PetscObjectView(dm,local_socket)
90874903a4fSStefano Zampini      is internally called inside VecView_GLVis, incurring
90974903a4fSStefano Zampini      in an error here */
91074903a4fSStefano Zampini   /* PetscCheckSameComm(dm,1,v,2); */
9118bfd1ab9SVaclav Hapla   ierr = PetscViewerCheckWritable(v);CHKERRQ(ierr);
912b1b135c8SBarry Smith 
91376a8abe0SBarry Smith   ierr = PetscViewerGetFormat(v,&format);CHKERRQ(ierr);
91476a8abe0SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject)dm),&size);CHKERRQ(ierr);
91576a8abe0SBarry Smith   if (size == 1 && format == PETSC_VIEWER_LOAD_BALANCE) PetscFunctionReturn(0);
91698c3331eSBarry Smith   ierr = PetscObjectPrintClassNamePrefixType((PetscObject)dm,v);CHKERRQ(ierr);
91732c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)v,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
91832c0f0efSBarry Smith   if (isbinary) {
91955849f57SBarry Smith     PetscInt classid = DM_FILE_CLASSID;
92032c0f0efSBarry Smith     char     type[256];
92132c0f0efSBarry Smith 
922f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,&classid,1,PETSC_INT);CHKERRQ(ierr);
92332c0f0efSBarry Smith     ierr = PetscStrncpy(type,((PetscObject)dm)->type_name,256);CHKERRQ(ierr);
924f253e43cSLisandro Dalcin     ierr = PetscViewerBinaryWrite(v,type,256,PETSC_CHAR);CHKERRQ(ierr);
92532c0f0efSBarry Smith   }
9260c010503SBarry Smith   if (dm->ops->view) {
9270c010503SBarry Smith     ierr = (*dm->ops->view)(dm,v);CHKERRQ(ierr);
92847c6ae99SBarry Smith   }
92947c6ae99SBarry Smith   PetscFunctionReturn(0);
93047c6ae99SBarry Smith }
93147c6ae99SBarry Smith 
93247c6ae99SBarry Smith /*@
9338472ad0fSDave May     DMCreateGlobalVector - Creates a global vector from a DM object
93447c6ae99SBarry Smith 
935d083f849SBarry Smith     Collective on dm
93647c6ae99SBarry Smith 
93747c6ae99SBarry Smith     Input Parameter:
93847c6ae99SBarry Smith .   dm - the DM object
93947c6ae99SBarry Smith 
94047c6ae99SBarry Smith     Output Parameter:
94147c6ae99SBarry Smith .   vec - the global vector
94247c6ae99SBarry Smith 
943073dac72SJed Brown     Level: beginner
94447c6ae99SBarry Smith 
945e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
94647c6ae99SBarry Smith 
94747c6ae99SBarry Smith @*/
9487087cfbeSBarry Smith PetscErrorCode  DMCreateGlobalVector(DM dm,Vec *vec)
94947c6ae99SBarry Smith {
95047c6ae99SBarry Smith   PetscErrorCode ierr;
95147c6ae99SBarry Smith 
95247c6ae99SBarry Smith   PetscFunctionBegin;
953171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
954b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
955b9d85ea2SLisandro Dalcin   if (!dm->ops->createglobalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateGlobalVector",((PetscObject)dm)->type_name);
95647c6ae99SBarry Smith   ierr = (*dm->ops->createglobalvector)(dm,vec);CHKERRQ(ierr);
95776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
958c6b011d8SStefano Zampini     DM vdm;
959c6b011d8SStefano Zampini 
960c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
961c6b011d8SStefano 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);
962c6b011d8SStefano Zampini   }
96347c6ae99SBarry Smith   PetscFunctionReturn(0);
96447c6ae99SBarry Smith }
96547c6ae99SBarry Smith 
96647c6ae99SBarry Smith /*@
9678472ad0fSDave May     DMCreateLocalVector - Creates a local vector from a DM object
96847c6ae99SBarry Smith 
96947c6ae99SBarry Smith     Not Collective
97047c6ae99SBarry Smith 
97147c6ae99SBarry Smith     Input Parameter:
97247c6ae99SBarry Smith .   dm - the DM object
97347c6ae99SBarry Smith 
97447c6ae99SBarry Smith     Output Parameter:
97547c6ae99SBarry Smith .   vec - the local vector
97647c6ae99SBarry Smith 
977073dac72SJed Brown     Level: beginner
97847c6ae99SBarry Smith 
979e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
98047c6ae99SBarry Smith 
98147c6ae99SBarry Smith @*/
9827087cfbeSBarry Smith PetscErrorCode  DMCreateLocalVector(DM dm,Vec *vec)
98347c6ae99SBarry Smith {
98447c6ae99SBarry Smith   PetscErrorCode ierr;
98547c6ae99SBarry Smith 
98647c6ae99SBarry Smith   PetscFunctionBegin;
987171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
988b9d85ea2SLisandro Dalcin   PetscValidPointer(vec,2);
989b9d85ea2SLisandro Dalcin   if (!dm->ops->createlocalvector) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateLocalVector",((PetscObject)dm)->type_name);
99047c6ae99SBarry Smith   ierr = (*dm->ops->createlocalvector)(dm,vec);CHKERRQ(ierr);
99176bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
992c6b011d8SStefano Zampini     DM vdm;
993c6b011d8SStefano Zampini 
994c6b011d8SStefano Zampini     ierr = VecGetDM(*vec,&vdm);CHKERRQ(ierr);
995c6b011d8SStefano 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);
996c6b011d8SStefano Zampini   }
99747c6ae99SBarry Smith   PetscFunctionReturn(0);
99847c6ae99SBarry Smith }
99947c6ae99SBarry Smith 
10001411c6eeSJed Brown /*@
10011411c6eeSJed Brown    DMGetLocalToGlobalMapping - Accesses the local-to-global mapping in a DM.
10021411c6eeSJed Brown 
1003d083f849SBarry Smith    Collective on dm
10041411c6eeSJed Brown 
10051411c6eeSJed Brown    Input Parameter:
10061411c6eeSJed Brown .  dm - the DM that provides the mapping
10071411c6eeSJed Brown 
10081411c6eeSJed Brown    Output Parameter:
10091411c6eeSJed Brown .  ltog - the mapping
10101411c6eeSJed Brown 
10111411c6eeSJed Brown    Level: intermediate
10121411c6eeSJed Brown 
10131411c6eeSJed Brown    Notes:
10141411c6eeSJed Brown    This mapping can then be used by VecSetLocalToGlobalMapping() or
10151411c6eeSJed Brown    MatSetLocalToGlobalMapping().
10161411c6eeSJed Brown 
1017fc31e74dSBarry Smith .seealso: DMCreateLocalVector()
10181411c6eeSJed Brown @*/
10197087cfbeSBarry Smith PetscErrorCode DMGetLocalToGlobalMapping(DM dm,ISLocalToGlobalMapping *ltog)
10201411c6eeSJed Brown {
10210be3e97aSMatthew G. Knepley   PetscInt       bs = -1, bsLocal[2], bsMinMax[2];
10221411c6eeSJed Brown   PetscErrorCode ierr;
10231411c6eeSJed Brown 
10241411c6eeSJed Brown   PetscFunctionBegin;
10251411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
10261411c6eeSJed Brown   PetscValidPointer(ltog,2);
10271411c6eeSJed Brown   if (!dm->ltogmap) {
102837d0c07bSMatthew G Knepley     PetscSection section, sectionGlobal;
102937d0c07bSMatthew G Knepley 
103092fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
103137d0c07bSMatthew G Knepley     if (section) {
1032a974ec88SMatthew G. Knepley       const PetscInt *cdofs;
103337d0c07bSMatthew G Knepley       PetscInt       *ltog;
1034ccf3bd66SMatthew G. Knepley       PetscInt        pStart, pEnd, n, p, k, l;
103537d0c07bSMatthew G Knepley 
1036e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
103737d0c07bSMatthew G Knepley       ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
1038ccf3bd66SMatthew G. Knepley       ierr = PetscSectionGetStorageSize(section, &n);CHKERRQ(ierr);
1039ccf3bd66SMatthew G. Knepley       ierr = PetscMalloc1(n, &ltog);CHKERRQ(ierr); /* We want the local+overlap size */
104037d0c07bSMatthew G Knepley       for (p = pStart, l = 0; p < pEnd; ++p) {
1041a974ec88SMatthew G. Knepley         PetscInt bdof, cdof, dof, off, c, cind = 0;
104237d0c07bSMatthew G Knepley 
104337d0c07bSMatthew G Knepley         /* Should probably use constrained dofs */
104437d0c07bSMatthew G Knepley         ierr = PetscSectionGetDof(section, p, &dof);CHKERRQ(ierr);
1045a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(section, p, &cdof);CHKERRQ(ierr);
1046a974ec88SMatthew G. Knepley         ierr = PetscSectionGetConstraintIndices(section, p, &cdofs);CHKERRQ(ierr);
104737d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &off);CHKERRQ(ierr);
10481a7dc684SMatthew G. Knepley         /* If you have dofs, and constraints, and they are unequal, we set the blocksize to 1 */
10491a7dc684SMatthew G. Knepley         bdof = cdof && (dof-cdof) ? 1 : dof;
10501a7dc684SMatthew G. Knepley         if (dof) {
1051b190aa46SMatthew G. Knepley           if (bs < 0)          {bs = bdof;}
1052b190aa46SMatthew G. Knepley           else if (bs != bdof) {bs = 1;}
10531a7dc684SMatthew G. Knepley         }
105437d0c07bSMatthew G Knepley         for (c = 0; c < dof; ++c, ++l) {
1055a974ec88SMatthew G. Knepley           if ((cind < cdof) && (c == cdofs[cind])) ltog[l] = off < 0 ? off-c : off+c;
1056a974ec88SMatthew G. Knepley           else                                     ltog[l] = (off < 0 ? -(off+1) : off) + c;
105737d0c07bSMatthew G Knepley         }
105837d0c07bSMatthew G Knepley       }
1059bff27382SMatthew G. Knepley       /* Must have same blocksize on all procs (some might have no points) */
10600be3e97aSMatthew G. Knepley       bsLocal[0] = bs < 0 ? PETSC_MAX_INT : bs; bsLocal[1] = bs;
10610be3e97aSMatthew G. Knepley       ierr = PetscGlobalMinMaxInt(PetscObjectComm((PetscObject) dm), bsLocal, bsMinMax);CHKERRQ(ierr);
10620be3e97aSMatthew G. Knepley       if (bsMinMax[0] != bsMinMax[1]) {bs = 1;}
10630be3e97aSMatthew G. Knepley       else                            {bs = bsMinMax[0];}
10647591dbb2SMatthew G. Knepley       bs = bs < 0 ? 1 : bs;
10657591dbb2SMatthew G. Knepley       /* Must reduce indices by blocksize */
1066ccf3bd66SMatthew G. Knepley       if (bs > 1) {
1067ccf3bd66SMatthew G. Knepley         for (l = 0, k = 0; l < n; l += bs, ++k) ltog[k] = ltog[l]/bs;
1068ccf3bd66SMatthew G. Knepley         n /= bs;
1069ccf3bd66SMatthew G. Knepley       }
1070ccf3bd66SMatthew G. Knepley       ierr = ISLocalToGlobalMappingCreate(PetscObjectComm((PetscObject)dm), bs, n, ltog, PETSC_OWN_POINTER, &dm->ltogmap);CHKERRQ(ierr);
10713bb1ff40SBarry Smith       ierr = PetscLogObjectParent((PetscObject)dm, (PetscObject)dm->ltogmap);CHKERRQ(ierr);
107237d0c07bSMatthew G Knepley     } else {
1073b9d85ea2SLisandro Dalcin       if (!dm->ops->getlocaltoglobalmapping) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetLocalToGlobalMapping",((PetscObject)dm)->type_name);
1074184d77edSJed Brown       ierr = (*dm->ops->getlocaltoglobalmapping)(dm);CHKERRQ(ierr);
10751411c6eeSJed Brown     }
107637d0c07bSMatthew G Knepley   }
10771411c6eeSJed Brown   *ltog = dm->ltogmap;
10781411c6eeSJed Brown   PetscFunctionReturn(0);
10791411c6eeSJed Brown }
10801411c6eeSJed Brown 
10811411c6eeSJed Brown /*@
10821411c6eeSJed Brown    DMGetBlockSize - Gets the inherent block size associated with a DM
10831411c6eeSJed Brown 
10841411c6eeSJed Brown    Not Collective
10851411c6eeSJed Brown 
10861411c6eeSJed Brown    Input Parameter:
10871411c6eeSJed Brown .  dm - the DM with block structure
10881411c6eeSJed Brown 
10891411c6eeSJed Brown    Output Parameter:
10901411c6eeSJed Brown .  bs - the block size, 1 implies no exploitable block structure
10911411c6eeSJed Brown 
10921411c6eeSJed Brown    Level: intermediate
10931411c6eeSJed Brown 
1094fc31e74dSBarry Smith .seealso: ISCreateBlock(), VecSetBlockSize(), MatSetBlockSize(), DMGetLocalToGlobalMapping()
10951411c6eeSJed Brown @*/
10967087cfbeSBarry Smith PetscErrorCode  DMGetBlockSize(DM dm,PetscInt *bs)
10971411c6eeSJed Brown {
10981411c6eeSJed Brown   PetscFunctionBegin;
10991411c6eeSJed Brown   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1100534a8f05SLisandro Dalcin   PetscValidIntPointer(bs,2);
11011411c6eeSJed 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");
11021411c6eeSJed Brown   *bs = dm->bs;
11031411c6eeSJed Brown   PetscFunctionReturn(0);
11041411c6eeSJed Brown }
11051411c6eeSJed Brown 
110647c6ae99SBarry Smith /*@
11078472ad0fSDave May     DMCreateInterpolation - Gets interpolation matrix between two DM objects
110847c6ae99SBarry Smith 
1109a5bc1bf3SBarry Smith     Collective on dmc
111047c6ae99SBarry Smith 
111147c6ae99SBarry Smith     Input Parameter:
1112a5bc1bf3SBarry Smith +   dmc - the DM object
1113a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
111447c6ae99SBarry Smith 
111547c6ae99SBarry Smith     Output Parameter:
111647c6ae99SBarry Smith +  mat - the interpolation
111747c6ae99SBarry Smith -  vec - the scaling (optional)
111847c6ae99SBarry Smith 
111947c6ae99SBarry Smith     Level: developer
112047c6ae99SBarry Smith 
112195452b02SPatrick Sanan     Notes:
112295452b02SPatrick 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
112385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
1124d52bd9f3SBarry Smith 
11251f588964SMatthew G Knepley         For DMDA objects you can use this interpolation (more precisely the interpolation from the DMGetCoordinateDM()) to interpolate the mesh coordinate vectors
1126d52bd9f3SBarry Smith         EXCEPT in the periodic case where it does not make sense since the coordinate vectors are not periodic.
112785afcc9aSBarry Smith 
112885afcc9aSBarry Smith 
11292ed6491fSPatrick Sanan .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolationScale()
113047c6ae99SBarry Smith 
113147c6ae99SBarry Smith @*/
1132a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInterpolation(DM dmc,DM dmf,Mat *mat,Vec *vec)
113347c6ae99SBarry Smith {
113447c6ae99SBarry Smith   PetscErrorCode ierr;
113547c6ae99SBarry Smith 
113647c6ae99SBarry Smith   PetscFunctionBegin;
1137a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1138a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
1139c7d20fa0SStefano Zampini   PetscValidPointer(mat,3);
1140a5bc1bf3SBarry Smith   if (!dmc->ops->createinterpolation) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInterpolation",((PetscObject)dmc)->type_name);
1141a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
1142a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createinterpolation)(dmc,dmf,mat,vec);CHKERRQ(ierr);
1143a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInterpolation,dmc,dmf,0,0);CHKERRQ(ierr);
114447c6ae99SBarry Smith   PetscFunctionReturn(0);
114547c6ae99SBarry Smith }
114647c6ae99SBarry Smith 
11473ad4599aSBarry Smith /*@
1148d3e313eaSPatrick Sanan     DMCreateInterpolationScale - Forms L = 1/(R*1) such that diag(L)*R preserves scale and is thus suitable for state (versus residual) restriction.
11492ed6491fSPatrick Sanan 
11502ed6491fSPatrick Sanan   Input Parameters:
11512ed6491fSPatrick Sanan +      dac - DM that defines a coarse mesh
11522ed6491fSPatrick Sanan .      daf - DM that defines a fine mesh
11532ed6491fSPatrick Sanan -      mat - the restriction (or interpolation operator) from fine to coarse
11542ed6491fSPatrick Sanan 
11552ed6491fSPatrick Sanan   Output Parameter:
11562ed6491fSPatrick Sanan .    scale - the scaled vector
11572ed6491fSPatrick Sanan 
11582ed6491fSPatrick Sanan   Level: developer
11592ed6491fSPatrick Sanan 
11602ed6491fSPatrick Sanan .seealso: DMCreateInterpolation()
11612ed6491fSPatrick Sanan 
11622ed6491fSPatrick Sanan @*/
11632ed6491fSPatrick Sanan PetscErrorCode  DMCreateInterpolationScale(DM dac,DM daf,Mat mat,Vec *scale)
11642ed6491fSPatrick Sanan {
11652ed6491fSPatrick Sanan   PetscErrorCode ierr;
11662ed6491fSPatrick Sanan   Vec            fine;
11672ed6491fSPatrick Sanan   PetscScalar    one = 1.0;
11682ed6491fSPatrick Sanan 
11692ed6491fSPatrick Sanan   PetscFunctionBegin;
11702ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(daf,&fine);CHKERRQ(ierr);
11712ed6491fSPatrick Sanan   ierr = DMCreateGlobalVector(dac,scale);CHKERRQ(ierr);
11722ed6491fSPatrick Sanan   ierr = VecSet(fine,one);CHKERRQ(ierr);
11732ed6491fSPatrick Sanan   ierr = MatRestrict(mat,fine,*scale);CHKERRQ(ierr);
11742ed6491fSPatrick Sanan   ierr = VecDestroy(&fine);CHKERRQ(ierr);
11752ed6491fSPatrick Sanan   ierr = VecReciprocal(*scale);CHKERRQ(ierr);
11762ed6491fSPatrick Sanan   PetscFunctionReturn(0);
11772ed6491fSPatrick Sanan }
11782ed6491fSPatrick Sanan 
11792ed6491fSPatrick Sanan /*@
11803ad4599aSBarry Smith     DMCreateRestriction - Gets restriction matrix between two DM objects
11813ad4599aSBarry Smith 
1182a5bc1bf3SBarry Smith     Collective on dmc
11833ad4599aSBarry Smith 
11843ad4599aSBarry Smith     Input Parameter:
1185a5bc1bf3SBarry Smith +   dmc - the DM object
1186a5bc1bf3SBarry Smith -   dmf - the second, finer DM object
11873ad4599aSBarry Smith 
11883ad4599aSBarry Smith     Output Parameter:
11893ad4599aSBarry Smith .  mat - the restriction
11903ad4599aSBarry Smith 
11913ad4599aSBarry Smith 
11923ad4599aSBarry Smith     Level: developer
11933ad4599aSBarry Smith 
119495452b02SPatrick Sanan     Notes:
119595452b02SPatrick 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
11963ad4599aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the interpolation.
11973ad4599aSBarry Smith 
11983ad4599aSBarry Smith 
11993ad4599aSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateInterpolation()
12003ad4599aSBarry Smith 
12013ad4599aSBarry Smith @*/
1202a5bc1bf3SBarry Smith PetscErrorCode  DMCreateRestriction(DM dmc,DM dmf,Mat *mat)
12033ad4599aSBarry Smith {
12043ad4599aSBarry Smith   PetscErrorCode ierr;
12053ad4599aSBarry Smith 
12063ad4599aSBarry Smith   PetscFunctionBegin;
1207a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmc,DM_CLASSID,1);
1208a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dmf,DM_CLASSID,2);
12095a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1210a5bc1bf3SBarry Smith   if (!dmc->ops->createrestriction) SETERRQ1(PetscObjectComm((PetscObject)dmc),PETSC_ERR_SUP,"DM type %s does not implement DMCreateRestriction",((PetscObject)dmc)->type_name);
1211a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
1212a5bc1bf3SBarry Smith   ierr = (*dmc->ops->createrestriction)(dmc,dmf,mat);CHKERRQ(ierr);
1213a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateRestriction,dmc,dmf,0,0);CHKERRQ(ierr);
12143ad4599aSBarry Smith   PetscFunctionReturn(0);
12153ad4599aSBarry Smith }
12163ad4599aSBarry Smith 
121747c6ae99SBarry Smith /*@
12188472ad0fSDave May     DMCreateInjection - Gets injection matrix between two DM objects
121947c6ae99SBarry Smith 
1220a5bc1bf3SBarry Smith     Collective on dac
122147c6ae99SBarry Smith 
122247c6ae99SBarry Smith     Input Parameter:
1223a5bc1bf3SBarry Smith +   dac - the DM object
1224a5bc1bf3SBarry Smith -   daf - the second, finer DM object
122547c6ae99SBarry Smith 
122647c6ae99SBarry Smith     Output Parameter:
12276dbf9973SLawrence Mitchell .   mat - the injection
122847c6ae99SBarry Smith 
122947c6ae99SBarry Smith     Level: developer
123047c6ae99SBarry Smith 
123195452b02SPatrick Sanan    Notes:
123295452b02SPatrick 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
123385afcc9aSBarry Smith         DMCoarsen(). The coordinates set into the DMDA are completely ignored in computing the injection.
123485afcc9aSBarry Smith 
1235e727c939SJed Brown .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateColoring(), DMCreateMatrix(), DMCreateInterpolation()
123647c6ae99SBarry Smith 
123747c6ae99SBarry Smith @*/
1238a5bc1bf3SBarry Smith PetscErrorCode  DMCreateInjection(DM dac,DM daf,Mat *mat)
123947c6ae99SBarry Smith {
124047c6ae99SBarry Smith   PetscErrorCode ierr;
124147c6ae99SBarry Smith 
124247c6ae99SBarry Smith   PetscFunctionBegin;
1243a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac,DM_CLASSID,1);
1244a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf,DM_CLASSID,2);
12455a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1246a5bc1bf3SBarry Smith   if (!dac->ops->createinjection) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateInjection",((PetscObject)dac)->type_name);
1247a5bc1bf3SBarry Smith   ierr = PetscLogEventBegin(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
1248a5bc1bf3SBarry Smith   ierr = (*dac->ops->createinjection)(dac,daf,mat);CHKERRQ(ierr);
1249a5bc1bf3SBarry Smith   ierr = PetscLogEventEnd(DM_CreateInjection,dac,daf,0,0);CHKERRQ(ierr);
125047c6ae99SBarry Smith   PetscFunctionReturn(0);
125147c6ae99SBarry Smith }
125247c6ae99SBarry Smith 
1253b412c318SBarry Smith /*@
1254bd041c0cSMatthew G. Knepley   DMCreateMassMatrix - Gets mass matrix between two DM objects, M_ij = \int \phi_i \psi_j
1255bd041c0cSMatthew G. Knepley 
1256a5bc1bf3SBarry Smith   Collective on dac
1257bd041c0cSMatthew G. Knepley 
1258bd041c0cSMatthew G. Knepley   Input Parameter:
1259a5bc1bf3SBarry Smith + dac - the DM object
1260a5bc1bf3SBarry Smith - daf - the second, finer DM object
1261bd041c0cSMatthew G. Knepley 
1262bd041c0cSMatthew G. Knepley   Output Parameter:
1263bd041c0cSMatthew G. Knepley . mat - the interpolation
1264bd041c0cSMatthew G. Knepley 
1265bd041c0cSMatthew G. Knepley   Level: developer
1266bd041c0cSMatthew G. Knepley 
1267bd041c0cSMatthew G. Knepley .seealso DMCreateMatrix(), DMRefine(), DMCoarsen(), DMCreateRestriction(), DMCreateInterpolation(), DMCreateInjection()
1268bd041c0cSMatthew G. Knepley @*/
1269a5bc1bf3SBarry Smith PetscErrorCode DMCreateMassMatrix(DM dac, DM daf, Mat *mat)
1270bd041c0cSMatthew G. Knepley {
1271bd041c0cSMatthew G. Knepley   PetscErrorCode ierr;
1272bd041c0cSMatthew G. Knepley 
1273bd041c0cSMatthew G. Knepley   PetscFunctionBegin;
1274a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dac, DM_CLASSID, 1);
1275a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(daf, DM_CLASSID, 2);
12765a84ad33SLisandro Dalcin   PetscValidPointer(mat,3);
1277a5bc1bf3SBarry Smith   if (!dac->ops->createmassmatrix) SETERRQ1(PetscObjectComm((PetscObject)dac),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMassMatrix",((PetscObject)dac)->type_name);
1278a5bc1bf3SBarry Smith   ierr = (*dac->ops->createmassmatrix)(dac, daf, mat);CHKERRQ(ierr);
1279bd041c0cSMatthew G. Knepley   PetscFunctionReturn(0);
1280bd041c0cSMatthew G. Knepley }
1281bd041c0cSMatthew G. Knepley 
1282bd041c0cSMatthew G. Knepley /*@
1283b412c318SBarry Smith     DMCreateColoring - Gets coloring for a DM
128447c6ae99SBarry Smith 
1285d083f849SBarry Smith     Collective on dm
128647c6ae99SBarry Smith 
128747c6ae99SBarry Smith     Input Parameter:
128847c6ae99SBarry Smith +   dm - the DM object
12895bdb020cSBarry Smith -   ctype - IS_COLORING_LOCAL or IS_COLORING_GLOBAL
129047c6ae99SBarry Smith 
129147c6ae99SBarry Smith     Output Parameter:
129247c6ae99SBarry Smith .   coloring - the coloring
129347c6ae99SBarry Smith 
1294ec5066bdSBarry Smith     Notes:
1295ec5066bdSBarry 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
1296ec5066bdSBarry Smith        matrix comes from. In general using the mesh produces a more optimal coloring (fewer colors).
1297ec5066bdSBarry Smith 
1298ec5066bdSBarry Smith        This produces a coloring with the distance of 2, see MatSetColoringDistance() which can be used for efficiently computing Jacobians with MatFDColoringCreate()
1299ec5066bdSBarry Smith 
130047c6ae99SBarry Smith     Level: developer
130147c6ae99SBarry Smith 
1302ec5066bdSBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateMatrix(), DMSetMatType(), MatColoring, MatFDColoringCreate()
130347c6ae99SBarry Smith 
1304aab9d709SJed Brown @*/
1305b412c318SBarry Smith PetscErrorCode  DMCreateColoring(DM dm,ISColoringType ctype,ISColoring *coloring)
130647c6ae99SBarry Smith {
130747c6ae99SBarry Smith   PetscErrorCode ierr;
130847c6ae99SBarry Smith 
130947c6ae99SBarry Smith   PetscFunctionBegin;
1310171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
13115a84ad33SLisandro Dalcin   PetscValidPointer(coloring,3);
1312b9d85ea2SLisandro Dalcin   if (!dm->ops->getcoloring) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateColoring",((PetscObject)dm)->type_name);
1313b412c318SBarry Smith   ierr = (*dm->ops->getcoloring)(dm,ctype,coloring);CHKERRQ(ierr);
131447c6ae99SBarry Smith   PetscFunctionReturn(0);
131547c6ae99SBarry Smith }
131647c6ae99SBarry Smith 
1317b412c318SBarry Smith /*@
13188472ad0fSDave May     DMCreateMatrix - Gets empty Jacobian for a DM
131947c6ae99SBarry Smith 
1320d083f849SBarry Smith     Collective on dm
132147c6ae99SBarry Smith 
132247c6ae99SBarry Smith     Input Parameter:
1323b412c318SBarry Smith .   dm - the DM object
132447c6ae99SBarry Smith 
132547c6ae99SBarry Smith     Output Parameter:
132647c6ae99SBarry Smith .   mat - the empty Jacobian
132747c6ae99SBarry Smith 
1328073dac72SJed Brown     Level: beginner
132947c6ae99SBarry Smith 
133095452b02SPatrick Sanan     Notes:
133195452b02SPatrick Sanan     This properly preallocates the number of nonzeros in the sparse matrix so you
133294013140SBarry Smith        do not need to do it yourself.
133394013140SBarry Smith 
133494013140SBarry Smith        By default it also sets the nonzero structure and puts in the zero entries. To prevent setting
13357889ec69SBarry Smith        the nonzero pattern call DMSetMatrixPreallocateOnly()
133694013140SBarry Smith 
133794013140SBarry 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
133894013140SBarry Smith        internally by PETSc.
133994013140SBarry Smith 
134094013140SBarry Smith        For structured grid problems, in general it is easiest to use MatSetValuesStencil() or MatSetValuesLocal() to put values into the matrix because MatSetValues() requires
1341aa219208SBarry Smith        the indices for the global numbering for DMDAs which is complicated.
134294013140SBarry Smith 
1343b412c318SBarry Smith .seealso DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMSetMatType()
134447c6ae99SBarry Smith 
1345aab9d709SJed Brown @*/
1346b412c318SBarry Smith PetscErrorCode  DMCreateMatrix(DM dm,Mat *mat)
134747c6ae99SBarry Smith {
134847c6ae99SBarry Smith   PetscErrorCode ierr;
134947c6ae99SBarry Smith 
135047c6ae99SBarry Smith   PetscFunctionBegin;
1351171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1352c7b7c8a4SJed Brown   PetscValidPointer(mat,3);
1353b9d85ea2SLisandro Dalcin   if (!dm->ops->creatematrix) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateMatrix",((PetscObject)dm)->type_name);
13545a84ad33SLisandro Dalcin   ierr = MatInitializePackage();CHKERRQ(ierr);
1355fdc842d1SBarry Smith   ierr = PetscLogEventBegin(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
1356b412c318SBarry Smith   ierr = (*dm->ops->creatematrix)(dm,mat);CHKERRQ(ierr);
135776bd3646SJed Brown   if (PetscDefined(USE_DEBUG)) {
1358c6b011d8SStefano Zampini     DM mdm;
1359c6b011d8SStefano Zampini 
1360c6b011d8SStefano Zampini     ierr = MatGetDM(*mat,&mdm);CHKERRQ(ierr);
1361c6b011d8SStefano 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);
1362c6b011d8SStefano Zampini   }
1363e571a35bSMatthew G. Knepley   /* Handle nullspace and near nullspace */
1364e5e52638SMatthew G. Knepley   if (dm->Nf) {
1365e571a35bSMatthew G. Knepley     MatNullSpace nullSpace;
1366e571a35bSMatthew G. Knepley     PetscInt     Nf;
1367e571a35bSMatthew G. Knepley 
1368e5e52638SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
1369e571a35bSMatthew G. Knepley     if (Nf == 1) {
1370e571a35bSMatthew G. Knepley       if (dm->nullspaceConstructors[0]) {
13718cda7954SMatthew G. Knepley         ierr = (*dm->nullspaceConstructors[0])(dm, 0, 0, &nullSpace);CHKERRQ(ierr);
1372e571a35bSMatthew G. Knepley         ierr = MatSetNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1373e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1374e571a35bSMatthew G. Knepley       }
1375e571a35bSMatthew G. Knepley       if (dm->nearnullspaceConstructors[0]) {
13768cda7954SMatthew G. Knepley         ierr = (*dm->nearnullspaceConstructors[0])(dm, 0, 0, &nullSpace);CHKERRQ(ierr);
1377e571a35bSMatthew G. Knepley         ierr = MatSetNearNullSpace(*mat, nullSpace);CHKERRQ(ierr);
1378e571a35bSMatthew G. Knepley         ierr = MatNullSpaceDestroy(&nullSpace);CHKERRQ(ierr);
1379e571a35bSMatthew G. Knepley       }
1380e571a35bSMatthew G. Knepley     }
1381e571a35bSMatthew G. Knepley   }
1382fdc842d1SBarry Smith   ierr = PetscLogEventEnd(DM_CreateMatrix,0,0,0,0);CHKERRQ(ierr);
138347c6ae99SBarry Smith   PetscFunctionReturn(0);
138447c6ae99SBarry Smith }
138547c6ae99SBarry Smith 
1386732e2eb9SMatthew G Knepley /*@
1387950540a4SJed Brown   DMSetMatrixPreallocateOnly - When DMCreateMatrix() is called the matrix will be properly
1388732e2eb9SMatthew G Knepley     preallocated but the nonzero structure and zero values will not be set.
1389732e2eb9SMatthew G Knepley 
1390d083f849SBarry Smith   Logically Collective on dm
1391732e2eb9SMatthew G Knepley 
1392732e2eb9SMatthew G Knepley   Input Parameter:
1393732e2eb9SMatthew G Knepley + dm - the DM
1394732e2eb9SMatthew G Knepley - only - PETSC_TRUE if only want preallocation
1395732e2eb9SMatthew G Knepley 
1396732e2eb9SMatthew G Knepley   Level: developer
1397b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixStructureOnly()
1398732e2eb9SMatthew G Knepley @*/
1399732e2eb9SMatthew G Knepley PetscErrorCode DMSetMatrixPreallocateOnly(DM dm, PetscBool only)
1400732e2eb9SMatthew G Knepley {
1401732e2eb9SMatthew G Knepley   PetscFunctionBegin;
1402732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1403732e2eb9SMatthew G Knepley   dm->prealloc_only = only;
1404732e2eb9SMatthew G Knepley   PetscFunctionReturn(0);
1405732e2eb9SMatthew G Knepley }
1406732e2eb9SMatthew G Knepley 
1407b06ff27eSHong Zhang /*@
1408b06ff27eSHong Zhang   DMSetMatrixStructureOnly - When DMCreateMatrix() is called, the matrix structure will be created
1409b06ff27eSHong Zhang     but the array for values will not be allocated.
1410b06ff27eSHong Zhang 
1411d083f849SBarry Smith   Logically Collective on dm
1412b06ff27eSHong Zhang 
1413b06ff27eSHong Zhang   Input Parameter:
1414b06ff27eSHong Zhang + dm - the DM
1415b06ff27eSHong Zhang - only - PETSC_TRUE if only want matrix stucture
1416b06ff27eSHong Zhang 
1417b06ff27eSHong Zhang   Level: developer
1418b06ff27eSHong Zhang .seealso DMCreateMatrix(), DMSetMatrixPreallocateOnly()
1419b06ff27eSHong Zhang @*/
1420b06ff27eSHong Zhang PetscErrorCode DMSetMatrixStructureOnly(DM dm, PetscBool only)
1421b06ff27eSHong Zhang {
1422b06ff27eSHong Zhang   PetscFunctionBegin;
1423b06ff27eSHong Zhang   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1424b06ff27eSHong Zhang   dm->structure_only = only;
1425b06ff27eSHong Zhang   PetscFunctionReturn(0);
1426b06ff27eSHong Zhang }
1427b06ff27eSHong Zhang 
1428a89ea682SMatthew G Knepley /*@C
1429aa1993deSMatthew G Knepley   DMGetWorkArray - Gets a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1430a89ea682SMatthew G Knepley 
1431a89ea682SMatthew G Knepley   Not Collective
1432a89ea682SMatthew G Knepley 
1433a89ea682SMatthew G Knepley   Input Parameters:
1434a89ea682SMatthew G Knepley + dm - the DM object
1435aa1993deSMatthew G Knepley . count - The minium size
143669291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT)
1437a89ea682SMatthew G Knepley 
1438a89ea682SMatthew G Knepley   Output Parameter:
1439a89ea682SMatthew G Knepley . array - the work array
1440a89ea682SMatthew G Knepley 
1441a89ea682SMatthew G Knepley   Level: developer
1442a89ea682SMatthew G Knepley 
1443a89ea682SMatthew G Knepley .seealso DMDestroy(), DMCreate()
1444a89ea682SMatthew G Knepley @*/
144569291d52SBarry Smith PetscErrorCode DMGetWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1446a89ea682SMatthew G Knepley {
1447a89ea682SMatthew G Knepley   PetscErrorCode ierr;
1448aa1993deSMatthew G Knepley   DMWorkLink     link;
144969291d52SBarry Smith   PetscMPIInt    dsize;
1450a89ea682SMatthew G Knepley 
1451a89ea682SMatthew G Knepley   PetscFunctionBegin;
1452a89ea682SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1453aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1454aa1993deSMatthew G Knepley   if (dm->workin) {
1455aa1993deSMatthew G Knepley     link       = dm->workin;
1456aa1993deSMatthew G Knepley     dm->workin = dm->workin->next;
1457aa1993deSMatthew G Knepley   } else {
1458b00a9115SJed Brown     ierr = PetscNewLog(dm,&link);CHKERRQ(ierr);
1459a89ea682SMatthew G Knepley   }
146069291d52SBarry Smith   ierr = MPI_Type_size(dtype,&dsize);CHKERRQ(ierr);
14615056fcd2SBarry Smith   if (((size_t)dsize*count) > link->bytes) {
1462aa1993deSMatthew G Knepley     ierr        = PetscFree(link->mem);CHKERRQ(ierr);
1463854ce69bSBarry Smith     ierr        = PetscMalloc(dsize*count,&link->mem);CHKERRQ(ierr);
1464854ce69bSBarry Smith     link->bytes = dsize*count;
1465aa1993deSMatthew G Knepley   }
1466aa1993deSMatthew G Knepley   link->next   = dm->workout;
1467aa1993deSMatthew G Knepley   dm->workout  = link;
146800d952a4SJed Brown #if defined(PETSC_HAVE_VALGRIND)
146900d952a4SJed Brown   VALGRIND_MAKE_MEM_NOACCESS((char*)link->mem + (size_t)dsize*count, link->bytes - (size_t)dsize*count);
147000d952a4SJed Brown   VALGRIND_MAKE_MEM_UNDEFINED(link->mem, (size_t)dsize*count);
147100d952a4SJed Brown #endif
1472aa1993deSMatthew G Knepley   *(void**)mem = link->mem;
1473a89ea682SMatthew G Knepley   PetscFunctionReturn(0);
1474a89ea682SMatthew G Knepley }
1475a89ea682SMatthew G Knepley 
1476aa1993deSMatthew G Knepley /*@C
1477aa1993deSMatthew G Knepley   DMRestoreWorkArray - Restores a work array guaranteed to be at least the input size, restore with DMRestoreWorkArray()
1478aa1993deSMatthew G Knepley 
1479aa1993deSMatthew G Knepley   Not Collective
1480aa1993deSMatthew G Knepley 
1481aa1993deSMatthew G Knepley   Input Parameters:
1482aa1993deSMatthew G Knepley + dm - the DM object
1483aa1993deSMatthew G Knepley . count - The minium size
148469291d52SBarry Smith - dtype - MPI data type, often MPIU_REAL, MPIU_SCALAR, MPIU_INT
1485aa1993deSMatthew G Knepley 
1486aa1993deSMatthew G Knepley   Output Parameter:
1487aa1993deSMatthew G Knepley . array - the work array
1488aa1993deSMatthew G Knepley 
1489aa1993deSMatthew G Knepley   Level: developer
1490aa1993deSMatthew G Knepley 
149195452b02SPatrick Sanan   Developer Notes:
149295452b02SPatrick Sanan     count and dtype are ignored, they are only needed for DMGetWorkArray()
1493aa1993deSMatthew G Knepley .seealso DMDestroy(), DMCreate()
1494aa1993deSMatthew G Knepley @*/
149569291d52SBarry Smith PetscErrorCode DMRestoreWorkArray(DM dm,PetscInt count,MPI_Datatype dtype,void *mem)
1496aa1993deSMatthew G Knepley {
1497aa1993deSMatthew G Knepley   DMWorkLink *p,link;
1498aa1993deSMatthew G Knepley 
1499aa1993deSMatthew G Knepley   PetscFunctionBegin;
1500aa1993deSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1501aa1993deSMatthew G Knepley   PetscValidPointer(mem,4);
1502aa1993deSMatthew G Knepley   for (p=&dm->workout; (link=*p); p=&link->next) {
1503aa1993deSMatthew G Knepley     if (link->mem == *(void**)mem) {
1504aa1993deSMatthew G Knepley       *p           = link->next;
1505aa1993deSMatthew G Knepley       link->next   = dm->workin;
1506aa1993deSMatthew G Knepley       dm->workin   = link;
15070298fd71SBarry Smith       *(void**)mem = NULL;
1508aa1993deSMatthew G Knepley       PetscFunctionReturn(0);
1509aa1993deSMatthew G Knepley     }
1510aa1993deSMatthew G Knepley   }
1511aa1993deSMatthew G Knepley   SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"Array was not checked out");
1512aa1993deSMatthew G Knepley }
1513e7c4fc90SDmitry Karpeev 
15148cda7954SMatthew G. Knepley /*@C
15158cda7954SMatthew G. Knepley   DMSetNullSpaceConstructor - Provide a callback function which constructs the nullspace for a given field
15168cda7954SMatthew G. Knepley 
15178cda7954SMatthew G. Knepley   Logically collective on DM
15188cda7954SMatthew G. Knepley 
15198cda7954SMatthew G. Knepley   Input Parameters:
15208cda7954SMatthew G. Knepley + dm     - The DM
15218cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15228cda7954SMatthew G. Knepley - nullsp - A callback to create the nullspace
15238cda7954SMatthew G. Knepley 
15248cda7954SMatthew G. Knepley   Notes:
15258cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15268cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15278cda7954SMatthew G. Knepley $ dm        - The present DM
15288cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15298cda7954SMatthew G. Knepley $ field     - The field number in dm
15308cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15318cda7954SMatthew G. Knepley 
15328cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15338cda7954SMatthew G. Knepley 
15348cda7954SMatthew G. Knepley .seealso: DMGetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15358cda7954SMatthew G. Knepley */
15368cda7954SMatthew G. Knepley PetscErrorCode DMSetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1537435a35e8SMatthew G Knepley {
1538435a35e8SMatthew G Knepley   PetscFunctionBegin;
1539435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
154082f516ccSBarry Smith   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1541435a35e8SMatthew G Knepley   dm->nullspaceConstructors[field] = nullsp;
1542435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1543435a35e8SMatthew G Knepley }
1544435a35e8SMatthew G Knepley 
15458cda7954SMatthew G. Knepley /*@C
15468cda7954SMatthew G. Knepley   DMGetNullSpaceConstructor - Return the callback function which constructs the nullspace for a given field, or NULL
15478cda7954SMatthew G. Knepley 
15488cda7954SMatthew G. Knepley   Not collective
15498cda7954SMatthew G. Knepley 
15508cda7954SMatthew G. Knepley   Input Parameters:
15518cda7954SMatthew G. Knepley + dm     - The DM
15528cda7954SMatthew G. Knepley - field  - The field number for the nullspace
15538cda7954SMatthew G. Knepley 
15548cda7954SMatthew G. Knepley   Output Parameter:
15558cda7954SMatthew G. Knepley . nullsp - A callback to create the nullspace
15568cda7954SMatthew G. Knepley 
15578cda7954SMatthew G. Knepley   Notes:
15588cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15598cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15608cda7954SMatthew G. Knepley $ dm        - The present DM
15618cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15628cda7954SMatthew G. Knepley $ field     - The field number in dm
15638cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15648cda7954SMatthew G. Knepley 
15658cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15668cda7954SMatthew G. Knepley 
15678cda7954SMatthew G. Knepley .seealso: DMSetNullSpaceConstructor(), DMSetNearNullSpaceConstructor(), DMGetNearNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
15688cda7954SMatthew G. Knepley */
15698cda7954SMatthew G. Knepley PetscErrorCode DMGetNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
15700a50eb56SMatthew G. Knepley {
15710a50eb56SMatthew G. Knepley   PetscFunctionBegin;
15720a50eb56SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1573f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
15740a50eb56SMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
15750a50eb56SMatthew G. Knepley   *nullsp = dm->nullspaceConstructors[field];
15760a50eb56SMatthew G. Knepley   PetscFunctionReturn(0);
15770a50eb56SMatthew G. Knepley }
15780a50eb56SMatthew G. Knepley 
15798cda7954SMatthew G. Knepley /*@C
15808cda7954SMatthew G. Knepley   DMSetNearNullSpaceConstructor - Provide a callback function which constructs the near-nullspace for a given field
15818cda7954SMatthew G. Knepley 
15828cda7954SMatthew G. Knepley   Logically collective on DM
15838cda7954SMatthew G. Knepley 
15848cda7954SMatthew G. Knepley   Input Parameters:
15858cda7954SMatthew G. Knepley + dm     - The DM
15868cda7954SMatthew G. Knepley . field  - The field number for the nullspace
15878cda7954SMatthew G. Knepley - nullsp - A callback to create the near-nullspace
15888cda7954SMatthew G. Knepley 
15898cda7954SMatthew G. Knepley   Notes:
15908cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
15918cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
15928cda7954SMatthew G. Knepley $ dm        - The present DM
15938cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
15948cda7954SMatthew G. Knepley $ field     - The field number in dm
15958cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
15968cda7954SMatthew G. Knepley 
15978cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
15988cda7954SMatthew G. Knepley 
15998cda7954SMatthew G. Knepley .seealso: DMGetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16008cda7954SMatthew G. Knepley */
16018cda7954SMatthew G. Knepley PetscErrorCode DMSetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (*nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1602f9d4088aSMatthew G. Knepley {
1603f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1604f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1605f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1606f9d4088aSMatthew G. Knepley   dm->nearnullspaceConstructors[field] = nullsp;
1607f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1608f9d4088aSMatthew G. Knepley }
1609f9d4088aSMatthew G. Knepley 
16108cda7954SMatthew G. Knepley /*@C
16118cda7954SMatthew G. Knepley   DMGetNearNullSpaceConstructor - Return the callback function which constructs the near-nullspace for a given field, or NULL
16128cda7954SMatthew G. Knepley 
16138cda7954SMatthew G. Knepley   Not collective
16148cda7954SMatthew G. Knepley 
16158cda7954SMatthew G. Knepley   Input Parameters:
16168cda7954SMatthew G. Knepley + dm     - The DM
16178cda7954SMatthew G. Knepley - field  - The field number for the nullspace
16188cda7954SMatthew G. Knepley 
16198cda7954SMatthew G. Knepley   Output Parameter:
16208cda7954SMatthew G. Knepley . nullsp - A callback to create the near-nullspace
16218cda7954SMatthew G. Knepley 
16228cda7954SMatthew G. Knepley   Notes:
16238cda7954SMatthew G. Knepley   The callback is intended to provide nullspaces when function spaces are joined or split, such as in DMCreateSubDM(). The calling sequence is
16248cda7954SMatthew G. Knepley $ PetscErrorCode nullsp(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace)
16258cda7954SMatthew G. Knepley $ dm        - The present DM
16268cda7954SMatthew G. Knepley $ origField - The field number given above, in the original DM
16278cda7954SMatthew G. Knepley $ field     - The field number in dm
16288cda7954SMatthew G. Knepley $ nullSpace - The nullspace for the given field
16298cda7954SMatthew G. Knepley 
16308cda7954SMatthew G. Knepley   This function is currently not available from Fortran.
16318cda7954SMatthew G. Knepley 
16328cda7954SMatthew G. Knepley .seealso: DMSetNearNullSpaceConstructor(), DMSetNullSpaceConstructor(), DMGetNullSpaceConstructor(), DMCreateSubDM(), DMCreateSuperDM()
16338cda7954SMatthew G. Knepley */
16348cda7954SMatthew G. Knepley PetscErrorCode DMGetNearNullSpaceConstructor(DM dm, PetscInt field, PetscErrorCode (**nullsp)(DM dm, PetscInt origField, PetscInt field, MatNullSpace *nullSpace))
1635f9d4088aSMatthew G. Knepley {
1636f9d4088aSMatthew G. Knepley   PetscFunctionBegin;
1637f9d4088aSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
1638f9d4088aSMatthew G. Knepley   PetscValidPointer(nullsp, 3);
1639f9d4088aSMatthew G. Knepley   if (field >= 10) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Cannot handle %d >= 10 fields", field);
1640f9d4088aSMatthew G. Knepley   *nullsp = dm->nearnullspaceConstructors[field];
1641f9d4088aSMatthew G. Knepley   PetscFunctionReturn(0);
1642f9d4088aSMatthew G. Knepley }
1643f9d4088aSMatthew G. Knepley 
16444f3b5142SJed Brown /*@C
16454d343eeaSMatthew G Knepley   DMCreateFieldIS - Creates a set of IS objects with the global indices of dofs for each field
16464d343eeaSMatthew G Knepley 
16474d343eeaSMatthew G Knepley   Not collective
16484d343eeaSMatthew G Knepley 
16494d343eeaSMatthew G Knepley   Input Parameter:
16504d343eeaSMatthew G Knepley . dm - the DM object
16514d343eeaSMatthew G Knepley 
16524d343eeaSMatthew G Knepley   Output Parameters:
16530298fd71SBarry Smith + numFields  - The number of fields (or NULL if not requested)
16540298fd71SBarry Smith . fieldNames - The name for each field (or NULL if not requested)
16550298fd71SBarry Smith - fields     - The global indices for each field (or NULL if not requested)
16564d343eeaSMatthew G Knepley 
16574d343eeaSMatthew G Knepley   Level: intermediate
16584d343eeaSMatthew G Knepley 
165921c9b008SJed Brown   Notes:
166021c9b008SJed Brown   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
166121c9b008SJed Brown   PetscFree(), every entry of fields should be destroyed with ISDestroy(), and both arrays should be freed with
166221c9b008SJed Brown   PetscFree().
166321c9b008SJed Brown 
16644d343eeaSMatthew G Knepley .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix()
16654d343eeaSMatthew G Knepley @*/
166637d0c07bSMatthew G Knepley PetscErrorCode DMCreateFieldIS(DM dm, PetscInt *numFields, char ***fieldNames, IS **fields)
16674d343eeaSMatthew G Knepley {
166837d0c07bSMatthew G Knepley   PetscSection   section, sectionGlobal;
16694d343eeaSMatthew G Knepley   PetscErrorCode ierr;
16704d343eeaSMatthew G Knepley 
16714d343eeaSMatthew G Knepley   PetscFunctionBegin;
16724d343eeaSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
167369ca1f37SDmitry Karpeev   if (numFields) {
1674534a8f05SLisandro Dalcin     PetscValidIntPointer(numFields,2);
167569ca1f37SDmitry Karpeev     *numFields = 0;
167669ca1f37SDmitry Karpeev   }
167737d0c07bSMatthew G Knepley   if (fieldNames) {
167837d0c07bSMatthew G Knepley     PetscValidPointer(fieldNames,3);
16790298fd71SBarry Smith     *fieldNames = NULL;
168069ca1f37SDmitry Karpeev   }
168169ca1f37SDmitry Karpeev   if (fields) {
168269ca1f37SDmitry Karpeev     PetscValidPointer(fields,4);
16830298fd71SBarry Smith     *fields = NULL;
168469ca1f37SDmitry Karpeev   }
168592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
168637d0c07bSMatthew G Knepley   if (section) {
16873a544194SStefano Zampini     PetscInt *fieldSizes, *fieldNc, **fieldIndices;
168837d0c07bSMatthew G Knepley     PetscInt nF, f, pStart, pEnd, p;
168937d0c07bSMatthew G Knepley 
1690e87a4003SBarry Smith     ierr = DMGetGlobalSection(dm, &sectionGlobal);CHKERRQ(ierr);
169137d0c07bSMatthew G Knepley     ierr = PetscSectionGetNumFields(section, &nF);CHKERRQ(ierr);
16923a544194SStefano Zampini     ierr = PetscMalloc3(nF,&fieldSizes,nF,&fieldNc,nF,&fieldIndices);CHKERRQ(ierr);
169337d0c07bSMatthew G Knepley     ierr = PetscSectionGetChart(sectionGlobal, &pStart, &pEnd);CHKERRQ(ierr);
169437d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
169537d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
16963a544194SStefano Zampini       ierr          = PetscSectionGetFieldComponents(section, f, &fieldNc[f]);CHKERRQ(ierr);
169737d0c07bSMatthew G Knepley     }
169837d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
169937d0c07bSMatthew G Knepley       PetscInt gdof;
170037d0c07bSMatthew G Knepley 
170137d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
170237d0c07bSMatthew G Knepley       if (gdof > 0) {
170337d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
17043a544194SStefano Zampini           PetscInt fdof, fcdof, fpdof;
170537d0c07bSMatthew G Knepley 
170637d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
170737d0c07bSMatthew G Knepley           ierr  = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
17083a544194SStefano Zampini           fpdof = fdof-fcdof;
17093a544194SStefano Zampini           if (fpdof && fpdof != fieldNc[f]) {
17103a544194SStefano Zampini             /* Layout does not admit a pointwise block size */
17113a544194SStefano Zampini             fieldNc[f] = 1;
17123a544194SStefano Zampini           }
17133a544194SStefano Zampini           fieldSizes[f] += fpdof;
171437d0c07bSMatthew G Knepley         }
171537d0c07bSMatthew G Knepley       }
171637d0c07bSMatthew G Knepley     }
171737d0c07bSMatthew G Knepley     for (f = 0; f < nF; ++f) {
1718785e854fSJed Brown       ierr          = PetscMalloc1(fieldSizes[f], &fieldIndices[f]);CHKERRQ(ierr);
171937d0c07bSMatthew G Knepley       fieldSizes[f] = 0;
172037d0c07bSMatthew G Knepley     }
172137d0c07bSMatthew G Knepley     for (p = pStart; p < pEnd; ++p) {
172237d0c07bSMatthew G Knepley       PetscInt gdof, goff;
172337d0c07bSMatthew G Knepley 
172437d0c07bSMatthew G Knepley       ierr = PetscSectionGetDof(sectionGlobal, p, &gdof);CHKERRQ(ierr);
172537d0c07bSMatthew G Knepley       if (gdof > 0) {
172637d0c07bSMatthew G Knepley         ierr = PetscSectionGetOffset(sectionGlobal, p, &goff);CHKERRQ(ierr);
172737d0c07bSMatthew G Knepley         for (f = 0; f < nF; ++f) {
172837d0c07bSMatthew G Knepley           PetscInt fdof, fcdof, fc;
172937d0c07bSMatthew G Knepley 
173037d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldDof(section, p, f, &fdof);CHKERRQ(ierr);
173137d0c07bSMatthew G Knepley           ierr = PetscSectionGetFieldConstraintDof(section, p, f, &fcdof);CHKERRQ(ierr);
173237d0c07bSMatthew G Knepley           for (fc = 0; fc < fdof-fcdof; ++fc, ++fieldSizes[f]) {
173337d0c07bSMatthew G Knepley             fieldIndices[f][fieldSizes[f]] = goff++;
173437d0c07bSMatthew G Knepley           }
173537d0c07bSMatthew G Knepley         }
173637d0c07bSMatthew G Knepley       }
173737d0c07bSMatthew G Knepley     }
17388865f1eaSKarl Rupp     if (numFields) *numFields = nF;
173937d0c07bSMatthew G Knepley     if (fieldNames) {
1740785e854fSJed Brown       ierr = PetscMalloc1(nF, fieldNames);CHKERRQ(ierr);
174137d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
174237d0c07bSMatthew G Knepley         const char *fieldName;
174337d0c07bSMatthew G Knepley 
174437d0c07bSMatthew G Knepley         ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
174537d0c07bSMatthew G Knepley         ierr = PetscStrallocpy(fieldName, (char**) &(*fieldNames)[f]);CHKERRQ(ierr);
174637d0c07bSMatthew G Knepley       }
174737d0c07bSMatthew G Knepley     }
174837d0c07bSMatthew G Knepley     if (fields) {
1749785e854fSJed Brown       ierr = PetscMalloc1(nF, fields);CHKERRQ(ierr);
175037d0c07bSMatthew G Knepley       for (f = 0; f < nF; ++f) {
17513a544194SStefano Zampini         PetscInt bs, in[2], out[2];
17523a544194SStefano Zampini 
175382f516ccSBarry Smith         ierr  = ISCreateGeneral(PetscObjectComm((PetscObject)dm), fieldSizes[f], fieldIndices[f], PETSC_OWN_POINTER, &(*fields)[f]);CHKERRQ(ierr);
17543a544194SStefano Zampini         in[0] = -fieldNc[f];
17553a544194SStefano Zampini         in[1] = fieldNc[f];
17563a544194SStefano Zampini         ierr  = MPIU_Allreduce(in, out, 2, MPIU_INT, MPI_MAX, PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
17573a544194SStefano Zampini         bs    = (-out[0] == out[1]) ? out[1] : 1;
17583a544194SStefano Zampini         ierr  = ISSetBlockSize((*fields)[f], bs);CHKERRQ(ierr);
175937d0c07bSMatthew G Knepley       }
176037d0c07bSMatthew G Knepley     }
17613a544194SStefano Zampini     ierr = PetscFree3(fieldSizes,fieldNc,fieldIndices);CHKERRQ(ierr);
17628865f1eaSKarl Rupp   } else if (dm->ops->createfieldis) {
17638865f1eaSKarl Rupp     ierr = (*dm->ops->createfieldis)(dm, numFields, fieldNames, fields);CHKERRQ(ierr);
176469ca1f37SDmitry Karpeev   }
17654d343eeaSMatthew G Knepley   PetscFunctionReturn(0);
17664d343eeaSMatthew G Knepley }
17674d343eeaSMatthew G Knepley 
176816621825SDmitry Karpeev 
176916621825SDmitry Karpeev /*@C
177016621825SDmitry Karpeev   DMCreateFieldDecomposition - Returns a list of IS objects defining a decomposition of a problem into subproblems
177116621825SDmitry Karpeev                           corresponding to different fields: each IS contains the global indices of the dofs of the
177216621825SDmitry Karpeev                           corresponding field. The optional list of DMs define the DM for each subproblem.
1773e7c4fc90SDmitry Karpeev                           Generalizes DMCreateFieldIS().
1774e7c4fc90SDmitry Karpeev 
1775e7c4fc90SDmitry Karpeev   Not collective
1776e7c4fc90SDmitry Karpeev 
1777e7c4fc90SDmitry Karpeev   Input Parameter:
1778e7c4fc90SDmitry Karpeev . dm - the DM object
1779e7c4fc90SDmitry Karpeev 
1780e7c4fc90SDmitry Karpeev   Output Parameters:
17810298fd71SBarry Smith + len       - The number of subproblems in the field decomposition (or NULL if not requested)
17820298fd71SBarry Smith . namelist  - The name for each field (or NULL if not requested)
17830298fd71SBarry Smith . islist    - The global indices for each field (or NULL if not requested)
17840298fd71SBarry Smith - dmlist    - The DMs for each field subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
1785e7c4fc90SDmitry Karpeev 
1786e7c4fc90SDmitry Karpeev   Level: intermediate
1787e7c4fc90SDmitry Karpeev 
1788e7c4fc90SDmitry Karpeev   Notes:
1789e7c4fc90SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
1790e7c4fc90SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
1791e7c4fc90SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
1792e7c4fc90SDmitry Karpeev 
1793e7c4fc90SDmitry Karpeev .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1794e7c4fc90SDmitry Karpeev @*/
179516621825SDmitry Karpeev PetscErrorCode DMCreateFieldDecomposition(DM dm, PetscInt *len, char ***namelist, IS **islist, DM **dmlist)
1796e7c4fc90SDmitry Karpeev {
1797e7c4fc90SDmitry Karpeev   PetscErrorCode ierr;
1798e7c4fc90SDmitry Karpeev 
1799e7c4fc90SDmitry Karpeev   PetscFunctionBegin;
1800e7c4fc90SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
18018865f1eaSKarl Rupp   if (len) {
1802534a8f05SLisandro Dalcin     PetscValidIntPointer(len,2);
18038865f1eaSKarl Rupp     *len = 0;
18048865f1eaSKarl Rupp   }
18058865f1eaSKarl Rupp   if (namelist) {
18068865f1eaSKarl Rupp     PetscValidPointer(namelist,3);
1807ea78f98cSLisandro Dalcin     *namelist = NULL;
18088865f1eaSKarl Rupp   }
18098865f1eaSKarl Rupp   if (islist) {
18108865f1eaSKarl Rupp     PetscValidPointer(islist,4);
1811ea78f98cSLisandro Dalcin     *islist = NULL;
18128865f1eaSKarl Rupp   }
18138865f1eaSKarl Rupp   if (dmlist) {
18148865f1eaSKarl Rupp     PetscValidPointer(dmlist,5);
1815ea78f98cSLisandro Dalcin     *dmlist = NULL;
18168865f1eaSKarl Rupp   }
1817f3f0edfdSDmitry Karpeev   /*
1818f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1819f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1820f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1821f3f0edfdSDmitry Karpeev    */
1822ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
182316621825SDmitry Karpeev   if (!dm->ops->createfielddecomposition) {
1824435a35e8SMatthew G Knepley     PetscSection section;
1825435a35e8SMatthew G Knepley     PetscInt     numFields, f;
1826435a35e8SMatthew G Knepley 
182792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
1828435a35e8SMatthew G Knepley     if (section) {ierr = PetscSectionGetNumFields(section, &numFields);CHKERRQ(ierr);}
1829435a35e8SMatthew G Knepley     if (section && numFields && dm->ops->createsubdm) {
1830f25d98f1SMatthew G. Knepley       if (len) *len = numFields;
183103dc3394SMatthew G. Knepley       if (namelist) {ierr = PetscMalloc1(numFields,namelist);CHKERRQ(ierr);}
183203dc3394SMatthew G. Knepley       if (islist)   {ierr = PetscMalloc1(numFields,islist);CHKERRQ(ierr);}
183303dc3394SMatthew G. Knepley       if (dmlist)   {ierr = PetscMalloc1(numFields,dmlist);CHKERRQ(ierr);}
1834435a35e8SMatthew G Knepley       for (f = 0; f < numFields; ++f) {
1835435a35e8SMatthew G Knepley         const char *fieldName;
1836435a35e8SMatthew G Knepley 
183703dc3394SMatthew G. Knepley         ierr = DMCreateSubDM(dm, 1, &f, islist ? &(*islist)[f] : NULL, dmlist ? &(*dmlist)[f] : NULL);CHKERRQ(ierr);
183803dc3394SMatthew G. Knepley         if (namelist) {
1839435a35e8SMatthew G Knepley           ierr = PetscSectionGetFieldName(section, f, &fieldName);CHKERRQ(ierr);
1840435a35e8SMatthew G Knepley           ierr = PetscStrallocpy(fieldName, (char**) &(*namelist)[f]);CHKERRQ(ierr);
1841435a35e8SMatthew G Knepley         }
184203dc3394SMatthew G. Knepley       }
1843435a35e8SMatthew G Knepley     } else {
184469ca1f37SDmitry Karpeev       ierr = DMCreateFieldIS(dm, len, namelist, islist);CHKERRQ(ierr);
1845e7c4fc90SDmitry Karpeev       /* By default there are no DMs associated with subproblems. */
18460298fd71SBarry Smith       if (dmlist) *dmlist = NULL;
1847e7c4fc90SDmitry Karpeev     }
18488865f1eaSKarl Rupp   } else {
184916621825SDmitry Karpeev     ierr = (*dm->ops->createfielddecomposition)(dm,len,namelist,islist,dmlist);CHKERRQ(ierr);
185016621825SDmitry Karpeev   }
185116621825SDmitry Karpeev   PetscFunctionReturn(0);
185216621825SDmitry Karpeev }
185316621825SDmitry Karpeev 
1854564cec59SMatthew G. Knepley /*@
1855435a35e8SMatthew G Knepley   DMCreateSubDM - Returns an IS and DM encapsulating a subproblem defined by the fields passed in.
1856435a35e8SMatthew G Knepley                   The fields are defined by DMCreateFieldIS().
1857435a35e8SMatthew G Knepley 
1858435a35e8SMatthew G Knepley   Not collective
1859435a35e8SMatthew G Knepley 
1860435a35e8SMatthew G Knepley   Input Parameters:
18612adcc780SMatthew G. Knepley + dm        - The DM object
18622adcc780SMatthew G. Knepley . numFields - The number of fields in this subproblem
18632adcc780SMatthew G. Knepley - fields    - The field numbers of the selected fields
1864435a35e8SMatthew G Knepley 
1865435a35e8SMatthew G Knepley   Output Parameters:
18662adcc780SMatthew G. Knepley + is - The global indices for the subproblem
18672adcc780SMatthew G. Knepley - subdm - The DM for the subproblem
1868435a35e8SMatthew G Knepley 
18695d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
18705d3b26e6SMatthew G. Knepley 
1871435a35e8SMatthew G Knepley   Level: intermediate
1872435a35e8SMatthew G Knepley 
18735d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
1874435a35e8SMatthew G Knepley @*/
187537bc7515SMatthew G. Knepley PetscErrorCode DMCreateSubDM(DM dm, PetscInt numFields, const PetscInt fields[], IS *is, DM *subdm)
1876435a35e8SMatthew G Knepley {
1877435a35e8SMatthew G Knepley   PetscErrorCode ierr;
1878435a35e8SMatthew G Knepley 
1879435a35e8SMatthew G Knepley   PetscFunctionBegin;
1880435a35e8SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
1881435a35e8SMatthew G Knepley   PetscValidPointer(fields,3);
18828865f1eaSKarl Rupp   if (is) PetscValidPointer(is,4);
18838865f1eaSKarl Rupp   if (subdm) PetscValidPointer(subdm,5);
1884b9d85ea2SLisandro Dalcin   if (!dm->ops->createsubdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSubDM",((PetscObject)dm)->type_name);
1885435a35e8SMatthew G Knepley   ierr = (*dm->ops->createsubdm)(dm, numFields, fields, is, subdm);CHKERRQ(ierr);
1886435a35e8SMatthew G Knepley   PetscFunctionReturn(0);
1887435a35e8SMatthew G Knepley }
1888435a35e8SMatthew G Knepley 
18892adcc780SMatthew G. Knepley /*@C
18902adcc780SMatthew G. Knepley   DMCreateSuperDM - Returns an arrays of ISes and DM encapsulating a superproblem defined by the DMs passed in.
18912adcc780SMatthew G. Knepley 
18922adcc780SMatthew G. Knepley   Not collective
18932adcc780SMatthew G. Knepley 
18942adcc780SMatthew G. Knepley   Input Parameter:
18952adcc780SMatthew G. Knepley + dms - The DM objects
18962adcc780SMatthew G. Knepley - len - The number of DMs
18972adcc780SMatthew G. Knepley 
18982adcc780SMatthew G. Knepley   Output Parameters:
1899a42bd24dSMatthew G. Knepley + is - The global indices for the subproblem, or NULL
19002adcc780SMatthew G. Knepley - superdm - The DM for the superproblem
19012adcc780SMatthew G. Knepley 
19025d3b26e6SMatthew G. Knepley   Note: You need to call DMPlexSetMigrationSF() on the original DM if you want the Global-To-Natural map to be automatically constructed
19035d3b26e6SMatthew G. Knepley 
19042adcc780SMatthew G. Knepley   Level: intermediate
19052adcc780SMatthew G. Knepley 
19065d3b26e6SMatthew G. Knepley .seealso DMPlexSetMigrationSF(), DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
19072adcc780SMatthew G. Knepley @*/
19082adcc780SMatthew G. Knepley PetscErrorCode DMCreateSuperDM(DM dms[], PetscInt len, IS **is, DM *superdm)
19092adcc780SMatthew G. Knepley {
19102adcc780SMatthew G. Knepley   PetscInt       i;
19112adcc780SMatthew G. Knepley   PetscErrorCode ierr;
19122adcc780SMatthew G. Knepley 
19132adcc780SMatthew G. Knepley   PetscFunctionBegin;
19142adcc780SMatthew G. Knepley   PetscValidPointer(dms,1);
19152adcc780SMatthew G. Knepley   for (i = 0; i < len; ++i) {PetscValidHeaderSpecific(dms[i],DM_CLASSID,1);}
19162adcc780SMatthew G. Knepley   if (is) PetscValidPointer(is,3);
1917a42bd24dSMatthew G. Knepley   PetscValidPointer(superdm,4);
19182adcc780SMatthew G. Knepley   if (len < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Number of DMs must be nonnegative: %D", len);
19192adcc780SMatthew G. Knepley   if (len) {
1920b9d85ea2SLisandro Dalcin     DM dm = dms[0];
1921b9d85ea2SLisandro Dalcin     if (!dm->ops->createsuperdm) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateSuperDM",((PetscObject)dm)->type_name);
1922b9d85ea2SLisandro Dalcin     ierr = (*dm->ops->createsuperdm)(dms, len, is, superdm);CHKERRQ(ierr);
19232adcc780SMatthew G. Knepley   }
19242adcc780SMatthew G. Knepley   PetscFunctionReturn(0);
19252adcc780SMatthew G. Knepley }
19262adcc780SMatthew G. Knepley 
192716621825SDmitry Karpeev 
192816621825SDmitry Karpeev /*@C
19298d4ac253SDmitry Karpeev   DMCreateDomainDecomposition - Returns lists of IS objects defining a decomposition of a problem into subproblems
19308d4ac253SDmitry Karpeev                           corresponding to restrictions to pairs nested subdomains: each IS contains the global
19318d4ac253SDmitry Karpeev                           indices of the dofs of the corresponding subdomains.  The inner subdomains conceptually
19328d4ac253SDmitry Karpeev                           define a nonoverlapping covering, while outer subdomains can overlap.
19338d4ac253SDmitry Karpeev                           The optional list of DMs define the DM for each subproblem.
193416621825SDmitry Karpeev 
193516621825SDmitry Karpeev   Not collective
193616621825SDmitry Karpeev 
193716621825SDmitry Karpeev   Input Parameter:
193816621825SDmitry Karpeev . dm - the DM object
193916621825SDmitry Karpeev 
194016621825SDmitry Karpeev   Output Parameters:
19410298fd71SBarry Smith + len         - The number of subproblems in the domain decomposition (or NULL if not requested)
19420298fd71SBarry Smith . namelist    - The name for each subdomain (or NULL if not requested)
19430298fd71SBarry Smith . innerislist - The global indices for each inner subdomain (or NULL, if not requested)
19440298fd71SBarry Smith . outerislist - The global indices for each outer subdomain (or NULL, if not requested)
19450298fd71SBarry Smith - dmlist      - The DMs for each subdomain subproblem (or NULL, if not requested; if NULL is returned, no DMs are defined)
194616621825SDmitry Karpeev 
194716621825SDmitry Karpeev   Level: intermediate
194816621825SDmitry Karpeev 
194916621825SDmitry Karpeev   Notes:
195016621825SDmitry Karpeev   The user is responsible for freeing all requested arrays. In particular, every entry of names should be freed with
195116621825SDmitry Karpeev   PetscFree(), every entry of is should be destroyed with ISDestroy(), every entry of dm should be destroyed with DMDestroy(),
195216621825SDmitry Karpeev   and all of the arrays should be freed with PetscFree().
195316621825SDmitry Karpeev 
1954245d9833Sprj- .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldDecomposition()
195516621825SDmitry Karpeev @*/
19568d4ac253SDmitry Karpeev PetscErrorCode DMCreateDomainDecomposition(DM dm, PetscInt *len, char ***namelist, IS **innerislist, IS **outerislist, DM **dmlist)
195716621825SDmitry Karpeev {
195816621825SDmitry Karpeev   PetscErrorCode      ierr;
1959be081cd6SPeter Brune   DMSubDomainHookLink link;
1960be081cd6SPeter Brune   PetscInt            i,l;
196116621825SDmitry Karpeev 
196216621825SDmitry Karpeev   PetscFunctionBegin;
196316621825SDmitry Karpeev   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
196414a18fd3SPeter Brune   if (len)           {PetscValidPointer(len,2);            *len         = 0;}
19650298fd71SBarry Smith   if (namelist)      {PetscValidPointer(namelist,3);       *namelist    = NULL;}
19660298fd71SBarry Smith   if (innerislist)   {PetscValidPointer(innerislist,4);    *innerislist = NULL;}
19670298fd71SBarry Smith   if (outerislist)   {PetscValidPointer(outerislist,5);    *outerislist = NULL;}
19680298fd71SBarry Smith   if (dmlist)        {PetscValidPointer(dmlist,6);         *dmlist      = NULL;}
1969f3f0edfdSDmitry Karpeev   /*
1970f3f0edfdSDmitry Karpeev    Is it a good idea to apply the following check across all impls?
1971f3f0edfdSDmitry Karpeev    Perhaps some impls can have a well-defined decomposition before DMSetUp?
1972f3f0edfdSDmitry Karpeev    This, however, follows the general principle that accessors are not well-behaved until the object is set up.
1973f3f0edfdSDmitry Karpeev    */
1974ce94432eSBarry Smith   if (!dm->setupcalled) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_WRONGSTATE, "Decomposition defined only after DMSetUp");
197516621825SDmitry Karpeev   if (dm->ops->createdomaindecomposition) {
1976be081cd6SPeter Brune     ierr = (*dm->ops->createdomaindecomposition)(dm,&l,namelist,innerislist,outerislist,dmlist);CHKERRQ(ierr);
197714a18fd3SPeter Brune     /* copy subdomain hooks and context over to the subdomain DMs */
1978f891f5b9SPatrick Sanan     if (dmlist && *dmlist) {
1979be081cd6SPeter Brune       for (i = 0; i < l; i++) {
1980be081cd6SPeter Brune         for (link=dm->subdomainhook; link; link=link->next) {
1981be081cd6SPeter Brune           if (link->ddhook) {ierr = (*link->ddhook)(dm,(*dmlist)[i],link->ctx);CHKERRQ(ierr);}
1982be081cd6SPeter Brune         }
1983648262bbSPatrick Sanan         if (dm->ctx) (*dmlist)[i]->ctx = dm->ctx;
1984e7c4fc90SDmitry Karpeev       }
198514a18fd3SPeter Brune     }
198614a18fd3SPeter Brune     if (len) *len = l;
198714a18fd3SPeter Brune   }
1988e30e807fSPeter Brune   PetscFunctionReturn(0);
1989e30e807fSPeter Brune }
1990e30e807fSPeter Brune 
1991e30e807fSPeter Brune 
1992e30e807fSPeter Brune /*@C
1993e30e807fSPeter Brune   DMCreateDomainDecompositionScatters - Returns scatters to the subdomain vectors from the global vector
1994e30e807fSPeter Brune 
1995e30e807fSPeter Brune   Not collective
1996e30e807fSPeter Brune 
1997e30e807fSPeter Brune   Input Parameters:
1998e30e807fSPeter Brune + dm - the DM object
1999e30e807fSPeter Brune . n  - the number of subdomain scatters
2000e30e807fSPeter Brune - subdms - the local subdomains
2001e30e807fSPeter Brune 
2002e30e807fSPeter Brune   Output Parameters:
2003e30e807fSPeter Brune + n     - the number of scatters returned
2004e30e807fSPeter Brune . iscat - scatter from global vector to nonoverlapping global vector entries on subdomain
2005e30e807fSPeter Brune . oscat - scatter from global vector to overlapping global vector entries on subdomain
2006e30e807fSPeter Brune - gscat - scatter from global vector to local vector on subdomain (fills in ghosts)
2007e30e807fSPeter Brune 
200895452b02SPatrick Sanan   Notes:
200995452b02SPatrick Sanan     This is an alternative to the iis and ois arguments in DMCreateDomainDecomposition that allow for the solution
2010e30e807fSPeter Brune   of general nonlinear problems with overlapping subdomain methods.  While merely having index sets that enable subsets
2011e30e807fSPeter Brune   of the residual equations to be created is fine for linear problems, nonlinear problems require local assembly of
2012e30e807fSPeter Brune   solution and residual data.
2013e30e807fSPeter Brune 
2014e30e807fSPeter Brune   Level: developer
2015e30e807fSPeter Brune 
2016e30e807fSPeter Brune .seealso DMDestroy(), DMView(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMCreateFieldIS()
2017e30e807fSPeter Brune @*/
2018e30e807fSPeter Brune PetscErrorCode DMCreateDomainDecompositionScatters(DM dm,PetscInt n,DM *subdms,VecScatter **iscat,VecScatter **oscat,VecScatter **gscat)
2019e30e807fSPeter Brune {
2020e30e807fSPeter Brune   PetscErrorCode ierr;
2021e30e807fSPeter Brune 
2022e30e807fSPeter Brune   PetscFunctionBegin;
2023e30e807fSPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2024e30e807fSPeter Brune   PetscValidPointer(subdms,3);
2025b9d85ea2SLisandro Dalcin   if (!dm->ops->createddscatters) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCreateDomainDecompositionScatters",((PetscObject)dm)->type_name);
2026e30e807fSPeter Brune   ierr = (*dm->ops->createddscatters)(dm,n,subdms,iscat,oscat,gscat);CHKERRQ(ierr);
2027e7c4fc90SDmitry Karpeev   PetscFunctionReturn(0);
2028e7c4fc90SDmitry Karpeev }
2029e7c4fc90SDmitry Karpeev 
203047c6ae99SBarry Smith /*@
203147c6ae99SBarry Smith   DMRefine - Refines a DM object
203247c6ae99SBarry Smith 
2033d083f849SBarry Smith   Collective on dm
203447c6ae99SBarry Smith 
203547c6ae99SBarry Smith   Input Parameter:
203647c6ae99SBarry Smith + dm   - the DM object
203791d95f02SJed Brown - comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
203847c6ae99SBarry Smith 
203947c6ae99SBarry Smith   Output Parameter:
20400298fd71SBarry Smith . dmf - the refined DM, or NULL
2041ae0a1c52SMatthew G Knepley 
2042412e9a14SMatthew G. Knepley   Options Dtabase Keys:
2043412e9a14SMatthew G. Knepley . -dm_plex_cell_refiner <strategy> - chooses the refinement strategy, e.g. regular, tohex
2044412e9a14SMatthew G. Knepley 
20450298fd71SBarry Smith   Note: If no refinement was done, the return value is NULL
204647c6ae99SBarry Smith 
204747c6ae99SBarry Smith   Level: developer
204847c6ae99SBarry Smith 
2049e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
205047c6ae99SBarry Smith @*/
20517087cfbeSBarry Smith PetscErrorCode  DMRefine(DM dm,MPI_Comm comm,DM *dmf)
205247c6ae99SBarry Smith {
205347c6ae99SBarry Smith   PetscErrorCode   ierr;
2054c833c3b5SJed Brown   DMRefineHookLink link;
205547c6ae99SBarry Smith 
205647c6ae99SBarry Smith   PetscFunctionBegin;
2057732e2eb9SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2058b9d85ea2SLisandro Dalcin   if (!dm->ops->refine) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMRefine",((PetscObject)dm)->type_name);
20591ac00216SMatthew G. Knepley   ierr = PetscLogEventBegin(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
206047c6ae99SBarry Smith   ierr = (*dm->ops->refine)(dm,comm,dmf);CHKERRQ(ierr);
20614057135bSMatthew G Knepley   if (*dmf) {
206243842a1eSJed Brown     (*dmf)->ops->creatematrix = dm->ops->creatematrix;
20638865f1eaSKarl Rupp 
20648cd211a4SJed Brown     ierr = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmf);CHKERRQ(ierr);
20658865f1eaSKarl Rupp 
2066644e2e5bSBarry Smith     (*dmf)->ctx       = dm->ctx;
20670598a293SJed Brown     (*dmf)->leveldown = dm->leveldown;
2068656b349aSBarry Smith     (*dmf)->levelup   = dm->levelup + 1;
20698865f1eaSKarl Rupp 
2070e4b4b23bSJed Brown     ierr = DMSetMatType(*dmf,dm->mattype);CHKERRQ(ierr);
2071c833c3b5SJed Brown     for (link=dm->refinehook; link; link=link->next) {
20728865f1eaSKarl Rupp       if (link->refinehook) {
20738865f1eaSKarl Rupp         ierr = (*link->refinehook)(dm,*dmf,link->ctx);CHKERRQ(ierr);
20748865f1eaSKarl Rupp       }
2075c833c3b5SJed Brown     }
2076c833c3b5SJed Brown   }
20771ac00216SMatthew G. Knepley   ierr = PetscLogEventEnd(DM_Refine,dm,0,0,0);CHKERRQ(ierr);
2078c833c3b5SJed Brown   PetscFunctionReturn(0);
2079c833c3b5SJed Brown }
2080c833c3b5SJed Brown 
2081bb9467b5SJed Brown /*@C
2082c833c3b5SJed Brown    DMRefineHookAdd - adds a callback to be run when interpolating a nonlinear problem to a finer grid
2083c833c3b5SJed Brown 
2084c833c3b5SJed Brown    Logically Collective
2085c833c3b5SJed Brown 
2086c833c3b5SJed Brown    Input Arguments:
2087c833c3b5SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
2088c833c3b5SJed Brown .  refinehook - function to run when setting up a coarser level
2089c833c3b5SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
20900298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2091c833c3b5SJed Brown 
2092c833c3b5SJed Brown    Calling sequence of refinehook:
2093c833c3b5SJed Brown $    refinehook(DM coarse,DM fine,void *ctx);
2094c833c3b5SJed Brown 
2095c833c3b5SJed Brown +  coarse - coarse level DM
2096c833c3b5SJed Brown .  fine - fine level DM to interpolate problem to
2097c833c3b5SJed Brown -  ctx - optional user-defined function context
2098c833c3b5SJed Brown 
2099c833c3b5SJed Brown    Calling sequence for interphook:
2100c833c3b5SJed Brown $    interphook(DM coarse,Mat interp,DM fine,void *ctx)
2101c833c3b5SJed Brown 
2102c833c3b5SJed Brown +  coarse - coarse level DM
2103c833c3b5SJed Brown .  interp - matrix interpolating a coarse-level solution to the finer grid
2104c833c3b5SJed Brown .  fine - fine level DM to update
2105c833c3b5SJed Brown -  ctx - optional user-defined function context
2106c833c3b5SJed Brown 
2107c833c3b5SJed Brown    Level: advanced
2108c833c3b5SJed Brown 
2109c833c3b5SJed Brown    Notes:
2110c833c3b5SJed Brown    This function is only needed if auxiliary data needs to be passed to fine grids while grid sequencing
2111c833c3b5SJed Brown 
2112c833c3b5SJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
2113c833c3b5SJed Brown 
2114bb9467b5SJed Brown    This function is currently not available from Fortran.
2115bb9467b5SJed Brown 
2116c833c3b5SJed Brown .seealso: DMCoarsenHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2117c833c3b5SJed Brown @*/
2118c833c3b5SJed Brown PetscErrorCode DMRefineHookAdd(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
2119c833c3b5SJed Brown {
2120c833c3b5SJed Brown   PetscErrorCode   ierr;
2121c833c3b5SJed Brown   DMRefineHookLink link,*p;
2122c833c3b5SJed Brown 
2123c833c3b5SJed Brown   PetscFunctionBegin;
2124c833c3b5SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21253d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
21263d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) PetscFunctionReturn(0);
21273d8e3701SJed Brown   }
212895dccacaSBarry Smith   ierr             = PetscNew(&link);CHKERRQ(ierr);
2129c833c3b5SJed Brown   link->refinehook = refinehook;
2130c833c3b5SJed Brown   link->interphook = interphook;
2131c833c3b5SJed Brown   link->ctx        = ctx;
21320298fd71SBarry Smith   link->next       = NULL;
2133c833c3b5SJed Brown   *p               = link;
2134c833c3b5SJed Brown   PetscFunctionReturn(0);
2135c833c3b5SJed Brown }
2136c833c3b5SJed Brown 
21373d8e3701SJed Brown /*@C
21383d8e3701SJed Brown    DMRefineHookRemove - remove a callback from the list of hooks to be run when interpolating a nonlinear problem to a finer grid
21393d8e3701SJed Brown 
21403d8e3701SJed Brown    Logically Collective
21413d8e3701SJed Brown 
21423d8e3701SJed Brown    Input Arguments:
21433d8e3701SJed Brown +  coarse - nonlinear solver context on which to run a hook when restricting to a coarser level
21443d8e3701SJed Brown .  refinehook - function to run when setting up a coarser level
21453d8e3701SJed Brown .  interphook - function to run to update data on finer levels (once per SNESSolve())
21463d8e3701SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
21473d8e3701SJed Brown 
21483d8e3701SJed Brown    Level: advanced
21493d8e3701SJed Brown 
21503d8e3701SJed Brown    Notes:
21513d8e3701SJed Brown    This function does nothing if the hook is not in the list.
21523d8e3701SJed Brown 
21533d8e3701SJed Brown    This function is currently not available from Fortran.
21543d8e3701SJed Brown 
21553d8e3701SJed Brown .seealso: DMCoarsenHookRemove(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
21563d8e3701SJed Brown @*/
21573d8e3701SJed Brown PetscErrorCode DMRefineHookRemove(DM coarse,PetscErrorCode (*refinehook)(DM,DM,void*),PetscErrorCode (*interphook)(DM,Mat,DM,void*),void *ctx)
21583d8e3701SJed Brown {
21593d8e3701SJed Brown   PetscErrorCode   ierr;
21603d8e3701SJed Brown   DMRefineHookLink link,*p;
21613d8e3701SJed Brown 
21623d8e3701SJed Brown   PetscFunctionBegin;
21633d8e3701SJed Brown   PetscValidHeaderSpecific(coarse,DM_CLASSID,1);
21643d8e3701SJed Brown   for (p=&coarse->refinehook; *p; p=&(*p)->next) { /* Search the list of current hooks */
21653d8e3701SJed Brown     if ((*p)->refinehook == refinehook && (*p)->interphook == interphook && (*p)->ctx == ctx) {
21663d8e3701SJed Brown       link = *p;
21673d8e3701SJed Brown       *p = link->next;
21683d8e3701SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
21693d8e3701SJed Brown       break;
21703d8e3701SJed Brown     }
21713d8e3701SJed Brown   }
21723d8e3701SJed Brown   PetscFunctionReturn(0);
21733d8e3701SJed Brown }
21743d8e3701SJed Brown 
2175c833c3b5SJed Brown /*@
2176c833c3b5SJed Brown    DMInterpolate - interpolates user-defined problem data to a finer DM by running hooks registered by DMRefineHookAdd()
2177c833c3b5SJed Brown 
2178c833c3b5SJed Brown    Collective if any hooks are
2179c833c3b5SJed Brown 
2180c833c3b5SJed Brown    Input Arguments:
2181c833c3b5SJed Brown +  coarse - coarser DM to use as a base
2182e91eccc2SStefano Zampini .  interp - interpolation matrix, apply using MatInterpolate()
2183c833c3b5SJed Brown -  fine - finer DM to update
2184c833c3b5SJed Brown 
2185c833c3b5SJed Brown    Level: developer
2186c833c3b5SJed Brown 
2187c833c3b5SJed Brown .seealso: DMRefineHookAdd(), MatInterpolate()
2188c833c3b5SJed Brown @*/
2189c833c3b5SJed Brown PetscErrorCode DMInterpolate(DM coarse,Mat interp,DM fine)
2190c833c3b5SJed Brown {
2191c833c3b5SJed Brown   PetscErrorCode   ierr;
2192c833c3b5SJed Brown   DMRefineHookLink link;
2193c833c3b5SJed Brown 
2194c833c3b5SJed Brown   PetscFunctionBegin;
2195c833c3b5SJed Brown   for (link=fine->refinehook; link; link=link->next) {
21968865f1eaSKarl Rupp     if (link->interphook) {
21978865f1eaSKarl Rupp       ierr = (*link->interphook)(coarse,interp,fine,link->ctx);CHKERRQ(ierr);
21988865f1eaSKarl Rupp     }
21994057135bSMatthew G Knepley   }
220047c6ae99SBarry Smith   PetscFunctionReturn(0);
220147c6ae99SBarry Smith }
220247c6ae99SBarry Smith 
2203eb3f98d2SBarry Smith /*@
2204aed49f88SRichard Tran Mills     DMGetRefineLevel - Gets the number of refinements that have generated this DM.
2205eb3f98d2SBarry Smith 
2206eb3f98d2SBarry Smith     Not Collective
2207eb3f98d2SBarry Smith 
2208eb3f98d2SBarry Smith     Input Parameter:
2209eb3f98d2SBarry Smith .   dm - the DM object
2210eb3f98d2SBarry Smith 
2211eb3f98d2SBarry Smith     Output Parameter:
2212eb3f98d2SBarry Smith .   level - number of refinements
2213eb3f98d2SBarry Smith 
2214eb3f98d2SBarry Smith     Level: developer
2215eb3f98d2SBarry Smith 
22166a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2217eb3f98d2SBarry Smith 
2218eb3f98d2SBarry Smith @*/
2219eb3f98d2SBarry Smith PetscErrorCode  DMGetRefineLevel(DM dm,PetscInt *level)
2220eb3f98d2SBarry Smith {
2221eb3f98d2SBarry Smith   PetscFunctionBegin;
2222eb3f98d2SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2223eb3f98d2SBarry Smith   *level = dm->levelup;
2224eb3f98d2SBarry Smith   PetscFunctionReturn(0);
2225eb3f98d2SBarry Smith }
2226eb3f98d2SBarry Smith 
2227fef3a512SBarry Smith /*@
2228aed49f88SRichard Tran Mills     DMSetRefineLevel - Sets the number of refinements that have generated this DM.
2229fef3a512SBarry Smith 
2230fef3a512SBarry Smith     Not Collective
2231fef3a512SBarry Smith 
2232fef3a512SBarry Smith     Input Parameter:
2233fef3a512SBarry Smith +   dm - the DM object
2234fef3a512SBarry Smith -   level - number of refinements
2235fef3a512SBarry Smith 
2236fef3a512SBarry Smith     Level: advanced
2237fef3a512SBarry Smith 
223895452b02SPatrick Sanan     Notes:
223995452b02SPatrick Sanan     This value is used by PCMG to determine how many multigrid levels to use
2240fef3a512SBarry Smith 
2241fef3a512SBarry Smith .seealso DMCoarsen(), DMGetCoarsenLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
2242fef3a512SBarry Smith 
2243fef3a512SBarry Smith @*/
2244fef3a512SBarry Smith PetscErrorCode  DMSetRefineLevel(DM dm,PetscInt level)
2245fef3a512SBarry Smith {
2246fef3a512SBarry Smith   PetscFunctionBegin;
2247fef3a512SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2248fef3a512SBarry Smith   dm->levelup = level;
2249fef3a512SBarry Smith   PetscFunctionReturn(0);
2250fef3a512SBarry Smith }
2251fef3a512SBarry Smith 
2252ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformDM_Internal(DM dm, DM *tdm)
2253ca3d3a14SMatthew G. Knepley {
2254ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2255ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2256ca3d3a14SMatthew G. Knepley   PetscValidPointer(tdm, 2);
2257ca3d3a14SMatthew G. Knepley   *tdm = dm->transformDM;
2258ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2259ca3d3a14SMatthew G. Knepley }
2260ca3d3a14SMatthew G. Knepley 
2261ca3d3a14SMatthew G. Knepley PetscErrorCode DMGetBasisTransformVec_Internal(DM dm, Vec *tv)
2262ca3d3a14SMatthew G. Knepley {
2263ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2264ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2265ca3d3a14SMatthew G. Knepley   PetscValidPointer(tv, 2);
2266ca3d3a14SMatthew G. Knepley   *tv = dm->transform;
2267ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2268ca3d3a14SMatthew G. Knepley }
2269ca3d3a14SMatthew G. Knepley 
2270ca3d3a14SMatthew G. Knepley /*@
2271c0f8e1fdSMatthew G. Knepley   DMHasBasisTransform - Whether we employ a basis transformation from functions in global vectors to functions in local vectors
2272ca3d3a14SMatthew G. Knepley 
2273ca3d3a14SMatthew G. Knepley   Input Parameter:
2274ca3d3a14SMatthew G. Knepley . dm - The DM
2275ca3d3a14SMatthew G. Knepley 
2276ca3d3a14SMatthew G. Knepley   Output Parameter:
2277ca3d3a14SMatthew G. Knepley . flg - PETSC_TRUE if a basis transformation should be done
2278ca3d3a14SMatthew G. Knepley 
2279ca3d3a14SMatthew G. Knepley   Level: developer
2280ca3d3a14SMatthew G. Knepley 
2281436bc73aSJed Brown .seealso: DMPlexGlobalToLocalBasis(), DMPlexLocalToGlobalBasis(), DMPlexCreateBasisRotation()
2282ca3d3a14SMatthew G. Knepley @*/
2283ca3d3a14SMatthew G. Knepley PetscErrorCode DMHasBasisTransform(DM dm, PetscBool *flg)
2284ca3d3a14SMatthew G. Knepley {
2285ca3d3a14SMatthew G. Knepley   Vec            tv;
2286ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2287ca3d3a14SMatthew G. Knepley 
2288ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2289ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2290534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg, 2);
2291ca3d3a14SMatthew G. Knepley   ierr = DMGetBasisTransformVec_Internal(dm, &tv);CHKERRQ(ierr);
2292ca3d3a14SMatthew G. Knepley   *flg = tv ? PETSC_TRUE : PETSC_FALSE;
2293ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2294ca3d3a14SMatthew G. Knepley }
2295ca3d3a14SMatthew G. Knepley 
2296ca3d3a14SMatthew G. Knepley PetscErrorCode DMConstructBasisTransform_Internal(DM dm)
2297ca3d3a14SMatthew G. Knepley {
2298ca3d3a14SMatthew G. Knepley   PetscSection   s, ts;
2299ca3d3a14SMatthew G. Knepley   PetscScalar   *ta;
2300ca3d3a14SMatthew G. Knepley   PetscInt       cdim, pStart, pEnd, p, Nf, f, Nc, dof;
2301ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2302ca3d3a14SMatthew G. Knepley 
2303ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2304ca3d3a14SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
230592fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
2306ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
2307ca3d3a14SMatthew G. Knepley   ierr = PetscSectionGetNumFields(s, &Nf);CHKERRQ(ierr);
2308ca3d3a14SMatthew G. Knepley   ierr = DMClone(dm, &dm->transformDM);CHKERRQ(ierr);
230992fd8e1eSJed Brown   ierr = DMGetLocalSection(dm->transformDM, &ts);CHKERRQ(ierr);
2310ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetNumFields(ts, Nf);CHKERRQ(ierr);
2311ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetChart(ts, pStart, pEnd);CHKERRQ(ierr);
2312ca3d3a14SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
2313ca3d3a14SMatthew G. Knepley     ierr = PetscSectionGetFieldComponents(s, f, &Nc);CHKERRQ(ierr);
2314ca3d3a14SMatthew G. Knepley     /* We could start to label fields by their transformation properties */
2315ca3d3a14SMatthew G. Knepley     if (Nc != cdim) continue;
2316ca3d3a14SMatthew G. Knepley     for (p = pStart; p < pEnd; ++p) {
2317ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(s, p, f, &dof);CHKERRQ(ierr);
2318ca3d3a14SMatthew G. Knepley       if (!dof) continue;
2319ca3d3a14SMatthew G. Knepley       ierr = PetscSectionSetFieldDof(ts, p, f, PetscSqr(cdim));CHKERRQ(ierr);
2320ca3d3a14SMatthew G. Knepley       ierr = PetscSectionAddDof(ts, p, PetscSqr(cdim));CHKERRQ(ierr);
2321ca3d3a14SMatthew G. Knepley     }
2322ca3d3a14SMatthew G. Knepley   }
2323ca3d3a14SMatthew G. Knepley   ierr = PetscSectionSetUp(ts);CHKERRQ(ierr);
2324ca3d3a14SMatthew G. Knepley   ierr = DMCreateLocalVector(dm->transformDM, &dm->transform);CHKERRQ(ierr);
2325ca3d3a14SMatthew G. Knepley   ierr = VecGetArray(dm->transform, &ta);CHKERRQ(ierr);
2326ca3d3a14SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
2327ca3d3a14SMatthew G. Knepley     for (f = 0; f < Nf; ++f) {
2328ca3d3a14SMatthew G. Knepley       ierr = PetscSectionGetFieldDof(ts, p, f, &dof);CHKERRQ(ierr);
2329ca3d3a14SMatthew G. Knepley       if (dof) {
2330ca3d3a14SMatthew G. Knepley         PetscReal          x[3] = {0.0, 0.0, 0.0};
2331ca3d3a14SMatthew G. Knepley         PetscScalar       *tva;
2332ca3d3a14SMatthew G. Knepley         const PetscScalar *A;
2333ca3d3a14SMatthew G. Knepley 
2334ca3d3a14SMatthew G. Knepley         /* TODO Get quadrature point for this dual basis vector for coordinate */
2335ca3d3a14SMatthew G. Knepley         ierr = (*dm->transformGetMatrix)(dm, x, PETSC_TRUE, &A, dm->transformCtx);CHKERRQ(ierr);
2336ca3d3a14SMatthew G. Knepley         ierr = DMPlexPointLocalFieldRef(dm->transformDM, p, f, ta, (void *) &tva);CHKERRQ(ierr);
2337580bdb30SBarry Smith         ierr = PetscArraycpy(tva, A, PetscSqr(cdim));CHKERRQ(ierr);
2338ca3d3a14SMatthew G. Knepley       }
2339ca3d3a14SMatthew G. Knepley     }
2340ca3d3a14SMatthew G. Knepley   }
2341ca3d3a14SMatthew G. Knepley   ierr = VecRestoreArray(dm->transform, &ta);CHKERRQ(ierr);
2342ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2343ca3d3a14SMatthew G. Knepley }
2344ca3d3a14SMatthew G. Knepley 
2345ca3d3a14SMatthew G. Knepley PetscErrorCode DMCopyTransform(DM dm, DM newdm)
2346ca3d3a14SMatthew G. Knepley {
2347ca3d3a14SMatthew G. Knepley   PetscErrorCode ierr;
2348ca3d3a14SMatthew G. Knepley 
2349ca3d3a14SMatthew G. Knepley   PetscFunctionBegin;
2350ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
2351ca3d3a14SMatthew G. Knepley   PetscValidHeaderSpecific(newdm, DM_CLASSID, 2);
2352ca3d3a14SMatthew G. Knepley   newdm->transformCtx       = dm->transformCtx;
2353ca3d3a14SMatthew G. Knepley   newdm->transformSetUp     = dm->transformSetUp;
2354ca3d3a14SMatthew G. Knepley   newdm->transformDestroy   = NULL;
2355ca3d3a14SMatthew G. Knepley   newdm->transformGetMatrix = dm->transformGetMatrix;
2356ca3d3a14SMatthew G. Knepley   if (newdm->transformSetUp) {ierr = DMConstructBasisTransform_Internal(newdm);CHKERRQ(ierr);}
2357ca3d3a14SMatthew G. Knepley   PetscFunctionReturn(0);
2358ca3d3a14SMatthew G. Knepley }
2359ca3d3a14SMatthew G. Knepley 
2360bb9467b5SJed Brown /*@C
2361baf369e7SPeter Brune    DMGlobalToLocalHookAdd - adds a callback to be run when global to local is called
2362baf369e7SPeter Brune 
2363baf369e7SPeter Brune    Logically Collective
2364baf369e7SPeter Brune 
2365baf369e7SPeter Brune    Input Arguments:
2366baf369e7SPeter Brune +  dm - the DM
2367baf369e7SPeter Brune .  beginhook - function to run at the beginning of DMGlobalToLocalBegin()
2368baf369e7SPeter Brune .  endhook - function to run after DMGlobalToLocalEnd() has completed
23690298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2370baf369e7SPeter Brune 
2371baf369e7SPeter Brune    Calling sequence for beginhook:
2372baf369e7SPeter Brune $    beginhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2373baf369e7SPeter Brune 
2374baf369e7SPeter Brune +  dm - global DM
2375baf369e7SPeter Brune .  g - global vector
2376baf369e7SPeter Brune .  mode - mode
2377baf369e7SPeter Brune .  l - local vector
2378baf369e7SPeter Brune -  ctx - optional user-defined function context
2379baf369e7SPeter Brune 
2380baf369e7SPeter Brune 
2381baf369e7SPeter Brune    Calling sequence for endhook:
2382ec4806b8SPeter Brune $    endhook(DM fine,VecScatter out,VecScatter in,DM coarse,void *ctx)
2383baf369e7SPeter Brune 
2384baf369e7SPeter Brune +  global - global DM
2385baf369e7SPeter Brune -  ctx - optional user-defined function context
2386baf369e7SPeter Brune 
2387baf369e7SPeter Brune    Level: advanced
2388baf369e7SPeter Brune 
2389baf369e7SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2390baf369e7SPeter Brune @*/
2391baf369e7SPeter Brune PetscErrorCode DMGlobalToLocalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2392baf369e7SPeter Brune {
2393baf369e7SPeter Brune   PetscErrorCode          ierr;
2394baf369e7SPeter Brune   DMGlobalToLocalHookLink link,*p;
2395baf369e7SPeter Brune 
2396baf369e7SPeter Brune   PetscFunctionBegin;
2397baf369e7SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2398baf369e7SPeter Brune   for (p=&dm->gtolhook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
239995dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2400baf369e7SPeter Brune   link->beginhook = beginhook;
2401baf369e7SPeter Brune   link->endhook   = endhook;
2402baf369e7SPeter Brune   link->ctx       = ctx;
24030298fd71SBarry Smith   link->next      = NULL;
2404baf369e7SPeter Brune   *p              = link;
2405baf369e7SPeter Brune   PetscFunctionReturn(0);
2406baf369e7SPeter Brune }
2407baf369e7SPeter Brune 
24084c274da1SToby Isaac static PetscErrorCode DMGlobalToLocalHook_Constraints(DM dm, Vec g, InsertMode mode, Vec l, void *ctx)
24094c274da1SToby Isaac {
24104c274da1SToby Isaac   Mat cMat;
24114c274da1SToby Isaac   Vec cVec;
24124c274da1SToby Isaac   PetscSection section, cSec;
24134c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
24144c274da1SToby Isaac   PetscErrorCode ierr;
24154c274da1SToby Isaac 
24164c274da1SToby Isaac   PetscFunctionBegin;
24174c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
24184c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
24194c274da1SToby Isaac   if (cMat && (mode == INSERT_VALUES || mode == INSERT_ALL_VALUES || mode == INSERT_BC_VALUES)) {
24205db9a05bSToby Isaac     PetscInt nRows;
24215db9a05bSToby Isaac 
24225db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
24235db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
242492fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
24257711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
24264c274da1SToby Isaac     ierr = MatMult(cMat,l,cVec);CHKERRQ(ierr);
24274c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
24284c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
24294c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
24304c274da1SToby Isaac       if (dof) {
24314c274da1SToby Isaac         PetscScalar *vals;
24324c274da1SToby Isaac         ierr = VecGetValuesSection(cVec,cSec,p,&vals);CHKERRQ(ierr);
24334c274da1SToby Isaac         ierr = VecSetValuesSection(l,section,p,vals,INSERT_ALL_VALUES);CHKERRQ(ierr);
24344c274da1SToby Isaac       }
24354c274da1SToby Isaac     }
24364c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
24374c274da1SToby Isaac   }
24384c274da1SToby Isaac   PetscFunctionReturn(0);
24394c274da1SToby Isaac }
24404c274da1SToby Isaac 
244147c6ae99SBarry Smith /*@
244201729b5cSPatrick Sanan     DMGlobalToLocal - update local vectors from global vector
244301729b5cSPatrick Sanan 
2444d083f849SBarry Smith     Neighbor-wise Collective on dm
244501729b5cSPatrick Sanan 
244601729b5cSPatrick Sanan     Input Parameters:
244701729b5cSPatrick Sanan +   dm - the DM object
244801729b5cSPatrick Sanan .   g - the global vector
244901729b5cSPatrick Sanan .   mode - INSERT_VALUES or ADD_VALUES
245001729b5cSPatrick Sanan -   l - the local vector
245101729b5cSPatrick Sanan 
245201729b5cSPatrick Sanan     Notes:
245301729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
245401729b5cSPatrick Sanan     DMGlobalToLocalBegin() and DMGlobalToLocalEnd().
245501729b5cSPatrick Sanan 
245601729b5cSPatrick Sanan     Level: beginner
245701729b5cSPatrick Sanan 
245801729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
245901729b5cSPatrick Sanan 
246001729b5cSPatrick Sanan @*/
246101729b5cSPatrick Sanan PetscErrorCode DMGlobalToLocal(DM dm,Vec g,InsertMode mode,Vec l)
246201729b5cSPatrick Sanan {
246301729b5cSPatrick Sanan   PetscErrorCode ierr;
246401729b5cSPatrick Sanan 
246501729b5cSPatrick Sanan   PetscFunctionBegin;
246601729b5cSPatrick Sanan   ierr = DMGlobalToLocalBegin(dm,g,mode,l);CHKERRQ(ierr);
246701729b5cSPatrick Sanan   ierr = DMGlobalToLocalEnd(dm,g,mode,l);CHKERRQ(ierr);
246801729b5cSPatrick Sanan   PetscFunctionReturn(0);
246901729b5cSPatrick Sanan }
247001729b5cSPatrick Sanan 
247101729b5cSPatrick Sanan /*@
247247c6ae99SBarry Smith     DMGlobalToLocalBegin - Begins updating local vectors from global vector
247347c6ae99SBarry Smith 
2474d083f849SBarry Smith     Neighbor-wise Collective on dm
247547c6ae99SBarry Smith 
247647c6ae99SBarry Smith     Input Parameters:
247747c6ae99SBarry Smith +   dm - the DM object
247847c6ae99SBarry Smith .   g - the global vector
247947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
248047c6ae99SBarry Smith -   l - the local vector
248147c6ae99SBarry Smith 
248201729b5cSPatrick Sanan     Level: intermediate
248347c6ae99SBarry Smith 
248401729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
248547c6ae99SBarry Smith 
248647c6ae99SBarry Smith @*/
24877087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
248847c6ae99SBarry Smith {
24897128ae9fSMatthew G Knepley   PetscSF                 sf;
249047c6ae99SBarry Smith   PetscErrorCode          ierr;
2491baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
249247c6ae99SBarry Smith 
2493*d0295fc0SJunchao Zhang 
249447c6ae99SBarry Smith   PetscFunctionBegin;
2495171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2496baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
24978865f1eaSKarl Rupp     if (link->beginhook) {
24988865f1eaSKarl Rupp       ierr = (*link->beginhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);
24998865f1eaSKarl Rupp     }
2500baf369e7SPeter Brune   }
25011bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
25027128ae9fSMatthew G Knepley   if (sf) {
2503ae5cfb4aSMatthew G. Knepley     const PetscScalar *gArray;
2504ae5cfb4aSMatthew G. Knepley     PetscScalar       *lArray;
2505*d0295fc0SJunchao Zhang     PetscMemType      lmtype,gmtype;
25067128ae9fSMatthew G Knepley 
250782f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
2508*d0295fc0SJunchao Zhang     ierr = VecGetArrayInPlace_Internal(l, &lArray, &lmtype);CHKERRQ(ierr);
2509*d0295fc0SJunchao Zhang     ierr = VecGetArrayReadInPlace_Internal(g, &gArray, &gmtype);CHKERRQ(ierr);
2510*d0295fc0SJunchao Zhang     ierr = PetscSFBcastWithMemTypeBegin(sf, MPIU_SCALAR, gmtype, gArray, lmtype, lArray);CHKERRQ(ierr);
2511fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2512fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
25137128ae9fSMatthew G Knepley   } else {
251433907cc2SStefano Zampini     if (!dm->ops->globaltolocalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalBegin() for type %s",((PetscObject)dm)->type_name);
2515843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25167128ae9fSMatthew G Knepley   }
251747c6ae99SBarry Smith   PetscFunctionReturn(0);
251847c6ae99SBarry Smith }
251947c6ae99SBarry Smith 
252047c6ae99SBarry Smith /*@
252147c6ae99SBarry Smith     DMGlobalToLocalEnd - Ends updating local vectors from global vector
252247c6ae99SBarry Smith 
2523d083f849SBarry Smith     Neighbor-wise Collective on dm
252447c6ae99SBarry Smith 
252547c6ae99SBarry Smith     Input Parameters:
252647c6ae99SBarry Smith +   dm - the DM object
252747c6ae99SBarry Smith .   g - the global vector
252847c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
252947c6ae99SBarry Smith -   l - the local vector
253047c6ae99SBarry Smith 
253101729b5cSPatrick Sanan     Level: intermediate
253247c6ae99SBarry Smith 
253301729b5cSPatrick Sanan .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMLocalToGlobalBegin(), DMLocalToGlobal(), DMLocalToGlobalBegin(), DMLocalToGlobalEnd()
253447c6ae99SBarry Smith 
253547c6ae99SBarry Smith @*/
25367087cfbeSBarry Smith PetscErrorCode  DMGlobalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
253747c6ae99SBarry Smith {
25387128ae9fSMatthew G Knepley   PetscSF                 sf;
253947c6ae99SBarry Smith   PetscErrorCode          ierr;
2540ae5cfb4aSMatthew G. Knepley   const PetscScalar      *gArray;
2541ae5cfb4aSMatthew G. Knepley   PetscScalar            *lArray;
2542ca3d3a14SMatthew G. Knepley   PetscBool               transform;
2543baf369e7SPeter Brune   DMGlobalToLocalHookLink link;
2544*d0295fc0SJunchao Zhang   PetscMemType            lmtype,gmtype;
254547c6ae99SBarry Smith 
254647c6ae99SBarry Smith   PetscFunctionBegin;
2547171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
25481bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
2549ca3d3a14SMatthew G. Knepley   ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
25507128ae9fSMatthew G Knepley   if (sf) {
255182f516ccSBarry Smith     if (mode == ADD_VALUES) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
25527128ae9fSMatthew G Knepley 
2553*d0295fc0SJunchao Zhang     ierr = VecGetArrayInPlace_Internal(l, &lArray, &lmtype);CHKERRQ(ierr);
2554*d0295fc0SJunchao Zhang     ierr = VecGetArrayReadInPlace_Internal(g, &gArray, &gmtype);CHKERRQ(ierr);
25557128ae9fSMatthew G Knepley     ierr = PetscSFBcastEnd(sf, MPIU_SCALAR, gArray, lArray);CHKERRQ(ierr);
2556fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(l, &lArray);CHKERRQ(ierr);
2557fa88e482SJed Brown     ierr = VecRestoreArrayReadInPlace(g, &gArray);CHKERRQ(ierr);
2558ca3d3a14SMatthew G. Knepley     if (transform) {ierr = DMPlexGlobalToLocalBasis(dm, l);CHKERRQ(ierr);}
25597128ae9fSMatthew G Knepley   } else {
256033907cc2SStefano Zampini     if (!dm->ops->globaltolocalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMGlobalToLocalEnd() for type %s",((PetscObject)dm)->type_name);
2561843c4018SMatthew G Knepley     ierr = (*dm->ops->globaltolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
25627128ae9fSMatthew G Knepley   }
25634c274da1SToby Isaac   ierr = DMGlobalToLocalHook_Constraints(dm,g,mode,l,NULL);CHKERRQ(ierr);
2564baf369e7SPeter Brune   for (link=dm->gtolhook; link; link=link->next) {
2565baf369e7SPeter Brune     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2566baf369e7SPeter Brune   }
256747c6ae99SBarry Smith   PetscFunctionReturn(0);
256847c6ae99SBarry Smith }
256947c6ae99SBarry Smith 
2570d4d07f1eSToby Isaac /*@C
2571d4d07f1eSToby Isaac    DMLocalToGlobalHookAdd - adds a callback to be run when a local to global is called
2572d4d07f1eSToby Isaac 
2573d4d07f1eSToby Isaac    Logically Collective
2574d4d07f1eSToby Isaac 
2575d4d07f1eSToby Isaac    Input Arguments:
2576d4d07f1eSToby Isaac +  dm - the DM
2577d4d07f1eSToby Isaac .  beginhook - function to run at the beginning of DMLocalToGlobalBegin()
2578d4d07f1eSToby Isaac .  endhook - function to run after DMLocalToGlobalEnd() has completed
2579d4d07f1eSToby Isaac -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
2580d4d07f1eSToby Isaac 
2581d4d07f1eSToby Isaac    Calling sequence for beginhook:
2582d4d07f1eSToby Isaac $    beginhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2583d4d07f1eSToby Isaac 
2584d4d07f1eSToby Isaac +  dm - global DM
2585d4d07f1eSToby Isaac .  l - local vector
2586d4d07f1eSToby Isaac .  mode - mode
2587d4d07f1eSToby Isaac .  g - global vector
2588d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2589d4d07f1eSToby Isaac 
2590d4d07f1eSToby Isaac 
2591d4d07f1eSToby Isaac    Calling sequence for endhook:
2592d4d07f1eSToby Isaac $    endhook(DM fine,Vec l,InsertMode mode,Vec g,void *ctx)
2593d4d07f1eSToby Isaac 
2594d4d07f1eSToby Isaac +  global - global DM
2595d4d07f1eSToby Isaac .  l - local vector
2596d4d07f1eSToby Isaac .  mode - mode
2597d4d07f1eSToby Isaac .  g - global vector
2598d4d07f1eSToby Isaac -  ctx - optional user-defined function context
2599d4d07f1eSToby Isaac 
2600d4d07f1eSToby Isaac    Level: advanced
2601d4d07f1eSToby Isaac 
2602d4d07f1eSToby Isaac .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
2603d4d07f1eSToby Isaac @*/
2604d4d07f1eSToby Isaac PetscErrorCode DMLocalToGlobalHookAdd(DM dm,PetscErrorCode (*beginhook)(DM,Vec,InsertMode,Vec,void*),PetscErrorCode (*endhook)(DM,Vec,InsertMode,Vec,void*),void *ctx)
2605d4d07f1eSToby Isaac {
2606d4d07f1eSToby Isaac   PetscErrorCode          ierr;
2607d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link,*p;
2608d4d07f1eSToby Isaac 
2609d4d07f1eSToby Isaac   PetscFunctionBegin;
2610d4d07f1eSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2611d4d07f1eSToby Isaac   for (p=&dm->ltoghook; *p; p=&(*p)->next) {} /* Scan to the end of the current list of hooks */
261295dccacaSBarry Smith   ierr            = PetscNew(&link);CHKERRQ(ierr);
2613d4d07f1eSToby Isaac   link->beginhook = beginhook;
2614d4d07f1eSToby Isaac   link->endhook   = endhook;
2615d4d07f1eSToby Isaac   link->ctx       = ctx;
2616d4d07f1eSToby Isaac   link->next      = NULL;
2617d4d07f1eSToby Isaac   *p              = link;
2618d4d07f1eSToby Isaac   PetscFunctionReturn(0);
2619d4d07f1eSToby Isaac }
2620d4d07f1eSToby Isaac 
26214c274da1SToby Isaac static PetscErrorCode DMLocalToGlobalHook_Constraints(DM dm, Vec l, InsertMode mode, Vec g, void *ctx)
26224c274da1SToby Isaac {
26234c274da1SToby Isaac   Mat cMat;
26244c274da1SToby Isaac   Vec cVec;
26254c274da1SToby Isaac   PetscSection section, cSec;
26264c274da1SToby Isaac   PetscInt pStart, pEnd, p, dof;
26274c274da1SToby Isaac   PetscErrorCode ierr;
26284c274da1SToby Isaac 
26294c274da1SToby Isaac   PetscFunctionBegin;
26304c274da1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
26314c274da1SToby Isaac   ierr = DMGetDefaultConstraints(dm,&cSec,&cMat);CHKERRQ(ierr);
26324c274da1SToby Isaac   if (cMat && (mode == ADD_VALUES || mode == ADD_ALL_VALUES || mode == ADD_BC_VALUES)) {
26335db9a05bSToby Isaac     PetscInt nRows;
26345db9a05bSToby Isaac 
26355db9a05bSToby Isaac     ierr = MatGetSize(cMat,&nRows,NULL);CHKERRQ(ierr);
26365db9a05bSToby Isaac     if (nRows <= 0) PetscFunctionReturn(0);
263792fd8e1eSJed Brown     ierr = DMGetLocalSection(dm,&section);CHKERRQ(ierr);
26387711e48fSToby Isaac     ierr = MatCreateVecs(cMat,NULL,&cVec);CHKERRQ(ierr);
26394c274da1SToby Isaac     ierr = PetscSectionGetChart(cSec,&pStart,&pEnd);CHKERRQ(ierr);
26404c274da1SToby Isaac     for (p = pStart; p < pEnd; p++) {
26414c274da1SToby Isaac       ierr = PetscSectionGetDof(cSec,p,&dof);CHKERRQ(ierr);
26424c274da1SToby Isaac       if (dof) {
26434c274da1SToby Isaac         PetscInt d;
26444c274da1SToby Isaac         PetscScalar *vals;
26454c274da1SToby Isaac         ierr = VecGetValuesSection(l,section,p,&vals);CHKERRQ(ierr);
26464c274da1SToby Isaac         ierr = VecSetValuesSection(cVec,cSec,p,vals,mode);CHKERRQ(ierr);
26474c274da1SToby Isaac         /* for this to be the true transpose, we have to zero the values that
26484c274da1SToby Isaac          * we just extracted */
26494c274da1SToby Isaac         for (d = 0; d < dof; d++) {
26504c274da1SToby Isaac           vals[d] = 0.;
26514c274da1SToby Isaac         }
26524c274da1SToby Isaac       }
26534c274da1SToby Isaac     }
26544c274da1SToby Isaac     ierr = MatMultTransposeAdd(cMat,cVec,l,l);CHKERRQ(ierr);
26554c274da1SToby Isaac     ierr = VecDestroy(&cVec);CHKERRQ(ierr);
26564c274da1SToby Isaac   }
26574c274da1SToby Isaac   PetscFunctionReturn(0);
26584c274da1SToby Isaac }
265901729b5cSPatrick Sanan /*@
266001729b5cSPatrick Sanan     DMLocalToGlobal - updates global vectors from local vectors
266101729b5cSPatrick Sanan 
2662d083f849SBarry Smith     Neighbor-wise Collective on dm
266301729b5cSPatrick Sanan 
266401729b5cSPatrick Sanan     Input Parameters:
266501729b5cSPatrick Sanan +   dm - the DM object
266601729b5cSPatrick Sanan .   l - the local vector
266701729b5cSPatrick 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.
266801729b5cSPatrick Sanan -   g - the global vector
266901729b5cSPatrick Sanan 
267001729b5cSPatrick Sanan     Notes:
267101729b5cSPatrick Sanan     The communication involved in this update can be overlapped with computation by using
267201729b5cSPatrick Sanan     DMLocalToGlobalBegin() and DMLocalToGlobalEnd().
267301729b5cSPatrick Sanan 
267401729b5cSPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
267501729b5cSPatrick 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.
267601729b5cSPatrick Sanan 
267701729b5cSPatrick Sanan     Level: beginner
267801729b5cSPatrick Sanan 
267901729b5cSPatrick Sanan .seealso DMLocalToGlobalBegin(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
268001729b5cSPatrick Sanan 
268101729b5cSPatrick Sanan @*/
268201729b5cSPatrick Sanan PetscErrorCode DMLocalToGlobal(DM dm,Vec l,InsertMode mode,Vec g)
268301729b5cSPatrick Sanan {
268401729b5cSPatrick Sanan   PetscErrorCode ierr;
268501729b5cSPatrick Sanan 
268601729b5cSPatrick Sanan   PetscFunctionBegin;
268701729b5cSPatrick Sanan   ierr = DMLocalToGlobalBegin(dm,l,mode,g);CHKERRQ(ierr);
268801729b5cSPatrick Sanan   ierr = DMLocalToGlobalEnd(dm,l,mode,g);CHKERRQ(ierr);
268901729b5cSPatrick Sanan   PetscFunctionReturn(0);
269001729b5cSPatrick Sanan }
26914c274da1SToby Isaac 
269247c6ae99SBarry Smith /*@
269301729b5cSPatrick Sanan     DMLocalToGlobalBegin - begins updating global vectors from local vectors
26949a42bb27SBarry Smith 
2695d083f849SBarry Smith     Neighbor-wise Collective on dm
26969a42bb27SBarry Smith 
26979a42bb27SBarry Smith     Input Parameters:
26989a42bb27SBarry Smith +   dm - the DM object
2699f6813fd5SJed Brown .   l - the local vector
27001eb28f2eSBarry 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.
27011eb28f2eSBarry Smith -   g - the global vector
27029a42bb27SBarry Smith 
270395452b02SPatrick Sanan     Notes:
270495452b02SPatrick Sanan     In the ADD_VALUES case you normally would zero the receiving vector before beginning this operation.
270584330215SMatthew 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.
27069a42bb27SBarry Smith 
270701729b5cSPatrick Sanan     Level: intermediate
27089a42bb27SBarry Smith 
270901729b5cSPatrick Sanan .seealso DMLocalToGlobal(), DMLocalToGlobalEnd(), DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocal(), DMGlobalToLocalEnd(), DMGlobalToLocalBegin()
27109a42bb27SBarry Smith 
27119a42bb27SBarry Smith @*/
27127087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalBegin(DM dm,Vec l,InsertMode mode,Vec g)
27139a42bb27SBarry Smith {
27147128ae9fSMatthew G Knepley   PetscSF                 sf;
271584330215SMatthew G. Knepley   PetscSection            s, gs;
2716d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2717ca3d3a14SMatthew G. Knepley   Vec                     tmpl;
2718ae5cfb4aSMatthew G. Knepley   const PetscScalar      *lArray;
2719ae5cfb4aSMatthew G. Knepley   PetscScalar            *gArray;
2720fa88e482SJed Brown   PetscBool               isInsert, transform, l_inplace = PETSC_FALSE, g_inplace = PETSC_FALSE;
272184330215SMatthew G. Knepley   PetscErrorCode          ierr;
2722*d0295fc0SJunchao Zhang   PetscMemType            lmtype=PETSC_MEMTYPE_HOST,gmtype=PETSC_MEMTYPE_HOST;
27239a42bb27SBarry Smith 
27249a42bb27SBarry Smith   PetscFunctionBegin;
2725171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2726d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2727d4d07f1eSToby Isaac     if (link->beginhook) {
2728d4d07f1eSToby Isaac       ierr = (*link->beginhook)(dm,l,mode,g,link->ctx);CHKERRQ(ierr);
2729d4d07f1eSToby Isaac     }
2730d4d07f1eSToby Isaac   }
27314c274da1SToby Isaac   ierr = DMLocalToGlobalHook_Constraints(dm,l,mode,g,NULL);CHKERRQ(ierr);
27321bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
273392fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
27347128ae9fSMatthew G Knepley   switch (mode) {
27357128ae9fSMatthew G Knepley   case INSERT_VALUES:
27367128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
2737304ab55fSMatthew G. Knepley   case INSERT_BC_VALUES:
273884330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
27397128ae9fSMatthew G Knepley   case ADD_VALUES:
27407128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
2741304ab55fSMatthew G. Knepley   case ADD_BC_VALUES:
274284330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
27437128ae9fSMatthew G Knepley   default:
274482f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
27457128ae9fSMatthew G Knepley   }
2746ca3d3a14SMatthew G. Knepley   if ((sf && !isInsert) || (s && isInsert)) {
2747ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2748ca3d3a14SMatthew G. Knepley     if (transform) {
2749ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2750ca3d3a14SMatthew G. Knepley       ierr = VecCopy(l, tmpl);CHKERRQ(ierr);
2751ca3d3a14SMatthew G. Knepley       ierr = DMPlexLocalToGlobalBasis(dm, tmpl);CHKERRQ(ierr);
2752ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2753fa88e482SJed Brown     } else if (isInsert) {
2754ae5cfb4aSMatthew G. Knepley       ierr = VecGetArrayRead(l, &lArray);CHKERRQ(ierr);
2755fa88e482SJed Brown     } else {
2756*d0295fc0SJunchao Zhang       ierr = VecGetArrayReadInPlace_Internal(l, &lArray, &lmtype);CHKERRQ(ierr);
2757fa88e482SJed Brown       l_inplace = PETSC_TRUE;
2758ca3d3a14SMatthew G. Knepley     }
2759fa88e482SJed Brown     if (s && isInsert) {
27607128ae9fSMatthew G Knepley       ierr = VecGetArray(g, &gArray);CHKERRQ(ierr);
2761fa88e482SJed Brown     } else {
2762*d0295fc0SJunchao Zhang       ierr = VecGetArrayInPlace_Internal(g, &gArray, &gmtype);CHKERRQ(ierr);
2763fa88e482SJed Brown       g_inplace = PETSC_TRUE;
2764fa88e482SJed Brown     }
2765ca3d3a14SMatthew G. Knepley     if (sf && !isInsert) {
2766*d0295fc0SJunchao Zhang       ierr = PetscSFReduceWithMemTypeBegin(sf, MPIU_SCALAR, lmtype, lArray, gmtype, gArray, MPIU_SUM);CHKERRQ(ierr);
276784330215SMatthew G. Knepley     } else if (s && isInsert) {
276884330215SMatthew G. Knepley       PetscInt gStart, pStart, pEnd, p;
276984330215SMatthew G. Knepley 
2770e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gs);CHKERRQ(ierr);
277184330215SMatthew G. Knepley       ierr = PetscSectionGetChart(s, &pStart, &pEnd);CHKERRQ(ierr);
277284330215SMatthew G. Knepley       ierr = VecGetOwnershipRange(g, &gStart, NULL);CHKERRQ(ierr);
277384330215SMatthew G. Knepley       for (p = pStart; p < pEnd; ++p) {
2774b3b16f48SMatthew G. Knepley         PetscInt dof, gdof, cdof, gcdof, off, goff, d, e;
277584330215SMatthew G. Knepley 
277684330215SMatthew G. Knepley         ierr = PetscSectionGetDof(s, p, &dof);CHKERRQ(ierr);
277703442857SMatthew G. Knepley         ierr = PetscSectionGetDof(gs, p, &gdof);CHKERRQ(ierr);
277884330215SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(s, p, &cdof);CHKERRQ(ierr);
2779b3b16f48SMatthew G. Knepley         ierr = PetscSectionGetConstraintDof(gs, p, &gcdof);CHKERRQ(ierr);
278084330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(s, p, &off);CHKERRQ(ierr);
278184330215SMatthew G. Knepley         ierr = PetscSectionGetOffset(gs, p, &goff);CHKERRQ(ierr);
2782b3b16f48SMatthew G. Knepley         /* Ignore off-process data and points with no global data */
278303442857SMatthew G. Knepley         if (!gdof || goff < 0) continue;
2784b3b16f48SMatthew 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);
2785b3b16f48SMatthew G. Knepley         /* If no constraints are enforced in the global vector */
2786b3b16f48SMatthew G. Knepley         if (!gcdof) {
278784330215SMatthew G. Knepley           for (d = 0; d < dof; ++d) gArray[goff-gStart+d] = lArray[off+d];
2788b3b16f48SMatthew G. Knepley           /* If constraints are enforced in the global vector */
2789b3b16f48SMatthew G. Knepley         } else if (cdof == gcdof) {
279084330215SMatthew G. Knepley           const PetscInt *cdofs;
279184330215SMatthew G. Knepley           PetscInt        cind = 0;
279284330215SMatthew G. Knepley 
279384330215SMatthew G. Knepley           ierr = PetscSectionGetConstraintIndices(s, p, &cdofs);CHKERRQ(ierr);
2794b3b16f48SMatthew G. Knepley           for (d = 0, e = 0; d < dof; ++d) {
279584330215SMatthew G. Knepley             if ((cind < cdof) && (d == cdofs[cind])) {++cind; continue;}
2796b3b16f48SMatthew G. Knepley             gArray[goff-gStart+e++] = lArray[off+d];
279784330215SMatthew G. Knepley           }
2798b3b16f48SMatthew 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);
279984330215SMatthew G. Knepley       }
2800ca3d3a14SMatthew G. Knepley     }
2801fa88e482SJed Brown     if (g_inplace) {
2802fa88e482SJed Brown       ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
2803fa88e482SJed Brown     } else {
28047128ae9fSMatthew G Knepley       ierr = VecRestoreArray(g, &gArray);CHKERRQ(ierr);
2805fa88e482SJed Brown     }
2806ca3d3a14SMatthew G. Knepley     if (transform) {
2807ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2808ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2809fa88e482SJed Brown     } else if (l_inplace) {
2810fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2811ca3d3a14SMatthew G. Knepley     } else {
2812ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(l, &lArray);CHKERRQ(ierr);
2813ca3d3a14SMatthew G. Knepley     }
28147128ae9fSMatthew G Knepley   } else {
2815b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalbegin) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalBegin() for type %s",((PetscObject)dm)->type_name);
2816843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalbegin)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28177128ae9fSMatthew G Knepley   }
28189a42bb27SBarry Smith   PetscFunctionReturn(0);
28199a42bb27SBarry Smith }
28209a42bb27SBarry Smith 
28219a42bb27SBarry Smith /*@
28229a42bb27SBarry Smith     DMLocalToGlobalEnd - updates global vectors from local vectors
282347c6ae99SBarry Smith 
2824d083f849SBarry Smith     Neighbor-wise Collective on dm
282547c6ae99SBarry Smith 
282647c6ae99SBarry Smith     Input Parameters:
282747c6ae99SBarry Smith +   dm - the DM object
2828f6813fd5SJed Brown .   l - the local vector
282947c6ae99SBarry Smith .   mode - INSERT_VALUES or ADD_VALUES
2830f6813fd5SJed Brown -   g - the global vector
283147c6ae99SBarry Smith 
283201729b5cSPatrick Sanan     Level: intermediate
283347c6ae99SBarry Smith 
2834e727c939SJed Brown .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMGlobalToLocalEnd(), DMGlobalToLocalEnd()
283547c6ae99SBarry Smith 
283647c6ae99SBarry Smith @*/
28377087cfbeSBarry Smith PetscErrorCode  DMLocalToGlobalEnd(DM dm,Vec l,InsertMode mode,Vec g)
283847c6ae99SBarry Smith {
28397128ae9fSMatthew G Knepley   PetscSF                 sf;
284084330215SMatthew G. Knepley   PetscSection            s;
2841d4d07f1eSToby Isaac   DMLocalToGlobalHookLink link;
2842ca3d3a14SMatthew G. Knepley   PetscBool               isInsert, transform;
284384330215SMatthew G. Knepley   PetscErrorCode          ierr;
284447c6ae99SBarry Smith 
284547c6ae99SBarry Smith   PetscFunctionBegin;
2846171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
28471bb6d2a8SBarry Smith   ierr = DMGetSectionSF(dm, &sf);CHKERRQ(ierr);
284892fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
28497128ae9fSMatthew G Knepley   switch (mode) {
28507128ae9fSMatthew G Knepley   case INSERT_VALUES:
28517128ae9fSMatthew G Knepley   case INSERT_ALL_VALUES:
285284330215SMatthew G. Knepley     isInsert = PETSC_TRUE; break;
28537128ae9fSMatthew G Knepley   case ADD_VALUES:
28547128ae9fSMatthew G Knepley   case ADD_ALL_VALUES:
285584330215SMatthew G. Knepley     isInsert = PETSC_FALSE; break;
28567128ae9fSMatthew G Knepley   default:
285782f516ccSBarry Smith     SETERRQ1(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid insertion mode %D", mode);
28587128ae9fSMatthew G Knepley   }
285984330215SMatthew G. Knepley   if (sf && !isInsert) {
2860ae5cfb4aSMatthew G. Knepley     const PetscScalar *lArray;
2861ae5cfb4aSMatthew G. Knepley     PetscScalar       *gArray;
2862ca3d3a14SMatthew G. Knepley     Vec                tmpl;
286384330215SMatthew G. Knepley 
2864ca3d3a14SMatthew G. Knepley     ierr = DMHasBasisTransform(dm, &transform);CHKERRQ(ierr);
2865ca3d3a14SMatthew G. Knepley     if (transform) {
2866ca3d3a14SMatthew G. Knepley       ierr = DMGetNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2867ca3d3a14SMatthew G. Knepley       ierr = VecGetArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2868ca3d3a14SMatthew G. Knepley     } else {
2869*d0295fc0SJunchao Zhang       ierr = VecGetArrayReadInPlace_Internal(l, &lArray, NULL);CHKERRQ(ierr);
2870ca3d3a14SMatthew G. Knepley     }
2871*d0295fc0SJunchao Zhang     ierr = VecGetArrayInPlace_Internal(g, &gArray, NULL);CHKERRQ(ierr);
2872a9b180a6SBarry Smith     ierr = PetscSFReduceEnd(sf, MPIU_SCALAR, lArray, gArray, MPIU_SUM);CHKERRQ(ierr);
2873ca3d3a14SMatthew G. Knepley     if (transform) {
2874ca3d3a14SMatthew G. Knepley       ierr = VecRestoreArrayRead(tmpl, &lArray);CHKERRQ(ierr);
2875ca3d3a14SMatthew G. Knepley       ierr = DMRestoreNamedLocalVector(dm, "__petsc_dm_transform_local_copy", &tmpl);CHKERRQ(ierr);
2876ca3d3a14SMatthew G. Knepley     } else {
2877fa88e482SJed Brown       ierr = VecRestoreArrayReadInPlace(l, &lArray);CHKERRQ(ierr);
2878ca3d3a14SMatthew G. Knepley     }
2879fa88e482SJed Brown     ierr = VecRestoreArrayInPlace(g, &gArray);CHKERRQ(ierr);
288084330215SMatthew G. Knepley   } else if (s && isInsert) {
28817128ae9fSMatthew G Knepley   } else {
2882b9d85ea2SLisandro Dalcin     if (!dm->ops->localtoglobalend) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Missing DMLocalToGlobalEnd() for type %s",((PetscObject)dm)->type_name);
2883843c4018SMatthew G Knepley     ierr = (*dm->ops->localtoglobalend)(dm,l,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),g);CHKERRQ(ierr);
28847128ae9fSMatthew G Knepley   }
2885d4d07f1eSToby Isaac   for (link=dm->ltoghook; link; link=link->next) {
2886d4d07f1eSToby Isaac     if (link->endhook) {ierr = (*link->endhook)(dm,g,mode,l,link->ctx);CHKERRQ(ierr);}
2887d4d07f1eSToby Isaac   }
288847c6ae99SBarry Smith   PetscFunctionReturn(0);
288947c6ae99SBarry Smith }
289047c6ae99SBarry Smith 
2891f089877aSRichard Tran Mills /*@
2892bc0a1609SRichard Tran Mills    DMLocalToLocalBegin - Maps from a local vector (including ghost points
2893bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2894d78e899eSRichard Tran Mills    points in the second are set correctly. Must be followed by DMLocalToLocalEnd().
2895f089877aSRichard Tran Mills 
2896d083f849SBarry Smith    Neighbor-wise Collective on dm
2897f089877aSRichard Tran Mills 
2898f089877aSRichard Tran Mills    Input Parameters:
2899f089877aSRichard Tran Mills +  dm - the DM object
2900bc0a1609SRichard Tran Mills .  g - the original local vector
2901bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2902f089877aSRichard Tran Mills 
2903bc0a1609SRichard Tran Mills    Output Parameter:
2904bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2905f089877aSRichard Tran Mills 
2906f089877aSRichard Tran Mills    Level: intermediate
2907f089877aSRichard Tran Mills 
2908bc0a1609SRichard Tran Mills    Notes:
2909bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2910bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2911bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2912bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2913bc0a1609SRichard Tran Mills 
2914bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalEnd(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2915f089877aSRichard Tran Mills 
2916f089877aSRichard Tran Mills @*/
2917f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalBegin(DM dm,Vec g,InsertMode mode,Vec l)
2918f089877aSRichard Tran Mills {
2919f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2920f089877aSRichard Tran Mills 
2921f089877aSRichard Tran Mills   PetscFunctionBegin;
2922f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2923bb358533SPatrick Sanan   if (!dm->ops->localtolocalbegin) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2924f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalbegin)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2925f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2926f089877aSRichard Tran Mills }
2927f089877aSRichard Tran Mills 
2928f089877aSRichard Tran Mills /*@
2929bc0a1609SRichard Tran Mills    DMLocalToLocalEnd - Maps from a local vector (including ghost points
2930bc0a1609SRichard Tran Mills    that contain irrelevant values) to another local vector where the ghost
2931d78e899eSRichard Tran Mills    points in the second are set correctly. Must be preceded by DMLocalToLocalBegin().
2932f089877aSRichard Tran Mills 
2933d083f849SBarry Smith    Neighbor-wise Collective on dm
2934f089877aSRichard Tran Mills 
2935f089877aSRichard Tran Mills    Input Parameters:
2936bc0a1609SRichard Tran Mills +  da - the DM object
2937bc0a1609SRichard Tran Mills .  g - the original local vector
2938bc0a1609SRichard Tran Mills -  mode - one of INSERT_VALUES or ADD_VALUES
2939f089877aSRichard Tran Mills 
2940bc0a1609SRichard Tran Mills    Output Parameter:
2941bc0a1609SRichard Tran Mills .  l  - the local vector with correct ghost values
2942f089877aSRichard Tran Mills 
2943f089877aSRichard Tran Mills    Level: intermediate
2944f089877aSRichard Tran Mills 
2945bc0a1609SRichard Tran Mills    Notes:
2946bc0a1609SRichard Tran Mills    The local vectors used here need not be the same as those
2947bc0a1609SRichard Tran Mills    obtained from DMCreateLocalVector(), BUT they
2948bc0a1609SRichard Tran Mills    must have the same parallel data layout; they could, for example, be
2949bc0a1609SRichard Tran Mills    obtained with VecDuplicate() from the DM originating vectors.
2950bc0a1609SRichard Tran Mills 
2951bc0a1609SRichard Tran Mills .seealso DMCoarsen(), DMDestroy(), DMView(), DMCreateLocalVector(), DMCreateGlobalVector(), DMCreateInterpolation(), DMLocalToLocalBegin(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin()
2952f089877aSRichard Tran Mills 
2953f089877aSRichard Tran Mills @*/
2954f089877aSRichard Tran Mills PetscErrorCode  DMLocalToLocalEnd(DM dm,Vec g,InsertMode mode,Vec l)
2955f089877aSRichard Tran Mills {
2956f089877aSRichard Tran Mills   PetscErrorCode          ierr;
2957f089877aSRichard Tran Mills 
2958f089877aSRichard Tran Mills   PetscFunctionBegin;
2959f089877aSRichard Tran Mills   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2960bb358533SPatrick Sanan   if (!dm->ops->localtolocalend) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"This DM does not support local to local maps");
2961f089877aSRichard Tran Mills   ierr = (*dm->ops->localtolocalend)(dm,g,mode == INSERT_ALL_VALUES ? INSERT_VALUES : (mode == ADD_ALL_VALUES ? ADD_VALUES : mode),l);CHKERRQ(ierr);
2962f089877aSRichard Tran Mills   PetscFunctionReturn(0);
2963f089877aSRichard Tran Mills }
2964f089877aSRichard Tran Mills 
2965f089877aSRichard Tran Mills 
296647c6ae99SBarry Smith /*@
296747c6ae99SBarry Smith     DMCoarsen - Coarsens a DM object
296847c6ae99SBarry Smith 
2969d083f849SBarry Smith     Collective on dm
297047c6ae99SBarry Smith 
297147c6ae99SBarry Smith     Input Parameter:
297247c6ae99SBarry Smith +   dm - the DM object
297391d95f02SJed Brown -   comm - the communicator to contain the new DM object (or MPI_COMM_NULL)
297447c6ae99SBarry Smith 
297547c6ae99SBarry Smith     Output Parameter:
297647c6ae99SBarry Smith .   dmc - the coarsened DM
297747c6ae99SBarry Smith 
297847c6ae99SBarry Smith     Level: developer
297947c6ae99SBarry Smith 
2980e727c939SJed Brown .seealso DMRefine(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
298147c6ae99SBarry Smith 
298247c6ae99SBarry Smith @*/
29837087cfbeSBarry Smith PetscErrorCode DMCoarsen(DM dm, MPI_Comm comm, DM *dmc)
298447c6ae99SBarry Smith {
298547c6ae99SBarry Smith   PetscErrorCode    ierr;
2986b17ce1afSJed Brown   DMCoarsenHookLink link;
298747c6ae99SBarry Smith 
298847c6ae99SBarry Smith   PetscFunctionBegin;
2989171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
2990b9d85ea2SLisandro Dalcin   if (!dm->ops->coarsen) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMCoarsen",((PetscObject)dm)->type_name);
299147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
299247c6ae99SBarry Smith   ierr = (*dm->ops->coarsen)(dm, comm, dmc);CHKERRQ(ierr);
2993b9d85ea2SLisandro Dalcin   if (*dmc) {
2994a8fb8f29SToby Isaac     ierr = DMSetCoarseDM(dm,*dmc);CHKERRQ(ierr);
299543842a1eSJed Brown     (*dmc)->ops->creatematrix = dm->ops->creatematrix;
29968cd211a4SJed Brown     ierr                      = PetscObjectCopyFortranFunctionPointers((PetscObject)dm,(PetscObject)*dmc);CHKERRQ(ierr);
2997644e2e5bSBarry Smith     (*dmc)->ctx               = dm->ctx;
29980598a293SJed Brown     (*dmc)->levelup           = dm->levelup;
2999656b349aSBarry Smith     (*dmc)->leveldown         = dm->leveldown + 1;
3000e4b4b23bSJed Brown     ierr                      = DMSetMatType(*dmc,dm->mattype);CHKERRQ(ierr);
3001b17ce1afSJed Brown     for (link=dm->coarsenhook; link; link=link->next) {
3002b17ce1afSJed Brown       if (link->coarsenhook) {ierr = (*link->coarsenhook)(dm,*dmc,link->ctx);CHKERRQ(ierr);}
3003b17ce1afSJed Brown     }
3004b9d85ea2SLisandro Dalcin   }
300547a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_Coarsen,dm,0,0,0);CHKERRQ(ierr);
3006b9d85ea2SLisandro Dalcin   if (!(*dmc)) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "NULL coarse mesh produced");
3007b17ce1afSJed Brown   PetscFunctionReturn(0);
3008b17ce1afSJed Brown }
3009b17ce1afSJed Brown 
3010bb9467b5SJed Brown /*@C
3011b17ce1afSJed Brown    DMCoarsenHookAdd - adds a callback to be run when restricting a nonlinear problem to the coarse grid
3012b17ce1afSJed Brown 
3013b17ce1afSJed Brown    Logically Collective
3014b17ce1afSJed Brown 
3015b17ce1afSJed Brown    Input Arguments:
3016b17ce1afSJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3017b17ce1afSJed Brown .  coarsenhook - function to run when setting up a coarser level
3018b17ce1afSJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
30190298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3020b17ce1afSJed Brown 
3021b17ce1afSJed Brown    Calling sequence of coarsenhook:
3022b17ce1afSJed Brown $    coarsenhook(DM fine,DM coarse,void *ctx);
3023b17ce1afSJed Brown 
3024b17ce1afSJed Brown +  fine - fine level DM
3025b17ce1afSJed Brown .  coarse - coarse level DM to restrict problem to
3026b17ce1afSJed Brown -  ctx - optional user-defined function context
3027b17ce1afSJed Brown 
3028b17ce1afSJed Brown    Calling sequence for restricthook:
3029c833c3b5SJed Brown $    restricthook(DM fine,Mat mrestrict,Vec rscale,Mat inject,DM coarse,void *ctx)
3030b17ce1afSJed Brown 
3031b17ce1afSJed Brown +  fine - fine level DM
3032b17ce1afSJed Brown .  mrestrict - matrix restricting a fine-level solution to the coarse grid
3033c833c3b5SJed Brown .  rscale - scaling vector for restriction
3034c833c3b5SJed Brown .  inject - matrix restricting by injection
3035b17ce1afSJed Brown .  coarse - coarse level DM to update
3036b17ce1afSJed Brown -  ctx - optional user-defined function context
3037b17ce1afSJed Brown 
3038b17ce1afSJed Brown    Level: advanced
3039b17ce1afSJed Brown 
3040b17ce1afSJed Brown    Notes:
3041b17ce1afSJed Brown    This function is only needed if auxiliary data needs to be set up on coarse grids.
3042b17ce1afSJed Brown 
3043b17ce1afSJed Brown    If this function is called multiple times, the hooks will be run in the order they are added.
3044b17ce1afSJed Brown 
3045b17ce1afSJed Brown    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3046b17ce1afSJed Brown    extract the finest level information from its context (instead of from the SNES).
3047b17ce1afSJed Brown 
3048bb9467b5SJed Brown    This function is currently not available from Fortran.
3049bb9467b5SJed Brown 
3050dc822a44SJed Brown .seealso: DMCoarsenHookRemove(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3051b17ce1afSJed Brown @*/
3052b17ce1afSJed Brown PetscErrorCode DMCoarsenHookAdd(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3053b17ce1afSJed Brown {
3054b17ce1afSJed Brown   PetscErrorCode    ierr;
3055b17ce1afSJed Brown   DMCoarsenHookLink link,*p;
3056b17ce1afSJed Brown 
3057b17ce1afSJed Brown   PetscFunctionBegin;
3058b17ce1afSJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
30591e3d8eccSJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
30601e3d8eccSJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
30611e3d8eccSJed Brown   }
306295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
3063b17ce1afSJed Brown   link->coarsenhook  = coarsenhook;
3064b17ce1afSJed Brown   link->restricthook = restricthook;
3065b17ce1afSJed Brown   link->ctx          = ctx;
30660298fd71SBarry Smith   link->next         = NULL;
3067b17ce1afSJed Brown   *p                 = link;
3068b17ce1afSJed Brown   PetscFunctionReturn(0);
3069b17ce1afSJed Brown }
3070b17ce1afSJed Brown 
3071dc822a44SJed Brown /*@C
3072dc822a44SJed Brown    DMCoarsenHookRemove - remove a callback from the list of hooks to be run when restricting a nonlinear problem to the coarse grid
3073dc822a44SJed Brown 
3074dc822a44SJed Brown    Logically Collective
3075dc822a44SJed Brown 
3076dc822a44SJed Brown    Input Arguments:
3077dc822a44SJed Brown +  fine - nonlinear solver context on which to run a hook when restricting to a coarser level
3078dc822a44SJed Brown .  coarsenhook - function to run when setting up a coarser level
3079dc822a44SJed Brown .  restricthook - function to run to update data on coarser levels (once per SNESSolve())
3080dc822a44SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3081dc822a44SJed Brown 
3082dc822a44SJed Brown    Level: advanced
3083dc822a44SJed Brown 
3084dc822a44SJed Brown    Notes:
3085dc822a44SJed Brown    This function does nothing if the hook is not in the list.
3086dc822a44SJed Brown 
3087dc822a44SJed Brown    This function is currently not available from Fortran.
3088dc822a44SJed Brown 
3089dc822a44SJed Brown .seealso: DMCoarsenHookAdd(), DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3090dc822a44SJed Brown @*/
3091dc822a44SJed Brown PetscErrorCode DMCoarsenHookRemove(DM fine,PetscErrorCode (*coarsenhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,Mat,Vec,Mat,DM,void*),void *ctx)
3092dc822a44SJed Brown {
3093dc822a44SJed Brown   PetscErrorCode    ierr;
3094dc822a44SJed Brown   DMCoarsenHookLink link,*p;
3095dc822a44SJed Brown 
3096dc822a44SJed Brown   PetscFunctionBegin;
3097dc822a44SJed Brown   PetscValidHeaderSpecific(fine,DM_CLASSID,1);
3098dc822a44SJed Brown   for (p=&fine->coarsenhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3099dc822a44SJed Brown     if ((*p)->coarsenhook == coarsenhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3100dc822a44SJed Brown       link = *p;
3101dc822a44SJed Brown       *p = link->next;
3102dc822a44SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3103dc822a44SJed Brown       break;
3104dc822a44SJed Brown     }
3105dc822a44SJed Brown   }
3106dc822a44SJed Brown   PetscFunctionReturn(0);
3107dc822a44SJed Brown }
3108dc822a44SJed Brown 
3109dc822a44SJed Brown 
3110b17ce1afSJed Brown /*@
3111b17ce1afSJed Brown    DMRestrict - restricts user-defined problem data to a coarser DM by running hooks registered by DMCoarsenHookAdd()
3112b17ce1afSJed Brown 
3113b17ce1afSJed Brown    Collective if any hooks are
3114b17ce1afSJed Brown 
3115b17ce1afSJed Brown    Input Arguments:
3116b17ce1afSJed Brown +  fine - finer DM to use as a base
3117b17ce1afSJed Brown .  restrct - restriction matrix, apply using MatRestrict()
3118e91eccc2SStefano Zampini .  rscale - scaling vector for restriction
3119b17ce1afSJed Brown .  inject - injection matrix, also use MatRestrict()
3120e91eccc2SStefano Zampini -  coarse - coarser DM to update
3121b17ce1afSJed Brown 
3122b17ce1afSJed Brown    Level: developer
3123b17ce1afSJed Brown 
3124b17ce1afSJed Brown .seealso: DMCoarsenHookAdd(), MatRestrict()
3125b17ce1afSJed Brown @*/
3126b17ce1afSJed Brown PetscErrorCode DMRestrict(DM fine,Mat restrct,Vec rscale,Mat inject,DM coarse)
3127b17ce1afSJed Brown {
3128b17ce1afSJed Brown   PetscErrorCode    ierr;
3129b17ce1afSJed Brown   DMCoarsenHookLink link;
3130b17ce1afSJed Brown 
3131b17ce1afSJed Brown   PetscFunctionBegin;
3132b17ce1afSJed Brown   for (link=fine->coarsenhook; link; link=link->next) {
31338865f1eaSKarl Rupp     if (link->restricthook) {
31348865f1eaSKarl Rupp       ierr = (*link->restricthook)(fine,restrct,rscale,inject,coarse,link->ctx);CHKERRQ(ierr);
31358865f1eaSKarl Rupp     }
3136b17ce1afSJed Brown   }
313747c6ae99SBarry Smith   PetscFunctionReturn(0);
313847c6ae99SBarry Smith }
313947c6ae99SBarry Smith 
3140bb9467b5SJed Brown /*@C
3141be081cd6SPeter Brune    DMSubDomainHookAdd - adds a callback to be run when restricting a problem to the coarse grid
31425dbd56e3SPeter Brune 
3143d083f849SBarry Smith    Logically Collective on global
31445dbd56e3SPeter Brune 
31455dbd56e3SPeter Brune    Input Arguments:
31465dbd56e3SPeter Brune +  global - global DM
3147ec4806b8SPeter Brune .  ddhook - function to run to pass data to the decomposition DM upon its creation
31485dbd56e3SPeter Brune .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
31490298fd71SBarry Smith -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
31505dbd56e3SPeter Brune 
3151ec4806b8SPeter Brune 
3152ec4806b8SPeter Brune    Calling sequence for ddhook:
3153ec4806b8SPeter Brune $    ddhook(DM global,DM block,void *ctx)
3154ec4806b8SPeter Brune 
3155ec4806b8SPeter Brune +  global - global DM
3156ec4806b8SPeter Brune .  block  - block DM
3157ec4806b8SPeter Brune -  ctx - optional user-defined function context
3158ec4806b8SPeter Brune 
31595dbd56e3SPeter Brune    Calling sequence for restricthook:
3160ec4806b8SPeter Brune $    restricthook(DM global,VecScatter out,VecScatter in,DM block,void *ctx)
31615dbd56e3SPeter Brune 
31625dbd56e3SPeter Brune +  global - global DM
31635dbd56e3SPeter Brune .  out    - scatter to the outer (with ghost and overlap points) block vector
31645dbd56e3SPeter Brune .  in     - scatter to block vector values only owned locally
3165ec4806b8SPeter Brune .  block  - block DM
31665dbd56e3SPeter Brune -  ctx - optional user-defined function context
31675dbd56e3SPeter Brune 
31685dbd56e3SPeter Brune    Level: advanced
31695dbd56e3SPeter Brune 
31705dbd56e3SPeter Brune    Notes:
3171ec4806b8SPeter Brune    This function is only needed if auxiliary data needs to be set up on subdomain DMs.
31725dbd56e3SPeter Brune 
31735dbd56e3SPeter Brune    If this function is called multiple times, the hooks will be run in the order they are added.
31745dbd56e3SPeter Brune 
31755dbd56e3SPeter Brune    In order to compose with nonlinear preconditioning without duplicating storage, the hook should be implemented to
3176ec4806b8SPeter Brune    extract the global information from its context (instead of from the SNES).
31775dbd56e3SPeter Brune 
3178bb9467b5SJed Brown    This function is currently not available from Fortran.
3179bb9467b5SJed Brown 
31805dbd56e3SPeter Brune .seealso: DMRefineHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
31815dbd56e3SPeter Brune @*/
3182be081cd6SPeter Brune PetscErrorCode DMSubDomainHookAdd(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
31835dbd56e3SPeter Brune {
31845dbd56e3SPeter Brune   PetscErrorCode      ierr;
3185be081cd6SPeter Brune   DMSubDomainHookLink link,*p;
31865dbd56e3SPeter Brune 
31875dbd56e3SPeter Brune   PetscFunctionBegin;
31885dbd56e3SPeter Brune   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3189b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Scan to the end of the current list of hooks */
3190b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) PetscFunctionReturn(0);
3191b3a6b972SJed Brown   }
319295dccacaSBarry Smith   ierr               = PetscNew(&link);CHKERRQ(ierr);
31935dbd56e3SPeter Brune   link->restricthook = restricthook;
3194be081cd6SPeter Brune   link->ddhook       = ddhook;
31955dbd56e3SPeter Brune   link->ctx          = ctx;
31960298fd71SBarry Smith   link->next         = NULL;
31975dbd56e3SPeter Brune   *p                 = link;
31985dbd56e3SPeter Brune   PetscFunctionReturn(0);
31995dbd56e3SPeter Brune }
32005dbd56e3SPeter Brune 
3201b3a6b972SJed Brown /*@C
3202b3a6b972SJed Brown    DMSubDomainHookRemove - remove a callback from the list to be run when restricting a problem to the coarse grid
3203b3a6b972SJed Brown 
3204b3a6b972SJed Brown    Logically Collective
3205b3a6b972SJed Brown 
3206b3a6b972SJed Brown    Input Arguments:
3207b3a6b972SJed Brown +  global - global DM
3208b3a6b972SJed Brown .  ddhook - function to run to pass data to the decomposition DM upon its creation
3209b3a6b972SJed Brown .  restricthook - function to run to update data on block solve (at the beginning of the block solve)
3210b3a6b972SJed Brown -  ctx - [optional] user-defined context for provide data for the hooks (may be NULL)
3211b3a6b972SJed Brown 
3212b3a6b972SJed Brown    Level: advanced
3213b3a6b972SJed Brown 
3214b3a6b972SJed Brown    Notes:
3215b3a6b972SJed Brown 
3216b3a6b972SJed Brown    This function is currently not available from Fortran.
3217b3a6b972SJed Brown 
3218b3a6b972SJed Brown .seealso: DMSubDomainHookAdd(), SNESFASGetInterpolation(), SNESFASGetInjection(), PetscObjectCompose(), PetscContainerCreate()
3219b3a6b972SJed Brown @*/
3220b3a6b972SJed Brown PetscErrorCode DMSubDomainHookRemove(DM global,PetscErrorCode (*ddhook)(DM,DM,void*),PetscErrorCode (*restricthook)(DM,VecScatter,VecScatter,DM,void*),void *ctx)
3221b3a6b972SJed Brown {
3222b3a6b972SJed Brown   PetscErrorCode      ierr;
3223b3a6b972SJed Brown   DMSubDomainHookLink link,*p;
3224b3a6b972SJed Brown 
3225b3a6b972SJed Brown   PetscFunctionBegin;
3226b3a6b972SJed Brown   PetscValidHeaderSpecific(global,DM_CLASSID,1);
3227b3a6b972SJed Brown   for (p=&global->subdomainhook; *p; p=&(*p)->next) { /* Search the list of current hooks */
3228b3a6b972SJed Brown     if ((*p)->ddhook == ddhook && (*p)->restricthook == restricthook && (*p)->ctx == ctx) {
3229b3a6b972SJed Brown       link = *p;
3230b3a6b972SJed Brown       *p = link->next;
3231b3a6b972SJed Brown       ierr = PetscFree(link);CHKERRQ(ierr);
3232b3a6b972SJed Brown       break;
3233b3a6b972SJed Brown     }
3234b3a6b972SJed Brown   }
3235b3a6b972SJed Brown   PetscFunctionReturn(0);
3236b3a6b972SJed Brown }
3237b3a6b972SJed Brown 
32385dbd56e3SPeter Brune /*@
3239be081cd6SPeter Brune    DMSubDomainRestrict - restricts user-defined problem data to a block DM by running hooks registered by DMSubDomainHookAdd()
32405dbd56e3SPeter Brune 
32415dbd56e3SPeter Brune    Collective if any hooks are
32425dbd56e3SPeter Brune 
32435dbd56e3SPeter Brune    Input Arguments:
32445dbd56e3SPeter Brune +  fine - finer DM to use as a base
3245be081cd6SPeter Brune .  oscatter - scatter from domain global vector filling subdomain global vector with overlap
3246be081cd6SPeter Brune .  gscatter - scatter from domain global vector filling subdomain local vector with ghosts
32475dbd56e3SPeter Brune -  coarse - coarer DM to update
32485dbd56e3SPeter Brune 
32495dbd56e3SPeter Brune    Level: developer
32505dbd56e3SPeter Brune 
32515dbd56e3SPeter Brune .seealso: DMCoarsenHookAdd(), MatRestrict()
32525dbd56e3SPeter Brune @*/
3253be081cd6SPeter Brune PetscErrorCode DMSubDomainRestrict(DM global,VecScatter oscatter,VecScatter gscatter,DM subdm)
32545dbd56e3SPeter Brune {
32555dbd56e3SPeter Brune   PetscErrorCode      ierr;
3256be081cd6SPeter Brune   DMSubDomainHookLink link;
32575dbd56e3SPeter Brune 
32585dbd56e3SPeter Brune   PetscFunctionBegin;
3259be081cd6SPeter Brune   for (link=global->subdomainhook; link; link=link->next) {
32608865f1eaSKarl Rupp     if (link->restricthook) {
32618865f1eaSKarl Rupp       ierr = (*link->restricthook)(global,oscatter,gscatter,subdm,link->ctx);CHKERRQ(ierr);
32628865f1eaSKarl Rupp     }
32635dbd56e3SPeter Brune   }
32645dbd56e3SPeter Brune   PetscFunctionReturn(0);
32655dbd56e3SPeter Brune }
32665dbd56e3SPeter Brune 
32675fe1f584SPeter Brune /*@
32686a7d9d85SPeter Brune     DMGetCoarsenLevel - Get's the number of coarsenings that have generated this DM.
32695fe1f584SPeter Brune 
32705fe1f584SPeter Brune     Not Collective
32715fe1f584SPeter Brune 
32725fe1f584SPeter Brune     Input Parameter:
32735fe1f584SPeter Brune .   dm - the DM object
32745fe1f584SPeter Brune 
32755fe1f584SPeter Brune     Output Parameter:
32766a7d9d85SPeter Brune .   level - number of coarsenings
32775fe1f584SPeter Brune 
32785fe1f584SPeter Brune     Level: developer
32795fe1f584SPeter Brune 
32806a7d9d85SPeter Brune .seealso DMCoarsen(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
32815fe1f584SPeter Brune 
32825fe1f584SPeter Brune @*/
32835fe1f584SPeter Brune PetscErrorCode  DMGetCoarsenLevel(DM dm,PetscInt *level)
32845fe1f584SPeter Brune {
32855fe1f584SPeter Brune   PetscFunctionBegin;
32865fe1f584SPeter Brune   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3287b9d85ea2SLisandro Dalcin   PetscValidIntPointer(level,2);
32885fe1f584SPeter Brune   *level = dm->leveldown;
32895fe1f584SPeter Brune   PetscFunctionReturn(0);
32905fe1f584SPeter Brune }
32915fe1f584SPeter Brune 
32929a64c4a8SMatthew G. Knepley /*@
32939a64c4a8SMatthew G. Knepley     DMSetCoarsenLevel - Sets the number of coarsenings that have generated this DM.
32949a64c4a8SMatthew G. Knepley 
32959a64c4a8SMatthew G. Knepley     Not Collective
32969a64c4a8SMatthew G. Knepley 
32979a64c4a8SMatthew G. Knepley     Input Parameters:
32989a64c4a8SMatthew G. Knepley +   dm - the DM object
32999a64c4a8SMatthew G. Knepley -   level - number of coarsenings
33009a64c4a8SMatthew G. Knepley 
33019a64c4a8SMatthew G. Knepley     Level: developer
33029a64c4a8SMatthew G. Knepley 
33039a64c4a8SMatthew G. Knepley .seealso DMCoarsen(), DMGetCoarsenLevel(), DMGetRefineLevel(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
33049a64c4a8SMatthew G. Knepley @*/
33059a64c4a8SMatthew G. Knepley PetscErrorCode DMSetCoarsenLevel(DM dm,PetscInt level)
33069a64c4a8SMatthew G. Knepley {
33079a64c4a8SMatthew G. Knepley   PetscFunctionBegin;
33089a64c4a8SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
33099a64c4a8SMatthew G. Knepley   dm->leveldown = level;
33109a64c4a8SMatthew G. Knepley   PetscFunctionReturn(0);
33119a64c4a8SMatthew G. Knepley }
33129a64c4a8SMatthew G. Knepley 
33135fe1f584SPeter Brune 
33145fe1f584SPeter Brune 
331547c6ae99SBarry Smith /*@C
331647c6ae99SBarry Smith     DMRefineHierarchy - Refines a DM object, all levels at once
331747c6ae99SBarry Smith 
3318d083f849SBarry Smith     Collective on dm
331947c6ae99SBarry Smith 
332047c6ae99SBarry Smith     Input Parameter:
332147c6ae99SBarry Smith +   dm - the DM object
332247c6ae99SBarry Smith -   nlevels - the number of levels of refinement
332347c6ae99SBarry Smith 
332447c6ae99SBarry Smith     Output Parameter:
332547c6ae99SBarry Smith .   dmf - the refined DM hierarchy
332647c6ae99SBarry Smith 
332747c6ae99SBarry Smith     Level: developer
332847c6ae99SBarry Smith 
3329e727c939SJed Brown .seealso DMCoarsenHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
333047c6ae99SBarry Smith 
333147c6ae99SBarry Smith @*/
33327087cfbeSBarry Smith PetscErrorCode  DMRefineHierarchy(DM dm,PetscInt nlevels,DM dmf[])
333347c6ae99SBarry Smith {
333447c6ae99SBarry Smith   PetscErrorCode ierr;
333547c6ae99SBarry Smith 
333647c6ae99SBarry Smith   PetscFunctionBegin;
3337171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3338ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
333947c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
3340b9d85ea2SLisandro Dalcin   PetscValidPointer(dmf,3);
334147c6ae99SBarry Smith   if (dm->ops->refinehierarchy) {
334247c6ae99SBarry Smith     ierr = (*dm->ops->refinehierarchy)(dm,nlevels,dmf);CHKERRQ(ierr);
334347c6ae99SBarry Smith   } else if (dm->ops->refine) {
334447c6ae99SBarry Smith     PetscInt i;
334547c6ae99SBarry Smith 
3346ce94432eSBarry Smith     ierr = DMRefine(dm,PetscObjectComm((PetscObject)dm),&dmf[0]);CHKERRQ(ierr);
334747c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3348ce94432eSBarry Smith       ierr = DMRefine(dmf[i-1],PetscObjectComm((PetscObject)dm),&dmf[i]);CHKERRQ(ierr);
334947c6ae99SBarry Smith     }
3350ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No RefineHierarchy for this DM yet");
335147c6ae99SBarry Smith   PetscFunctionReturn(0);
335247c6ae99SBarry Smith }
335347c6ae99SBarry Smith 
335447c6ae99SBarry Smith /*@C
335547c6ae99SBarry Smith     DMCoarsenHierarchy - Coarsens a DM object, all levels at once
335647c6ae99SBarry Smith 
3357d083f849SBarry Smith     Collective on dm
335847c6ae99SBarry Smith 
335947c6ae99SBarry Smith     Input Parameter:
336047c6ae99SBarry Smith +   dm - the DM object
336147c6ae99SBarry Smith -   nlevels - the number of levels of coarsening
336247c6ae99SBarry Smith 
336347c6ae99SBarry Smith     Output Parameter:
336447c6ae99SBarry Smith .   dmc - the coarsened DM hierarchy
336547c6ae99SBarry Smith 
336647c6ae99SBarry Smith     Level: developer
336747c6ae99SBarry Smith 
3368e727c939SJed Brown .seealso DMRefineHierarchy(), DMDestroy(), DMView(), DMCreateGlobalVector(), DMCreateInterpolation()
336947c6ae99SBarry Smith 
337047c6ae99SBarry Smith @*/
33717087cfbeSBarry Smith PetscErrorCode  DMCoarsenHierarchy(DM dm, PetscInt nlevels, DM dmc[])
337247c6ae99SBarry Smith {
337347c6ae99SBarry Smith   PetscErrorCode ierr;
337447c6ae99SBarry Smith 
337547c6ae99SBarry Smith   PetscFunctionBegin;
3376171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3377ce94432eSBarry Smith   if (nlevels < 0) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_OUTOFRANGE,"nlevels cannot be negative");
337847c6ae99SBarry Smith   if (nlevels == 0) PetscFunctionReturn(0);
337947c6ae99SBarry Smith   PetscValidPointer(dmc,3);
338047c6ae99SBarry Smith   if (dm->ops->coarsenhierarchy) {
338147c6ae99SBarry Smith     ierr = (*dm->ops->coarsenhierarchy)(dm, nlevels, dmc);CHKERRQ(ierr);
338247c6ae99SBarry Smith   } else if (dm->ops->coarsen) {
338347c6ae99SBarry Smith     PetscInt i;
338447c6ae99SBarry Smith 
3385ce94432eSBarry Smith     ierr = DMCoarsen(dm,PetscObjectComm((PetscObject)dm),&dmc[0]);CHKERRQ(ierr);
338647c6ae99SBarry Smith     for (i=1; i<nlevels; i++) {
3387ce94432eSBarry Smith       ierr = DMCoarsen(dmc[i-1],PetscObjectComm((PetscObject)dm),&dmc[i]);CHKERRQ(ierr);
338847c6ae99SBarry Smith     }
3389ce94432eSBarry Smith   } else SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"No CoarsenHierarchy for this DM yet");
339047c6ae99SBarry Smith   PetscFunctionReturn(0);
339147c6ae99SBarry Smith }
339247c6ae99SBarry Smith 
33931a266240SBarry Smith /*@C
33941a266240SBarry Smith     DMSetApplicationContextDestroy - Sets a user function that will be called to destroy the application context when the DM is destroyed
33951a266240SBarry Smith 
33961a266240SBarry Smith     Not Collective
33971a266240SBarry Smith 
33981a266240SBarry Smith     Input Parameters:
33991a266240SBarry Smith +   dm - the DM object
34001a266240SBarry Smith -   destroy - the destroy function
34011a266240SBarry Smith 
34021a266240SBarry Smith     Level: intermediate
34031a266240SBarry Smith 
3404e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
34051a266240SBarry Smith 
3406f07f9ceaSJed Brown @*/
34071a266240SBarry Smith PetscErrorCode  DMSetApplicationContextDestroy(DM dm,PetscErrorCode (*destroy)(void**))
34081a266240SBarry Smith {
34091a266240SBarry Smith   PetscFunctionBegin;
3410171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34111a266240SBarry Smith   dm->ctxdestroy = destroy;
34121a266240SBarry Smith   PetscFunctionReturn(0);
34131a266240SBarry Smith }
34141a266240SBarry Smith 
3415b07ff414SBarry Smith /*@
34161b2093e4SBarry Smith     DMSetApplicationContext - Set a user context into a DM object
341747c6ae99SBarry Smith 
341847c6ae99SBarry Smith     Not Collective
341947c6ae99SBarry Smith 
342047c6ae99SBarry Smith     Input Parameters:
342147c6ae99SBarry Smith +   dm - the DM object
342247c6ae99SBarry Smith -   ctx - the user context
342347c6ae99SBarry Smith 
342447c6ae99SBarry Smith     Level: intermediate
342547c6ae99SBarry Smith 
3426e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
342747c6ae99SBarry Smith 
342847c6ae99SBarry Smith @*/
34291b2093e4SBarry Smith PetscErrorCode  DMSetApplicationContext(DM dm,void *ctx)
343047c6ae99SBarry Smith {
343147c6ae99SBarry Smith   PetscFunctionBegin;
3432171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
343347c6ae99SBarry Smith   dm->ctx = ctx;
343447c6ae99SBarry Smith   PetscFunctionReturn(0);
343547c6ae99SBarry Smith }
343647c6ae99SBarry Smith 
343747c6ae99SBarry Smith /*@
34381b2093e4SBarry Smith     DMGetApplicationContext - Gets a user context from a DM object
343947c6ae99SBarry Smith 
344047c6ae99SBarry Smith     Not Collective
344147c6ae99SBarry Smith 
344247c6ae99SBarry Smith     Input Parameter:
344347c6ae99SBarry Smith .   dm - the DM object
344447c6ae99SBarry Smith 
344547c6ae99SBarry Smith     Output Parameter:
344647c6ae99SBarry Smith .   ctx - the user context
344747c6ae99SBarry Smith 
344847c6ae99SBarry Smith     Level: intermediate
344947c6ae99SBarry Smith 
3450e727c939SJed Brown .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
345147c6ae99SBarry Smith 
345247c6ae99SBarry Smith @*/
34531b2093e4SBarry Smith PetscErrorCode  DMGetApplicationContext(DM dm,void *ctx)
345447c6ae99SBarry Smith {
345547c6ae99SBarry Smith   PetscFunctionBegin;
3456171400e9SBarry Smith   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
34571b2093e4SBarry Smith   *(void**)ctx = dm->ctx;
345847c6ae99SBarry Smith   PetscFunctionReturn(0);
345947c6ae99SBarry Smith }
346047c6ae99SBarry Smith 
346108da532bSDmitry Karpeev /*@C
3462df3898eeSBarry Smith     DMSetVariableBounds - sets a function to compute the lower and upper bound vectors for SNESVI.
346308da532bSDmitry Karpeev 
3464d083f849SBarry Smith     Logically Collective on dm
346508da532bSDmitry Karpeev 
346608da532bSDmitry Karpeev     Input Parameter:
346708da532bSDmitry Karpeev +   dm - the DM object
34680298fd71SBarry Smith -   f - the function that computes variable bounds used by SNESVI (use NULL to cancel a previous function that was set)
346908da532bSDmitry Karpeev 
347008da532bSDmitry Karpeev     Level: intermediate
347108da532bSDmitry Karpeev 
3472835c3ec7SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext(),
347308da532bSDmitry Karpeev          DMSetJacobian()
347408da532bSDmitry Karpeev 
347508da532bSDmitry Karpeev @*/
347608da532bSDmitry Karpeev PetscErrorCode DMSetVariableBounds(DM dm,PetscErrorCode (*f)(DM,Vec,Vec))
347708da532bSDmitry Karpeev {
347808da532bSDmitry Karpeev   PetscFunctionBegin;
34795a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
348008da532bSDmitry Karpeev   dm->ops->computevariablebounds = f;
348108da532bSDmitry Karpeev   PetscFunctionReturn(0);
348208da532bSDmitry Karpeev }
348308da532bSDmitry Karpeev 
348408da532bSDmitry Karpeev /*@
348508da532bSDmitry Karpeev     DMHasVariableBounds - does the DM object have a variable bounds function?
348608da532bSDmitry Karpeev 
348708da532bSDmitry Karpeev     Not Collective
348808da532bSDmitry Karpeev 
348908da532bSDmitry Karpeev     Input Parameter:
349008da532bSDmitry Karpeev .   dm - the DM object to destroy
349108da532bSDmitry Karpeev 
349208da532bSDmitry Karpeev     Output Parameter:
349308da532bSDmitry Karpeev .   flg - PETSC_TRUE if the variable bounds function exists
349408da532bSDmitry Karpeev 
349508da532bSDmitry Karpeev     Level: developer
349608da532bSDmitry Karpeev 
349774e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
349808da532bSDmitry Karpeev 
349908da532bSDmitry Karpeev @*/
350008da532bSDmitry Karpeev PetscErrorCode DMHasVariableBounds(DM dm,PetscBool *flg)
350108da532bSDmitry Karpeev {
350208da532bSDmitry Karpeev   PetscFunctionBegin;
35035a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3504534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
350508da532bSDmitry Karpeev   *flg =  (dm->ops->computevariablebounds) ? PETSC_TRUE : PETSC_FALSE;
350608da532bSDmitry Karpeev   PetscFunctionReturn(0);
350708da532bSDmitry Karpeev }
350808da532bSDmitry Karpeev 
350908da532bSDmitry Karpeev /*@C
351008da532bSDmitry Karpeev     DMComputeVariableBounds - compute variable bounds used by SNESVI.
351108da532bSDmitry Karpeev 
3512d083f849SBarry Smith     Logically Collective on dm
351308da532bSDmitry Karpeev 
351408da532bSDmitry Karpeev     Input Parameters:
3515907376e6SBarry Smith .   dm - the DM object
351608da532bSDmitry Karpeev 
351708da532bSDmitry Karpeev     Output parameters:
351808da532bSDmitry Karpeev +   xl - lower bound
351908da532bSDmitry Karpeev -   xu - upper bound
352008da532bSDmitry Karpeev 
3521907376e6SBarry Smith     Level: advanced
3522907376e6SBarry Smith 
352395452b02SPatrick Sanan     Notes:
352495452b02SPatrick Sanan     This is generally not called by users. It calls the function provided by the user with DMSetVariableBounds()
352508da532bSDmitry Karpeev 
352674e1e8c1SBarry Smith .seealso DMView(), DMCreateGlobalVector(), DMCreateInterpolation(), DMCreateColoring(), DMCreateMatrix(), DMGetApplicationContext()
352708da532bSDmitry Karpeev 
352808da532bSDmitry Karpeev @*/
352908da532bSDmitry Karpeev PetscErrorCode DMComputeVariableBounds(DM dm, Vec xl, Vec xu)
353008da532bSDmitry Karpeev {
353108da532bSDmitry Karpeev   PetscErrorCode ierr;
35325fd66863SKarl Rupp 
353308da532bSDmitry Karpeev   PetscFunctionBegin;
35345a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
353508da532bSDmitry Karpeev   PetscValidHeaderSpecific(xl,VEC_CLASSID,2);
35365a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(xu,VEC_CLASSID,3);
3537b9d85ea2SLisandro Dalcin   if (!dm->ops->computevariablebounds) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeVariableBounds",((PetscObject)dm)->type_name);
353808da532bSDmitry Karpeev   ierr = (*dm->ops->computevariablebounds)(dm, xl,xu);CHKERRQ(ierr);
353908da532bSDmitry Karpeev   PetscFunctionReturn(0);
354008da532bSDmitry Karpeev }
354108da532bSDmitry Karpeev 
3542b0ae01b7SPeter Brune /*@
3543b0ae01b7SPeter Brune     DMHasColoring - does the DM object have a method of providing a coloring?
3544b0ae01b7SPeter Brune 
3545b0ae01b7SPeter Brune     Not Collective
3546b0ae01b7SPeter Brune 
3547b0ae01b7SPeter Brune     Input Parameter:
3548b0ae01b7SPeter Brune .   dm - the DM object
3549b0ae01b7SPeter Brune 
3550b0ae01b7SPeter Brune     Output Parameter:
3551b0ae01b7SPeter Brune .   flg - PETSC_TRUE if the DM has facilities for DMCreateColoring().
3552b0ae01b7SPeter Brune 
3553b0ae01b7SPeter Brune     Level: developer
3554b0ae01b7SPeter Brune 
35551565f0a7SPatrick Sanan .seealso DMCreateColoring()
3556b0ae01b7SPeter Brune 
3557b0ae01b7SPeter Brune @*/
3558b0ae01b7SPeter Brune PetscErrorCode DMHasColoring(DM dm,PetscBool *flg)
3559b0ae01b7SPeter Brune {
3560b0ae01b7SPeter Brune   PetscFunctionBegin;
35615a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3562534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
3563b0ae01b7SPeter Brune   *flg =  (dm->ops->getcoloring) ? PETSC_TRUE : PETSC_FALSE;
3564b0ae01b7SPeter Brune   PetscFunctionReturn(0);
3565b0ae01b7SPeter Brune }
3566b0ae01b7SPeter Brune 
35673ad4599aSBarry Smith /*@
35683ad4599aSBarry Smith     DMHasCreateRestriction - does the DM object have a method of providing a restriction?
35693ad4599aSBarry Smith 
35703ad4599aSBarry Smith     Not Collective
35713ad4599aSBarry Smith 
35723ad4599aSBarry Smith     Input Parameter:
35733ad4599aSBarry Smith .   dm - the DM object
35743ad4599aSBarry Smith 
35753ad4599aSBarry Smith     Output Parameter:
35763ad4599aSBarry Smith .   flg - PETSC_TRUE if the DM has facilities for DMCreateRestriction().
35773ad4599aSBarry Smith 
35783ad4599aSBarry Smith     Level: developer
35793ad4599aSBarry Smith 
35801565f0a7SPatrick Sanan .seealso DMCreateRestriction()
35813ad4599aSBarry Smith 
35823ad4599aSBarry Smith @*/
35833ad4599aSBarry Smith PetscErrorCode DMHasCreateRestriction(DM dm,PetscBool *flg)
35843ad4599aSBarry Smith {
35853ad4599aSBarry Smith   PetscFunctionBegin;
35865a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3587534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
35883ad4599aSBarry Smith   *flg =  (dm->ops->createrestriction) ? PETSC_TRUE : PETSC_FALSE;
35893ad4599aSBarry Smith   PetscFunctionReturn(0);
35903ad4599aSBarry Smith }
35913ad4599aSBarry Smith 
3592a7058e45SLawrence Mitchell 
3593a7058e45SLawrence Mitchell /*@
3594a7058e45SLawrence Mitchell     DMHasCreateInjection - does the DM object have a method of providing an injection?
3595a7058e45SLawrence Mitchell 
3596a7058e45SLawrence Mitchell     Not Collective
3597a7058e45SLawrence Mitchell 
3598a7058e45SLawrence Mitchell     Input Parameter:
3599a7058e45SLawrence Mitchell .   dm - the DM object
3600a7058e45SLawrence Mitchell 
3601a7058e45SLawrence Mitchell     Output Parameter:
3602a7058e45SLawrence Mitchell .   flg - PETSC_TRUE if the DM has facilities for DMCreateInjection().
3603a7058e45SLawrence Mitchell 
3604a7058e45SLawrence Mitchell     Level: developer
3605a7058e45SLawrence Mitchell 
36061565f0a7SPatrick Sanan .seealso DMCreateInjection()
3607a7058e45SLawrence Mitchell 
3608a7058e45SLawrence Mitchell @*/
3609a7058e45SLawrence Mitchell PetscErrorCode DMHasCreateInjection(DM dm,PetscBool *flg)
3610a7058e45SLawrence Mitchell {
36114a7a4c06SLawrence Mitchell   PetscErrorCode ierr;
36125a84ad33SLisandro Dalcin 
3613a7058e45SLawrence Mitchell   PetscFunctionBegin;
36145a84ad33SLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
3615534a8f05SLisandro Dalcin   PetscValidBoolPointer(flg,2);
36165a84ad33SLisandro Dalcin   if (dm->ops->hascreateinjection) {
36174a7a4c06SLawrence Mitchell     ierr = (*dm->ops->hascreateinjection)(dm,flg);CHKERRQ(ierr);
36185a84ad33SLisandro Dalcin   } else {
36195a84ad33SLisandro Dalcin     *flg = (dm->ops->createinjection) ? PETSC_TRUE : PETSC_FALSE;
36205a84ad33SLisandro Dalcin   }
3621a7058e45SLawrence Mitchell   PetscFunctionReturn(0);
3622a7058e45SLawrence Mitchell }
3623a7058e45SLawrence Mitchell 
36240298fd71SBarry Smith PetscFunctionList DMList              = NULL;
3625264ace61SBarry Smith PetscBool         DMRegisterAllCalled = PETSC_FALSE;
3626264ace61SBarry Smith 
3627264ace61SBarry Smith /*@C
3628264ace61SBarry Smith   DMSetType - Builds a DM, for a particular DM implementation.
3629264ace61SBarry Smith 
3630d083f849SBarry Smith   Collective on dm
3631264ace61SBarry Smith 
3632264ace61SBarry Smith   Input Parameters:
3633264ace61SBarry Smith + dm     - The DM object
3634264ace61SBarry Smith - method - The name of the DM type
3635264ace61SBarry Smith 
3636264ace61SBarry Smith   Options Database Key:
3637264ace61SBarry Smith . -dm_type <type> - Sets the DM type; use -help for a list of available types
3638264ace61SBarry Smith 
3639264ace61SBarry Smith   Notes:
3640e1589f56SBarry Smith   See "petsc/include/petscdm.h" for available DM types (for instance, DM1D, DM2D, or DM3D).
3641264ace61SBarry Smith 
3642264ace61SBarry Smith   Level: intermediate
3643264ace61SBarry Smith 
3644264ace61SBarry Smith .seealso: DMGetType(), DMCreate()
3645264ace61SBarry Smith @*/
364619fd82e9SBarry Smith PetscErrorCode  DMSetType(DM dm, DMType method)
3647264ace61SBarry Smith {
3648264ace61SBarry Smith   PetscErrorCode (*r)(DM);
3649264ace61SBarry Smith   PetscBool      match;
3650264ace61SBarry Smith   PetscErrorCode ierr;
3651264ace61SBarry Smith 
3652264ace61SBarry Smith   PetscFunctionBegin;
3653264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3654251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, method, &match);CHKERRQ(ierr);
3655264ace61SBarry Smith   if (match) PetscFunctionReturn(0);
3656264ace61SBarry Smith 
36570f51fdf8SToby Isaac   ierr = DMRegisterAll();CHKERRQ(ierr);
36581c9cd337SJed Brown   ierr = PetscFunctionListFind(DMList,method,&r);CHKERRQ(ierr);
3659ce94432eSBarry Smith   if (!r) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_ARG_UNKNOWN_TYPE, "Unknown DM type: %s", method);
3660264ace61SBarry Smith 
3661264ace61SBarry Smith   if (dm->ops->destroy) {
3662264ace61SBarry Smith     ierr = (*dm->ops->destroy)(dm);CHKERRQ(ierr);
3663264ace61SBarry Smith   }
3664d57f96a3SLisandro Dalcin   ierr = PetscMemzero(dm->ops,sizeof(*dm->ops));CHKERRQ(ierr);
3665264ace61SBarry Smith   ierr = PetscObjectChangeTypeName((PetscObject)dm,method);CHKERRQ(ierr);
3666d57f96a3SLisandro Dalcin   ierr = (*r)(dm);CHKERRQ(ierr);
3667264ace61SBarry Smith   PetscFunctionReturn(0);
3668264ace61SBarry Smith }
3669264ace61SBarry Smith 
3670264ace61SBarry Smith /*@C
3671264ace61SBarry Smith   DMGetType - Gets the DM type name (as a string) from the DM.
3672264ace61SBarry Smith 
3673264ace61SBarry Smith   Not Collective
3674264ace61SBarry Smith 
3675264ace61SBarry Smith   Input Parameter:
3676264ace61SBarry Smith . dm  - The DM
3677264ace61SBarry Smith 
3678264ace61SBarry Smith   Output Parameter:
3679264ace61SBarry Smith . type - The DM type name
3680264ace61SBarry Smith 
3681264ace61SBarry Smith   Level: intermediate
3682264ace61SBarry Smith 
3683264ace61SBarry Smith .seealso: DMSetType(), DMCreate()
3684264ace61SBarry Smith @*/
368519fd82e9SBarry Smith PetscErrorCode  DMGetType(DM dm, DMType *type)
3686264ace61SBarry Smith {
3687264ace61SBarry Smith   PetscErrorCode ierr;
3688264ace61SBarry Smith 
3689264ace61SBarry Smith   PetscFunctionBegin;
3690264ace61SBarry Smith   PetscValidHeaderSpecific(dm, DM_CLASSID,1);
3691c959eef4SJed Brown   PetscValidPointer(type,2);
3692607a6623SBarry Smith   ierr = DMRegisterAll();CHKERRQ(ierr);
3693264ace61SBarry Smith   *type = ((PetscObject)dm)->type_name;
3694264ace61SBarry Smith   PetscFunctionReturn(0);
3695264ace61SBarry Smith }
3696264ace61SBarry Smith 
369767a56275SMatthew G Knepley /*@C
369867a56275SMatthew G Knepley   DMConvert - Converts a DM to another DM, either of the same or different type.
369967a56275SMatthew G Knepley 
3700d083f849SBarry Smith   Collective on dm
370167a56275SMatthew G Knepley 
370267a56275SMatthew G Knepley   Input Parameters:
370367a56275SMatthew G Knepley + dm - the DM
370467a56275SMatthew G Knepley - newtype - new DM type (use "same" for the same type)
370567a56275SMatthew G Knepley 
370667a56275SMatthew G Knepley   Output Parameter:
370767a56275SMatthew G Knepley . M - pointer to new DM
370867a56275SMatthew G Knepley 
370967a56275SMatthew G Knepley   Notes:
371067a56275SMatthew G Knepley   Cannot be used to convert a sequential DM to parallel or parallel to sequential,
371167a56275SMatthew G Knepley   the MPI communicator of the generated DM is always the same as the communicator
371267a56275SMatthew G Knepley   of the input DM.
371367a56275SMatthew G Knepley 
371467a56275SMatthew G Knepley   Level: intermediate
371567a56275SMatthew G Knepley 
371667a56275SMatthew G Knepley .seealso: DMCreate()
371767a56275SMatthew G Knepley @*/
371819fd82e9SBarry Smith PetscErrorCode DMConvert(DM dm, DMType newtype, DM *M)
371967a56275SMatthew G Knepley {
372067a56275SMatthew G Knepley   DM             B;
372167a56275SMatthew G Knepley   char           convname[256];
3722c067b6caSMatthew G. Knepley   PetscBool      sametype/*, issame */;
372367a56275SMatthew G Knepley   PetscErrorCode ierr;
372467a56275SMatthew G Knepley 
372567a56275SMatthew G Knepley   PetscFunctionBegin;
372667a56275SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
372767a56275SMatthew G Knepley   PetscValidType(dm,1);
372867a56275SMatthew G Knepley   PetscValidPointer(M,3);
3729251f4c67SDmitry Karpeev   ierr = PetscObjectTypeCompare((PetscObject) dm, newtype, &sametype);CHKERRQ(ierr);
3730c067b6caSMatthew G. Knepley   /* ierr = PetscStrcmp(newtype, "same", &issame);CHKERRQ(ierr); */
3731c067b6caSMatthew G. Knepley   if (sametype) {
3732c067b6caSMatthew G. Knepley     *M   = dm;
3733c067b6caSMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) dm);CHKERRQ(ierr);
3734c067b6caSMatthew G. Knepley     PetscFunctionReturn(0);
3735c067b6caSMatthew G. Knepley   } else {
37360298fd71SBarry Smith     PetscErrorCode (*conv)(DM, DMType, DM*) = NULL;
373767a56275SMatthew G Knepley 
373867a56275SMatthew G Knepley     /*
373967a56275SMatthew G Knepley        Order of precedence:
374067a56275SMatthew G Knepley        1) See if a specialized converter is known to the current DM.
374167a56275SMatthew G Knepley        2) See if a specialized converter is known to the desired DM class.
374267a56275SMatthew G Knepley        3) See if a good general converter is registered for the desired class
374367a56275SMatthew G Knepley        4) See if a good general converter is known for the current matrix.
374467a56275SMatthew G Knepley        5) Use a really basic converter.
374567a56275SMatthew G Knepley     */
374667a56275SMatthew G Knepley 
374767a56275SMatthew G Knepley     /* 1) See if a specialized converter is known to the current DM and the desired class */
3748a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3749a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3750a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3751a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3752a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37530005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)dm,convname,&conv);CHKERRQ(ierr);
375467a56275SMatthew G Knepley     if (conv) goto foundconv;
375567a56275SMatthew G Knepley 
375667a56275SMatthew G Knepley     /* 2)  See if a specialized converter is known to the desired DM class. */
375782f516ccSBarry Smith     ierr = DMCreate(PetscObjectComm((PetscObject)dm), &B);CHKERRQ(ierr);
375867a56275SMatthew G Knepley     ierr = DMSetType(B, newtype);CHKERRQ(ierr);
3759a126751eSBarry Smith     ierr = PetscStrncpy(convname,"DMConvert_",sizeof(convname));CHKERRQ(ierr);
3760a126751eSBarry Smith     ierr = PetscStrlcat(convname,((PetscObject) dm)->type_name,sizeof(convname));CHKERRQ(ierr);
3761a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_",sizeof(convname));CHKERRQ(ierr);
3762a126751eSBarry Smith     ierr = PetscStrlcat(convname,newtype,sizeof(convname));CHKERRQ(ierr);
3763a126751eSBarry Smith     ierr = PetscStrlcat(convname,"_C",sizeof(convname));CHKERRQ(ierr);
37640005d66cSJed Brown     ierr = PetscObjectQueryFunction((PetscObject)B,convname,&conv);CHKERRQ(ierr);
376567a56275SMatthew G Knepley     if (conv) {
3766fcfd50ebSBarry Smith       ierr = DMDestroy(&B);CHKERRQ(ierr);
376767a56275SMatthew G Knepley       goto foundconv;
376867a56275SMatthew G Knepley     }
376967a56275SMatthew G Knepley 
377067a56275SMatthew G Knepley #if 0
377167a56275SMatthew G Knepley     /* 3) See if a good general converter is registered for the desired class */
377267a56275SMatthew G Knepley     conv = B->ops->convertfrom;
3773fcfd50ebSBarry Smith     ierr = DMDestroy(&B);CHKERRQ(ierr);
377467a56275SMatthew G Knepley     if (conv) goto foundconv;
377567a56275SMatthew G Knepley 
377667a56275SMatthew G Knepley     /* 4) See if a good general converter is known for the current matrix */
377767a56275SMatthew G Knepley     if (dm->ops->convert) {
377867a56275SMatthew G Knepley       conv = dm->ops->convert;
377967a56275SMatthew G Knepley     }
378067a56275SMatthew G Knepley     if (conv) goto foundconv;
378167a56275SMatthew G Knepley #endif
378267a56275SMatthew G Knepley 
378367a56275SMatthew G Knepley     /* 5) Use a really basic converter. */
378482f516ccSBarry Smith     SETERRQ2(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "No conversion possible between DM types %s and %s", ((PetscObject) dm)->type_name, newtype);
378567a56275SMatthew G Knepley 
378667a56275SMatthew G Knepley foundconv:
378767a56275SMatthew G Knepley     ierr = PetscLogEventBegin(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
378867a56275SMatthew G Knepley     ierr = (*conv)(dm,newtype,M);CHKERRQ(ierr);
378912fa691eSMatthew G. Knepley     /* Things that are independent of DM type: We should consult DMClone() here */
379090b157c4SStefano Zampini     {
379190b157c4SStefano Zampini       PetscBool             isper;
379212fa691eSMatthew G. Knepley       const PetscReal      *maxCell, *L;
379312fa691eSMatthew G. Knepley       const DMBoundaryType *bd;
379490b157c4SStefano Zampini       ierr = DMGetPeriodicity(dm, &isper, &maxCell, &L, &bd);CHKERRQ(ierr);
379590b157c4SStefano Zampini       ierr = DMSetPeriodicity(*M, isper, maxCell,  L,  bd);CHKERRQ(ierr);
379612fa691eSMatthew G. Knepley     }
379767a56275SMatthew G Knepley     ierr = PetscLogEventEnd(DM_Convert,dm,0,0,0);CHKERRQ(ierr);
379867a56275SMatthew G Knepley   }
379967a56275SMatthew G Knepley   ierr = PetscObjectStateIncrease((PetscObject) *M);CHKERRQ(ierr);
380067a56275SMatthew G Knepley   PetscFunctionReturn(0);
380167a56275SMatthew G Knepley }
3802264ace61SBarry Smith 
3803264ace61SBarry Smith /*--------------------------------------------------------------------------------------------------------------------*/
3804264ace61SBarry Smith 
3805264ace61SBarry Smith /*@C
38061c84c290SBarry Smith   DMRegister -  Adds a new DM component implementation
38071c84c290SBarry Smith 
38081c84c290SBarry Smith   Not Collective
38091c84c290SBarry Smith 
38101c84c290SBarry Smith   Input Parameters:
38111c84c290SBarry Smith + name        - The name of a new user-defined creation routine
38121c84c290SBarry Smith - create_func - The creation routine itself
38131c84c290SBarry Smith 
38141c84c290SBarry Smith   Notes:
38151c84c290SBarry Smith   DMRegister() may be called multiple times to add several user-defined DMs
38161c84c290SBarry Smith 
38171c84c290SBarry Smith 
38181c84c290SBarry Smith   Sample usage:
38191c84c290SBarry Smith .vb
3820bdf89e91SBarry Smith     DMRegister("my_da", MyDMCreate);
38211c84c290SBarry Smith .ve
38221c84c290SBarry Smith 
38231c84c290SBarry Smith   Then, your DM type can be chosen with the procedural interface via
38241c84c290SBarry Smith .vb
38251c84c290SBarry Smith     DMCreate(MPI_Comm, DM *);
38261c84c290SBarry Smith     DMSetType(DM,"my_da");
38271c84c290SBarry Smith .ve
38281c84c290SBarry Smith    or at runtime via the option
38291c84c290SBarry Smith .vb
38301c84c290SBarry Smith     -da_type my_da
38311c84c290SBarry Smith .ve
3832264ace61SBarry Smith 
3833264ace61SBarry Smith   Level: advanced
38341c84c290SBarry Smith 
3835bdf89e91SBarry Smith .seealso: DMRegisterAll(), DMRegisterDestroy()
38361c84c290SBarry Smith 
3837264ace61SBarry Smith @*/
3838bdf89e91SBarry Smith PetscErrorCode  DMRegister(const char sname[],PetscErrorCode (*function)(DM))
3839264ace61SBarry Smith {
3840264ace61SBarry Smith   PetscErrorCode ierr;
3841264ace61SBarry Smith 
3842264ace61SBarry Smith   PetscFunctionBegin;
38431d36bdfdSBarry Smith   ierr = DMInitializePackage();CHKERRQ(ierr);
3844a240a19fSJed Brown   ierr = PetscFunctionListAdd(&DMList,sname,function);CHKERRQ(ierr);
3845264ace61SBarry Smith   PetscFunctionReturn(0);
3846264ace61SBarry Smith }
3847264ace61SBarry Smith 
3848b859378eSBarry Smith /*@C
384955849f57SBarry Smith   DMLoad - Loads a DM that has been stored in binary  with DMView().
3850b859378eSBarry Smith 
3851d083f849SBarry Smith   Collective on viewer
3852b859378eSBarry Smith 
3853b859378eSBarry Smith   Input Parameters:
3854b859378eSBarry Smith + newdm - the newly loaded DM, this needs to have been created with DMCreate() or
3855b859378eSBarry Smith            some related function before a call to DMLoad().
3856b859378eSBarry Smith - viewer - binary file viewer, obtained from PetscViewerBinaryOpen() or
3857b859378eSBarry Smith            HDF5 file viewer, obtained from PetscViewerHDF5Open()
3858b859378eSBarry Smith 
3859b859378eSBarry Smith    Level: intermediate
3860b859378eSBarry Smith 
3861b859378eSBarry Smith   Notes:
386255849f57SBarry Smith    The type is determined by the data in the file, any type set into the DM before this call is ignored.
3863b859378eSBarry Smith 
3864b859378eSBarry Smith   Notes for advanced users:
3865b859378eSBarry Smith   Most users should not need to know the details of the binary storage
3866b859378eSBarry Smith   format, since DMLoad() and DMView() completely hide these details.
3867b859378eSBarry Smith   But for anyone who's interested, the standard binary matrix storage
3868b859378eSBarry Smith   format is
3869b859378eSBarry Smith .vb
3870b859378eSBarry Smith      has not yet been determined
3871b859378eSBarry Smith .ve
3872b859378eSBarry Smith 
3873b859378eSBarry Smith .seealso: PetscViewerBinaryOpen(), DMView(), MatLoad(), VecLoad()
3874b859378eSBarry Smith @*/
3875b859378eSBarry Smith PetscErrorCode  DMLoad(DM newdm, PetscViewer viewer)
3876b859378eSBarry Smith {
38779331c7a4SMatthew G. Knepley   PetscBool      isbinary, ishdf5;
3878b859378eSBarry Smith   PetscErrorCode ierr;
3879b859378eSBarry Smith 
3880b859378eSBarry Smith   PetscFunctionBegin;
3881b859378eSBarry Smith   PetscValidHeaderSpecific(newdm,DM_CLASSID,1);
3882b859378eSBarry Smith   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
3883fb694a9eSVaclav Hapla   ierr = PetscViewerCheckReadable(viewer);CHKERRQ(ierr);
388432c0f0efSBarry Smith   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERBINARY,&isbinary);CHKERRQ(ierr);
38859331c7a4SMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject)viewer,PETSCVIEWERHDF5,&ishdf5);CHKERRQ(ierr);
388658cd63d5SVaclav Hapla   ierr = PetscLogEventBegin(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
38879331c7a4SMatthew G. Knepley   if (isbinary) {
38889331c7a4SMatthew G. Knepley     PetscInt classid;
38899331c7a4SMatthew G. Knepley     char     type[256];
3890b859378eSBarry Smith 
3891060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,&classid,1,NULL,PETSC_INT);CHKERRQ(ierr);
38929200755eSBarry Smith     if (classid != DM_FILE_CLASSID) SETERRQ1(PetscObjectComm((PetscObject)newdm),PETSC_ERR_ARG_WRONG,"Not DM next in file, classid found %d",(int)classid);
3893060da220SMatthew G. Knepley     ierr = PetscViewerBinaryRead(viewer,type,256,NULL,PETSC_CHAR);CHKERRQ(ierr);
389432c0f0efSBarry Smith     ierr = DMSetType(newdm, type);CHKERRQ(ierr);
38959331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38969331c7a4SMatthew G. Knepley   } else if (ishdf5) {
38979331c7a4SMatthew G. Knepley     if (newdm->ops->load) {ierr = (*newdm->ops->load)(newdm,viewer);CHKERRQ(ierr);}
38989331c7a4SMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Invalid viewer; open viewer with PetscViewerBinaryOpen() or PetscViewerHDF5Open()");
389958cd63d5SVaclav Hapla   ierr = PetscLogEventEnd(DM_Load,viewer,0,0,0);CHKERRQ(ierr);
3900b859378eSBarry Smith   PetscFunctionReturn(0);
3901b859378eSBarry Smith }
3902b859378eSBarry Smith 
3903b2e4378dSMatthew G. Knepley /*@
3904b2e4378dSMatthew G. Knepley   DMGetLocalBoundingBox - Returns the bounding box for the piece of the DM on this process.
3905b2e4378dSMatthew G. Knepley 
3906b2e4378dSMatthew G. Knepley   Not collective
3907b2e4378dSMatthew G. Knepley 
3908b2e4378dSMatthew G. Knepley   Input Parameter:
3909b2e4378dSMatthew G. Knepley . dm - the DM
3910b2e4378dSMatthew G. Knepley 
3911b2e4378dSMatthew G. Knepley   Output Parameters:
3912b2e4378dSMatthew G. Knepley + lmin - local minimum coordinates (length coord dim, optional)
3913b2e4378dSMatthew G. Knepley - lmax - local maximim coordinates (length coord dim, optional)
3914b2e4378dSMatthew G. Knepley 
3915b2e4378dSMatthew G. Knepley   Level: beginner
3916b2e4378dSMatthew G. Knepley 
3917b2e4378dSMatthew G. Knepley   Note: If the DM is a DMDA and has no coordinates, the index bounds are returned instead.
3918b2e4378dSMatthew G. Knepley 
3919b2e4378dSMatthew G. Knepley 
3920b2e4378dSMatthew G. Knepley .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetBoundingBox()
3921b2e4378dSMatthew G. Knepley @*/
3922b2e4378dSMatthew G. Knepley PetscErrorCode DMGetLocalBoundingBox(DM dm, PetscReal lmin[], PetscReal lmax[])
3923b2e4378dSMatthew G. Knepley {
3924b2e4378dSMatthew G. Knepley   Vec                coords = NULL;
3925b2e4378dSMatthew G. Knepley   PetscReal          min[3] = {PETSC_MAX_REAL, PETSC_MAX_REAL, PETSC_MAX_REAL};
3926b2e4378dSMatthew G. Knepley   PetscReal          max[3] = {PETSC_MIN_REAL, PETSC_MIN_REAL, PETSC_MIN_REAL};
3927b2e4378dSMatthew G. Knepley   const PetscScalar *local_coords;
3928b2e4378dSMatthew G. Knepley   PetscInt           N, Ni;
3929b2e4378dSMatthew G. Knepley   PetscInt           cdim, i, j;
3930b2e4378dSMatthew G. Knepley   PetscErrorCode     ierr;
3931b2e4378dSMatthew G. Knepley 
3932b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3933b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3934b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3935b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinates(dm, &coords);CHKERRQ(ierr);
3936b2e4378dSMatthew G. Knepley   if (coords) {
3937b2e4378dSMatthew G. Knepley     ierr = VecGetArrayRead(coords, &local_coords);CHKERRQ(ierr);
3938b2e4378dSMatthew G. Knepley     ierr = VecGetLocalSize(coords, &N);CHKERRQ(ierr);
3939b2e4378dSMatthew G. Knepley     Ni   = N/cdim;
3940b2e4378dSMatthew G. Knepley     for (i = 0; i < Ni; ++i) {
3941b2e4378dSMatthew G. Knepley       for (j = 0; j < 3; ++j) {
3942b2e4378dSMatthew G. Knepley         min[j] = j < cdim ? PetscMin(min[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3943b2e4378dSMatthew G. Knepley         max[j] = j < cdim ? PetscMax(max[j], PetscRealPart(local_coords[i*cdim+j])) : 0;
3944b2e4378dSMatthew G. Knepley       }
3945b2e4378dSMatthew G. Knepley     }
3946b2e4378dSMatthew G. Knepley     ierr = VecRestoreArrayRead(coords, &local_coords);CHKERRQ(ierr);
3947b2e4378dSMatthew G. Knepley   } else {
3948b2e4378dSMatthew G. Knepley     PetscBool isda;
3949b2e4378dSMatthew G. Knepley 
3950b2e4378dSMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) dm, DMDA, &isda);CHKERRQ(ierr);
3951b2e4378dSMatthew G. Knepley     if (isda) {ierr = DMGetLocalBoundingIndices_DMDA(dm, min, max);CHKERRQ(ierr);}
3952b2e4378dSMatthew G. Knepley   }
3953b2e4378dSMatthew G. Knepley   if (lmin) {ierr = PetscArraycpy(lmin, min, cdim);CHKERRQ(ierr);}
3954b2e4378dSMatthew G. Knepley   if (lmax) {ierr = PetscArraycpy(lmax, max, cdim);CHKERRQ(ierr);}
3955b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3956b2e4378dSMatthew G. Knepley }
3957b2e4378dSMatthew G. Knepley 
3958b2e4378dSMatthew G. Knepley /*@
3959b2e4378dSMatthew G. Knepley   DMGetBoundingBox - Returns the global bounding box for the DM.
3960b2e4378dSMatthew G. Knepley 
3961b2e4378dSMatthew G. Knepley   Collective
3962b2e4378dSMatthew G. Knepley 
3963b2e4378dSMatthew G. Knepley   Input Parameter:
3964b2e4378dSMatthew G. Knepley . dm - the DM
3965b2e4378dSMatthew G. Knepley 
3966b2e4378dSMatthew G. Knepley   Output Parameters:
3967b2e4378dSMatthew G. Knepley + gmin - global minimum coordinates (length coord dim, optional)
3968b2e4378dSMatthew G. Knepley - gmax - global maximim coordinates (length coord dim, optional)
3969b2e4378dSMatthew G. Knepley 
3970b2e4378dSMatthew G. Knepley   Level: beginner
3971b2e4378dSMatthew G. Knepley 
3972b2e4378dSMatthew G. Knepley .seealso: DMGetLocalBoundingBox(), DMGetCoordinates(), DMGetCoordinatesLocal()
3973b2e4378dSMatthew G. Knepley @*/
3974b2e4378dSMatthew G. Knepley PetscErrorCode DMGetBoundingBox(DM dm, PetscReal gmin[], PetscReal gmax[])
3975b2e4378dSMatthew G. Knepley {
3976b2e4378dSMatthew G. Knepley   PetscReal      lmin[3], lmax[3];
39776ce308c4SMatthew G. Knepley   PetscInt       cdim;
39786ce308c4SMatthew G. Knepley   PetscMPIInt    count;
3979b2e4378dSMatthew G. Knepley   PetscErrorCode ierr;
3980b2e4378dSMatthew G. Knepley 
3981b2e4378dSMatthew G. Knepley   PetscFunctionBegin;
3982b2e4378dSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
3983b2e4378dSMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
3984b2e4378dSMatthew G. Knepley   ierr = PetscMPIIntCast(cdim, &count);CHKERRQ(ierr);
3985b2e4378dSMatthew G. Knepley   ierr = DMGetLocalBoundingBox(dm, lmin, lmax);CHKERRQ(ierr);
3986b2e4378dSMatthew G. Knepley   if (gmin) {ierr = MPIU_Allreduce(lmin, gmin, count, MPIU_REAL, MPIU_MIN, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3987b2e4378dSMatthew G. Knepley   if (gmax) {ierr = MPIU_Allreduce(lmax, gmax, count, MPIU_REAL, MPIU_MAX, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);}
3988b2e4378dSMatthew G. Knepley   PetscFunctionReturn(0);
3989b2e4378dSMatthew G. Knepley }
3990b2e4378dSMatthew G. Knepley 
39917da65231SMatthew G Knepley /******************************** FEM Support **********************************/
39927da65231SMatthew G Knepley 
3993a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellVector(PetscInt c, const char name[], PetscInt len, const PetscScalar x[])
3994a6dfd86eSKarl Rupp {
39951d47ebbbSSatish Balay   PetscInt       f;
39961b30c384SMatthew G Knepley   PetscErrorCode ierr;
39971b30c384SMatthew G Knepley 
39987da65231SMatthew G Knepley   PetscFunctionBegin;
399974778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
40001d47ebbbSSatish Balay   for (f = 0; f < len; ++f) {
400157622a8eSBarry Smith     ierr = PetscPrintf(PETSC_COMM_SELF, "  | %g |\n", (double)PetscRealPart(x[f]));CHKERRQ(ierr);
40027da65231SMatthew G Knepley   }
40037da65231SMatthew G Knepley   PetscFunctionReturn(0);
40047da65231SMatthew G Knepley }
40057da65231SMatthew G Knepley 
4006a6dfd86eSKarl Rupp PetscErrorCode DMPrintCellMatrix(PetscInt c, const char name[], PetscInt rows, PetscInt cols, const PetscScalar A[])
4007a6dfd86eSKarl Rupp {
40081b30c384SMatthew G Knepley   PetscInt       f, g;
40097da65231SMatthew G Knepley   PetscErrorCode ierr;
40107da65231SMatthew G Knepley 
40117da65231SMatthew G Knepley   PetscFunctionBegin;
401274778d6cSJed Brown   ierr = PetscPrintf(PETSC_COMM_SELF, "Cell %D Element %s\n", c, name);CHKERRQ(ierr);
40131d47ebbbSSatish Balay   for (f = 0; f < rows; ++f) {
401474778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, "  |");CHKERRQ(ierr);
40151d47ebbbSSatish Balay     for (g = 0; g < cols; ++g) {
4016e3556bceSMatthew G. Knepley       ierr = PetscPrintf(PETSC_COMM_SELF, " % 9.5g", PetscRealPart(A[f*cols+g]));CHKERRQ(ierr);
40177da65231SMatthew G Knepley     }
401874778d6cSJed Brown     ierr = PetscPrintf(PETSC_COMM_SELF, " |\n");CHKERRQ(ierr);
40197da65231SMatthew G Knepley   }
40207da65231SMatthew G Knepley   PetscFunctionReturn(0);
40217da65231SMatthew G Knepley }
4022e7c4fc90SDmitry Karpeev 
40236113b454SMatthew G. Knepley PetscErrorCode DMPrintLocalVec(DM dm, const char name[], PetscReal tol, Vec X)
4024e759306cSMatthew G. Knepley {
40250c5b8624SToby Isaac   PetscInt          localSize, bs;
40260c5b8624SToby Isaac   PetscMPIInt       size;
40270c5b8624SToby Isaac   Vec               x, xglob;
40280c5b8624SToby Isaac   const PetscScalar *xarray;
4029e759306cSMatthew G. Knepley   PetscErrorCode    ierr;
4030e759306cSMatthew G. Knepley 
4031e759306cSMatthew G. Knepley   PetscFunctionBegin;
40329852e123SBarry Smith   ierr = MPI_Comm_size(PetscObjectComm((PetscObject) dm),&size);CHKERRQ(ierr);
4033e759306cSMatthew G. Knepley   ierr = VecDuplicate(X, &x);CHKERRQ(ierr);
4034e759306cSMatthew G. Knepley   ierr = VecCopy(X, x);CHKERRQ(ierr);
40356113b454SMatthew G. Knepley   ierr = VecChop(x, tol);CHKERRQ(ierr);
40360c5b8624SToby Isaac   ierr = PetscPrintf(PetscObjectComm((PetscObject) dm),"%s:\n",name);CHKERRQ(ierr);
40370c5b8624SToby Isaac   if (size > 1) {
40380c5b8624SToby Isaac     ierr = VecGetLocalSize(x,&localSize);CHKERRQ(ierr);
40390c5b8624SToby Isaac     ierr = VecGetArrayRead(x,&xarray);CHKERRQ(ierr);
40400c5b8624SToby Isaac     ierr = VecGetBlockSize(x,&bs);CHKERRQ(ierr);
40410c5b8624SToby Isaac     ierr = VecCreateMPIWithArray(PetscObjectComm((PetscObject) dm),bs,localSize,PETSC_DETERMINE,xarray,&xglob);CHKERRQ(ierr);
40420c5b8624SToby Isaac   } else {
40430c5b8624SToby Isaac     xglob = x;
40440c5b8624SToby Isaac   }
40450c5b8624SToby Isaac   ierr = VecView(xglob,PETSC_VIEWER_STDOUT_(PetscObjectComm((PetscObject) dm)));CHKERRQ(ierr);
40460c5b8624SToby Isaac   if (size > 1) {
40470c5b8624SToby Isaac     ierr = VecDestroy(&xglob);CHKERRQ(ierr);
40480c5b8624SToby Isaac     ierr = VecRestoreArrayRead(x,&xarray);CHKERRQ(ierr);
40490c5b8624SToby Isaac   }
4050e759306cSMatthew G. Knepley   ierr = VecDestroy(&x);CHKERRQ(ierr);
4051e759306cSMatthew G. Knepley   PetscFunctionReturn(0);
4052e759306cSMatthew G. Knepley }
4053e759306cSMatthew G. Knepley 
405488ed4aceSMatthew G Knepley /*@
40551bb6d2a8SBarry Smith   DMGetSection - Get the PetscSection encoding the local data layout for the DM.   This is equivalent to DMGetLocalSection(). Deprecated in v3.12
4056061576a5SJed Brown 
4057061576a5SJed Brown   Input Parameter:
4058061576a5SJed Brown . dm - The DM
4059061576a5SJed Brown 
4060061576a5SJed Brown   Output Parameter:
4061061576a5SJed Brown . section - The PetscSection
4062061576a5SJed Brown 
4063061576a5SJed Brown   Options Database Keys:
4064061576a5SJed Brown . -dm_petscsection_view - View the Section created by the DM
4065061576a5SJed Brown 
4066061576a5SJed Brown   Level: advanced
4067061576a5SJed Brown 
4068061576a5SJed Brown   Notes:
4069061576a5SJed Brown   Use DMGetLocalSection() in new code.
4070061576a5SJed Brown 
4071061576a5SJed Brown   This gets a borrowed reference, so the user should not destroy this PetscSection.
4072061576a5SJed Brown 
4073061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetLocalSection(), DMGetGlobalSection()
4074061576a5SJed Brown @*/
4075061576a5SJed Brown PetscErrorCode DMGetSection(DM dm, PetscSection *section)
4076061576a5SJed Brown {
4077061576a5SJed Brown   PetscErrorCode ierr;
4078061576a5SJed Brown 
4079061576a5SJed Brown   PetscFunctionBegin;
4080061576a5SJed Brown   ierr = DMGetLocalSection(dm,section);CHKERRQ(ierr);
4081061576a5SJed Brown   PetscFunctionReturn(0);
4082061576a5SJed Brown }
4083061576a5SJed Brown 
4084061576a5SJed Brown /*@
4085061576a5SJed Brown   DMGetLocalSection - Get the PetscSection encoding the local data layout for the DM.
408688ed4aceSMatthew G Knepley 
408788ed4aceSMatthew G Knepley   Input Parameter:
408888ed4aceSMatthew G Knepley . dm - The DM
408988ed4aceSMatthew G Knepley 
409088ed4aceSMatthew G Knepley   Output Parameter:
409188ed4aceSMatthew G Knepley . section - The PetscSection
409288ed4aceSMatthew G Knepley 
4093e5893cccSMatthew G. Knepley   Options Database Keys:
4094e5893cccSMatthew G. Knepley . -dm_petscsection_view - View the Section created by the DM
4095e5893cccSMatthew G. Knepley 
409688ed4aceSMatthew G Knepley   Level: intermediate
409788ed4aceSMatthew G Knepley 
409888ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
409988ed4aceSMatthew G Knepley 
4100061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetGlobalSection()
410188ed4aceSMatthew G Knepley @*/
4102061576a5SJed Brown PetscErrorCode DMGetLocalSection(DM dm, PetscSection *section)
41030adebc6cSBarry Smith {
4104fd59a867SMatthew G. Knepley   PetscErrorCode ierr;
4105fd59a867SMatthew G. Knepley 
410688ed4aceSMatthew G Knepley   PetscFunctionBegin;
410788ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
410888ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
41091bb6d2a8SBarry Smith   if (!dm->localSection && dm->ops->createlocalsection) {
4110e5e52638SMatthew G. Knepley     PetscInt d;
4111e5e52638SMatthew G. Knepley 
4112e5e52638SMatthew G. Knepley     if (dm->setfromoptionscalled) for (d = 0; d < dm->Nds; ++d) {ierr = PetscDSSetFromOptions(dm->probs[d].ds);CHKERRQ(ierr);}
41131bb6d2a8SBarry Smith     ierr = (*dm->ops->createlocalsection)(dm);CHKERRQ(ierr);
41141bb6d2a8SBarry Smith     if (dm->localSection) {ierr = PetscObjectViewFromOptions((PetscObject) dm->localSection, NULL, "-dm_petscsection_view");CHKERRQ(ierr);}
41152f0f8703SMatthew G. Knepley   }
41161bb6d2a8SBarry Smith   *section = dm->localSection;
411788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
411888ed4aceSMatthew G Knepley }
411988ed4aceSMatthew G Knepley 
412088ed4aceSMatthew G Knepley /*@
41211bb6d2a8SBarry Smith   DMSetSection - Set the PetscSection encoding the local data layout for the DM.  This is equivalent to DMSetLocalSection(). Deprecated in v3.12
4122061576a5SJed Brown 
4123061576a5SJed Brown   Input Parameters:
4124061576a5SJed Brown + dm - The DM
4125061576a5SJed Brown - section - The PetscSection
4126061576a5SJed Brown 
4127061576a5SJed Brown   Level: advanced
4128061576a5SJed Brown 
4129061576a5SJed Brown   Notes:
4130061576a5SJed Brown   Use DMSetLocalSection() in new code.
4131061576a5SJed Brown 
4132061576a5SJed Brown   Any existing Section will be destroyed
4133061576a5SJed Brown 
4134061576a5SJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection(), DMSetGlobalSection()
4135061576a5SJed Brown @*/
4136061576a5SJed Brown PetscErrorCode DMSetSection(DM dm, PetscSection section)
4137061576a5SJed Brown {
4138061576a5SJed Brown   PetscErrorCode ierr;
4139061576a5SJed Brown 
4140061576a5SJed Brown   PetscFunctionBegin;
4141061576a5SJed Brown   ierr = DMSetLocalSection(dm,section);CHKERRQ(ierr);
4142061576a5SJed Brown   PetscFunctionReturn(0);
4143061576a5SJed Brown }
4144061576a5SJed Brown 
4145061576a5SJed Brown /*@
4146061576a5SJed Brown   DMSetLocalSection - Set the PetscSection encoding the local data layout for the DM.
414788ed4aceSMatthew G Knepley 
414888ed4aceSMatthew G Knepley   Input Parameters:
414988ed4aceSMatthew G Knepley + dm - The DM
415088ed4aceSMatthew G Knepley - section - The PetscSection
415188ed4aceSMatthew G Knepley 
415288ed4aceSMatthew G Knepley   Level: intermediate
415388ed4aceSMatthew G Knepley 
415488ed4aceSMatthew G Knepley   Note: Any existing Section will be destroyed
415588ed4aceSMatthew G Knepley 
4156061576a5SJed Brown .seealso: DMGetLocalSection(), DMSetGlobalSection()
415788ed4aceSMatthew G Knepley @*/
4158061576a5SJed Brown PetscErrorCode DMSetLocalSection(DM dm, PetscSection section)
41590adebc6cSBarry Smith {
4160c473ab19SMatthew G. Knepley   PetscInt       numFields = 0;
4161af122d2aSMatthew G Knepley   PetscInt       f;
416288ed4aceSMatthew G Knepley   PetscErrorCode ierr;
416388ed4aceSMatthew G Knepley 
416488ed4aceSMatthew G Knepley   PetscFunctionBegin;
416588ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4166b9d85ea2SLisandro Dalcin   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
41671d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
41681bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->localSection);CHKERRQ(ierr);
41691bb6d2a8SBarry Smith   dm->localSection = section;
41701bb6d2a8SBarry Smith   if (section) {ierr = PetscSectionGetNumFields(dm->localSection, &numFields);CHKERRQ(ierr);}
4171af122d2aSMatthew G Knepley   if (numFields) {
4172af122d2aSMatthew G Knepley     ierr = DMSetNumFields(dm, numFields);CHKERRQ(ierr);
4173af122d2aSMatthew G Knepley     for (f = 0; f < numFields; ++f) {
41740f21e855SMatthew G. Knepley       PetscObject disc;
4175af122d2aSMatthew G Knepley       const char *name;
4176af122d2aSMatthew G Knepley 
41771bb6d2a8SBarry Smith       ierr = PetscSectionGetFieldName(dm->localSection, f, &name);CHKERRQ(ierr);
417844a7f3ddSMatthew G. Knepley       ierr = DMGetField(dm, f, NULL, &disc);CHKERRQ(ierr);
41790f21e855SMatthew G. Knepley       ierr = PetscObjectSetName(disc, name);CHKERRQ(ierr);
4180af122d2aSMatthew G Knepley     }
4181af122d2aSMatthew G Knepley   }
4182e87a4003SBarry Smith   /* The global section will be rebuilt in the next call to DMGetGlobalSection(). */
41831bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
418488ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
418588ed4aceSMatthew G Knepley }
418688ed4aceSMatthew G Knepley 
41879435951eSToby Isaac /*@
4188b7385021SStefano Zampini   DMGetDefaultConstraints - Get the PetscSection and Mat that specify the local constraint interpolation. See DMSetDefaultConstraints() for a description of the purpose of constraint interpolation.
41899435951eSToby Isaac 
4190e228b242SToby Isaac   not collective
4191e228b242SToby Isaac 
41929435951eSToby Isaac   Input Parameter:
41939435951eSToby Isaac . dm - The DM
41949435951eSToby Isaac 
41959435951eSToby Isaac   Output Parameter:
41969435951eSToby 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.
41979435951eSToby 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.
41989435951eSToby Isaac 
41999435951eSToby Isaac   Level: advanced
42009435951eSToby Isaac 
42019435951eSToby Isaac   Note: This gets borrowed references, so the user should not destroy the PetscSection or the Mat.
42029435951eSToby Isaac 
42039435951eSToby Isaac .seealso: DMSetDefaultConstraints()
42049435951eSToby Isaac @*/
42059435951eSToby Isaac PetscErrorCode DMGetDefaultConstraints(DM dm, PetscSection *section, Mat *mat)
42069435951eSToby Isaac {
42079435951eSToby Isaac   PetscErrorCode ierr;
42089435951eSToby Isaac 
42099435951eSToby Isaac   PetscFunctionBegin;
42109435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
42119435951eSToby Isaac   if (!dm->defaultConstraintSection && !dm->defaultConstraintMat && dm->ops->createdefaultconstraints) {ierr = (*dm->ops->createdefaultconstraints)(dm);CHKERRQ(ierr);}
421245a75d81SToby Isaac   if (section) {*section = dm->defaultConstraintSection;}
421345a75d81SToby Isaac   if (mat) {*mat = dm->defaultConstraintMat;}
42149435951eSToby Isaac   PetscFunctionReturn(0);
42159435951eSToby Isaac }
42169435951eSToby Isaac 
42179435951eSToby Isaac /*@
4218b7385021SStefano Zampini   DMSetDefaultConstraints - Set the PetscSection and Mat that specify the local constraint interpolation.
42199435951eSToby Isaac 
42209435951eSToby 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().
42219435951eSToby Isaac 
42229435951eSToby 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.
42239435951eSToby Isaac 
4224e228b242SToby Isaac   collective on dm
4225e228b242SToby Isaac 
42269435951eSToby Isaac   Input Parameters:
42279435951eSToby Isaac + dm - The DM
4228e228b242SToby 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).
4229e228b242SToby 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).
42309435951eSToby Isaac 
42319435951eSToby Isaac   Level: advanced
42329435951eSToby Isaac 
42339435951eSToby Isaac   Note: This increments the references of the PetscSection and the Mat, so they user can destroy them
42349435951eSToby Isaac 
42359435951eSToby Isaac .seealso: DMGetDefaultConstraints()
42369435951eSToby Isaac @*/
42379435951eSToby Isaac PetscErrorCode DMSetDefaultConstraints(DM dm, PetscSection section, Mat mat)
42389435951eSToby Isaac {
4239e228b242SToby Isaac   PetscMPIInt result;
42409435951eSToby Isaac   PetscErrorCode ierr;
42419435951eSToby Isaac 
42429435951eSToby Isaac   PetscFunctionBegin;
42439435951eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4244e228b242SToby Isaac   if (section) {
4245e228b242SToby Isaac     PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
4246e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)section),&result);CHKERRQ(ierr);
4247f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint section must have local communicator");
4248e228b242SToby Isaac   }
4249e228b242SToby Isaac   if (mat) {
4250e228b242SToby Isaac     PetscValidHeaderSpecific(mat,MAT_CLASSID,3);
4251e228b242SToby Isaac     ierr = MPI_Comm_compare(PETSC_COMM_SELF,PetscObjectComm((PetscObject)mat),&result);CHKERRQ(ierr);
4252f60917d2SBarry Smith     if (result != MPI_CONGRUENT && result != MPI_IDENT) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_NOTSAMECOMM,"constraint matrix must have local communicator");
4253e228b242SToby Isaac   }
42549435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
42559435951eSToby Isaac   ierr = PetscSectionDestroy(&dm->defaultConstraintSection);CHKERRQ(ierr);
42569435951eSToby Isaac   dm->defaultConstraintSection = section;
42579435951eSToby Isaac   ierr = PetscObjectReference((PetscObject)mat);CHKERRQ(ierr);
42589435951eSToby Isaac   ierr = MatDestroy(&dm->defaultConstraintMat);CHKERRQ(ierr);
42599435951eSToby Isaac   dm->defaultConstraintMat = mat;
42609435951eSToby Isaac   PetscFunctionReturn(0);
42619435951eSToby Isaac }
42629435951eSToby Isaac 
4263497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
4264507e4973SMatthew G. Knepley /*
4265507e4973SMatthew G. Knepley   DMDefaultSectionCheckConsistency - Check the consistentcy of the global and local sections.
4266507e4973SMatthew G. Knepley 
4267507e4973SMatthew G. Knepley   Input Parameters:
4268507e4973SMatthew G. Knepley + dm - The DM
4269507e4973SMatthew G. Knepley . localSection - PetscSection describing the local data layout
4270507e4973SMatthew G. Knepley - globalSection - PetscSection describing the global data layout
4271507e4973SMatthew G. Knepley 
4272507e4973SMatthew G. Knepley   Level: intermediate
4273507e4973SMatthew G. Knepley 
42741bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF()
4275507e4973SMatthew G. Knepley */
4276f741bcd2SMatthew G. Knepley static PetscErrorCode DMDefaultSectionCheckConsistency_Internal(DM dm, PetscSection localSection, PetscSection globalSection)
4277507e4973SMatthew G. Knepley {
4278507e4973SMatthew G. Knepley   MPI_Comm        comm;
4279507e4973SMatthew G. Knepley   PetscLayout     layout;
4280507e4973SMatthew G. Knepley   const PetscInt *ranges;
4281507e4973SMatthew G. Knepley   PetscInt        pStart, pEnd, p, nroots;
4282507e4973SMatthew G. Knepley   PetscMPIInt     size, rank;
4283507e4973SMatthew G. Knepley   PetscBool       valid = PETSC_TRUE, gvalid;
4284507e4973SMatthew G. Knepley   PetscErrorCode  ierr;
4285507e4973SMatthew G. Knepley 
4286507e4973SMatthew G. Knepley   PetscFunctionBegin;
4287507e4973SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
4288507e4973SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4289507e4973SMatthew G. Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
4290507e4973SMatthew G. Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
4291507e4973SMatthew G. Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
4292507e4973SMatthew G. Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
4293507e4973SMatthew G. Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
4294507e4973SMatthew G. Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
4295507e4973SMatthew G. Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
4296507e4973SMatthew G. Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
4297507e4973SMatthew G. Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4298507e4973SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
4299f741bcd2SMatthew G. Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d;
4300507e4973SMatthew G. Knepley 
4301507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
4302507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
4303507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
4304507e4973SMatthew G. Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
4305507e4973SMatthew G. Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4306507e4973SMatthew G. Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
4307507e4973SMatthew G. Knepley     if (!gdof) continue; /* Censored point */
4308507e4973SMatthew 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;}
4309507e4973SMatthew 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;}
4310507e4973SMatthew G. Knepley     if (gdof < 0) {
4311507e4973SMatthew G. Knepley       gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
4312507e4973SMatthew G. Knepley       for (d = 0; d < gsize; ++d) {
4313507e4973SMatthew G. Knepley         PetscInt offset = -(goff+1) + d, r;
4314507e4973SMatthew G. Knepley 
4315507e4973SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
4316507e4973SMatthew G. Knepley         if (r < 0) r = -(r+2);
4317507e4973SMatthew 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;}
4318507e4973SMatthew G. Knepley       }
4319507e4973SMatthew G. Knepley     }
4320507e4973SMatthew G. Knepley   }
4321507e4973SMatthew G. Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
4322507e4973SMatthew G. Knepley   ierr = PetscSynchronizedFlush(comm, NULL);CHKERRQ(ierr);
4323b2566f29SBarry Smith   ierr = MPIU_Allreduce(&valid, &gvalid, 1, MPIU_BOOL, MPI_LAND, comm);CHKERRQ(ierr);
4324507e4973SMatthew G. Knepley   if (!gvalid) {
4325507e4973SMatthew G. Knepley     ierr = DMView(dm, NULL);CHKERRQ(ierr);
4326507e4973SMatthew G. Knepley     SETERRQ(comm, PETSC_ERR_ARG_WRONG, "Inconsistent local and global sections");
4327507e4973SMatthew G. Knepley   }
4328507e4973SMatthew G. Knepley   PetscFunctionReturn(0);
4329507e4973SMatthew G. Knepley }
4330f741bcd2SMatthew G. Knepley #endif
4331507e4973SMatthew G. Knepley 
433288ed4aceSMatthew G Knepley /*@
4333e87a4003SBarry Smith   DMGetGlobalSection - Get the PetscSection encoding the global data layout for the DM.
433488ed4aceSMatthew G Knepley 
4335d083f849SBarry Smith   Collective on dm
43368b1ab98fSJed Brown 
433788ed4aceSMatthew G Knepley   Input Parameter:
433888ed4aceSMatthew G Knepley . dm - The DM
433988ed4aceSMatthew G Knepley 
434088ed4aceSMatthew G Knepley   Output Parameter:
434188ed4aceSMatthew G Knepley . section - The PetscSection
434288ed4aceSMatthew G Knepley 
434388ed4aceSMatthew G Knepley   Level: intermediate
434488ed4aceSMatthew G Knepley 
434588ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSection.
434688ed4aceSMatthew G Knepley 
434792fd8e1eSJed Brown .seealso: DMSetLocalSection(), DMGetLocalSection()
434888ed4aceSMatthew G Knepley @*/
4349e87a4003SBarry Smith PetscErrorCode DMGetGlobalSection(DM dm, PetscSection *section)
43500adebc6cSBarry Smith {
435188ed4aceSMatthew G Knepley   PetscErrorCode ierr;
435288ed4aceSMatthew G Knepley 
435388ed4aceSMatthew G Knepley   PetscFunctionBegin;
435488ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
435588ed4aceSMatthew G Knepley   PetscValidPointer(section, 2);
43561bb6d2a8SBarry Smith   if (!dm->globalSection) {
4357fd59a867SMatthew G. Knepley     PetscSection s;
4358fd59a867SMatthew G. Knepley 
435992fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &s);CHKERRQ(ierr);
4360fd59a867SMatthew 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");
436133907cc2SStefano 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");
43621bb6d2a8SBarry Smith     ierr = PetscSectionCreateGlobalSection(s, dm->sf, PETSC_FALSE, PETSC_FALSE, &dm->globalSection);CHKERRQ(ierr);
4363cf06b437SMatthew G. Knepley     ierr = PetscLayoutDestroy(&dm->map);CHKERRQ(ierr);
43641bb6d2a8SBarry Smith     ierr = PetscSectionGetValueLayout(PetscObjectComm((PetscObject)dm), dm->globalSection, &dm->map);CHKERRQ(ierr);
43651bb6d2a8SBarry Smith     ierr = PetscSectionViewFromOptions(dm->globalSection, NULL, "-global_section_view");CHKERRQ(ierr);
436688ed4aceSMatthew G Knepley   }
43671bb6d2a8SBarry Smith   *section = dm->globalSection;
436888ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
436988ed4aceSMatthew G Knepley }
437088ed4aceSMatthew G Knepley 
4371b21d0597SMatthew G Knepley /*@
4372e87a4003SBarry Smith   DMSetGlobalSection - Set the PetscSection encoding the global data layout for the DM.
4373b21d0597SMatthew G Knepley 
4374b21d0597SMatthew G Knepley   Input Parameters:
4375b21d0597SMatthew G Knepley + dm - The DM
43765080bbdbSMatthew G Knepley - section - The PetscSection, or NULL
4377b21d0597SMatthew G Knepley 
4378b21d0597SMatthew G Knepley   Level: intermediate
4379b21d0597SMatthew G Knepley 
4380b21d0597SMatthew G Knepley   Note: Any existing Section will be destroyed
4381b21d0597SMatthew G Knepley 
438292fd8e1eSJed Brown .seealso: DMGetGlobalSection(), DMSetLocalSection()
4383b21d0597SMatthew G Knepley @*/
4384e87a4003SBarry Smith PetscErrorCode DMSetGlobalSection(DM dm, PetscSection section)
43850adebc6cSBarry Smith {
4386b21d0597SMatthew G Knepley   PetscErrorCode ierr;
4387b21d0597SMatthew G Knepley 
4388b21d0597SMatthew G Knepley   PetscFunctionBegin;
4389b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
43905080bbdbSMatthew G Knepley   if (section) PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,2);
43911d799100SJed Brown   ierr = PetscObjectReference((PetscObject)section);CHKERRQ(ierr);
43921bb6d2a8SBarry Smith   ierr = PetscSectionDestroy(&dm->globalSection);CHKERRQ(ierr);
43931bb6d2a8SBarry Smith   dm->globalSection = section;
4394497880caSRichard Tran Mills #if defined(PETSC_USE_DEBUG)
43951bb6d2a8SBarry Smith   if (section) {ierr = DMDefaultSectionCheckConsistency_Internal(dm, dm->localSection, section);CHKERRQ(ierr);}
4396507e4973SMatthew G. Knepley #endif
4397b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4398b21d0597SMatthew G Knepley }
4399b21d0597SMatthew G Knepley 
440088ed4aceSMatthew G Knepley /*@
44011bb6d2a8SBarry Smith   DMGetSectionSF - Get the PetscSF encoding the parallel dof overlap for the DM. If it has not been set,
440288ed4aceSMatthew G Knepley   it is created from the default PetscSection layouts in the DM.
440388ed4aceSMatthew G Knepley 
440488ed4aceSMatthew G Knepley   Input Parameter:
440588ed4aceSMatthew G Knepley . dm - The DM
440688ed4aceSMatthew G Knepley 
440788ed4aceSMatthew G Knepley   Output Parameter:
440888ed4aceSMatthew G Knepley . sf - The PetscSF
440988ed4aceSMatthew G Knepley 
441088ed4aceSMatthew G Knepley   Level: intermediate
441188ed4aceSMatthew G Knepley 
441288ed4aceSMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
441388ed4aceSMatthew G Knepley 
44141bb6d2a8SBarry Smith .seealso: DMSetSectionSF(), DMCreateSectionSF()
441588ed4aceSMatthew G Knepley @*/
44161bb6d2a8SBarry Smith PetscErrorCode DMGetSectionSF(DM dm, PetscSF *sf)
44170adebc6cSBarry Smith {
441888ed4aceSMatthew G Knepley   PetscInt       nroots;
441988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
442088ed4aceSMatthew G Knepley 
442188ed4aceSMatthew G Knepley   PetscFunctionBegin;
442288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
442388ed4aceSMatthew G Knepley   PetscValidPointer(sf, 2);
44241bb6d2a8SBarry Smith   if (!dm->sectionSF) {
44251bb6d2a8SBarry Smith     ierr = PetscSFCreate(PetscObjectComm((PetscObject)dm),&dm->sectionSF);CHKERRQ(ierr);
442633907cc2SStefano Zampini   }
44271bb6d2a8SBarry Smith   ierr = PetscSFGetGraph(dm->sectionSF, &nroots, NULL, NULL, NULL);CHKERRQ(ierr);
442888ed4aceSMatthew G Knepley   if (nroots < 0) {
442988ed4aceSMatthew G Knepley     PetscSection section, gSection;
443088ed4aceSMatthew G Knepley 
443192fd8e1eSJed Brown     ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
443231ea6d37SMatthew G Knepley     if (section) {
4433e87a4003SBarry Smith       ierr = DMGetGlobalSection(dm, &gSection);CHKERRQ(ierr);
44341bb6d2a8SBarry Smith       ierr = DMCreateSectionSF(dm, section, gSection);CHKERRQ(ierr);
443531ea6d37SMatthew G Knepley     } else {
44360298fd71SBarry Smith       *sf = NULL;
443731ea6d37SMatthew G Knepley       PetscFunctionReturn(0);
443831ea6d37SMatthew G Knepley     }
443988ed4aceSMatthew G Knepley   }
44401bb6d2a8SBarry Smith   *sf = dm->sectionSF;
444188ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
444288ed4aceSMatthew G Knepley }
444388ed4aceSMatthew G Knepley 
444488ed4aceSMatthew G Knepley /*@
44451bb6d2a8SBarry Smith   DMSetSectionSF - Set the PetscSF encoding the parallel dof overlap for the DM
444688ed4aceSMatthew G Knepley 
444788ed4aceSMatthew G Knepley   Input Parameters:
444888ed4aceSMatthew G Knepley + dm - The DM
444988ed4aceSMatthew G Knepley - sf - The PetscSF
445088ed4aceSMatthew G Knepley 
445188ed4aceSMatthew G Knepley   Level: intermediate
445288ed4aceSMatthew G Knepley 
445388ed4aceSMatthew G Knepley   Note: Any previous SF is destroyed
445488ed4aceSMatthew G Knepley 
44551bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMCreateSectionSF()
445688ed4aceSMatthew G Knepley @*/
44571bb6d2a8SBarry Smith PetscErrorCode DMSetSectionSF(DM dm, PetscSF sf)
44580adebc6cSBarry Smith {
445988ed4aceSMatthew G Knepley   PetscErrorCode ierr;
446088ed4aceSMatthew G Knepley 
446188ed4aceSMatthew G Knepley   PetscFunctionBegin;
446288ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4463b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
446433907cc2SStefano Zampini   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
44651bb6d2a8SBarry Smith   ierr = PetscSFDestroy(&dm->sectionSF);CHKERRQ(ierr);
44661bb6d2a8SBarry Smith   dm->sectionSF = sf;
446788ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
446888ed4aceSMatthew G Knepley }
446988ed4aceSMatthew G Knepley 
447088ed4aceSMatthew G Knepley /*@C
44711bb6d2a8SBarry Smith   DMCreateSectionSF - Create the PetscSF encoding the parallel dof overlap for the DM based upon the PetscSections
447288ed4aceSMatthew G Knepley   describing the data layout.
447388ed4aceSMatthew G Knepley 
447488ed4aceSMatthew G Knepley   Input Parameters:
447588ed4aceSMatthew G Knepley + dm - The DM
447688ed4aceSMatthew G Knepley . localSection - PetscSection describing the local data layout
447788ed4aceSMatthew G Knepley - globalSection - PetscSection describing the global data layout
447888ed4aceSMatthew G Knepley 
44791bb6d2a8SBarry Smith   Notes: One usually uses DMGetSectionSF() to obtain the PetscSF
448088ed4aceSMatthew G Knepley 
44811bb6d2a8SBarry Smith   Level: developer
44821bb6d2a8SBarry Smith 
44831bb6d2a8SBarry Smith   Developer Note: Since this routine has for arguments the two sections from the DM and puts the resulting PetscSF
44841bb6d2a8SBarry Smith                   directly into the DM, perhaps this function should not take the local and global sections as
44851bb6d2a8SBarry Smith                   input and should just obtain them from the DM?
44861bb6d2a8SBarry Smith 
44871bb6d2a8SBarry Smith .seealso: DMGetSectionSF(), DMSetSectionSF(), DMGetLocalSection(), DMGetGlobalSection()
448888ed4aceSMatthew G Knepley @*/
44891bb6d2a8SBarry Smith PetscErrorCode DMCreateSectionSF(DM dm, PetscSection localSection, PetscSection globalSection)
449088ed4aceSMatthew G Knepley {
449182f516ccSBarry Smith   MPI_Comm       comm;
449288ed4aceSMatthew G Knepley   PetscLayout    layout;
449388ed4aceSMatthew G Knepley   const PetscInt *ranges;
449488ed4aceSMatthew G Knepley   PetscInt       *local;
449588ed4aceSMatthew G Knepley   PetscSFNode    *remote;
4496ecd73843SMatthew G. Knepley   PetscInt       pStart, pEnd, p, nroots, nleaves = 0, l;
449788ed4aceSMatthew G Knepley   PetscMPIInt    size, rank;
449888ed4aceSMatthew G Knepley   PetscErrorCode ierr;
449988ed4aceSMatthew G Knepley 
450088ed4aceSMatthew G Knepley   PetscFunctionBegin;
450188ed4aceSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4502367003a6SStefano Zampini   ierr = PetscObjectGetComm((PetscObject)dm,&comm);CHKERRQ(ierr);
450388ed4aceSMatthew G Knepley   ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr);
450488ed4aceSMatthew G Knepley   ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr);
450588ed4aceSMatthew G Knepley   ierr = PetscSectionGetChart(globalSection, &pStart, &pEnd);CHKERRQ(ierr);
450688ed4aceSMatthew G Knepley   ierr = PetscSectionGetConstrainedStorageSize(globalSection, &nroots);CHKERRQ(ierr);
450788ed4aceSMatthew G Knepley   ierr = PetscLayoutCreate(comm, &layout);CHKERRQ(ierr);
450888ed4aceSMatthew G Knepley   ierr = PetscLayoutSetBlockSize(layout, 1);CHKERRQ(ierr);
450988ed4aceSMatthew G Knepley   ierr = PetscLayoutSetLocalSize(layout, nroots);CHKERRQ(ierr);
451088ed4aceSMatthew G Knepley   ierr = PetscLayoutSetUp(layout);CHKERRQ(ierr);
451188ed4aceSMatthew G Knepley   ierr = PetscLayoutGetRanges(layout, &ranges);CHKERRQ(ierr);
4512ecd73843SMatthew G. Knepley   for (p = pStart; p < pEnd; ++p) {
45136636e97aSMatthew G Knepley     PetscInt gdof, gcdof;
451488ed4aceSMatthew G Knepley 
45156636e97aSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
45166636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
4517235fbf56SMatthew 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));
45186636e97aSMatthew G Knepley     nleaves += gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
451988ed4aceSMatthew G Knepley   }
4520785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &local);CHKERRQ(ierr);
4521785e854fSJed Brown   ierr = PetscMalloc1(nleaves, &remote);CHKERRQ(ierr);
452288ed4aceSMatthew G Knepley   for (p = pStart, l = 0; p < pEnd; ++p) {
45231f588964SMatthew G Knepley     const PetscInt *cind;
45246636e97aSMatthew G Knepley     PetscInt       dof, cdof, off, gdof, gcdof, goff, gsize, d, c;
452588ed4aceSMatthew G Knepley 
452688ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(localSection, p, &dof);CHKERRQ(ierr);
452788ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(localSection, p, &off);CHKERRQ(ierr);
452888ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(localSection, p, &cdof);CHKERRQ(ierr);
452988ed4aceSMatthew G Knepley     ierr = PetscSectionGetConstraintIndices(localSection, p, &cind);CHKERRQ(ierr);
453088ed4aceSMatthew G Knepley     ierr = PetscSectionGetDof(globalSection, p, &gdof);CHKERRQ(ierr);
45316636e97aSMatthew G Knepley     ierr = PetscSectionGetConstraintDof(globalSection, p, &gcdof);CHKERRQ(ierr);
453288ed4aceSMatthew G Knepley     ierr = PetscSectionGetOffset(globalSection, p, &goff);CHKERRQ(ierr);
45336636e97aSMatthew G Knepley     if (!gdof) continue; /* Censored point */
45346636e97aSMatthew G Knepley     gsize = gdof < 0 ? -(gdof+1)-gcdof : gdof-gcdof;
45356636e97aSMatthew G Knepley     if (gsize != dof-cdof) {
4536057b4bcdSMatthew 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);
45376636e97aSMatthew G Knepley       cdof = 0; /* Ignore constraints */
45386636e97aSMatthew G Knepley     }
453988ed4aceSMatthew G Knepley     for (d = 0, c = 0; d < dof; ++d) {
454088ed4aceSMatthew G Knepley       if ((c < cdof) && (cind[c] == d)) {++c; continue;}
454188ed4aceSMatthew G Knepley       local[l+d-c] = off+d;
454288ed4aceSMatthew G Knepley     }
454388ed4aceSMatthew G Knepley     if (gdof < 0) {
45446636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
454588ed4aceSMatthew G Knepley         PetscInt offset = -(goff+1) + d, r;
454688ed4aceSMatthew G Knepley 
454705376888SMatthew G. Knepley         ierr = PetscFindInt(offset,size+1,ranges,&r);CHKERRQ(ierr);
454831d3f06eSJed Brown         if (r < 0) r = -(r+2);
454905376888SMatthew 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);
455088ed4aceSMatthew G Knepley         remote[l].rank  = r;
455188ed4aceSMatthew G Knepley         remote[l].index = offset - ranges[r];
455288ed4aceSMatthew G Knepley       }
455388ed4aceSMatthew G Knepley     } else {
45546636e97aSMatthew G Knepley       for (d = 0; d < gsize; ++d, ++l) {
455588ed4aceSMatthew G Knepley         remote[l].rank  = rank;
455688ed4aceSMatthew G Knepley         remote[l].index = goff+d - ranges[rank];
455788ed4aceSMatthew G Knepley       }
455888ed4aceSMatthew G Knepley     }
455988ed4aceSMatthew G Knepley   }
45606636e97aSMatthew G Knepley   if (l != nleaves) SETERRQ2(comm, PETSC_ERR_PLIB, "Iteration error, l %d != nleaves %d", l, nleaves);
456188ed4aceSMatthew G Knepley   ierr = PetscLayoutDestroy(&layout);CHKERRQ(ierr);
45621bb6d2a8SBarry Smith   ierr = PetscSFSetGraph(dm->sectionSF, nroots, nleaves, local, PETSC_OWN_POINTER, remote, PETSC_OWN_POINTER);CHKERRQ(ierr);
456388ed4aceSMatthew G Knepley   PetscFunctionReturn(0);
456488ed4aceSMatthew G Knepley }
4565af122d2aSMatthew G Knepley 
4566b21d0597SMatthew G Knepley /*@
4567b21d0597SMatthew G Knepley   DMGetPointSF - Get the PetscSF encoding the parallel section point overlap for the DM.
4568b21d0597SMatthew G Knepley 
4569b21d0597SMatthew G Knepley   Input Parameter:
4570b21d0597SMatthew G Knepley . dm - The DM
4571b21d0597SMatthew G Knepley 
4572b21d0597SMatthew G Knepley   Output Parameter:
4573b21d0597SMatthew G Knepley . sf - The PetscSF
4574b21d0597SMatthew G Knepley 
4575b21d0597SMatthew G Knepley   Level: intermediate
4576b21d0597SMatthew G Knepley 
4577b21d0597SMatthew G Knepley   Note: This gets a borrowed reference, so the user should not destroy this PetscSF.
4578b21d0597SMatthew G Knepley 
45791bb6d2a8SBarry Smith .seealso: DMSetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4580b21d0597SMatthew G Knepley @*/
45810adebc6cSBarry Smith PetscErrorCode DMGetPointSF(DM dm, PetscSF *sf)
45820adebc6cSBarry Smith {
4583b21d0597SMatthew G Knepley   PetscFunctionBegin;
4584b21d0597SMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4585b21d0597SMatthew G Knepley   PetscValidPointer(sf, 2);
4586b21d0597SMatthew G Knepley   *sf = dm->sf;
4587b21d0597SMatthew G Knepley   PetscFunctionReturn(0);
4588b21d0597SMatthew G Knepley }
4589b21d0597SMatthew G Knepley 
4590057b4bcdSMatthew G Knepley /*@
4591057b4bcdSMatthew G Knepley   DMSetPointSF - Set the PetscSF encoding the parallel section point overlap for the DM.
4592057b4bcdSMatthew G Knepley 
4593057b4bcdSMatthew G Knepley   Input Parameters:
4594057b4bcdSMatthew G Knepley + dm - The DM
4595057b4bcdSMatthew G Knepley - sf - The PetscSF
4596057b4bcdSMatthew G Knepley 
4597057b4bcdSMatthew G Knepley   Level: intermediate
4598057b4bcdSMatthew G Knepley 
45991bb6d2a8SBarry Smith .seealso: DMGetPointSF(), DMGetSectionSF(), DMSetSectionSF(), DMCreateSectionSF()
4600057b4bcdSMatthew G Knepley @*/
46010adebc6cSBarry Smith PetscErrorCode DMSetPointSF(DM dm, PetscSF sf)
46020adebc6cSBarry Smith {
4603057b4bcdSMatthew G Knepley   PetscErrorCode ierr;
4604057b4bcdSMatthew G Knepley 
4605057b4bcdSMatthew G Knepley   PetscFunctionBegin;
4606057b4bcdSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4607b9d85ea2SLisandro Dalcin   if (sf) PetscValidHeaderSpecific(sf, PETSCSF_CLASSID, 2);
4608057b4bcdSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) sf);CHKERRQ(ierr);
460933907cc2SStefano Zampini   ierr = PetscSFDestroy(&dm->sf);CHKERRQ(ierr);
4610057b4bcdSMatthew G Knepley   dm->sf = sf;
4611057b4bcdSMatthew G Knepley   PetscFunctionReturn(0);
4612057b4bcdSMatthew G Knepley }
4613057b4bcdSMatthew G Knepley 
461434aa8a36SMatthew G. Knepley static PetscErrorCode DMSetDefaultAdjacency_Private(DM dm, PetscInt f, PetscObject disc)
461534aa8a36SMatthew G. Knepley {
461634aa8a36SMatthew G. Knepley   PetscClassId   id;
461734aa8a36SMatthew G. Knepley   PetscErrorCode ierr;
461834aa8a36SMatthew G. Knepley 
461934aa8a36SMatthew G. Knepley   PetscFunctionBegin;
462034aa8a36SMatthew G. Knepley   ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
462134aa8a36SMatthew G. Knepley   if (id == PETSCFE_CLASSID) {
462234aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
462334aa8a36SMatthew G. Knepley   } else if (id == PETSCFV_CLASSID) {
462434aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_TRUE, PETSC_FALSE);CHKERRQ(ierr);
462517c1d62eSMatthew G. Knepley   } else {
462617c1d62eSMatthew G. Knepley     ierr = DMSetAdjacency(dm, f, PETSC_FALSE, PETSC_TRUE);CHKERRQ(ierr);
462734aa8a36SMatthew G. Knepley   }
462834aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
462934aa8a36SMatthew G. Knepley }
463034aa8a36SMatthew G. Knepley 
463144a7f3ddSMatthew G. Knepley static PetscErrorCode DMFieldEnlarge_Static(DM dm, PetscInt NfNew)
463244a7f3ddSMatthew G. Knepley {
463344a7f3ddSMatthew G. Knepley   RegionField   *tmpr;
463444a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf, f;
463544a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
463644a7f3ddSMatthew G. Knepley 
463744a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
463844a7f3ddSMatthew G. Knepley   if (Nf >= NfNew) PetscFunctionReturn(0);
463944a7f3ddSMatthew G. Knepley   ierr = PetscMalloc1(NfNew, &tmpr);CHKERRQ(ierr);
464044a7f3ddSMatthew G. Knepley   for (f = 0; f < Nf; ++f) tmpr[f] = dm->fields[f];
464144a7f3ddSMatthew G. Knepley   for (f = Nf; f < NfNew; ++f) {tmpr[f].disc = NULL; tmpr[f].label = NULL;}
464244a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
464344a7f3ddSMatthew G. Knepley   dm->Nf     = NfNew;
464444a7f3ddSMatthew G. Knepley   dm->fields = tmpr;
464544a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
464644a7f3ddSMatthew G. Knepley }
464744a7f3ddSMatthew G. Knepley 
464844a7f3ddSMatthew G. Knepley /*@
464944a7f3ddSMatthew G. Knepley   DMClearFields - Remove all fields from the DM
465044a7f3ddSMatthew G. Knepley 
4651d083f849SBarry Smith   Logically collective on dm
465244a7f3ddSMatthew G. Knepley 
465344a7f3ddSMatthew G. Knepley   Input Parameter:
465444a7f3ddSMatthew G. Knepley . dm - The DM
465544a7f3ddSMatthew G. Knepley 
465644a7f3ddSMatthew G. Knepley   Level: intermediate
465744a7f3ddSMatthew G. Knepley 
465844a7f3ddSMatthew G. Knepley .seealso: DMGetNumFields(), DMSetNumFields(), DMSetField()
465944a7f3ddSMatthew G. Knepley @*/
466044a7f3ddSMatthew G. Knepley PetscErrorCode DMClearFields(DM dm)
466144a7f3ddSMatthew G. Knepley {
466244a7f3ddSMatthew G. Knepley   PetscInt       f;
466344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
466444a7f3ddSMatthew G. Knepley 
466544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
466644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
466744a7f3ddSMatthew G. Knepley   for (f = 0; f < dm->Nf; ++f) {
466844a7f3ddSMatthew G. Knepley     ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
466944a7f3ddSMatthew G. Knepley     ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
467044a7f3ddSMatthew G. Knepley   }
467144a7f3ddSMatthew G. Knepley   ierr = PetscFree(dm->fields);CHKERRQ(ierr);
467244a7f3ddSMatthew G. Knepley   dm->fields = NULL;
467344a7f3ddSMatthew G. Knepley   dm->Nf     = 0;
467444a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
467544a7f3ddSMatthew G. Knepley }
467644a7f3ddSMatthew G. Knepley 
4677689b5837SMatthew G. Knepley /*@
4678689b5837SMatthew G. Knepley   DMGetNumFields - Get the number of fields in the DM
4679689b5837SMatthew G. Knepley 
4680689b5837SMatthew G. Knepley   Not collective
4681689b5837SMatthew G. Knepley 
4682689b5837SMatthew G. Knepley   Input Parameter:
4683689b5837SMatthew G. Knepley . dm - The DM
4684689b5837SMatthew G. Knepley 
4685689b5837SMatthew G. Knepley   Output Parameter:
4686689b5837SMatthew G. Knepley . Nf - The number of fields
4687689b5837SMatthew G. Knepley 
4688689b5837SMatthew G. Knepley   Level: intermediate
4689689b5837SMatthew G. Knepley 
4690689b5837SMatthew G. Knepley .seealso: DMSetNumFields(), DMSetField()
4691689b5837SMatthew G. Knepley @*/
46920f21e855SMatthew G. Knepley PetscErrorCode DMGetNumFields(DM dm, PetscInt *numFields)
46930f21e855SMatthew G. Knepley {
46940f21e855SMatthew G. Knepley   PetscFunctionBegin;
46950f21e855SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4696534a8f05SLisandro Dalcin   PetscValidIntPointer(numFields, 2);
469744a7f3ddSMatthew G. Knepley   *numFields = dm->Nf;
4698af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4699af122d2aSMatthew G Knepley }
4700af122d2aSMatthew G Knepley 
4701689b5837SMatthew G. Knepley /*@
4702689b5837SMatthew G. Knepley   DMSetNumFields - Set the number of fields in the DM
4703689b5837SMatthew G. Knepley 
4704d083f849SBarry Smith   Logically collective on dm
4705689b5837SMatthew G. Knepley 
4706689b5837SMatthew G. Knepley   Input Parameters:
4707689b5837SMatthew G. Knepley + dm - The DM
4708689b5837SMatthew G. Knepley - Nf - The number of fields
4709689b5837SMatthew G. Knepley 
4710689b5837SMatthew G. Knepley   Level: intermediate
4711689b5837SMatthew G. Knepley 
4712689b5837SMatthew G. Knepley .seealso: DMGetNumFields(), DMSetField()
4713689b5837SMatthew G. Knepley @*/
4714af122d2aSMatthew G Knepley PetscErrorCode DMSetNumFields(DM dm, PetscInt numFields)
4715af122d2aSMatthew G Knepley {
47160f21e855SMatthew G. Knepley   PetscInt       Nf, f;
4717af122d2aSMatthew G Knepley   PetscErrorCode ierr;
4718af122d2aSMatthew G Knepley 
4719af122d2aSMatthew G Knepley   PetscFunctionBegin;
4720af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
472144a7f3ddSMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
47220f21e855SMatthew G. Knepley   for (f = Nf; f < numFields; ++f) {
47230f21e855SMatthew G. Knepley     PetscContainer obj;
47240f21e855SMatthew G. Knepley 
47250f21e855SMatthew G. Knepley     ierr = PetscContainerCreate(PetscObjectComm((PetscObject) dm), &obj);CHKERRQ(ierr);
472644a7f3ddSMatthew G. Knepley     ierr = DMAddField(dm, NULL, (PetscObject) obj);CHKERRQ(ierr);
47270f21e855SMatthew G. Knepley     ierr = PetscContainerDestroy(&obj);CHKERRQ(ierr);
4728af122d2aSMatthew G Knepley   }
4729af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4730af122d2aSMatthew G Knepley }
4731af122d2aSMatthew G Knepley 
4732c1929be8SMatthew G. Knepley /*@
4733c1929be8SMatthew G. Knepley   DMGetField - Return the discretization object for a given DM field
4734c1929be8SMatthew G. Knepley 
4735c1929be8SMatthew G. Knepley   Not collective
4736c1929be8SMatthew G. Knepley 
4737c1929be8SMatthew G. Knepley   Input Parameters:
4738c1929be8SMatthew G. Knepley + dm - The DM
4739c1929be8SMatthew G. Knepley - f  - The field number
4740c1929be8SMatthew G. Knepley 
474144a7f3ddSMatthew G. Knepley   Output Parameters:
474244a7f3ddSMatthew G. Knepley + label - The label indicating the support of the field, or NULL for the entire mesh
474344a7f3ddSMatthew G. Knepley - field - The discretization object
4744c1929be8SMatthew G. Knepley 
474544a7f3ddSMatthew G. Knepley   Level: intermediate
4746c1929be8SMatthew G. Knepley 
474744a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMSetField()
4748c1929be8SMatthew G. Knepley @*/
474944a7f3ddSMatthew G. Knepley PetscErrorCode DMGetField(DM dm, PetscInt f, DMLabel *label, PetscObject *field)
4750af122d2aSMatthew G Knepley {
4751af122d2aSMatthew G Knepley   PetscFunctionBegin;
4752af122d2aSMatthew G Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
475344a7f3ddSMatthew G. Knepley   PetscValidPointer(field, 3);
475444a7f3ddSMatthew 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);
475544a7f3ddSMatthew G. Knepley   if (label) *label = dm->fields[f].label;
475644a7f3ddSMatthew G. Knepley   if (field) *field = dm->fields[f].disc;
4757decb47aaSMatthew G. Knepley   PetscFunctionReturn(0);
4758decb47aaSMatthew G. Knepley }
4759decb47aaSMatthew G. Knepley 
4760083401c6SMatthew G. Knepley /* Does not clear the DS */
4761083401c6SMatthew G. Knepley PetscErrorCode DMSetField_Internal(DM dm, PetscInt f, DMLabel label, PetscObject field)
4762083401c6SMatthew G. Knepley {
4763083401c6SMatthew G. Knepley   PetscErrorCode ierr;
4764083401c6SMatthew G. Knepley 
4765083401c6SMatthew G. Knepley   PetscFunctionBegin;
4766083401c6SMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, f+1);CHKERRQ(ierr);
4767083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->fields[f].label);CHKERRQ(ierr);
4768083401c6SMatthew G. Knepley   ierr = PetscObjectDestroy(&dm->fields[f].disc);CHKERRQ(ierr);
4769083401c6SMatthew G. Knepley   dm->fields[f].label = label;
4770083401c6SMatthew G. Knepley   dm->fields[f].disc  = field;
4771083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
4772083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
4773083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
4774083401c6SMatthew G. Knepley }
4775083401c6SMatthew G. Knepley 
4776c1929be8SMatthew G. Knepley /*@
4777c1929be8SMatthew G. Knepley   DMSetField - Set the discretization object for a given DM field
4778c1929be8SMatthew G. Knepley 
4779d083f849SBarry Smith   Logically collective on dm
4780c1929be8SMatthew G. Knepley 
4781c1929be8SMatthew G. Knepley   Input Parameters:
4782c1929be8SMatthew G. Knepley + dm    - The DM
4783c1929be8SMatthew G. Knepley . f     - The field number
478444a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
4785c1929be8SMatthew G. Knepley - field - The discretization object
4786c1929be8SMatthew G. Knepley 
478744a7f3ddSMatthew G. Knepley   Level: intermediate
4788c1929be8SMatthew G. Knepley 
478944a7f3ddSMatthew G. Knepley .seealso: DMAddField(), DMGetField()
4790c1929be8SMatthew G. Knepley @*/
479144a7f3ddSMatthew G. Knepley PetscErrorCode DMSetField(DM dm, PetscInt f, DMLabel label, PetscObject field)
4792decb47aaSMatthew G. Knepley {
4793decb47aaSMatthew G. Knepley   PetscErrorCode ierr;
4794decb47aaSMatthew G. Knepley 
4795decb47aaSMatthew G. Knepley   PetscFunctionBegin;
4796decb47aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4797e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
479844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 4);
4799e5e52638SMatthew G. Knepley   if (f < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be non-negative", f);
4800083401c6SMatthew G. Knepley   ierr = DMSetField_Internal(dm, f, label, field);CHKERRQ(ierr);
480134aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, f, field);CHKERRQ(ierr);
48022df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
480344a7f3ddSMatthew G. Knepley   PetscFunctionReturn(0);
480444a7f3ddSMatthew G. Knepley }
480544a7f3ddSMatthew G. Knepley 
480644a7f3ddSMatthew G. Knepley /*@
480744a7f3ddSMatthew G. Knepley   DMAddField - Add the discretization object for the given DM field
480844a7f3ddSMatthew G. Knepley 
4809d083f849SBarry Smith   Logically collective on dm
481044a7f3ddSMatthew G. Knepley 
481144a7f3ddSMatthew G. Knepley   Input Parameters:
481244a7f3ddSMatthew G. Knepley + dm    - The DM
481344a7f3ddSMatthew G. Knepley . label - The label indicating the support of the field, or NULL for the entire mesh
481444a7f3ddSMatthew G. Knepley - field - The discretization object
481544a7f3ddSMatthew G. Knepley 
481644a7f3ddSMatthew G. Knepley   Level: intermediate
481744a7f3ddSMatthew G. Knepley 
481844a7f3ddSMatthew G. Knepley .seealso: DMSetField(), DMGetField()
481944a7f3ddSMatthew G. Knepley @*/
482044a7f3ddSMatthew G. Knepley PetscErrorCode DMAddField(DM dm, DMLabel label, PetscObject field)
482144a7f3ddSMatthew G. Knepley {
482244a7f3ddSMatthew G. Knepley   PetscInt       Nf = dm->Nf;
482344a7f3ddSMatthew G. Knepley   PetscErrorCode ierr;
482444a7f3ddSMatthew G. Knepley 
482544a7f3ddSMatthew G. Knepley   PetscFunctionBegin;
482644a7f3ddSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
482744a7f3ddSMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);
482844a7f3ddSMatthew G. Knepley   PetscValidHeader(field, 3);
482944a7f3ddSMatthew G. Knepley   ierr = DMFieldEnlarge_Static(dm, Nf+1);CHKERRQ(ierr);
483044a7f3ddSMatthew G. Knepley   dm->fields[Nf].label = label;
483144a7f3ddSMatthew G. Knepley   dm->fields[Nf].disc  = field;
483244a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
483344a7f3ddSMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) field);CHKERRQ(ierr);
483434aa8a36SMatthew G. Knepley   ierr = DMSetDefaultAdjacency_Private(dm, Nf, field);CHKERRQ(ierr);
48352df9ee95SMatthew G. Knepley   ierr = DMClearDS(dm);CHKERRQ(ierr);
4836af122d2aSMatthew G Knepley   PetscFunctionReturn(0);
4837af122d2aSMatthew G Knepley }
48386636e97aSMatthew G Knepley 
4839e5e52638SMatthew G. Knepley /*@
4840e5e52638SMatthew G. Knepley   DMCopyFields - Copy the discretizations for the DM into another DM
4841e5e52638SMatthew G. Knepley 
4842d083f849SBarry Smith   Collective on dm
4843e5e52638SMatthew G. Knepley 
4844e5e52638SMatthew G. Knepley   Input Parameter:
4845e5e52638SMatthew G. Knepley . dm - The DM
4846e5e52638SMatthew G. Knepley 
4847e5e52638SMatthew G. Knepley   Output Parameter:
4848e5e52638SMatthew G. Knepley . newdm - The DM
4849e5e52638SMatthew G. Knepley 
4850e5e52638SMatthew G. Knepley   Level: advanced
4851e5e52638SMatthew G. Knepley 
4852e5e52638SMatthew G. Knepley .seealso: DMGetField(), DMSetField(), DMAddField(), DMCopyDS(), DMGetDS(), DMGetCellDS()
4853e5e52638SMatthew G. Knepley @*/
4854e5e52638SMatthew G. Knepley PetscErrorCode DMCopyFields(DM dm, DM newdm)
4855e5e52638SMatthew G. Knepley {
4856e5e52638SMatthew G. Knepley   PetscInt       Nf, f;
4857e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
4858e5e52638SMatthew G. Knepley 
4859e5e52638SMatthew G. Knepley   PetscFunctionBegin;
4860e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
4861e5e52638SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4862e5e52638SMatthew G. Knepley   ierr = DMClearFields(newdm);CHKERRQ(ierr);
4863e5e52638SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
4864e5e52638SMatthew G. Knepley     DMLabel     label;
4865e5e52638SMatthew G. Knepley     PetscObject field;
486634aa8a36SMatthew G. Knepley     PetscBool   useCone, useClosure;
4867e5e52638SMatthew G. Knepley 
4868e5e52638SMatthew G. Knepley     ierr = DMGetField(dm, f, &label, &field);CHKERRQ(ierr);
4869e5e52638SMatthew G. Knepley     ierr = DMSetField(newdm, f, label, field);CHKERRQ(ierr);
487034aa8a36SMatthew G. Knepley     ierr = DMGetAdjacency(dm, f, &useCone, &useClosure);CHKERRQ(ierr);
487134aa8a36SMatthew G. Knepley     ierr = DMSetAdjacency(newdm, f, useCone, useClosure);CHKERRQ(ierr);
487234aa8a36SMatthew G. Knepley   }
487334aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
487434aa8a36SMatthew G. Knepley }
487534aa8a36SMatthew G. Knepley 
487634aa8a36SMatthew G. Knepley /*@
487734aa8a36SMatthew G. Knepley   DMGetAdjacency - Returns the flags for determining variable influence
487834aa8a36SMatthew G. Knepley 
487934aa8a36SMatthew G. Knepley   Not collective
488034aa8a36SMatthew G. Knepley 
488134aa8a36SMatthew G. Knepley   Input Parameters:
488234aa8a36SMatthew G. Knepley + dm - The DM object
488334aa8a36SMatthew G. Knepley - f  - The field number, or PETSC_DEFAULT for the default adjacency
488434aa8a36SMatthew G. Knepley 
488534aa8a36SMatthew G. Knepley   Output Parameter:
488634aa8a36SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
488734aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
488834aa8a36SMatthew G. Knepley 
488934aa8a36SMatthew G. Knepley   Notes:
489034aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
489134aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
489234aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4893979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
489434aa8a36SMatthew G. Knepley 
489534aa8a36SMatthew G. Knepley   Level: developer
489634aa8a36SMatthew G. Knepley 
489734aa8a36SMatthew G. Knepley .seealso: DMSetAdjacency(), DMGetField(), DMSetField()
489834aa8a36SMatthew G. Knepley @*/
489934aa8a36SMatthew G. Knepley PetscErrorCode DMGetAdjacency(DM dm, PetscInt f, PetscBool *useCone, PetscBool *useClosure)
490034aa8a36SMatthew G. Knepley {
490134aa8a36SMatthew G. Knepley   PetscFunctionBegin;
490234aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4903534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4904534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
490534aa8a36SMatthew G. Knepley   if (f < 0) {
490634aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->adjacency[0];
490734aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->adjacency[1];
490834aa8a36SMatthew G. Knepley   } else {
490934aa8a36SMatthew G. Knepley     PetscInt       Nf;
491034aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
491134aa8a36SMatthew G. Knepley 
491234aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
491334aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
491434aa8a36SMatthew G. Knepley     if (useCone)    *useCone    = dm->fields[f].adjacency[0];
491534aa8a36SMatthew G. Knepley     if (useClosure) *useClosure = dm->fields[f].adjacency[1];
491634aa8a36SMatthew G. Knepley   }
491734aa8a36SMatthew G. Knepley   PetscFunctionReturn(0);
491834aa8a36SMatthew G. Knepley }
491934aa8a36SMatthew G. Knepley 
492034aa8a36SMatthew G. Knepley /*@
492134aa8a36SMatthew G. Knepley   DMSetAdjacency - Set the flags for determining variable influence
492234aa8a36SMatthew G. Knepley 
492334aa8a36SMatthew G. Knepley   Not collective
492434aa8a36SMatthew G. Knepley 
492534aa8a36SMatthew G. Knepley   Input Parameters:
492634aa8a36SMatthew G. Knepley + dm         - The DM object
492734aa8a36SMatthew G. Knepley . f          - The field number
492834aa8a36SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
492934aa8a36SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
493034aa8a36SMatthew G. Knepley 
493134aa8a36SMatthew G. Knepley   Notes:
493234aa8a36SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
493334aa8a36SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
493434aa8a36SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4935979e053dSMatthew G. Knepley   Further explanation can be found in the User's Manual Section on the Influence of Variables on One Another.
493634aa8a36SMatthew G. Knepley 
493734aa8a36SMatthew G. Knepley   Level: developer
493834aa8a36SMatthew G. Knepley 
493934aa8a36SMatthew G. Knepley .seealso: DMGetAdjacency(), DMGetField(), DMSetField()
494034aa8a36SMatthew G. Knepley @*/
494134aa8a36SMatthew G. Knepley PetscErrorCode DMSetAdjacency(DM dm, PetscInt f, PetscBool useCone, PetscBool useClosure)
494234aa8a36SMatthew G. Knepley {
494334aa8a36SMatthew G. Knepley   PetscFunctionBegin;
494434aa8a36SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
494534aa8a36SMatthew G. Knepley   if (f < 0) {
494634aa8a36SMatthew G. Knepley     dm->adjacency[0] = useCone;
494734aa8a36SMatthew G. Knepley     dm->adjacency[1] = useClosure;
494834aa8a36SMatthew G. Knepley   } else {
494934aa8a36SMatthew G. Knepley     PetscInt       Nf;
495034aa8a36SMatthew G. Knepley     PetscErrorCode ierr;
495134aa8a36SMatthew G. Knepley 
495234aa8a36SMatthew G. Knepley     ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
495334aa8a36SMatthew G. Knepley     if (f >= Nf) SETERRQ2(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Field number %d must be in [0, %d)", f, Nf);
495434aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[0] = useCone;
495534aa8a36SMatthew G. Knepley     dm->fields[f].adjacency[1] = useClosure;
4956e5e52638SMatthew G. Knepley   }
4957e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
4958e5e52638SMatthew G. Knepley }
4959e5e52638SMatthew G. Knepley 
4960b0441da4SMatthew G. Knepley /*@
4961b0441da4SMatthew G. Knepley   DMGetBasicAdjacency - Returns the flags for determining variable influence, using either the default or field 0 if it is defined
4962b0441da4SMatthew G. Knepley 
4963b0441da4SMatthew G. Knepley   Not collective
4964b0441da4SMatthew G. Knepley 
4965b0441da4SMatthew G. Knepley   Input Parameters:
4966b0441da4SMatthew G. Knepley . dm - The DM object
4967b0441da4SMatthew G. Knepley 
4968b0441da4SMatthew G. Knepley   Output Parameter:
4969b0441da4SMatthew G. Knepley + useCone    - Flag for variable influence starting with the cone operation
4970b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
4971b0441da4SMatthew G. Knepley 
4972b0441da4SMatthew G. Knepley   Notes:
4973b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
4974b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
4975b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
4976b0441da4SMatthew G. Knepley 
4977b0441da4SMatthew G. Knepley   Level: developer
4978b0441da4SMatthew G. Knepley 
4979b0441da4SMatthew G. Knepley .seealso: DMSetBasicAdjacency(), DMGetField(), DMSetField()
4980b0441da4SMatthew G. Knepley @*/
4981b0441da4SMatthew G. Knepley PetscErrorCode DMGetBasicAdjacency(DM dm, PetscBool *useCone, PetscBool *useClosure)
4982b0441da4SMatthew G. Knepley {
4983b0441da4SMatthew G. Knepley   PetscInt       Nf;
4984b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
4985b0441da4SMatthew G. Knepley 
4986b0441da4SMatthew G. Knepley   PetscFunctionBegin;
4987b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
4988534a8f05SLisandro Dalcin   if (useCone)    PetscValidBoolPointer(useCone, 3);
4989534a8f05SLisandro Dalcin   if (useClosure) PetscValidBoolPointer(useClosure, 4);
4990b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
4991b0441da4SMatthew G. Knepley   if (!Nf) {
4992b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
4993b0441da4SMatthew G. Knepley   } else {
4994b0441da4SMatthew G. Knepley     ierr = DMGetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
4995b0441da4SMatthew G. Knepley   }
4996b0441da4SMatthew G. Knepley   PetscFunctionReturn(0);
4997b0441da4SMatthew G. Knepley }
4998b0441da4SMatthew G. Knepley 
4999b0441da4SMatthew G. Knepley /*@
5000b0441da4SMatthew G. Knepley   DMSetBasicAdjacency - Set the flags for determining variable influence, using either the default or field 0 if it is defined
5001b0441da4SMatthew G. Knepley 
5002b0441da4SMatthew G. Knepley   Not collective
5003b0441da4SMatthew G. Knepley 
5004b0441da4SMatthew G. Knepley   Input Parameters:
5005b0441da4SMatthew G. Knepley + dm         - The DM object
5006b0441da4SMatthew G. Knepley . useCone    - Flag for variable influence starting with the cone operation
5007b0441da4SMatthew G. Knepley - useClosure - Flag for variable influence using transitive closure
5008b0441da4SMatthew G. Knepley 
5009b0441da4SMatthew G. Knepley   Notes:
5010b0441da4SMatthew G. Knepley $     FEM:   Two points p and q are adjacent if q \in closure(star(p)),   useCone = PETSC_FALSE, useClosure = PETSC_TRUE
5011b0441da4SMatthew G. Knepley $     FVM:   Two points p and q are adjacent if q \in support(p+cone(p)), useCone = PETSC_TRUE,  useClosure = PETSC_FALSE
5012b0441da4SMatthew G. Knepley $     FVM++: Two points p and q are adjacent if q \in star(closure(p)),   useCone = PETSC_TRUE,  useClosure = PETSC_TRUE
5013b0441da4SMatthew G. Knepley 
5014b0441da4SMatthew G. Knepley   Level: developer
5015b0441da4SMatthew G. Knepley 
5016b0441da4SMatthew G. Knepley .seealso: DMGetBasicAdjacency(), DMGetField(), DMSetField()
5017b0441da4SMatthew G. Knepley @*/
5018b0441da4SMatthew G. Knepley PetscErrorCode DMSetBasicAdjacency(DM dm, PetscBool useCone, PetscBool useClosure)
5019b0441da4SMatthew G. Knepley {
5020b0441da4SMatthew G. Knepley   PetscInt       Nf;
5021b0441da4SMatthew G. Knepley   PetscErrorCode ierr;
5022b0441da4SMatthew G. Knepley 
5023b0441da4SMatthew G. Knepley   PetscFunctionBegin;
5024b0441da4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5025b0441da4SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
5026b0441da4SMatthew G. Knepley   if (!Nf) {
5027b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, PETSC_DEFAULT, useCone, useClosure);CHKERRQ(ierr);
5028b0441da4SMatthew G. Knepley   } else {
5029b0441da4SMatthew G. Knepley     ierr = DMSetAdjacency(dm, 0, useCone, useClosure);CHKERRQ(ierr);
5030e5e52638SMatthew G. Knepley   }
5031e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5032e5e52638SMatthew G. Knepley }
5033e5e52638SMatthew G. Knepley 
5034783e2ec8SMatthew G. Knepley /* Complete labels that are being used for FEM BC */
5035783e2ec8SMatthew G. Knepley static PetscErrorCode DMCompleteBoundaryLabel_Internal(DM dm, PetscDS ds, PetscInt field, PetscInt bdNum, const char labelname[])
5036783e2ec8SMatthew G. Knepley {
5037783e2ec8SMatthew G. Knepley   DMLabel        label;
5038783e2ec8SMatthew G. Knepley   PetscObject    obj;
5039783e2ec8SMatthew G. Knepley   PetscClassId   id;
5040783e2ec8SMatthew G. Knepley   PetscInt       Nbd, bd;
5041783e2ec8SMatthew G. Knepley   PetscBool      isFE      = PETSC_FALSE;
5042783e2ec8SMatthew G. Knepley   PetscBool      duplicate = PETSC_FALSE;
5043783e2ec8SMatthew G. Knepley   PetscErrorCode ierr;
5044783e2ec8SMatthew G. Knepley 
5045783e2ec8SMatthew G. Knepley   PetscFunctionBegin;
5046783e2ec8SMatthew G. Knepley   ierr = DMGetField(dm, field, NULL, &obj);CHKERRQ(ierr);
5047783e2ec8SMatthew G. Knepley   ierr = PetscObjectGetClassId(obj, &id);CHKERRQ(ierr);
5048783e2ec8SMatthew G. Knepley   if (id == PETSCFE_CLASSID) isFE = PETSC_TRUE;
5049783e2ec8SMatthew G. Knepley   ierr = DMGetLabel(dm, labelname, &label);CHKERRQ(ierr);
5050783e2ec8SMatthew G. Knepley   if (isFE && label) {
5051783e2ec8SMatthew G. Knepley     /* Only want to modify label once */
5052783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5053783e2ec8SMatthew G. Knepley     for (bd = 0; bd < PetscMin(Nbd, bdNum); ++bd) {
5054783e2ec8SMatthew G. Knepley       const char *lname;
5055783e2ec8SMatthew G. Knepley 
505656cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, NULL, &lname, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5057783e2ec8SMatthew G. Knepley       ierr = PetscStrcmp(lname, labelname, &duplicate);CHKERRQ(ierr);
5058783e2ec8SMatthew G. Knepley       if (duplicate) break;
5059783e2ec8SMatthew G. Knepley     }
5060783e2ec8SMatthew G. Knepley     if (!duplicate) {
5061783e2ec8SMatthew G. Knepley       DM plex;
5062783e2ec8SMatthew G. Knepley 
5063783e2ec8SMatthew G. Knepley       ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
5064783e2ec8SMatthew G. Knepley       if (plex) {ierr = DMPlexLabelComplete(plex, label);CHKERRQ(ierr);}
5065783e2ec8SMatthew G. Knepley       ierr = DMDestroy(&plex);CHKERRQ(ierr);
5066783e2ec8SMatthew G. Knepley     }
5067783e2ec8SMatthew G. Knepley   }
5068783e2ec8SMatthew G. Knepley   PetscFunctionReturn(0);
5069783e2ec8SMatthew G. Knepley }
5070783e2ec8SMatthew G. Knepley 
5071e5e52638SMatthew G. Knepley static PetscErrorCode DMDSEnlarge_Static(DM dm, PetscInt NdsNew)
5072e5e52638SMatthew G. Knepley {
5073e5e52638SMatthew G. Knepley   DMSpace       *tmpd;
5074e5e52638SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5075e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5076e5e52638SMatthew G. Knepley 
5077e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5078e5e52638SMatthew G. Knepley   if (Nds >= NdsNew) PetscFunctionReturn(0);
5079e5e52638SMatthew G. Knepley   ierr = PetscMalloc1(NdsNew, &tmpd);CHKERRQ(ierr);
5080e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) tmpd[s] = dm->probs[s];
5081b3cf3223SMatthew G. Knepley   for (s = Nds; s < NdsNew; ++s) {tmpd[s].ds = NULL; tmpd[s].label = NULL; tmpd[s].fields = NULL;}
5082e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5083e5e52638SMatthew G. Knepley   dm->Nds   = NdsNew;
5084e5e52638SMatthew G. Knepley   dm->probs = tmpd;
5085e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5086e5e52638SMatthew G. Knepley }
5087e5e52638SMatthew G. Knepley 
5088e5e52638SMatthew G. Knepley /*@
5089e5e52638SMatthew G. Knepley   DMGetNumDS - Get the number of discrete systems in the DM
5090e5e52638SMatthew G. Knepley 
5091e5e52638SMatthew G. Knepley   Not collective
5092e5e52638SMatthew G. Knepley 
5093e5e52638SMatthew G. Knepley   Input Parameter:
5094e5e52638SMatthew G. Knepley . dm - The DM
5095e5e52638SMatthew G. Knepley 
5096e5e52638SMatthew G. Knepley   Output Parameter:
5097e5e52638SMatthew G. Knepley . Nds - The number of PetscDS objects
5098e5e52638SMatthew G. Knepley 
5099e5e52638SMatthew G. Knepley   Level: intermediate
5100e5e52638SMatthew G. Knepley 
5101e5e52638SMatthew G. Knepley .seealso: DMGetDS(), DMGetCellDS()
5102e5e52638SMatthew G. Knepley @*/
5103e5e52638SMatthew G. Knepley PetscErrorCode DMGetNumDS(DM dm, PetscInt *Nds)
5104e5e52638SMatthew G. Knepley {
5105e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5106e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5107534a8f05SLisandro Dalcin   PetscValidIntPointer(Nds, 2);
5108e5e52638SMatthew G. Knepley   *Nds = dm->Nds;
5109e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5110e5e52638SMatthew G. Knepley }
5111e5e52638SMatthew G. Knepley 
5112e5e52638SMatthew G. Knepley /*@
5113e5e52638SMatthew G. Knepley   DMClearDS - Remove all discrete systems from the DM
5114e5e52638SMatthew G. Knepley 
5115d083f849SBarry Smith   Logically collective on dm
5116e5e52638SMatthew G. Knepley 
5117e5e52638SMatthew G. Knepley   Input Parameter:
5118e5e52638SMatthew G. Knepley . dm - The DM
5119e5e52638SMatthew G. Knepley 
5120e5e52638SMatthew G. Knepley   Level: intermediate
5121e5e52638SMatthew G. Knepley 
5122e5e52638SMatthew G. Knepley .seealso: DMGetNumDS(), DMGetDS(), DMSetField()
5123e5e52638SMatthew G. Knepley @*/
5124e5e52638SMatthew G. Knepley PetscErrorCode DMClearDS(DM dm)
5125e5e52638SMatthew G. Knepley {
5126e5e52638SMatthew G. Knepley   PetscInt       s;
5127e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5128e5e52638SMatthew G. Knepley 
5129e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5130e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5131e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5132e5e52638SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5133e5e52638SMatthew G. Knepley     ierr = DMLabelDestroy(&dm->probs[s].label);CHKERRQ(ierr);
5134b3cf3223SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[s].fields);CHKERRQ(ierr);
5135e5e52638SMatthew G. Knepley   }
5136e5e52638SMatthew G. Knepley   ierr = PetscFree(dm->probs);CHKERRQ(ierr);
5137e5e52638SMatthew G. Knepley   dm->probs = NULL;
5138e5e52638SMatthew G. Knepley   dm->Nds   = 0;
5139e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5140e5e52638SMatthew G. Knepley }
5141e5e52638SMatthew G. Knepley 
5142e5e52638SMatthew G. Knepley /*@
5143e5e52638SMatthew G. Knepley   DMGetDS - Get the default PetscDS
5144e5e52638SMatthew G. Knepley 
5145e5e52638SMatthew G. Knepley   Not collective
5146e5e52638SMatthew G. Knepley 
5147e5e52638SMatthew G. Knepley   Input Parameter:
5148e5e52638SMatthew G. Knepley . dm    - The DM
5149e5e52638SMatthew G. Knepley 
5150e5e52638SMatthew G. Knepley   Output Parameter:
5151e5e52638SMatthew G. Knepley . prob - The default PetscDS
5152e5e52638SMatthew G. Knepley 
5153e5e52638SMatthew G. Knepley   Level: intermediate
5154e5e52638SMatthew G. Knepley 
5155e5e52638SMatthew G. Knepley .seealso: DMGetCellDS(), DMGetRegionDS()
5156e5e52638SMatthew G. Knepley @*/
5157e5e52638SMatthew G. Knepley PetscErrorCode DMGetDS(DM dm, PetscDS *prob)
5158e5e52638SMatthew G. Knepley {
5159b0143b4dSMatthew G. Knepley   PetscErrorCode ierr;
5160b0143b4dSMatthew G. Knepley 
5161e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5162e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5163e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 2);
5164b0143b4dSMatthew G. Knepley   if (dm->Nds <= 0) {
5165b0143b4dSMatthew G. Knepley     PetscDS ds;
5166b0143b4dSMatthew G. Knepley 
5167b0143b4dSMatthew G. Knepley     ierr = PetscDSCreate(PetscObjectComm((PetscObject) dm), &ds);CHKERRQ(ierr);
5168b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(dm, NULL, NULL, ds);CHKERRQ(ierr);
5169b0143b4dSMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5170b0143b4dSMatthew G. Knepley   }
5171b0143b4dSMatthew G. Knepley   *prob = dm->probs[0].ds;
5172e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5173e5e52638SMatthew G. Knepley }
5174e5e52638SMatthew G. Knepley 
5175e5e52638SMatthew G. Knepley /*@
5176e5e52638SMatthew G. Knepley   DMGetCellDS - Get the PetscDS defined on a given cell
5177e5e52638SMatthew G. Knepley 
5178e5e52638SMatthew G. Knepley   Not collective
5179e5e52638SMatthew G. Knepley 
5180e5e52638SMatthew G. Knepley   Input Parameters:
5181e5e52638SMatthew G. Knepley + dm    - The DM
5182e5e52638SMatthew G. Knepley - point - Cell for the DS
5183e5e52638SMatthew G. Knepley 
5184e5e52638SMatthew G. Knepley   Output Parameter:
5185e5e52638SMatthew G. Knepley . prob - The PetscDS defined on the given cell
5186e5e52638SMatthew G. Knepley 
5187e5e52638SMatthew G. Knepley   Level: developer
5188e5e52638SMatthew G. Knepley 
5189b0143b4dSMatthew G. Knepley .seealso: DMGetDS(), DMSetRegionDS()
5190e5e52638SMatthew G. Knepley @*/
5191e5e52638SMatthew G. Knepley PetscErrorCode DMGetCellDS(DM dm, PetscInt point, PetscDS *prob)
5192e5e52638SMatthew G. Knepley {
5193e5e52638SMatthew G. Knepley   PetscDS        probDef = NULL;
5194e5e52638SMatthew G. Knepley   PetscInt       s;
5195e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5196e5e52638SMatthew G. Knepley 
5197e5e52638SMatthew G. Knepley   PetscFunctionBeginHot;
5198e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5199e5e52638SMatthew G. Knepley   PetscValidPointer(prob, 3);
5200360cf244SMatthew G. Knepley   if (point < 0) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Mesh point cannot be negative: %D", point);
5201e5e52638SMatthew G. Knepley   *prob = NULL;
5202e5e52638SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5203e5e52638SMatthew G. Knepley     PetscInt val;
5204e5e52638SMatthew G. Knepley 
5205e5e52638SMatthew G. Knepley     if (!dm->probs[s].label) {probDef = dm->probs[s].ds;}
5206e5e52638SMatthew G. Knepley     else {
5207e5e52638SMatthew G. Knepley       ierr = DMLabelGetValue(dm->probs[s].label, point, &val);CHKERRQ(ierr);
5208e5e52638SMatthew G. Knepley       if (val >= 0) {*prob = dm->probs[s].ds; break;}
5209e5e52638SMatthew G. Knepley     }
5210e5e52638SMatthew G. Knepley   }
5211e5e52638SMatthew G. Knepley   if (!*prob) *prob = probDef;
5212e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5213e5e52638SMatthew G. Knepley }
5214e5e52638SMatthew G. Knepley 
5215e5e52638SMatthew G. Knepley /*@
5216e5e52638SMatthew G. Knepley   DMGetRegionDS - Get the PetscDS for a given mesh region, defined by a DMLabel
5217e5e52638SMatthew G. Knepley 
5218e5e52638SMatthew G. Knepley   Not collective
5219e5e52638SMatthew G. Knepley 
5220e5e52638SMatthew G. Knepley   Input Parameters:
5221e5e52638SMatthew G. Knepley + dm    - The DM
5222e5e52638SMatthew G. Knepley - label - The DMLabel defining the mesh region, or NULL for the entire mesh
5223e5e52638SMatthew G. Knepley 
5224b3cf3223SMatthew G. Knepley   Output Parameters:
5225b3cf3223SMatthew G. Knepley + fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5226b3cf3223SMatthew G. Knepley - prob - The PetscDS defined on the given region, or NULL
5227e5e52638SMatthew G. Knepley 
5228e5e52638SMatthew G. Knepley   Note: If the label is missing, this function returns an error
5229e5e52638SMatthew G. Knepley 
5230e5e52638SMatthew G. Knepley   Level: advanced
5231e5e52638SMatthew G. Knepley 
5232e5e52638SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5233e5e52638SMatthew G. Knepley @*/
5234b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionDS(DM dm, DMLabel label, IS *fields, PetscDS *ds)
5235e5e52638SMatthew G. Knepley {
5236e5e52638SMatthew G. Knepley   PetscInt Nds = dm->Nds, s;
5237e5e52638SMatthew G. Knepley 
5238e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5239e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5240e5e52638SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5241b3cf3223SMatthew G. Knepley   if (fields) {PetscValidPointer(fields, 3); *fields = NULL;}
5242b3cf3223SMatthew G. Knepley   if (ds)     {PetscValidPointer(ds, 4);     *ds     = NULL;}
5243e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5244b3cf3223SMatthew G. Knepley     if (dm->probs[s].label == label) {
5245b3cf3223SMatthew G. Knepley       if (fields) *fields = dm->probs[s].fields;
5246b3cf3223SMatthew G. Knepley       if (ds)     *ds     = dm->probs[s].ds;
5247b3cf3223SMatthew G. Knepley       PetscFunctionReturn(0);
5248b3cf3223SMatthew G. Knepley     }
5249e5e52638SMatthew G. Knepley   }
52502df9ee95SMatthew G. Knepley   PetscFunctionReturn(0);
5251e5e52638SMatthew G. Knepley }
5252e5e52638SMatthew G. Knepley 
5253e5e52638SMatthew G. Knepley /*@
5254083401c6SMatthew G. Knepley   DMSetRegionDS - Set the PetscDS for a given mesh region, defined by a DMLabel
5255083401c6SMatthew G. Knepley 
5256083401c6SMatthew G. Knepley   Collective on dm
5257083401c6SMatthew G. Knepley 
5258083401c6SMatthew G. Knepley   Input Parameters:
5259083401c6SMatthew G. Knepley + dm     - The DM
5260083401c6SMatthew G. Knepley . label  - The DMLabel defining the mesh region, or NULL for the entire mesh
5261083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL for all fields
5262083401c6SMatthew G. Knepley - prob   - The PetscDS defined on the given cell
5263083401c6SMatthew G. Knepley 
5264083401c6SMatthew 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,
5265083401c6SMatthew G. Knepley   the fields argument is ignored.
5266083401c6SMatthew G. Knepley 
5267083401c6SMatthew G. Knepley   Level: advanced
5268083401c6SMatthew G. Knepley 
5269083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionNumDS(), DMGetDS(), DMGetCellDS()
5270083401c6SMatthew G. Knepley @*/
5271083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionDS(DM dm, DMLabel label, IS fields, PetscDS ds)
5272083401c6SMatthew G. Knepley {
5273083401c6SMatthew G. Knepley   PetscInt       Nds = dm->Nds, s;
5274083401c6SMatthew G. Knepley   PetscErrorCode ierr;
5275083401c6SMatthew G. Knepley 
5276083401c6SMatthew G. Knepley   PetscFunctionBegin;
5277083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5278083401c6SMatthew G. Knepley   if (label) PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 2);
5279083401c6SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 3);
5280083401c6SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5281083401c6SMatthew G. Knepley     if (dm->probs[s].label == label) {
5282083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dm->probs[s].ds);CHKERRQ(ierr);
5283083401c6SMatthew G. Knepley       dm->probs[s].ds = ds;
5284083401c6SMatthew G. Knepley       PetscFunctionReturn(0);
5285083401c6SMatthew G. Knepley     }
5286083401c6SMatthew G. Knepley   }
5287083401c6SMatthew G. Knepley   ierr = DMDSEnlarge_Static(dm, Nds+1);CHKERRQ(ierr);
5288083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5289083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5290083401c6SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5291083401c6SMatthew G. Knepley   if (!label) {
5292083401c6SMatthew G. Knepley     /* Put the NULL label at the front, so it is returned as the default */
5293083401c6SMatthew G. Knepley     for (s = Nds-1; s >=0; --s) dm->probs[s+1] = dm->probs[s];
5294083401c6SMatthew G. Knepley     Nds = 0;
5295083401c6SMatthew G. Knepley   }
5296083401c6SMatthew G. Knepley   dm->probs[Nds].label  = label;
5297083401c6SMatthew G. Knepley   dm->probs[Nds].fields = fields;
5298083401c6SMatthew G. Knepley   dm->probs[Nds].ds     = ds;
5299083401c6SMatthew G. Knepley   PetscFunctionReturn(0);
5300083401c6SMatthew G. Knepley }
5301083401c6SMatthew G. Knepley 
5302083401c6SMatthew G. Knepley /*@
5303e5e52638SMatthew G. Knepley   DMGetRegionNumDS - Get the PetscDS for a given mesh region, defined by the region number
5304e5e52638SMatthew G. Knepley 
5305e5e52638SMatthew G. Knepley   Not collective
5306e5e52638SMatthew G. Knepley 
5307e5e52638SMatthew G. Knepley   Input Parameters:
5308e5e52638SMatthew G. Knepley + dm  - The DM
5309e5e52638SMatthew G. Knepley - num - The region number, in [0, Nds)
5310e5e52638SMatthew G. Knepley 
5311e5e52638SMatthew G. Knepley   Output Parameters:
5312b3cf3223SMatthew G. Knepley + label  - The region label, or NULL
5313b3cf3223SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL
5314083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL
5315e5e52638SMatthew G. Knepley 
5316e5e52638SMatthew G. Knepley   Level: advanced
5317e5e52638SMatthew G. Knepley 
5318e5e52638SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5319e5e52638SMatthew G. Knepley @*/
5320b3cf3223SMatthew G. Knepley PetscErrorCode DMGetRegionNumDS(DM dm, PetscInt num, DMLabel *label, IS *fields, PetscDS *ds)
5321e5e52638SMatthew G. Knepley {
5322e5e52638SMatthew G. Knepley   PetscInt       Nds;
5323e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5324e5e52638SMatthew G. Knepley 
5325e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5326e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5327e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5328e5e52638SMatthew 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);
5329e5e52638SMatthew G. Knepley   if (label) {
5330e5e52638SMatthew G. Knepley     PetscValidPointer(label, 3);
5331e5e52638SMatthew G. Knepley     *label = dm->probs[num].label;
5332e5e52638SMatthew G. Knepley   }
5333b3cf3223SMatthew G. Knepley   if (fields) {
5334b3cf3223SMatthew G. Knepley     PetscValidPointer(fields, 4);
5335b3cf3223SMatthew G. Knepley     *fields = dm->probs[num].fields;
5336b3cf3223SMatthew G. Knepley   }
5337e5e52638SMatthew G. Knepley   if (ds) {
5338b3cf3223SMatthew G. Knepley     PetscValidPointer(ds, 5);
5339e5e52638SMatthew G. Knepley     *ds = dm->probs[num].ds;
5340e5e52638SMatthew G. Knepley   }
5341e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5342e5e52638SMatthew G. Knepley }
5343e5e52638SMatthew G. Knepley 
5344e5e52638SMatthew G. Knepley /*@
5345083401c6SMatthew G. Knepley   DMSetRegionNumDS - Set the PetscDS for a given mesh region, defined by the region number
5346e5e52638SMatthew G. Knepley 
5347083401c6SMatthew G. Knepley   Not collective
5348e5e52638SMatthew G. Knepley 
5349e5e52638SMatthew G. Knepley   Input Parameters:
5350e5e52638SMatthew G. Knepley + dm     - The DM
5351083401c6SMatthew G. Knepley . num    - The region number, in [0, Nds)
5352083401c6SMatthew G. Knepley . label  - The region label, or NULL
5353083401c6SMatthew G. Knepley . fields - The IS containing the DM field numbers for the fields in this DS, or NULL to prevent setting
5354083401c6SMatthew G. Knepley - ds     - The PetscDS defined on the given region, or NULL to prevent setting
5355e5e52638SMatthew G. Knepley 
5356e5e52638SMatthew G. Knepley   Level: advanced
5357e5e52638SMatthew G. Knepley 
5358083401c6SMatthew G. Knepley .seealso: DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
5359e5e52638SMatthew G. Knepley @*/
5360083401c6SMatthew G. Knepley PetscErrorCode DMSetRegionNumDS(DM dm, PetscInt num, DMLabel label, IS fields, PetscDS ds)
5361e5e52638SMatthew G. Knepley {
5362083401c6SMatthew G. Knepley   PetscInt       Nds;
5363e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5364e5e52638SMatthew G. Knepley 
5365e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5366e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5367083401c6SMatthew G. Knepley   if (label) {PetscValidHeaderSpecific(label, DMLABEL_CLASSID, 3);}
5368083401c6SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5369083401c6SMatthew 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);
5370e5e52638SMatthew G. Knepley   ierr = PetscObjectReference((PetscObject) label);CHKERRQ(ierr);
5371083401c6SMatthew G. Knepley   ierr = DMLabelDestroy(&dm->probs[num].label);CHKERRQ(ierr);
5372083401c6SMatthew G. Knepley   dm->probs[num].label = label;
5373083401c6SMatthew G. Knepley   if (fields) {
5374083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(fields, IS_CLASSID, 4);
5375b3cf3223SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) fields);CHKERRQ(ierr);
5376083401c6SMatthew G. Knepley     ierr = ISDestroy(&dm->probs[num].fields);CHKERRQ(ierr);
5377083401c6SMatthew G. Knepley     dm->probs[num].fields = fields;
5378e5e52638SMatthew G. Knepley   }
5379083401c6SMatthew G. Knepley   if (ds) {
5380083401c6SMatthew G. Knepley     PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 5);
5381083401c6SMatthew G. Knepley     ierr = PetscObjectReference((PetscObject) ds);CHKERRQ(ierr);
5382083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dm->probs[num].ds);CHKERRQ(ierr);
5383083401c6SMatthew G. Knepley     dm->probs[num].ds = ds;
5384083401c6SMatthew G. Knepley   }
5385e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5386e5e52638SMatthew G. Knepley }
5387e5e52638SMatthew G. Knepley 
5388e5e52638SMatthew G. Knepley /*@
53891d3af9e0SMatthew G. Knepley   DMFindRegionNum - Find the region number for a given PetscDS, or -1 if it is not found.
53901d3af9e0SMatthew G. Knepley 
53911d3af9e0SMatthew G. Knepley   Not collective
53921d3af9e0SMatthew G. Knepley 
53931d3af9e0SMatthew G. Knepley   Input Parameters:
53941d3af9e0SMatthew G. Knepley + dm  - The DM
53951d3af9e0SMatthew G. Knepley - ds  - The PetscDS defined on the given region
53961d3af9e0SMatthew G. Knepley 
53971d3af9e0SMatthew G. Knepley   Output Parameter:
53981d3af9e0SMatthew G. Knepley . num - The region number, in [0, Nds), or -1 if not found
53991d3af9e0SMatthew G. Knepley 
54001d3af9e0SMatthew G. Knepley   Level: advanced
54011d3af9e0SMatthew G. Knepley 
54021d3af9e0SMatthew G. Knepley .seealso: DMGetRegionNumDS(), DMGetRegionDS(), DMSetRegionDS(), DMGetDS(), DMGetCellDS()
54031d3af9e0SMatthew G. Knepley @*/
54041d3af9e0SMatthew G. Knepley PetscErrorCode DMFindRegionNum(DM dm, PetscDS ds, PetscInt *num)
54051d3af9e0SMatthew G. Knepley {
54061d3af9e0SMatthew G. Knepley   PetscInt       Nds, n;
54071d3af9e0SMatthew G. Knepley   PetscErrorCode ierr;
54081d3af9e0SMatthew G. Knepley 
54091d3af9e0SMatthew G. Knepley   PetscFunctionBegin;
54101d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
54111d3af9e0SMatthew G. Knepley   PetscValidHeaderSpecific(ds, PETSCDS_CLASSID, 2);
54121d3af9e0SMatthew G. Knepley   PetscValidPointer(num, 3);
54131d3af9e0SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
54141d3af9e0SMatthew G. Knepley   for (n = 0; n < Nds; ++n) if (ds == dm->probs[n].ds) break;
54151d3af9e0SMatthew G. Knepley   if (n >= Nds) *num = -1;
54161d3af9e0SMatthew G. Knepley   else          *num = n;
54171d3af9e0SMatthew G. Knepley   PetscFunctionReturn(0);
54181d3af9e0SMatthew G. Knepley }
54191d3af9e0SMatthew G. Knepley 
54201d3af9e0SMatthew G. Knepley /*@
5421e5e52638SMatthew G. Knepley   DMCreateDS - Create the discrete systems for the DM based upon the fields added to the DM
5422e5e52638SMatthew G. Knepley 
5423d083f849SBarry Smith   Collective on dm
5424e5e52638SMatthew G. Knepley 
5425e5e52638SMatthew G. Knepley   Input Parameter:
5426e5e52638SMatthew G. Knepley . dm - The DM
5427e5e52638SMatthew G. Knepley 
5428e5e52638SMatthew G. Knepley   Note: If the label has a DS defined, it will be replaced. Otherwise, it will be added to the DM.
5429e5e52638SMatthew G. Knepley 
5430e5e52638SMatthew G. Knepley   Level: intermediate
5431e5e52638SMatthew G. Knepley 
5432e5e52638SMatthew G. Knepley .seealso: DMSetField, DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5433e5e52638SMatthew G. Knepley @*/
5434e5e52638SMatthew G. Knepley PetscErrorCode DMCreateDS(DM dm)
5435e5e52638SMatthew G. Knepley {
5436e5e52638SMatthew G. Knepley   MPI_Comm       comm;
5437083401c6SMatthew G. Knepley   PetscDS        dsDef;
5438083401c6SMatthew G. Knepley   DMLabel       *labelSet;
5439083401c6SMatthew G. Knepley   PetscInt       dE, Nf = dm->Nf, f, s, Nl, l, Ndef;
5440e5e52638SMatthew G. Knepley   PetscBool      doSetup = PETSC_TRUE;
5441e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5442e5e52638SMatthew G. Knepley 
5443e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5444e5e52638SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5445e5e52638SMatthew G. Knepley   if (!dm->fields) PetscFunctionReturn(0);
5446e5e52638SMatthew G. Knepley   ierr = PetscObjectGetComm((PetscObject) dm, &comm);CHKERRQ(ierr);
5447083401c6SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dE);CHKERRQ(ierr);
5448083401c6SMatthew G. Knepley   /* Determine how many regions we have */
5449083401c6SMatthew G. Knepley   ierr = PetscMalloc1(Nf, &labelSet);CHKERRQ(ierr);
5450083401c6SMatthew G. Knepley   Nl   = 0;
5451083401c6SMatthew G. Knepley   Ndef = 0;
5452083401c6SMatthew G. Knepley   for (f = 0; f < Nf; ++f) {
5453083401c6SMatthew G. Knepley     DMLabel  label = dm->fields[f].label;
5454083401c6SMatthew G. Knepley     PetscInt l;
5455083401c6SMatthew G. Knepley 
5456083401c6SMatthew G. Knepley     if (!label) {++Ndef; continue;}
5457083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) if (label == labelSet[l]) break;
5458083401c6SMatthew G. Knepley     if (l < Nl) continue;
5459083401c6SMatthew G. Knepley     labelSet[Nl++] = label;
5460083401c6SMatthew G. Knepley   }
5461083401c6SMatthew G. Knepley   /* Create default DS if there are no labels to intersect with */
5462083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5463083401c6SMatthew G. Knepley   if (!dsDef && Ndef && !Nl) {
5464b3cf3223SMatthew G. Knepley     IS        fields;
5465b3cf3223SMatthew G. Knepley     PetscInt *fld, nf;
5466b3cf3223SMatthew G. Knepley 
5467b3cf3223SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) ++nf;
5468083401c6SMatthew G. Knepley     if (nf) {
5469b3cf3223SMatthew G. Knepley       ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5470b3cf3223SMatthew G. Knepley       for (f = 0, nf = 0; f < Nf; ++f) if (!dm->fields[f].label) fld[nf++] = f;
547188f0c812SMatthew G. Knepley       ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
547288f0c812SMatthew G. Knepley       ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
547388f0c812SMatthew G. Knepley       ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
547488f0c812SMatthew G. Knepley       ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
547588f0c812SMatthew G. Knepley 
5476083401c6SMatthew G. Knepley       ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5477083401c6SMatthew G. Knepley       ierr = DMSetRegionDS(dm, NULL, fields, dsDef);CHKERRQ(ierr);
5478083401c6SMatthew G. Knepley       ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5479b3cf3223SMatthew G. Knepley       ierr = ISDestroy(&fields);CHKERRQ(ierr);
54802df9ee95SMatthew G. Knepley     }
5481083401c6SMatthew G. Knepley   }
5482083401c6SMatthew G. Knepley   ierr = DMGetRegionDS(dm, NULL, NULL, &dsDef);CHKERRQ(ierr);
5483083401c6SMatthew G. Knepley   if (dsDef) {ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);}
5484083401c6SMatthew G. Knepley   /* Intersect labels with default fields */
5485083401c6SMatthew G. Knepley   if (Ndef && Nl) {
54860122748bSMatthew G. Knepley     DM              plex;
5487083401c6SMatthew G. Knepley     DMLabel         cellLabel;
5488083401c6SMatthew G. Knepley     IS              fieldIS, allcellIS, defcellIS = NULL;
5489083401c6SMatthew G. Knepley     PetscInt       *fields;
5490083401c6SMatthew G. Knepley     const PetscInt *cells;
5491083401c6SMatthew G. Knepley     PetscInt        depth, nf = 0, n, c;
54920122748bSMatthew G. Knepley 
54930122748bSMatthew G. Knepley     ierr = DMConvert(dm, DMPLEX, &plex);CHKERRQ(ierr);
54940122748bSMatthew G. Knepley     ierr = DMPlexGetDepth(plex, &depth);CHKERRQ(ierr);
5495083401c6SMatthew G. Knepley     ierr = DMGetStratumIS(plex, "dim", depth, &allcellIS);CHKERRQ(ierr);
5496083401c6SMatthew G. Knepley     if (!allcellIS) {ierr = DMGetStratumIS(plex, "depth", depth, &allcellIS);CHKERRQ(ierr);}
5497083401c6SMatthew G. Knepley     for (l = 0; l < Nl; ++l) {
5498083401c6SMatthew G. Knepley       DMLabel label = labelSet[l];
5499083401c6SMatthew G. Knepley       IS      pointIS;
5500083401c6SMatthew G. Knepley 
5501083401c6SMatthew G. Knepley       ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5502083401c6SMatthew G. Knepley       ierr = DMLabelGetStratumIS(label, 1, &pointIS);CHKERRQ(ierr);
5503083401c6SMatthew G. Knepley       ierr = ISDifference(allcellIS, pointIS, &defcellIS);CHKERRQ(ierr);
5504083401c6SMatthew G. Knepley       ierr = ISDestroy(&pointIS);CHKERRQ(ierr);
5505083401c6SMatthew G. Knepley     }
5506083401c6SMatthew G. Knepley     ierr = ISDestroy(&allcellIS);CHKERRQ(ierr);
5507083401c6SMatthew G. Knepley 
5508083401c6SMatthew G. Knepley     ierr = DMLabelCreate(PETSC_COMM_SELF, "defaultCells", &cellLabel);CHKERRQ(ierr);
5509083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(defcellIS, &n);CHKERRQ(ierr);
5510083401c6SMatthew G. Knepley     ierr = ISGetIndices(defcellIS, &cells);CHKERRQ(ierr);
5511083401c6SMatthew G. Knepley     for (c = 0; c < n; ++c) {ierr = DMLabelSetValue(cellLabel, cells[c], 1);CHKERRQ(ierr);}
5512083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(defcellIS, &cells);CHKERRQ(ierr);
5513083401c6SMatthew G. Knepley     ierr = ISDestroy(&defcellIS);CHKERRQ(ierr);
5514083401c6SMatthew G. Knepley     ierr = DMPlexLabelComplete(plex, cellLabel);CHKERRQ(ierr);
5515083401c6SMatthew G. Knepley 
5516083401c6SMatthew G. Knepley     ierr = PetscMalloc1(Ndef, &fields);CHKERRQ(ierr);
5517083401c6SMatthew G. Knepley     for (f = 0; f < Nf; ++f) if (!dm->fields[f].label) fields[nf++] = f;
5518083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fieldIS);CHKERRQ(ierr);
5519083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fieldIS, "dm_fields_");CHKERRQ(ierr);
5520083401c6SMatthew G. Knepley     ierr = ISSetType(fieldIS, ISGENERAL);CHKERRQ(ierr);
5521083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fieldIS, nf, fields, PETSC_OWN_POINTER);CHKERRQ(ierr);
5522083401c6SMatthew G. Knepley 
5523083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &dsDef);CHKERRQ(ierr);
5524083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, cellLabel, fieldIS, dsDef);CHKERRQ(ierr);
5525083401c6SMatthew G. Knepley     ierr = DMLabelDestroy(&cellLabel);CHKERRQ(ierr);
5526083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(dsDef, dE);CHKERRQ(ierr);
5527083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&dsDef);CHKERRQ(ierr);
5528083401c6SMatthew G. Knepley     ierr = ISDestroy(&fieldIS);CHKERRQ(ierr);
55290122748bSMatthew G. Knepley     ierr = DMDestroy(&plex);CHKERRQ(ierr);
5530083401c6SMatthew G. Knepley   }
5531083401c6SMatthew G. Knepley   /* Create label DSes
5532083401c6SMatthew G. Knepley      - WE ONLY SUPPORT IDENTICAL OR DISJOINT LABELS
5533083401c6SMatthew G. Knepley   */
5534083401c6SMatthew G. Knepley   /* TODO Should check that labels are disjoint */
5535083401c6SMatthew G. Knepley   for (l = 0; l < Nl; ++l) {
5536083401c6SMatthew G. Knepley     DMLabel   label = labelSet[l];
5537083401c6SMatthew G. Knepley     PetscDS   ds;
5538083401c6SMatthew G. Knepley     IS        fields;
5539083401c6SMatthew G. Knepley     PetscInt *fld, nf;
5540083401c6SMatthew G. Knepley 
5541083401c6SMatthew G. Knepley     ierr = PetscDSCreate(comm, &ds);CHKERRQ(ierr);
5542083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) ++nf;
5543083401c6SMatthew G. Knepley     ierr = PetscMalloc1(nf, &fld);CHKERRQ(ierr);
5544083401c6SMatthew G. Knepley     for (f = 0, nf = 0; f < Nf; ++f) if (label == dm->fields[f].label || !dm->fields[f].label) fld[nf++] = f;
5545083401c6SMatthew G. Knepley     ierr = ISCreate(PETSC_COMM_SELF, &fields);CHKERRQ(ierr);
5546083401c6SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) fields, "dm_fields_");CHKERRQ(ierr);
5547083401c6SMatthew G. Knepley     ierr = ISSetType(fields, ISGENERAL);CHKERRQ(ierr);
5548083401c6SMatthew G. Knepley     ierr = ISGeneralSetIndices(fields, nf, fld, PETSC_OWN_POINTER);CHKERRQ(ierr);
5549083401c6SMatthew G. Knepley     ierr = DMSetRegionDS(dm, label, fields, ds);CHKERRQ(ierr);
5550083401c6SMatthew G. Knepley     ierr = ISDestroy(&fields);CHKERRQ(ierr);
5551083401c6SMatthew G. Knepley     ierr = PetscDSSetCoordinateDimension(ds, dE);CHKERRQ(ierr);
5552083401c6SMatthew G. Knepley     {
5553083401c6SMatthew G. Knepley       DMPolytopeType ct;
5554083401c6SMatthew G. Knepley       PetscInt       lStart, lEnd;
5555083401c6SMatthew G. Knepley       PetscBool      isHybridLocal = PETSC_FALSE, isHybrid;
55560122748bSMatthew G. Knepley 
5557e5e52638SMatthew G. Knepley       ierr = DMLabelGetBounds(label, &lStart, &lEnd);CHKERRQ(ierr);
5558665f567fSMatthew G. Knepley       if (lStart >= 0) {
5559412e9a14SMatthew G. Knepley         ierr = DMPlexGetCellType(dm, lStart, &ct);CHKERRQ(ierr);
5560412e9a14SMatthew G. Knepley         switch (ct) {
5561412e9a14SMatthew G. Knepley           case DM_POLYTOPE_POINT_PRISM_TENSOR:
5562412e9a14SMatthew G. Knepley           case DM_POLYTOPE_SEG_PRISM_TENSOR:
5563412e9a14SMatthew G. Knepley           case DM_POLYTOPE_TRI_PRISM_TENSOR:
5564412e9a14SMatthew G. Knepley           case DM_POLYTOPE_QUAD_PRISM_TENSOR:
5565083401c6SMatthew G. Knepley             isHybridLocal = PETSC_TRUE;break;
5566083401c6SMatthew G. Knepley           default: break;
5567412e9a14SMatthew G. Knepley         }
5568665f567fSMatthew G. Knepley       }
5569083401c6SMatthew G. Knepley       ierr = MPI_Allreduce(&isHybridLocal, &isHybrid, 1, MPIU_BOOL, MPI_LOR, comm);CHKERRQ(ierr);
5570083401c6SMatthew G. Knepley       ierr = PetscDSSetHybrid(ds, isHybrid);CHKERRQ(ierr);
5571e5e52638SMatthew G. Knepley     }
5572083401c6SMatthew G. Knepley     ierr = PetscDSDestroy(&ds);CHKERRQ(ierr);
5573e5e52638SMatthew G. Knepley   }
5574083401c6SMatthew G. Knepley   ierr = PetscFree(labelSet);CHKERRQ(ierr);
5575e5e52638SMatthew G. Knepley   /* Set fields in DSes */
5576083401c6SMatthew G. Knepley   for (s = 0; s < dm->Nds; ++s) {
5577083401c6SMatthew G. Knepley     PetscDS         ds     = dm->probs[s].ds;
5578083401c6SMatthew G. Knepley     IS              fields = dm->probs[s].fields;
5579083401c6SMatthew G. Knepley     const PetscInt *fld;
5580083401c6SMatthew G. Knepley     PetscInt        nf;
5581e5e52638SMatthew G. Knepley 
5582083401c6SMatthew G. Knepley     ierr = ISGetLocalSize(fields, &nf);CHKERRQ(ierr);
5583083401c6SMatthew G. Knepley     ierr = ISGetIndices(fields, &fld);CHKERRQ(ierr);
5584083401c6SMatthew G. Knepley     for (f = 0; f < nf; ++f) {
5585083401c6SMatthew G. Knepley       PetscObject  disc  = dm->fields[fld[f]].disc;
5586083401c6SMatthew G. Knepley       PetscBool    isHybrid;
5587e5e52638SMatthew G. Knepley       PetscClassId id;
5588e5e52638SMatthew G. Knepley 
5589083401c6SMatthew G. Knepley       ierr = PetscDSGetHybrid(ds, &isHybrid);CHKERRQ(ierr);
5590083401c6SMatthew G. Knepley       /* If this is a cohesive cell, then it needs the lower dimensional discretization */
5591083401c6SMatthew G. Knepley       if (isHybrid && f < nf-1) {ierr = PetscFEGetHeightSubspace((PetscFE) disc, 1, (PetscFE *) &disc);CHKERRQ(ierr);}
5592083401c6SMatthew G. Knepley       ierr = PetscDSSetDiscretization(ds, f, disc);CHKERRQ(ierr);
5593083401c6SMatthew G. Knepley       /* We allow people to have placeholder fields and construct the Section by hand */
5594e5e52638SMatthew G. Knepley       ierr = PetscObjectGetClassId(disc, &id);CHKERRQ(ierr);
5595e5e52638SMatthew G. Knepley       if ((id != PETSCFE_CLASSID) && (id != PETSCFV_CLASSID)) doSetup = PETSC_FALSE;
5596e5e52638SMatthew G. Knepley     }
5597083401c6SMatthew G. Knepley     ierr = ISRestoreIndices(fields, &fld);CHKERRQ(ierr);
5598e5e52638SMatthew G. Knepley   }
5599e5e52638SMatthew G. Knepley   /* Setup DSes */
5600e5e52638SMatthew G. Knepley   if (doSetup) {
5601e5e52638SMatthew G. Knepley     for (s = 0; s < dm->Nds; ++s) {ierr = PetscDSSetUp(dm->probs[s].ds);CHKERRQ(ierr);}
5602e5e52638SMatthew G. Knepley   }
5603e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5604e5e52638SMatthew G. Knepley }
5605e5e52638SMatthew G. Knepley 
5606e5e52638SMatthew G. Knepley /*@
56077f96f943SMatthew G. Knepley   DMComputeExactSolution - Compute the exact solution for a given DM, using the PetscDS information.
56087f96f943SMatthew G. Knepley 
5609f2cacb80SMatthew G. Knepley   Collective on DM
5610f2cacb80SMatthew G. Knepley 
56117f96f943SMatthew G. Knepley   Input Parameters:
56127f96f943SMatthew G. Knepley + dm   - The DM
56137f96f943SMatthew G. Knepley - time - The time
56147f96f943SMatthew G. Knepley 
56157f96f943SMatthew G. Knepley   Output Parameters:
5616f2cacb80SMatthew G. Knepley + u    - The vector will be filled with exact solution values, or NULL
5617f2cacb80SMatthew G. Knepley - u_t  - The vector will be filled with the time derivative of exact solution values, or NULL
56187f96f943SMatthew G. Knepley 
56197f96f943SMatthew G. Knepley   Note: The user must call PetscDSSetExactSolution() beforehand
56207f96f943SMatthew G. Knepley 
56217f96f943SMatthew G. Knepley   Level: developer
56227f96f943SMatthew G. Knepley 
56237f96f943SMatthew G. Knepley .seealso: PetscDSSetExactSolution()
56247f96f943SMatthew G. Knepley @*/
5625f2cacb80SMatthew G. Knepley PetscErrorCode DMComputeExactSolution(DM dm, PetscReal time, Vec u, Vec u_t)
56267f96f943SMatthew G. Knepley {
56277f96f943SMatthew G. Knepley   PetscErrorCode (**exacts)(PetscInt, PetscReal, const PetscReal x[], PetscInt, PetscScalar *u, void *ctx);
56287f96f943SMatthew G. Knepley   void            **ectxs;
56297f96f943SMatthew G. Knepley   PetscInt          Nf, Nds, s;
56307f96f943SMatthew G. Knepley   PetscErrorCode    ierr;
56317f96f943SMatthew G. Knepley 
56327f96f943SMatthew G. Knepley   PetscFunctionBegin;
5633f2cacb80SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5634f2cacb80SMatthew G. Knepley   if (u)   PetscValidHeaderSpecific(u, VEC_CLASSID, 3);
5635f2cacb80SMatthew G. Knepley   if (u_t) PetscValidHeaderSpecific(u_t, VEC_CLASSID, 4);
56367f96f943SMatthew G. Knepley   ierr = DMGetNumFields(dm, &Nf);CHKERRQ(ierr);
56377f96f943SMatthew G. Knepley   ierr = PetscMalloc2(Nf, &exacts, Nf, &ectxs);CHKERRQ(ierr);
56387f96f943SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
56397f96f943SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
56407f96f943SMatthew G. Knepley     PetscDS         ds;
56417f96f943SMatthew G. Knepley     DMLabel         label;
56427f96f943SMatthew G. Knepley     IS              fieldIS;
56437f96f943SMatthew G. Knepley     const PetscInt *fields, id = 1;
56447f96f943SMatthew G. Knepley     PetscInt        dsNf, f;
56457f96f943SMatthew G. Knepley 
56467f96f943SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fieldIS, &ds);CHKERRQ(ierr);
56477f96f943SMatthew G. Knepley     ierr = PetscDSGetNumFields(ds, &dsNf);CHKERRQ(ierr);
56487f96f943SMatthew G. Knepley     ierr = ISGetIndices(fieldIS, &fields);CHKERRQ(ierr);
56497f96f943SMatthew G. Knepley     ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
56507f96f943SMatthew G. Knepley     ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5651f2cacb80SMatthew G. Knepley     if (u) {
56527f96f943SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
56537f96f943SMatthew G. Knepley         const PetscInt field = fields[f];
56547f96f943SMatthew G. Knepley         ierr = PetscDSGetExactSolution(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
56557f96f943SMatthew G. Knepley       }
56567f96f943SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
56577f96f943SMatthew G. Knepley       if (label) {
56587f96f943SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56597f96f943SMatthew G. Knepley       } else {
56607f96f943SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u);CHKERRQ(ierr);
56617f96f943SMatthew G. Knepley       }
56627f96f943SMatthew G. Knepley     }
5663f2cacb80SMatthew G. Knepley     if (u_t) {
5664f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(exacts, Nf);CHKERRQ(ierr);
5665f2cacb80SMatthew G. Knepley       ierr = PetscArrayzero(ectxs, Nf);CHKERRQ(ierr);
5666f2cacb80SMatthew G. Knepley       for (f = 0; f < dsNf; ++f) {
5667f2cacb80SMatthew G. Knepley         const PetscInt field = fields[f];
5668f2cacb80SMatthew G. Knepley         ierr = PetscDSGetExactSolutionTimeDerivative(ds, field, &exacts[field], &ectxs[field]);CHKERRQ(ierr);
5669f2cacb80SMatthew G. Knepley       }
5670f2cacb80SMatthew G. Knepley       ierr = ISRestoreIndices(fieldIS, &fields);CHKERRQ(ierr);
5671f2cacb80SMatthew G. Knepley       if (label) {
5672f2cacb80SMatthew G. Knepley         ierr = DMProjectFunctionLabel(dm, time, label, 1, &id, 0, NULL, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5673f2cacb80SMatthew G. Knepley       } else {
5674f2cacb80SMatthew G. Knepley         ierr = DMProjectFunction(dm, time, exacts, ectxs, INSERT_ALL_VALUES, u_t);CHKERRQ(ierr);
5675f2cacb80SMatthew G. Knepley       }
5676f2cacb80SMatthew G. Knepley     }
5677f2cacb80SMatthew G. Knepley   }
5678f2cacb80SMatthew G. Knepley   if (u) {
56797f96f943SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution");CHKERRQ(ierr);
56807f96f943SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u, "exact_");CHKERRQ(ierr);
5681f2cacb80SMatthew G. Knepley   }
5682f2cacb80SMatthew G. Knepley   if (u_t) {
5683f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetName((PetscObject) u, "Exact Solution Time Derivative");CHKERRQ(ierr);
5684f2cacb80SMatthew G. Knepley     ierr = PetscObjectSetOptionsPrefix((PetscObject) u_t, "exact_t_");CHKERRQ(ierr);
5685f2cacb80SMatthew G. Knepley   }
56867f96f943SMatthew G. Knepley   ierr = PetscFree2(exacts, ectxs);CHKERRQ(ierr);
56877f96f943SMatthew G. Knepley   PetscFunctionReturn(0);
56887f96f943SMatthew G. Knepley }
56897f96f943SMatthew G. Knepley 
56907f96f943SMatthew G. Knepley /*@
5691e5e52638SMatthew G. Knepley   DMCopyDS - Copy the discrete systems for the DM into another DM
5692e5e52638SMatthew G. Knepley 
5693d083f849SBarry Smith   Collective on dm
5694e5e52638SMatthew G. Knepley 
5695e5e52638SMatthew G. Knepley   Input Parameter:
5696e5e52638SMatthew G. Knepley . dm - The DM
5697e5e52638SMatthew G. Knepley 
5698e5e52638SMatthew G. Knepley   Output Parameter:
5699e5e52638SMatthew G. Knepley . newdm - The DM
5700e5e52638SMatthew G. Knepley 
5701e5e52638SMatthew G. Knepley   Level: advanced
5702e5e52638SMatthew G. Knepley 
5703e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMAddField(), DMGetDS(), DMGetCellDS(), DMGetRegionDS(), DMSetRegionDS()
5704e5e52638SMatthew G. Knepley @*/
5705e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDS(DM dm, DM newdm)
5706e5e52638SMatthew G. Knepley {
5707e5e52638SMatthew G. Knepley   PetscInt       Nds, s;
5708e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5709e5e52638SMatthew G. Knepley 
5710e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5711e5e52638SMatthew G. Knepley   if (dm == newdm) PetscFunctionReturn(0);
5712e5e52638SMatthew G. Knepley   ierr = DMGetNumDS(dm, &Nds);CHKERRQ(ierr);
5713e5e52638SMatthew G. Knepley   ierr = DMClearDS(newdm);CHKERRQ(ierr);
5714e5e52638SMatthew G. Knepley   for (s = 0; s < Nds; ++s) {
5715e5e52638SMatthew G. Knepley     DMLabel  label;
5716b3cf3223SMatthew G. Knepley     IS       fields;
5717e5e52638SMatthew G. Knepley     PetscDS  ds;
5718783e2ec8SMatthew G. Knepley     PetscInt Nbd, bd;
5719e5e52638SMatthew G. Knepley 
5720b3cf3223SMatthew G. Knepley     ierr = DMGetRegionNumDS(dm, s, &label, &fields, &ds);CHKERRQ(ierr);
5721b3cf3223SMatthew G. Knepley     ierr = DMSetRegionDS(newdm, label, fields, ds);CHKERRQ(ierr);
5722783e2ec8SMatthew G. Knepley     ierr = PetscDSGetNumBoundary(ds, &Nbd);CHKERRQ(ierr);
5723783e2ec8SMatthew G. Knepley     for (bd = 0; bd < Nbd; ++bd) {
5724783e2ec8SMatthew G. Knepley       const char *labelname, *name;
5725783e2ec8SMatthew G. Knepley       PetscInt    field;
5726783e2ec8SMatthew G. Knepley 
5727783e2ec8SMatthew G. Knepley       /* Do not check if label exists here, since p4est calls this for the reference tree which does not have the labels */
572856cf3b9cSMatthew G. Knepley       ierr = PetscDSGetBoundary(ds, bd, NULL, &name, &labelname, &field, NULL, NULL, NULL, NULL, NULL, NULL, NULL);CHKERRQ(ierr);
5729783e2ec8SMatthew G. Knepley       ierr = DMCompleteBoundaryLabel_Internal(newdm, ds, field, bd, labelname);CHKERRQ(ierr);
5730783e2ec8SMatthew G. Knepley     }
5731e5e52638SMatthew G. Knepley   }
5732e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5733e5e52638SMatthew G. Knepley }
5734e5e52638SMatthew G. Knepley 
5735e5e52638SMatthew G. Knepley /*@
5736e5e52638SMatthew G. Knepley   DMCopyDisc - Copy the fields and discrete systems for the DM into another DM
5737e5e52638SMatthew G. Knepley 
5738d083f849SBarry Smith   Collective on dm
5739e5e52638SMatthew G. Knepley 
5740e5e52638SMatthew G. Knepley   Input Parameter:
5741e5e52638SMatthew G. Knepley . dm - The DM
5742e5e52638SMatthew G. Knepley 
5743e5e52638SMatthew G. Knepley   Output Parameter:
5744e5e52638SMatthew G. Knepley . newdm - The DM
5745e5e52638SMatthew G. Knepley 
5746e5e52638SMatthew G. Knepley   Level: advanced
5747e5e52638SMatthew G. Knepley 
5748e5e52638SMatthew G. Knepley .seealso: DMCopyFields(), DMCopyDS()
5749e5e52638SMatthew G. Knepley @*/
5750e5e52638SMatthew G. Knepley PetscErrorCode DMCopyDisc(DM dm, DM newdm)
5751e5e52638SMatthew G. Knepley {
5752e5e52638SMatthew G. Knepley   PetscErrorCode ierr;
5753e5e52638SMatthew G. Knepley 
5754e5e52638SMatthew G. Knepley   PetscFunctionBegin;
5755e5e52638SMatthew G. Knepley   ierr = DMCopyFields(dm, newdm);CHKERRQ(ierr);
5756e5e52638SMatthew G. Knepley   ierr = DMCopyDS(dm, newdm);CHKERRQ(ierr);
5757e5e52638SMatthew G. Knepley   PetscFunctionReturn(0);
5758e5e52638SMatthew G. Knepley }
5759e5e52638SMatthew G. Knepley 
5760b64e0483SPeter Brune PetscErrorCode DMRestrictHook_Coordinates(DM dm,DM dmc,void *ctx)
5761b64e0483SPeter Brune {
5762b64e0483SPeter Brune   DM dm_coord,dmc_coord;
5763b64e0483SPeter Brune   PetscErrorCode ierr;
5764b64e0483SPeter Brune   Vec coords,ccoords;
57656dbf9973SLawrence Mitchell   Mat inject;
5766b64e0483SPeter Brune   PetscFunctionBegin;
5767b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
5768b64e0483SPeter Brune   ierr = DMGetCoordinateDM(dmc,&dmc_coord);CHKERRQ(ierr);
5769b64e0483SPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
5770b64e0483SPeter Brune   ierr = DMGetCoordinates(dmc,&ccoords);CHKERRQ(ierr);
5771b64e0483SPeter Brune   if (coords && !ccoords) {
5772b64e0483SPeter Brune     ierr = DMCreateGlobalVector(dmc_coord,&ccoords);CHKERRQ(ierr);
57736668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
57746dbf9973SLawrence Mitchell     ierr = DMCreateInjection(dmc_coord,dm_coord,&inject);CHKERRQ(ierr);
57752adcf181SLawrence Mitchell     ierr = MatRestrict(inject,coords,ccoords);CHKERRQ(ierr);
57766dbf9973SLawrence Mitchell     ierr = MatDestroy(&inject);CHKERRQ(ierr);
5777b64e0483SPeter Brune     ierr = DMSetCoordinates(dmc,ccoords);CHKERRQ(ierr);
5778b64e0483SPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
5779b64e0483SPeter Brune   }
5780b64e0483SPeter Brune   PetscFunctionReturn(0);
5781b64e0483SPeter Brune }
5782b64e0483SPeter Brune 
578303dadc2fSPeter Brune static PetscErrorCode DMSubDomainHook_Coordinates(DM dm,DM subdm,void *ctx)
578403dadc2fSPeter Brune {
578503dadc2fSPeter Brune   DM dm_coord,subdm_coord;
578603dadc2fSPeter Brune   PetscErrorCode ierr;
578703dadc2fSPeter Brune   Vec coords,ccoords,clcoords;
578803dadc2fSPeter Brune   VecScatter *scat_i,*scat_g;
578903dadc2fSPeter Brune   PetscFunctionBegin;
579003dadc2fSPeter Brune   ierr = DMGetCoordinateDM(dm,&dm_coord);CHKERRQ(ierr);
579103dadc2fSPeter Brune   ierr = DMGetCoordinateDM(subdm,&subdm_coord);CHKERRQ(ierr);
579203dadc2fSPeter Brune   ierr = DMGetCoordinates(dm,&coords);CHKERRQ(ierr);
579303dadc2fSPeter Brune   ierr = DMGetCoordinates(subdm,&ccoords);CHKERRQ(ierr);
579403dadc2fSPeter Brune   if (coords && !ccoords) {
579503dadc2fSPeter Brune     ierr = DMCreateGlobalVector(subdm_coord,&ccoords);CHKERRQ(ierr);
57966668ed41SLisandro Dalcin     ierr = PetscObjectSetName((PetscObject)ccoords,"coordinates");CHKERRQ(ierr);
579703dadc2fSPeter Brune     ierr = DMCreateLocalVector(subdm_coord,&clcoords);CHKERRQ(ierr);
579824640c55SToby Isaac     ierr = PetscObjectSetName((PetscObject)clcoords,"coordinates");CHKERRQ(ierr);
579903dadc2fSPeter Brune     ierr = DMCreateDomainDecompositionScatters(dm_coord,1,&subdm_coord,NULL,&scat_i,&scat_g);CHKERRQ(ierr);
580003dadc2fSPeter Brune     ierr = VecScatterBegin(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
580103dadc2fSPeter Brune     ierr = VecScatterEnd(scat_i[0],coords,ccoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
58021ed9ada7SJunchao Zhang     ierr = VecScatterBegin(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
580303dadc2fSPeter Brune     ierr = VecScatterEnd(scat_g[0],coords,clcoords,INSERT_VALUES,SCATTER_FORWARD);CHKERRQ(ierr);
580403dadc2fSPeter Brune     ierr = DMSetCoordinates(subdm,ccoords);CHKERRQ(ierr);
580503dadc2fSPeter Brune     ierr = DMSetCoordinatesLocal(subdm,clcoords);CHKERRQ(ierr);
580603dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_i[0]);CHKERRQ(ierr);
580703dadc2fSPeter Brune     ierr = VecScatterDestroy(&scat_g[0]);CHKERRQ(ierr);
580803dadc2fSPeter Brune     ierr = VecDestroy(&ccoords);CHKERRQ(ierr);
580903dadc2fSPeter Brune     ierr = VecDestroy(&clcoords);CHKERRQ(ierr);
581003dadc2fSPeter Brune     ierr = PetscFree(scat_i);CHKERRQ(ierr);
581103dadc2fSPeter Brune     ierr = PetscFree(scat_g);CHKERRQ(ierr);
581203dadc2fSPeter Brune   }
581303dadc2fSPeter Brune   PetscFunctionReturn(0);
581403dadc2fSPeter Brune }
581503dadc2fSPeter Brune 
5816c73cfb54SMatthew G. Knepley /*@
5817c73cfb54SMatthew G. Knepley   DMGetDimension - Return the topological dimension of the DM
5818c73cfb54SMatthew G. Knepley 
5819c73cfb54SMatthew G. Knepley   Not collective
5820c73cfb54SMatthew G. Knepley 
5821c73cfb54SMatthew G. Knepley   Input Parameter:
5822c73cfb54SMatthew G. Knepley . dm - The DM
5823c73cfb54SMatthew G. Knepley 
5824c73cfb54SMatthew G. Knepley   Output Parameter:
5825c73cfb54SMatthew G. Knepley . dim - The topological dimension
5826c73cfb54SMatthew G. Knepley 
5827c73cfb54SMatthew G. Knepley   Level: beginner
5828c73cfb54SMatthew G. Knepley 
5829c73cfb54SMatthew G. Knepley .seealso: DMSetDimension(), DMCreate()
5830c73cfb54SMatthew G. Knepley @*/
5831c73cfb54SMatthew G. Knepley PetscErrorCode DMGetDimension(DM dm, PetscInt *dim)
5832c73cfb54SMatthew G. Knepley {
5833c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5834c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5835534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
5836c73cfb54SMatthew G. Knepley   *dim = dm->dim;
5837c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5838c73cfb54SMatthew G. Knepley }
5839c73cfb54SMatthew G. Knepley 
5840c73cfb54SMatthew G. Knepley /*@
5841c73cfb54SMatthew G. Knepley   DMSetDimension - Set the topological dimension of the DM
5842c73cfb54SMatthew G. Knepley 
5843c73cfb54SMatthew G. Knepley   Collective on dm
5844c73cfb54SMatthew G. Knepley 
5845c73cfb54SMatthew G. Knepley   Input Parameters:
5846c73cfb54SMatthew G. Knepley + dm - The DM
5847c73cfb54SMatthew G. Knepley - dim - The topological dimension
5848c73cfb54SMatthew G. Knepley 
5849c73cfb54SMatthew G. Knepley   Level: beginner
5850c73cfb54SMatthew G. Knepley 
5851c73cfb54SMatthew G. Knepley .seealso: DMGetDimension(), DMCreate()
5852c73cfb54SMatthew G. Knepley @*/
5853c73cfb54SMatthew G. Knepley PetscErrorCode DMSetDimension(DM dm, PetscInt dim)
5854c73cfb54SMatthew G. Knepley {
5855e5e52638SMatthew G. Knepley   PetscDS        ds;
5856f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
5857f17e8794SMatthew G. Knepley 
5858c73cfb54SMatthew G. Knepley   PetscFunctionBegin;
5859c73cfb54SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
5860c73cfb54SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, dim, 2);
5861c73cfb54SMatthew G. Knepley   dm->dim = dim;
5862e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
5863e5e52638SMatthew G. Knepley   if (ds->dimEmbed < 0) {ierr = PetscDSSetCoordinateDimension(ds, dm->dim);CHKERRQ(ierr);}
5864c73cfb54SMatthew G. Knepley   PetscFunctionReturn(0);
5865c73cfb54SMatthew G. Knepley }
5866c73cfb54SMatthew G. Knepley 
5867793f3fe5SMatthew G. Knepley /*@
5868793f3fe5SMatthew G. Knepley   DMGetDimPoints - Get the half-open interval for all points of a given dimension
5869793f3fe5SMatthew G. Knepley 
5870d083f849SBarry Smith   Collective on dm
5871793f3fe5SMatthew G. Knepley 
5872793f3fe5SMatthew G. Knepley   Input Parameters:
5873793f3fe5SMatthew G. Knepley + dm - the DM
5874793f3fe5SMatthew G. Knepley - dim - the dimension
5875793f3fe5SMatthew G. Knepley 
5876793f3fe5SMatthew G. Knepley   Output Parameters:
5877793f3fe5SMatthew G. Knepley + pStart - The first point of the given dimension
5878aa049354SPatrick Sanan - pEnd - The first point following points of the given dimension
5879793f3fe5SMatthew G. Knepley 
5880793f3fe5SMatthew G. Knepley   Note:
5881793f3fe5SMatthew G. Knepley   The points are vertices in the Hasse diagram encoding the topology. This is explained in
5882a8d69d7bSBarry Smith   https://arxiv.org/abs/0908.4427. If no points exist of this dimension in the storage scheme,
5883793f3fe5SMatthew G. Knepley   then the interval is empty.
5884793f3fe5SMatthew G. Knepley 
5885793f3fe5SMatthew G. Knepley   Level: intermediate
5886793f3fe5SMatthew G. Knepley 
5887793f3fe5SMatthew G. Knepley .seealso: DMPLEX, DMPlexGetDepthStratum(), DMPlexGetHeightStratum()
5888793f3fe5SMatthew G. Knepley @*/
5889793f3fe5SMatthew G. Knepley PetscErrorCode DMGetDimPoints(DM dm, PetscInt dim, PetscInt *pStart, PetscInt *pEnd)
5890793f3fe5SMatthew G. Knepley {
5891793f3fe5SMatthew G. Knepley   PetscInt       d;
5892793f3fe5SMatthew G. Knepley   PetscErrorCode ierr;
5893793f3fe5SMatthew G. Knepley 
5894793f3fe5SMatthew G. Knepley   PetscFunctionBegin;
5895793f3fe5SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
5896793f3fe5SMatthew G. Knepley   ierr = DMGetDimension(dm, &d);CHKERRQ(ierr);
5897793f3fe5SMatthew G. Knepley   if ((dim < 0) || (dim > d)) SETERRQ2(PetscObjectComm((PetscObject) dm), PETSC_ERR_ARG_OUTOFRANGE, "Invalid dimension %d 1", dim, d);
5898b9d85ea2SLisandro Dalcin   if (!dm->ops->getdimpoints) SETERRQ1(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "DM type %s does not implement DMGetDimPoints",((PetscObject)dm)->type_name);
5899793f3fe5SMatthew G. Knepley   ierr = (*dm->ops->getdimpoints)(dm, dim, pStart, pEnd);CHKERRQ(ierr);
5900793f3fe5SMatthew G. Knepley   PetscFunctionReturn(0);
5901793f3fe5SMatthew G. Knepley }
5902793f3fe5SMatthew G. Knepley 
59036636e97aSMatthew G Knepley /*@
59046636e97aSMatthew G Knepley   DMSetCoordinates - Sets into the DM a global vector that holds the coordinates
59056636e97aSMatthew G Knepley 
5906d083f849SBarry Smith   Collective on dm
59076636e97aSMatthew G Knepley 
59086636e97aSMatthew G Knepley   Input Parameters:
59096636e97aSMatthew G Knepley + dm - the DM
59106636e97aSMatthew G Knepley - c - coordinate vector
59116636e97aSMatthew G Knepley 
59122dd40e9bSPatrick Sanan   Notes:
59132dd40e9bSPatrick Sanan   The coordinates do include those for ghost points, which are in the local vector.
59142dd40e9bSPatrick Sanan 
59152dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59166636e97aSMatthew G Knepley 
59176636e97aSMatthew G Knepley   Level: intermediate
59186636e97aSMatthew G Knepley 
59192dd40e9bSPatrick Sanan .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59206636e97aSMatthew G Knepley @*/
59216636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinates(DM dm, Vec c)
59226636e97aSMatthew G Knepley {
59236636e97aSMatthew G Knepley   PetscErrorCode ierr;
59246636e97aSMatthew G Knepley 
59256636e97aSMatthew G Knepley   PetscFunctionBegin;
59266636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59276636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59286636e97aSMatthew G Knepley   ierr            = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59296636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59306636e97aSMatthew G Knepley   dm->coordinates = c;
59316636e97aSMatthew G Knepley   ierr            = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
5932b64e0483SPeter Brune   ierr            = DMCoarsenHookAdd(dm,DMRestrictHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
593303dadc2fSPeter Brune   ierr            = DMSubDomainHookAdd(dm,DMSubDomainHook_Coordinates,NULL,NULL);CHKERRQ(ierr);
59346636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59356636e97aSMatthew G Knepley }
59366636e97aSMatthew G Knepley 
59376636e97aSMatthew G Knepley /*@
59386636e97aSMatthew G Knepley   DMSetCoordinatesLocal - Sets into the DM a local vector that holds the coordinates
59396636e97aSMatthew G Knepley 
59407058e716SVaclav Hapla   Not collective
59416636e97aSMatthew G Knepley 
59426636e97aSMatthew G Knepley    Input Parameters:
59436636e97aSMatthew G Knepley +  dm - the DM
59446636e97aSMatthew G Knepley -  c - coordinate vector
59456636e97aSMatthew G Knepley 
59462dd40e9bSPatrick Sanan   Notes:
59476636e97aSMatthew G Knepley   The coordinates of ghost points can be set using DMSetCoordinates()
59486636e97aSMatthew G Knepley   followed by DMGetCoordinatesLocal(). This is intended to enable the
59496636e97aSMatthew G Knepley   setting of ghost coordinates outside of the domain.
59506636e97aSMatthew G Knepley 
59512dd40e9bSPatrick Sanan   The vector c should be destroyed by the caller.
59522dd40e9bSPatrick Sanan 
59536636e97aSMatthew G Knepley   Level: intermediate
59546636e97aSMatthew G Knepley 
59556636e97aSMatthew G Knepley .seealso: DMGetCoordinatesLocal(), DMSetCoordinates(), DMGetCoordinates(), DMGetCoordinateDM()
59566636e97aSMatthew G Knepley @*/
59576636e97aSMatthew G Knepley PetscErrorCode DMSetCoordinatesLocal(DM dm, Vec c)
59586636e97aSMatthew G Knepley {
59596636e97aSMatthew G Knepley   PetscErrorCode ierr;
59606636e97aSMatthew G Knepley 
59616636e97aSMatthew G Knepley   PetscFunctionBegin;
59626636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
59636636e97aSMatthew G Knepley   PetscValidHeaderSpecific(c,VEC_CLASSID,2);
59646636e97aSMatthew G Knepley   ierr = PetscObjectReference((PetscObject) c);CHKERRQ(ierr);
59656636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinatesLocal);CHKERRQ(ierr);
59668865f1eaSKarl Rupp 
59676636e97aSMatthew G Knepley   dm->coordinatesLocal = c;
59688865f1eaSKarl Rupp 
59696636e97aSMatthew G Knepley   ierr = VecDestroy(&dm->coordinates);CHKERRQ(ierr);
59706636e97aSMatthew G Knepley   PetscFunctionReturn(0);
59716636e97aSMatthew G Knepley }
59726636e97aSMatthew G Knepley 
59736636e97aSMatthew G Knepley /*@
59746636e97aSMatthew G Knepley   DMGetCoordinates - Gets a global vector with the coordinates associated with the DM.
59756636e97aSMatthew G Knepley 
5976d083f849SBarry Smith   Collective on dm
59776636e97aSMatthew G Knepley 
59786636e97aSMatthew G Knepley   Input Parameter:
59796636e97aSMatthew G Knepley . dm - the DM
59806636e97aSMatthew G Knepley 
59816636e97aSMatthew G Knepley   Output Parameter:
59826636e97aSMatthew G Knepley . c - global coordinate vector
59836636e97aSMatthew G Knepley 
59846636e97aSMatthew G Knepley   Note:
59856636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
59866636e97aSMatthew G Knepley 
59876636e97aSMatthew G Knepley   Each process has only the local coordinates (does NOT have the ghost coordinates).
59886636e97aSMatthew G Knepley 
59896636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
59906636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
59916636e97aSMatthew G Knepley 
59926636e97aSMatthew G Knepley   Level: intermediate
59936636e97aSMatthew G Knepley 
59946636e97aSMatthew G Knepley .seealso: DMSetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM()
59956636e97aSMatthew G Knepley @*/
59966636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinates(DM dm, Vec *c)
59976636e97aSMatthew G Knepley {
59986636e97aSMatthew G Knepley   PetscErrorCode ierr;
59996636e97aSMatthew G Knepley 
60006636e97aSMatthew G Knepley   PetscFunctionBegin;
60016636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60026636e97aSMatthew G Knepley   PetscValidPointer(c,2);
60031f588964SMatthew G Knepley   if (!dm->coordinates && dm->coordinatesLocal) {
60040298fd71SBarry Smith     DM        cdm = NULL;
60051970a576SMatthew G. Knepley     PetscBool localized;
60066636e97aSMatthew G Knepley 
60076636e97aSMatthew G Knepley     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
60086636e97aSMatthew G Knepley     ierr = DMCreateGlobalVector(cdm, &dm->coordinates);CHKERRQ(ierr);
60091970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60101970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateGlobalVector() if coordinates are localized */
60111970a576SMatthew G. Knepley     if (localized) {
60121970a576SMatthew G. Knepley       PetscInt cdim;
60131970a576SMatthew G. Knepley 
60141970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60151970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60161970a576SMatthew G. Knepley     }
60176636e97aSMatthew G Knepley     ierr = PetscObjectSetName((PetscObject) dm->coordinates, "coordinates");CHKERRQ(ierr);
60186636e97aSMatthew G Knepley     ierr = DMLocalToGlobalBegin(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60196636e97aSMatthew G Knepley     ierr = DMLocalToGlobalEnd(cdm, dm->coordinatesLocal, INSERT_VALUES, dm->coordinates);CHKERRQ(ierr);
60206636e97aSMatthew G Knepley   }
60216636e97aSMatthew G Knepley   *c = dm->coordinates;
60226636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60236636e97aSMatthew G Knepley }
60246636e97aSMatthew G Knepley 
60256636e97aSMatthew G Knepley /*@
602681e9a530SVaclav Hapla   DMGetCoordinatesLocalSetUp - Prepares a local vector of coordinates, so that DMGetCoordinatesLocalNoncollective() can be used as non-collective afterwards.
602781e9a530SVaclav Hapla 
6028d083f849SBarry Smith   Collective on dm
602981e9a530SVaclav Hapla 
603081e9a530SVaclav Hapla   Input Parameter:
603181e9a530SVaclav Hapla . dm - the DM
603281e9a530SVaclav Hapla 
603381e9a530SVaclav Hapla   Level: advanced
603481e9a530SVaclav Hapla 
603581e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalNoncollective()
603681e9a530SVaclav Hapla @*/
603781e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalSetUp(DM dm)
603881e9a530SVaclav Hapla {
603981e9a530SVaclav Hapla   PetscErrorCode ierr;
604081e9a530SVaclav Hapla 
604181e9a530SVaclav Hapla   PetscFunctionBegin;
604281e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
604381e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) {
60441970a576SMatthew G. Knepley     DM        cdm = NULL;
60451970a576SMatthew G. Knepley     PetscBool localized;
60461970a576SMatthew G. Knepley 
604781e9a530SVaclav Hapla     ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
604881e9a530SVaclav Hapla     ierr = DMCreateLocalVector(cdm, &dm->coordinatesLocal);CHKERRQ(ierr);
60491970a576SMatthew G. Knepley     ierr = DMGetCoordinatesLocalized(dm, &localized);CHKERRQ(ierr);
60501970a576SMatthew G. Knepley     /* Block size is not correctly set by CreateLocalVector() if coordinates are localized */
60511970a576SMatthew G. Knepley     if (localized) {
60521970a576SMatthew G. Knepley       PetscInt cdim;
60531970a576SMatthew G. Knepley 
60541970a576SMatthew G. Knepley       ierr = DMGetCoordinateDim(dm, &cdim);CHKERRQ(ierr);
60551970a576SMatthew G. Knepley       ierr = VecSetBlockSize(dm->coordinates, cdim);CHKERRQ(ierr);
60561970a576SMatthew G. Knepley     }
605781e9a530SVaclav Hapla     ierr = PetscObjectSetName((PetscObject) dm->coordinatesLocal, "coordinates");CHKERRQ(ierr);
605881e9a530SVaclav Hapla     ierr = DMGlobalToLocalBegin(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
605981e9a530SVaclav Hapla     ierr = DMGlobalToLocalEnd(cdm, dm->coordinates, INSERT_VALUES, dm->coordinatesLocal);CHKERRQ(ierr);
606081e9a530SVaclav Hapla   }
606181e9a530SVaclav Hapla   PetscFunctionReturn(0);
606281e9a530SVaclav Hapla }
606381e9a530SVaclav Hapla 
606481e9a530SVaclav Hapla /*@
60656636e97aSMatthew G Knepley   DMGetCoordinatesLocal - Gets a local vector with the coordinates associated with the DM.
60666636e97aSMatthew G Knepley 
6067d083f849SBarry Smith   Collective on dm
60686636e97aSMatthew G Knepley 
60696636e97aSMatthew G Knepley   Input Parameter:
60706636e97aSMatthew G Knepley . dm - the DM
60716636e97aSMatthew G Knepley 
60726636e97aSMatthew G Knepley   Output Parameter:
60736636e97aSMatthew G Knepley . c - coordinate vector
60746636e97aSMatthew G Knepley 
60756636e97aSMatthew G Knepley   Note:
60766636e97aSMatthew G Knepley   This is a borrowed reference, so the user should NOT destroy this vector
60776636e97aSMatthew G Knepley 
60786636e97aSMatthew G Knepley   Each process has the local and ghost coordinates
60796636e97aSMatthew G Knepley 
60806636e97aSMatthew G Knepley   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
60816636e97aSMatthew G Knepley   and (x_0,y_0,z_0,x_1,y_1,z_1...)
60826636e97aSMatthew G Knepley 
60836636e97aSMatthew G Knepley   Level: intermediate
60846636e97aSMatthew G Knepley 
608581e9a530SVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM(), DMGetCoordinatesLocalNoncollective()
60866636e97aSMatthew G Knepley @*/
60876636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinatesLocal(DM dm, Vec *c)
60886636e97aSMatthew G Knepley {
60896636e97aSMatthew G Knepley   PetscErrorCode ierr;
60906636e97aSMatthew G Knepley 
60916636e97aSMatthew G Knepley   PetscFunctionBegin;
60926636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
60936636e97aSMatthew G Knepley   PetscValidPointer(c,2);
609481e9a530SVaclav Hapla   ierr = DMGetCoordinatesLocalSetUp(dm);CHKERRQ(ierr);
60956636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
60966636e97aSMatthew G Knepley   PetscFunctionReturn(0);
60976636e97aSMatthew G Knepley }
60986636e97aSMatthew G Knepley 
609981e9a530SVaclav Hapla /*@
610081e9a530SVaclav Hapla   DMGetCoordinatesLocalNoncollective - Non-collective version of DMGetCoordinatesLocal(). Fails if global coordinates have been set and DMGetCoordinatesLocalSetUp() not called.
610181e9a530SVaclav Hapla 
610281e9a530SVaclav Hapla   Not collective
610381e9a530SVaclav Hapla 
610481e9a530SVaclav Hapla   Input Parameter:
610581e9a530SVaclav Hapla . dm - the DM
610681e9a530SVaclav Hapla 
610781e9a530SVaclav Hapla   Output Parameter:
610881e9a530SVaclav Hapla . c - coordinate vector
610981e9a530SVaclav Hapla 
611081e9a530SVaclav Hapla   Level: advanced
611181e9a530SVaclav Hapla 
611281e9a530SVaclav Hapla .seealso: DMGetCoordinatesLocalSetUp(), DMGetCoordinatesLocal(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
611381e9a530SVaclav Hapla @*/
611481e9a530SVaclav Hapla PetscErrorCode DMGetCoordinatesLocalNoncollective(DM dm, Vec *c)
611581e9a530SVaclav Hapla {
611681e9a530SVaclav Hapla   PetscFunctionBegin;
611781e9a530SVaclav Hapla   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
611881e9a530SVaclav Hapla   PetscValidPointer(c,2);
611981e9a530SVaclav Hapla   if (!dm->coordinatesLocal && dm->coordinates) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called");
61206636e97aSMatthew G Knepley   *c = dm->coordinatesLocal;
61216636e97aSMatthew G Knepley   PetscFunctionReturn(0);
61226636e97aSMatthew G Knepley }
61236636e97aSMatthew G Knepley 
61242db98f8dSVaclav Hapla /*@
61252db98f8dSVaclav Hapla   DMGetCoordinatesLocalTuple - Gets a local vector with the coordinates of specified points and section describing its layout.
61262db98f8dSVaclav Hapla 
61272db98f8dSVaclav Hapla   Not collective
61282db98f8dSVaclav Hapla 
61292db98f8dSVaclav Hapla   Input Parameter:
61302db98f8dSVaclav Hapla + dm - the DM
61312db98f8dSVaclav Hapla - p - the IS of points whose coordinates will be returned
61322db98f8dSVaclav Hapla 
61332db98f8dSVaclav Hapla   Output Parameter:
61342db98f8dSVaclav Hapla + pCoordSection - the PetscSection describing the layout of pCoord, i.e. each point corresponds to one point in p, and DOFs correspond to coordinates
61352db98f8dSVaclav Hapla - pCoord - the Vec with coordinates of points in p
61362db98f8dSVaclav Hapla 
61372db98f8dSVaclav Hapla   Note:
61382db98f8dSVaclav Hapla   DMGetCoordinatesLocalSetUp() must be called first. This function employs DMGetCoordinatesLocalNoncollective() so it is not collective.
61392db98f8dSVaclav Hapla 
61402db98f8dSVaclav Hapla   This creates a new vector, so the user SHOULD destroy this vector
61412db98f8dSVaclav Hapla 
61422db98f8dSVaclav Hapla   Each process has the local and ghost coordinates
61432db98f8dSVaclav Hapla 
61442db98f8dSVaclav Hapla   For DMDA, in two and three dimensions coordinates are interlaced (x_0,y_0,x_1,y_1,...)
61452db98f8dSVaclav Hapla   and (x_0,y_0,z_0,x_1,y_1,z_1...)
61462db98f8dSVaclav Hapla 
61472db98f8dSVaclav Hapla   Level: advanced
61482db98f8dSVaclav Hapla 
61492db98f8dSVaclav Hapla .seealso: DMSetCoordinatesLocal(), DMGetCoordinatesLocal(), DMGetCoordinatesLocalNoncollective(), DMGetCoordinatesLocalSetUp(), DMGetCoordinates(), DMSetCoordinates(), DMGetCoordinateDM()
61502db98f8dSVaclav Hapla @*/
61512db98f8dSVaclav Hapla PetscErrorCode DMGetCoordinatesLocalTuple(DM dm, IS p, PetscSection *pCoordSection, Vec *pCoord)
61522db98f8dSVaclav Hapla {
61532db98f8dSVaclav Hapla   PetscSection        cs, newcs;
61542db98f8dSVaclav Hapla   Vec                 coords;
61552db98f8dSVaclav Hapla   const PetscScalar   *arr;
61562db98f8dSVaclav Hapla   PetscScalar         *newarr=NULL;
61572db98f8dSVaclav Hapla   PetscInt            n;
61582db98f8dSVaclav Hapla   PetscErrorCode      ierr;
61592db98f8dSVaclav Hapla 
61602db98f8dSVaclav Hapla   PetscFunctionBegin;
61612db98f8dSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
61622db98f8dSVaclav Hapla   PetscValidHeaderSpecific(p, IS_CLASSID, 2);
61632db98f8dSVaclav Hapla   if (pCoordSection) PetscValidPointer(pCoordSection, 3);
61642db98f8dSVaclav Hapla   if (pCoord) PetscValidPointer(pCoord, 4);
61652db98f8dSVaclav Hapla   if (!dm->coordinatesLocal) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DMGetCoordinatesLocalSetUp() has not been called or coordinates not set");
61661bb6d2a8SBarry Smith   if (!dm->coordinateDM || !dm->coordinateDM->localSection) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONGSTATE, "DM not supported");
61671bb6d2a8SBarry Smith   cs = dm->coordinateDM->localSection;
61682db98f8dSVaclav Hapla   coords = dm->coordinatesLocal;
61692db98f8dSVaclav Hapla   ierr = VecGetArrayRead(coords, &arr);CHKERRQ(ierr);
61702db98f8dSVaclav Hapla   ierr = PetscSectionExtractDofsFromArray(cs, MPIU_SCALAR, arr, p, &newcs, pCoord ? ((void**)&newarr) : NULL);CHKERRQ(ierr);
61712db98f8dSVaclav Hapla   ierr = VecRestoreArrayRead(coords, &arr);CHKERRQ(ierr);
61722db98f8dSVaclav Hapla   if (pCoord) {
61732db98f8dSVaclav Hapla     ierr = PetscSectionGetStorageSize(newcs, &n);CHKERRQ(ierr);
61742db98f8dSVaclav Hapla     /* set array in two steps to mimic PETSC_OWN_POINTER */
61752db98f8dSVaclav Hapla     ierr = VecCreateSeqWithArray(PetscObjectComm((PetscObject)p), 1, n, NULL, pCoord);CHKERRQ(ierr);
61762db98f8dSVaclav Hapla     ierr = VecReplaceArray(*pCoord, newarr);CHKERRQ(ierr);
6177ad9ac99dSVaclav Hapla   } else {
6178ad9ac99dSVaclav Hapla     ierr = PetscFree(newarr);CHKERRQ(ierr);
61792db98f8dSVaclav Hapla   }
6180ad9ac99dSVaclav Hapla   if (pCoordSection) {*pCoordSection = newcs;}
6181ad9ac99dSVaclav Hapla   else               {ierr = PetscSectionDestroy(&newcs);CHKERRQ(ierr);}
61822db98f8dSVaclav Hapla   PetscFunctionReturn(0);
61832db98f8dSVaclav Hapla }
61842db98f8dSVaclav Hapla 
6185f19dbd58SToby Isaac PetscErrorCode DMGetCoordinateField(DM dm, DMField *field)
6186f19dbd58SToby Isaac {
6187f19dbd58SToby Isaac   PetscErrorCode ierr;
6188f19dbd58SToby Isaac 
6189f19dbd58SToby Isaac   PetscFunctionBegin;
6190f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6191f19dbd58SToby Isaac   PetscValidPointer(field,2);
6192f19dbd58SToby Isaac   if (!dm->coordinateField) {
6193f19dbd58SToby Isaac     if (dm->ops->createcoordinatefield) {
6194f19dbd58SToby Isaac       ierr = (*dm->ops->createcoordinatefield)(dm,&dm->coordinateField);CHKERRQ(ierr);
6195f19dbd58SToby Isaac     }
6196f19dbd58SToby Isaac   }
6197f19dbd58SToby Isaac   *field = dm->coordinateField;
6198f19dbd58SToby Isaac   PetscFunctionReturn(0);
6199f19dbd58SToby Isaac }
6200f19dbd58SToby Isaac 
6201f19dbd58SToby Isaac PetscErrorCode DMSetCoordinateField(DM dm, DMField field)
6202f19dbd58SToby Isaac {
6203f19dbd58SToby Isaac   PetscErrorCode ierr;
6204f19dbd58SToby Isaac 
6205f19dbd58SToby Isaac   PetscFunctionBegin;
6206f19dbd58SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6207f19dbd58SToby Isaac   if (field) PetscValidHeaderSpecific(field,DMFIELD_CLASSID,2);
6208c4e6da2cSBarry Smith   ierr = PetscObjectReference((PetscObject)field);CHKERRQ(ierr);
6209f19dbd58SToby Isaac   ierr = DMFieldDestroy(&dm->coordinateField);CHKERRQ(ierr);
6210f19dbd58SToby Isaac   dm->coordinateField = field;
6211f19dbd58SToby Isaac   PetscFunctionReturn(0);
6212f19dbd58SToby Isaac }
6213f19dbd58SToby Isaac 
62146636e97aSMatthew G Knepley /*@
62151cfe2091SMatthew G. Knepley   DMGetCoordinateDM - Gets the DM that prescribes coordinate layout and scatters between global and local coordinates
62166636e97aSMatthew G Knepley 
6217d083f849SBarry Smith   Collective on dm
62186636e97aSMatthew G Knepley 
62196636e97aSMatthew G Knepley   Input Parameter:
62206636e97aSMatthew G Knepley . dm - the DM
62216636e97aSMatthew G Knepley 
62226636e97aSMatthew G Knepley   Output Parameter:
62236636e97aSMatthew G Knepley . cdm - coordinate DM
62246636e97aSMatthew G Knepley 
62256636e97aSMatthew G Knepley   Level: intermediate
62266636e97aSMatthew G Knepley 
62271cfe2091SMatthew G. Knepley .seealso: DMSetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62286636e97aSMatthew G Knepley @*/
62296636e97aSMatthew G Knepley PetscErrorCode DMGetCoordinateDM(DM dm, DM *cdm)
62306636e97aSMatthew G Knepley {
62316636e97aSMatthew G Knepley   PetscErrorCode ierr;
62326636e97aSMatthew G Knepley 
62336636e97aSMatthew G Knepley   PetscFunctionBegin;
62346636e97aSMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62356636e97aSMatthew G Knepley   PetscValidPointer(cdm,2);
62366636e97aSMatthew G Knepley   if (!dm->coordinateDM) {
6237308f8a94SToby Isaac     DM cdm;
6238308f8a94SToby Isaac 
623982f516ccSBarry Smith     if (!dm->ops->createcoordinatedm) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Unable to create coordinates for this DM");
6240308f8a94SToby Isaac     ierr = (*dm->ops->createcoordinatedm)(dm, &cdm);CHKERRQ(ierr);
6241308f8a94SToby Isaac     /* Just in case the DM sets the coordinate DM when creating it (DMP4est can do this, because it may not setup
6242308f8a94SToby Isaac      * until the call to CreateCoordinateDM) */
6243308f8a94SToby Isaac     ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
6244308f8a94SToby Isaac     dm->coordinateDM = cdm;
62456636e97aSMatthew G Knepley   }
62466636e97aSMatthew G Knepley   *cdm = dm->coordinateDM;
62476636e97aSMatthew G Knepley   PetscFunctionReturn(0);
62486636e97aSMatthew G Knepley }
6249e87bb0d3SMatthew G Knepley 
62501cfe2091SMatthew G. Knepley /*@
62511cfe2091SMatthew G. Knepley   DMSetCoordinateDM - Sets the DM that prescribes coordinate layout and scatters between global and local coordinates
62521cfe2091SMatthew G. Knepley 
6253d083f849SBarry Smith   Logically Collective on dm
62541cfe2091SMatthew G. Knepley 
62551cfe2091SMatthew G. Knepley   Input Parameters:
62561cfe2091SMatthew G. Knepley + dm - the DM
62571cfe2091SMatthew G. Knepley - cdm - coordinate DM
62581cfe2091SMatthew G. Knepley 
62591cfe2091SMatthew G. Knepley   Level: intermediate
62601cfe2091SMatthew G. Knepley 
62611cfe2091SMatthew G. Knepley .seealso: DMGetCoordinateDM(), DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal()
62621cfe2091SMatthew G. Knepley @*/
62631cfe2091SMatthew G. Knepley PetscErrorCode DMSetCoordinateDM(DM dm, DM cdm)
62641cfe2091SMatthew G. Knepley {
62651cfe2091SMatthew G. Knepley   PetscErrorCode ierr;
62661cfe2091SMatthew G. Knepley 
62671cfe2091SMatthew G. Knepley   PetscFunctionBegin;
62681cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
62691cfe2091SMatthew G. Knepley   PetscValidHeaderSpecific(cdm,DM_CLASSID,2);
6270f26b38b9SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
62711cfe2091SMatthew G. Knepley   ierr = DMDestroy(&dm->coordinateDM);CHKERRQ(ierr);
62721cfe2091SMatthew G. Knepley   dm->coordinateDM = cdm;
62731cfe2091SMatthew G. Knepley   PetscFunctionReturn(0);
62741cfe2091SMatthew G. Knepley }
62751cfe2091SMatthew G. Knepley 
627646e270d4SMatthew G. Knepley /*@
627746e270d4SMatthew G. Knepley   DMGetCoordinateDim - Retrieve the dimension of embedding space for coordinate values.
627846e270d4SMatthew G. Knepley 
627946e270d4SMatthew G. Knepley   Not Collective
628046e270d4SMatthew G. Knepley 
628146e270d4SMatthew G. Knepley   Input Parameter:
628246e270d4SMatthew G. Knepley . dm - The DM object
628346e270d4SMatthew G. Knepley 
628446e270d4SMatthew G. Knepley   Output Parameter:
628546e270d4SMatthew G. Knepley . dim - The embedding dimension
628646e270d4SMatthew G. Knepley 
628746e270d4SMatthew G. Knepley   Level: intermediate
628846e270d4SMatthew G. Knepley 
628992fd8e1eSJed Brown .seealso: DMSetCoordinateDim(), DMGetCoordinateSection(), DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
629046e270d4SMatthew G. Knepley @*/
629146e270d4SMatthew G. Knepley PetscErrorCode DMGetCoordinateDim(DM dm, PetscInt *dim)
629246e270d4SMatthew G. Knepley {
629346e270d4SMatthew G. Knepley   PetscFunctionBegin;
629446e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6295534a8f05SLisandro Dalcin   PetscValidIntPointer(dim, 2);
62969a9a41abSToby Isaac   if (dm->dimEmbed == PETSC_DEFAULT) {
62979a9a41abSToby Isaac     dm->dimEmbed = dm->dim;
62989a9a41abSToby Isaac   }
629946e270d4SMatthew G. Knepley   *dim = dm->dimEmbed;
630046e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
630146e270d4SMatthew G. Knepley }
630246e270d4SMatthew G. Knepley 
630346e270d4SMatthew G. Knepley /*@
630446e270d4SMatthew G. Knepley   DMSetCoordinateDim - Set the dimension of the embedding space for coordinate values.
630546e270d4SMatthew G. Knepley 
630646e270d4SMatthew G. Knepley   Not Collective
630746e270d4SMatthew G. Knepley 
630846e270d4SMatthew G. Knepley   Input Parameters:
630946e270d4SMatthew G. Knepley + dm  - The DM object
631046e270d4SMatthew G. Knepley - dim - The embedding dimension
631146e270d4SMatthew G. Knepley 
631246e270d4SMatthew G. Knepley   Level: intermediate
631346e270d4SMatthew G. Knepley 
631492fd8e1eSJed Brown .seealso: DMGetCoordinateDim(), DMSetCoordinateSection(), DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
631546e270d4SMatthew G. Knepley @*/
631646e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateDim(DM dm, PetscInt dim)
631746e270d4SMatthew G. Knepley {
6318e5e52638SMatthew G. Knepley   PetscDS        ds;
6319f17e8794SMatthew G. Knepley   PetscErrorCode ierr;
6320f17e8794SMatthew G. Knepley 
632146e270d4SMatthew G. Knepley   PetscFunctionBegin;
632246e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
632346e270d4SMatthew G. Knepley   dm->dimEmbed = dim;
6324e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
6325e5e52638SMatthew G. Knepley   ierr = PetscDSSetCoordinateDimension(ds, dim);CHKERRQ(ierr);
632646e270d4SMatthew G. Knepley   PetscFunctionReturn(0);
632746e270d4SMatthew G. Knepley }
632846e270d4SMatthew G. Knepley 
6329e8abe2deSMatthew G. Knepley /*@
6330e8abe2deSMatthew G. Knepley   DMGetCoordinateSection - Retrieve the layout of coordinate values over the mesh.
6331e8abe2deSMatthew G. Knepley 
6332d083f849SBarry Smith   Collective on dm
6333e8abe2deSMatthew G. Knepley 
6334e8abe2deSMatthew G. Knepley   Input Parameter:
6335e8abe2deSMatthew G. Knepley . dm - The DM object
6336e8abe2deSMatthew G. Knepley 
6337e8abe2deSMatthew G. Knepley   Output Parameter:
6338e8abe2deSMatthew G. Knepley . section - The PetscSection object
6339e8abe2deSMatthew G. Knepley 
6340e8abe2deSMatthew G. Knepley   Level: intermediate
6341e8abe2deSMatthew G. Knepley 
634292fd8e1eSJed Brown .seealso: DMGetCoordinateDM(), DMGetLocalSection(), DMSetLocalSection()
6343e8abe2deSMatthew G. Knepley @*/
6344e8abe2deSMatthew G. Knepley PetscErrorCode DMGetCoordinateSection(DM dm, PetscSection *section)
6345e8abe2deSMatthew G. Knepley {
6346e8abe2deSMatthew G. Knepley   DM             cdm;
6347e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6348e8abe2deSMatthew G. Knepley 
6349e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6350e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6351e8abe2deSMatthew G. Knepley   PetscValidPointer(section, 2);
6352e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
635392fd8e1eSJed Brown   ierr = DMGetLocalSection(cdm, section);CHKERRQ(ierr);
6354e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6355e8abe2deSMatthew G. Knepley }
6356e8abe2deSMatthew G. Knepley 
6357e8abe2deSMatthew G. Knepley /*@
6358e8abe2deSMatthew G. Knepley   DMSetCoordinateSection - Set the layout of coordinate values over the mesh.
6359e8abe2deSMatthew G. Knepley 
6360e8abe2deSMatthew G. Knepley   Not Collective
6361e8abe2deSMatthew G. Knepley 
6362e8abe2deSMatthew G. Knepley   Input Parameters:
6363e8abe2deSMatthew G. Knepley + dm      - The DM object
636446e270d4SMatthew G. Knepley . dim     - The embedding dimension, or PETSC_DETERMINE
6365e8abe2deSMatthew G. Knepley - section - The PetscSection object
6366e8abe2deSMatthew G. Knepley 
6367e8abe2deSMatthew G. Knepley   Level: intermediate
6368e8abe2deSMatthew G. Knepley 
636992fd8e1eSJed Brown .seealso: DMGetCoordinateSection(), DMGetLocalSection(), DMSetLocalSection()
6370e8abe2deSMatthew G. Knepley @*/
637146e270d4SMatthew G. Knepley PetscErrorCode DMSetCoordinateSection(DM dm, PetscInt dim, PetscSection section)
6372e8abe2deSMatthew G. Knepley {
6373e8abe2deSMatthew G. Knepley   DM             cdm;
6374e8abe2deSMatthew G. Knepley   PetscErrorCode ierr;
6375e8abe2deSMatthew G. Knepley 
6376e8abe2deSMatthew G. Knepley   PetscFunctionBegin;
6377e8abe2deSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
637846e270d4SMatthew G. Knepley   PetscValidHeaderSpecific(section,PETSC_SECTION_CLASSID,3);
6379e8abe2deSMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
638092fd8e1eSJed Brown   ierr = DMSetLocalSection(cdm, section);CHKERRQ(ierr);
638146e270d4SMatthew G. Knepley   if (dim == PETSC_DETERMINE) {
63824c1069a6SMatthew G. Knepley     PetscInt d = PETSC_DEFAULT;
638346e270d4SMatthew G. Knepley     PetscInt pStart, pEnd, vStart, vEnd, v, dd;
638446e270d4SMatthew G. Knepley 
638546e270d4SMatthew G. Knepley     ierr = PetscSectionGetChart(section, &pStart, &pEnd);CHKERRQ(ierr);
638646e270d4SMatthew G. Knepley     ierr = DMGetDimPoints(dm, 0, &vStart, &vEnd);CHKERRQ(ierr);
638746e270d4SMatthew G. Knepley     pStart = PetscMax(vStart, pStart);
638846e270d4SMatthew G. Knepley     pEnd   = PetscMin(vEnd, pEnd);
638946e270d4SMatthew G. Knepley     for (v = pStart; v < pEnd; ++v) {
639046e270d4SMatthew G. Knepley       ierr = PetscSectionGetDof(section, v, &dd);CHKERRQ(ierr);
639146e270d4SMatthew G. Knepley       if (dd) {d = dd; break;}
639246e270d4SMatthew G. Knepley     }
6393ebfe4b0dSMatthew G. Knepley     if (d >= 0) {ierr = DMSetCoordinateDim(dm, d);CHKERRQ(ierr);}
639446e270d4SMatthew G. Knepley   }
6395e8abe2deSMatthew G. Knepley   PetscFunctionReturn(0);
6396e8abe2deSMatthew G. Knepley }
6397e8abe2deSMatthew G. Knepley 
6398d864a3eaSLisandro Dalcin /*@
6399d864a3eaSLisandro Dalcin   DMProjectCoordinates - Project coordinates to a different space
6400d864a3eaSLisandro Dalcin 
6401d864a3eaSLisandro Dalcin   Input Parameters:
6402d864a3eaSLisandro Dalcin + dm      - The DM object
6403d864a3eaSLisandro Dalcin - disc    - The new coordinate discretization
6404d864a3eaSLisandro Dalcin 
6405d864a3eaSLisandro Dalcin   Level: intermediate
6406d864a3eaSLisandro Dalcin 
6407d864a3eaSLisandro Dalcin .seealso: DMGetCoordinateField()
6408d864a3eaSLisandro Dalcin @*/
6409d864a3eaSLisandro Dalcin PetscErrorCode DMProjectCoordinates(DM dm, PetscFE disc)
6410d864a3eaSLisandro Dalcin {
6411d864a3eaSLisandro Dalcin   PetscObject    discOld;
6412d864a3eaSLisandro Dalcin   PetscClassId   classid;
6413d864a3eaSLisandro Dalcin   DM             cdmOld,cdmNew;
6414d864a3eaSLisandro Dalcin   Vec            coordsOld,coordsNew;
6415d864a3eaSLisandro Dalcin   Mat            matInterp;
6416d864a3eaSLisandro Dalcin   PetscErrorCode ierr;
6417d864a3eaSLisandro Dalcin 
6418d864a3eaSLisandro Dalcin   PetscFunctionBegin;
6419d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6420d864a3eaSLisandro Dalcin   PetscValidHeaderSpecific(disc,PETSCFE_CLASSID,2);
6421d864a3eaSLisandro Dalcin 
6422d864a3eaSLisandro Dalcin   ierr = DMGetCoordinateDM(dm, &cdmOld);CHKERRQ(ierr);
6423d864a3eaSLisandro Dalcin   /* Check current discretization is compatible */
6424d864a3eaSLisandro Dalcin   ierr = DMGetField(cdmOld, 0, NULL, &discOld);CHKERRQ(ierr);
6425d864a3eaSLisandro Dalcin   ierr = PetscObjectGetClassId(discOld, &classid);CHKERRQ(ierr);
6426d864a3eaSLisandro Dalcin   if (classid != PETSCFE_CLASSID) SETERRQ(PetscObjectComm(discOld), PETSC_ERR_SUP, "Discretization type not supported");
6427d864a3eaSLisandro Dalcin   /* Make a fresh clone of the coordinate DM */
6428d864a3eaSLisandro Dalcin   ierr = DMClone(cdmOld, &cdmNew);CHKERRQ(ierr);
6429d864a3eaSLisandro Dalcin   ierr = DMSetField(cdmNew, 0, NULL, (PetscObject) disc);CHKERRQ(ierr);
6430d864a3eaSLisandro Dalcin   ierr = DMCreateDS(cdmNew);CHKERRQ(ierr);
6431d864a3eaSLisandro Dalcin   /* Project the coordinate vector from old to new space  */
6432d864a3eaSLisandro Dalcin   ierr = DMGetCoordinates(dm, &coordsOld);CHKERRQ(ierr);
6433d864a3eaSLisandro Dalcin   ierr = DMCreateGlobalVector(cdmNew, &coordsNew);CHKERRQ(ierr);
6434d864a3eaSLisandro Dalcin   ierr = DMCreateInterpolation(cdmOld, cdmNew, &matInterp, NULL);CHKERRQ(ierr);
6435d864a3eaSLisandro Dalcin   ierr = MatInterpolate(matInterp, coordsOld, coordsNew);CHKERRQ(ierr);
6436d864a3eaSLisandro Dalcin   ierr = MatDestroy(&matInterp);CHKERRQ(ierr);
6437d864a3eaSLisandro Dalcin   /* Set new coordinate structures */
6438d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateField(dm, NULL);CHKERRQ(ierr);
6439d864a3eaSLisandro Dalcin   ierr = DMSetCoordinateDM(dm, cdmNew);CHKERRQ(ierr);
6440d864a3eaSLisandro Dalcin   ierr = DMSetCoordinates(dm, coordsNew);CHKERRQ(ierr);
6441d864a3eaSLisandro Dalcin   ierr = VecDestroy(&coordsNew);CHKERRQ(ierr);
6442d864a3eaSLisandro Dalcin   ierr = DMDestroy(&cdmNew);CHKERRQ(ierr);
6443d864a3eaSLisandro Dalcin   PetscFunctionReturn(0);
6444d864a3eaSLisandro Dalcin }
6445d864a3eaSLisandro Dalcin 
64465dc8c3f7SMatthew G. Knepley /*@C
644790b157c4SStefano Zampini   DMGetPeriodicity - Get the description of mesh periodicity
64485dc8c3f7SMatthew G. Knepley 
64495dc8c3f7SMatthew G. Knepley   Input Parameters:
645090b157c4SStefano Zampini . dm      - The DM object
645190b157c4SStefano Zampini 
645290b157c4SStefano Zampini   Output Parameters:
645390b157c4SStefano Zampini + per     - Whether the DM is periodic or not
64545dc8c3f7SMatthew 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
64555dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64565dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64575dc8c3f7SMatthew G. Knepley 
64585dc8c3f7SMatthew G. Knepley   Level: developer
64595dc8c3f7SMatthew G. Knepley 
64605dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64615dc8c3f7SMatthew G. Knepley @*/
646290b157c4SStefano Zampini PetscErrorCode DMGetPeriodicity(DM dm, PetscBool *per, const PetscReal **maxCell, const PetscReal **L, const DMBoundaryType **bd)
6463c6b900c6SMatthew G. Knepley {
6464c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6465c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
646690b157c4SStefano Zampini   if (per)     *per     = dm->periodic;
6467c6b900c6SMatthew G. Knepley   if (L)       *L       = dm->L;
6468c6b900c6SMatthew G. Knepley   if (maxCell) *maxCell = dm->maxCell;
64695dc8c3f7SMatthew G. Knepley   if (bd)      *bd      = dm->bdtype;
6470c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6471c6b900c6SMatthew G. Knepley }
6472c6b900c6SMatthew G. Knepley 
64735dc8c3f7SMatthew G. Knepley /*@C
64745dc8c3f7SMatthew G. Knepley   DMSetPeriodicity - Set the description of mesh periodicity
64755dc8c3f7SMatthew G. Knepley 
64765dc8c3f7SMatthew G. Knepley   Input Parameters:
64775dc8c3f7SMatthew G. Knepley + dm      - The DM object
6478db2bf62eSStefano Zampini . per     - Whether the DM is periodic or not.
6479db2bf62eSStefano 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.
64805dc8c3f7SMatthew G. Knepley . L       - If we assume the mesh is a torus, this is the length of each coordinate
64815dc8c3f7SMatthew G. Knepley - bd      - This describes the type of periodicity in each topological dimension
64825dc8c3f7SMatthew G. Knepley 
6483db2bf62eSStefano 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.
6484db2bf62eSStefano Zampini 
64855dc8c3f7SMatthew G. Knepley   Level: developer
64865dc8c3f7SMatthew G. Knepley 
64875dc8c3f7SMatthew G. Knepley .seealso: DMGetPeriodicity()
64885dc8c3f7SMatthew G. Knepley @*/
648990b157c4SStefano Zampini PetscErrorCode DMSetPeriodicity(DM dm, PetscBool per, const PetscReal maxCell[], const PetscReal L[], const DMBoundaryType bd[])
6490c6b900c6SMatthew G. Knepley {
6491c6b900c6SMatthew G. Knepley   PetscInt       dim, d;
6492c6b900c6SMatthew G. Knepley   PetscErrorCode ierr;
6493c6b900c6SMatthew G. Knepley 
6494c6b900c6SMatthew G. Knepley   PetscFunctionBegin;
6495c6b900c6SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
649690b157c4SStefano Zampini   PetscValidLogicalCollectiveBool(dm,per,2);
6497412e9a14SMatthew G. Knepley   if (maxCell) {PetscValidRealPointer(maxCell,3);}
6498412e9a14SMatthew G. Knepley   if (L)       {PetscValidRealPointer(L,4);}
6499412e9a14SMatthew G. Knepley   if (bd)      {PetscValidPointer(bd,5);}
65005dc8c3f7SMatthew G. Knepley   ierr = DMGetDimension(dm, &dim);CHKERRQ(ierr);
650190b157c4SStefano Zampini   if (maxCell) {
6502412e9a14SMatthew G. Knepley     if (!dm->maxCell) {ierr = PetscMalloc1(dim, &dm->maxCell);CHKERRQ(ierr);}
6503412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->maxCell[d] = maxCell[d];
6504db2bf62eSStefano Zampini   } else { /* remove maxCell information to disable automatic computation of localized vertices */
6505db2bf62eSStefano Zampini     ierr = PetscFree(dm->maxCell);CHKERRQ(ierr);
6506412e9a14SMatthew G. Knepley   }
6507db2bf62eSStefano Zampini 
6508412e9a14SMatthew G. Knepley   if (L) {
6509412e9a14SMatthew G. Knepley     if (!dm->L) {ierr = PetscMalloc1(dim, &dm->L);CHKERRQ(ierr);}
6510412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->L[d] = L[d];
6511412e9a14SMatthew G. Knepley   }
6512412e9a14SMatthew G. Knepley   if (bd) {
6513412e9a14SMatthew G. Knepley     if (!dm->bdtype) {ierr = PetscMalloc1(dim, &dm->bdtype);CHKERRQ(ierr);}
6514412e9a14SMatthew G. Knepley     for (d = 0; d < dim; ++d) dm->bdtype[d] = bd[d];
651590b157c4SStefano Zampini   }
6516072d7d67SStefano Zampini   dm->periodic = per;
6517c6b900c6SMatthew G. Knepley   PetscFunctionReturn(0);
6518c6b900c6SMatthew G. Knepley }
6519c6b900c6SMatthew G. Knepley 
65202e17dfb7SMatthew G. Knepley /*@
65212e17dfb7SMatthew 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.
65222e17dfb7SMatthew G. Knepley 
65232e17dfb7SMatthew G. Knepley   Input Parameters:
65242e17dfb7SMatthew G. Knepley + dm     - The DM
652565da65dcSMatthew G. Knepley . in     - The input coordinate point (dim numbers)
652665da65dcSMatthew G. Knepley - endpoint - Include the endpoint L_i
65272e17dfb7SMatthew G. Knepley 
65282e17dfb7SMatthew G. Knepley   Output Parameter:
65292e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65302e17dfb7SMatthew G. Knepley 
65312e17dfb7SMatthew G. Knepley   Level: developer
65322e17dfb7SMatthew G. Knepley 
65332e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65342e17dfb7SMatthew G. Knepley @*/
653565da65dcSMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate(DM dm, const PetscScalar in[], PetscBool endpoint, PetscScalar out[])
65362e17dfb7SMatthew G. Knepley {
65372e17dfb7SMatthew G. Knepley   PetscInt       dim, d;
65382e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
65392e17dfb7SMatthew G. Knepley 
65402e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65412e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDim(dm, &dim);CHKERRQ(ierr);
65422e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65432e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65442e17dfb7SMatthew G. Knepley   } else {
654565da65dcSMatthew G. Knepley     if (endpoint) {
654665da65dcSMatthew G. Knepley       for (d = 0; d < dim; ++d) {
6547da3333bfSMatthew 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)) {
6548da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*(PetscFloorReal(PetscRealPart(in[d])/dm->L[d]) - 1);
654965da65dcSMatthew G. Knepley         } else {
6550da3333bfSMatthew G. Knepley           out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
655165da65dcSMatthew G. Knepley         }
655265da65dcSMatthew G. Knepley       }
655365da65dcSMatthew G. Knepley     } else {
65542e17dfb7SMatthew G. Knepley       for (d = 0; d < dim; ++d) {
65551118d4bcSLisandro Dalcin         out[d] = in[d] - dm->L[d]*PetscFloorReal(PetscRealPart(in[d])/dm->L[d]);
65562e17dfb7SMatthew G. Knepley       }
65572e17dfb7SMatthew G. Knepley     }
655865da65dcSMatthew G. Knepley   }
65592e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65602e17dfb7SMatthew G. Knepley }
65612e17dfb7SMatthew G. Knepley 
65622e17dfb7SMatthew G. Knepley /*
65632e17dfb7SMatthew 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.
65642e17dfb7SMatthew G. Knepley 
65652e17dfb7SMatthew G. Knepley   Input Parameters:
65662e17dfb7SMatthew G. Knepley + dm     - The DM
65672e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
65682e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
65692e17dfb7SMatthew G. Knepley - in     - The input coordinate point (dim numbers)
65702e17dfb7SMatthew G. Knepley 
65712e17dfb7SMatthew G. Knepley   Output Parameter:
65722e17dfb7SMatthew G. Knepley . out - The localized coordinate point
65732e17dfb7SMatthew G. Knepley 
65742e17dfb7SMatthew G. Knepley   Level: developer
65752e17dfb7SMatthew G. Knepley 
65762e17dfb7SMatthew 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
65772e17dfb7SMatthew G. Knepley 
65782e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeAddCoordinate()
65792e17dfb7SMatthew G. Knepley */
65802e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
65812e17dfb7SMatthew G. Knepley {
65822e17dfb7SMatthew G. Knepley   PetscInt d;
65832e17dfb7SMatthew G. Knepley 
65842e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
65852e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
65862e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
65872e17dfb7SMatthew G. Knepley   } else {
65882e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6589908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > dm->maxCell[d])) {
65902e17dfb7SMatthew G. Knepley         out[d] = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
65912e17dfb7SMatthew G. Knepley       } else {
65922e17dfb7SMatthew G. Knepley         out[d] = in[d];
65932e17dfb7SMatthew G. Knepley       }
65942e17dfb7SMatthew G. Knepley     }
65952e17dfb7SMatthew G. Knepley   }
65962e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
65972e17dfb7SMatthew G. Knepley }
6598a5801f52SStefano Zampini 
65992e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinateReal_Internal(DM dm, PetscInt dim, const PetscReal anchor[], const PetscReal in[], PetscReal out[])
66002e17dfb7SMatthew G. Knepley {
66012e17dfb7SMatthew G. Knepley   PetscInt d;
66022e17dfb7SMatthew G. Knepley 
66032e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66042e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66052e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] = in[d];
66062e17dfb7SMatthew G. Knepley   } else {
66072e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6608908eca10SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsReal(anchor[d] - in[d]) > dm->maxCell[d])) {
66092e17dfb7SMatthew G. Knepley         out[d] = anchor[d] > in[d] ? dm->L[d] + in[d] : in[d] - dm->L[d];
66102e17dfb7SMatthew G. Knepley       } else {
66112e17dfb7SMatthew G. Knepley         out[d] = in[d];
66122e17dfb7SMatthew G. Knepley       }
66132e17dfb7SMatthew G. Knepley     }
66142e17dfb7SMatthew G. Knepley   }
66152e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66162e17dfb7SMatthew G. Knepley }
66172e17dfb7SMatthew G. Knepley 
66182e17dfb7SMatthew G. Knepley /*
66192e17dfb7SMatthew 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.
66202e17dfb7SMatthew G. Knepley 
66212e17dfb7SMatthew G. Knepley   Input Parameters:
66222e17dfb7SMatthew G. Knepley + dm     - The DM
66232e17dfb7SMatthew G. Knepley . dim    - The spatial dimension
66242e17dfb7SMatthew G. Knepley . anchor - The anchor point, the input point can be no more than maxCell away from it
66252e17dfb7SMatthew G. Knepley . in     - The input coordinate delta (dim numbers)
66262e17dfb7SMatthew G. Knepley - out    - The input coordinate point (dim numbers)
66272e17dfb7SMatthew G. Knepley 
66282e17dfb7SMatthew G. Knepley   Output Parameter:
66292e17dfb7SMatthew G. Knepley . out    - The localized coordinate in + out
66302e17dfb7SMatthew G. Knepley 
66312e17dfb7SMatthew G. Knepley   Level: developer
66322e17dfb7SMatthew G. Knepley 
66332e17dfb7SMatthew 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
66342e17dfb7SMatthew G. Knepley 
66352e17dfb7SMatthew G. Knepley .seealso: DMLocalizeCoordinates(), DMLocalizeCoordinate()
66362e17dfb7SMatthew G. Knepley */
66372e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeAddCoordinate_Internal(DM dm, PetscInt dim, const PetscScalar anchor[], const PetscScalar in[], PetscScalar out[])
66382e17dfb7SMatthew G. Knepley {
66392e17dfb7SMatthew G. Knepley   PetscInt d;
66402e17dfb7SMatthew G. Knepley 
66412e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
66422e17dfb7SMatthew G. Knepley   if (!dm->maxCell) {
66432e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) out[d] += in[d];
66442e17dfb7SMatthew G. Knepley   } else {
66452e17dfb7SMatthew G. Knepley     for (d = 0; d < dim; ++d) {
6646412e9a14SMatthew G. Knepley       const PetscReal maxC = dm->maxCell[d];
6647412e9a14SMatthew G. Knepley 
6648412e9a14SMatthew G. Knepley       if ((dm->bdtype[d] != DM_BOUNDARY_NONE) && (PetscAbsScalar(anchor[d] - in[d]) > maxC)) {
6649412e9a14SMatthew G. Knepley         const PetscScalar newCoord = PetscRealPart(anchor[d]) > PetscRealPart(in[d]) ? dm->L[d] + in[d] : in[d] - dm->L[d];
6650412e9a14SMatthew G. Knepley 
6651412e9a14SMatthew G. Knepley         if (PetscAbsScalar(newCoord - anchor[d]) > maxC)
6652412e9a14SMatthew 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]));
6653412e9a14SMatthew G. Knepley         out[d] += newCoord;
66542e17dfb7SMatthew G. Knepley       } else {
66552e17dfb7SMatthew G. Knepley         out[d] += in[d];
66562e17dfb7SMatthew G. Knepley       }
66572e17dfb7SMatthew G. Knepley     }
66582e17dfb7SMatthew G. Knepley   }
66592e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
66602e17dfb7SMatthew G. Knepley }
66612e17dfb7SMatthew G. Knepley 
666236447a5eSToby Isaac /*@
66638f700142SStefano Zampini   DMGetCoordinatesLocalizedLocal - Check if the DM coordinates have been localized for cells on this process
66648f700142SStefano Zampini 
66658f700142SStefano Zampini   Not collective
666636447a5eSToby Isaac 
666736447a5eSToby Isaac   Input Parameter:
666836447a5eSToby Isaac . dm - The DM
666936447a5eSToby Isaac 
667036447a5eSToby Isaac   Output Parameter:
667136447a5eSToby Isaac   areLocalized - True if localized
667236447a5eSToby Isaac 
667336447a5eSToby Isaac   Level: developer
667436447a5eSToby Isaac 
66758f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMGetCoordinatesLocalized(), DMSetPeriodicity()
667636447a5eSToby Isaac @*/
66778f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalizedLocal(DM dm,PetscBool *areLocalized)
667836447a5eSToby Isaac {
667936447a5eSToby Isaac   DM             cdm;
668036447a5eSToby Isaac   PetscSection   coordSection;
668146a3a80fSLisandro Dalcin   PetscInt       cStart, cEnd, sStart, sEnd, c, dof;
668246a3a80fSLisandro Dalcin   PetscBool      isPlex, alreadyLocalized;
668336447a5eSToby Isaac   PetscErrorCode ierr;
668436447a5eSToby Isaac 
668536447a5eSToby Isaac   PetscFunctionBegin;
668636447a5eSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6687534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
66888b09590cSToby Isaac   *areLocalized = PETSC_FALSE;
668946a3a80fSLisandro Dalcin 
669036447a5eSToby Isaac   /* We need some generic way of refering to cells/vertices */
669136447a5eSToby Isaac   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
669246a3a80fSLisandro Dalcin   ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isPlex);CHKERRQ(ierr);
66939f7230bfSMatthew G. Knepley   if (!isPlex) PetscFunctionReturn(0);
669446a3a80fSLisandro Dalcin 
66959f7230bfSMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
669646a3a80fSLisandro Dalcin   ierr = DMPlexGetHeightStratum(cdm, 0, &cStart, &cEnd);CHKERRQ(ierr);
669736447a5eSToby Isaac   ierr = PetscSectionGetChart(coordSection, &sStart, &sEnd);CHKERRQ(ierr);
669836447a5eSToby Isaac   alreadyLocalized = PETSC_FALSE;
669946a3a80fSLisandro Dalcin   for (c = cStart; c < cEnd; ++c) {
670046a3a80fSLisandro Dalcin     if (c < sStart || c >= sEnd) continue;
670136447a5eSToby Isaac     ierr = PetscSectionGetDof(coordSection, c, &dof);CHKERRQ(ierr);
670246a3a80fSLisandro Dalcin     if (dof) { alreadyLocalized = PETSC_TRUE; break; }
670336447a5eSToby Isaac   }
67048f700142SStefano Zampini   *areLocalized = alreadyLocalized;
670536447a5eSToby Isaac   PetscFunctionReturn(0);
670636447a5eSToby Isaac }
670736447a5eSToby Isaac 
67088f700142SStefano Zampini /*@
67098f700142SStefano Zampini   DMGetCoordinatesLocalized - Check if the DM coordinates have been localized for cells
67108f700142SStefano Zampini 
67118f700142SStefano Zampini   Collective on dm
67128f700142SStefano Zampini 
67138f700142SStefano Zampini   Input Parameter:
67148f700142SStefano Zampini . dm - The DM
67158f700142SStefano Zampini 
67168f700142SStefano Zampini   Output Parameter:
67178f700142SStefano Zampini   areLocalized - True if localized
67188f700142SStefano Zampini 
67198f700142SStefano Zampini   Level: developer
67208f700142SStefano Zampini 
67218f700142SStefano Zampini .seealso: DMLocalizeCoordinates(), DMSetPeriodicity(), DMGetCoordinatesLocalizedLocal()
67228f700142SStefano Zampini @*/
67238f700142SStefano Zampini PetscErrorCode DMGetCoordinatesLocalized(DM dm,PetscBool *areLocalized)
67248f700142SStefano Zampini {
67258f700142SStefano Zampini   PetscBool      localized;
67268f700142SStefano Zampini   PetscErrorCode ierr;
67278f700142SStefano Zampini 
67288f700142SStefano Zampini   PetscFunctionBegin;
67298f700142SStefano Zampini   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
6730534a8f05SLisandro Dalcin   PetscValidBoolPointer(areLocalized, 2);
67318f700142SStefano Zampini   ierr = DMGetCoordinatesLocalizedLocal(dm,&localized);CHKERRQ(ierr);
67328f700142SStefano Zampini   ierr = MPIU_Allreduce(&localized,areLocalized,1,MPIU_BOOL,MPI_LOR,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
67338f700142SStefano Zampini   PetscFunctionReturn(0);
67348f700142SStefano Zampini }
673536447a5eSToby Isaac 
67362e17dfb7SMatthew G. Knepley /*@
6737492b8470SStefano Zampini   DMLocalizeCoordinates - If a mesh is periodic, create local coordinates for cells having periodic faces
67382e17dfb7SMatthew G. Knepley 
67398f700142SStefano Zampini   Collective on dm
67408f700142SStefano Zampini 
67412e17dfb7SMatthew G. Knepley   Input Parameter:
67422e17dfb7SMatthew G. Knepley . dm - The DM
67432e17dfb7SMatthew G. Knepley 
67442e17dfb7SMatthew G. Knepley   Level: developer
67452e17dfb7SMatthew G. Knepley 
67468f700142SStefano Zampini .seealso: DMSetPeriodicity(), DMLocalizeCoordinate(), DMLocalizeAddCoordinate()
67472e17dfb7SMatthew G. Knepley @*/
67482e17dfb7SMatthew G. Knepley PetscErrorCode DMLocalizeCoordinates(DM dm)
67492e17dfb7SMatthew G. Knepley {
67502e17dfb7SMatthew G. Knepley   DM             cdm;
67512e17dfb7SMatthew G. Knepley   PetscSection   coordSection, cSection;
67522e17dfb7SMatthew G. Knepley   Vec            coordinates,  cVec;
67533e922f36SToby Isaac   PetscScalar   *coords, *coords2, *anchor, *localized;
67543e922f36SToby Isaac   PetscInt       Nc, vStart, vEnd, v, sStart, sEnd, newStart = PETSC_MAX_INT, newEnd = PETSC_MIN_INT, dof, d, off, off2, bs, coordSize;
6755e0ae35bbSToby Isaac   PetscBool      alreadyLocalized, alreadyLocalizedGlobal;
67563e922f36SToby Isaac   PetscInt       maxHeight = 0, h;
67573e922f36SToby Isaac   PetscInt       *pStart = NULL, *pEnd = NULL;
67582e17dfb7SMatthew G. Knepley   PetscErrorCode ierr;
67592e17dfb7SMatthew G. Knepley 
67602e17dfb7SMatthew G. Knepley   PetscFunctionBegin;
67612e17dfb7SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
676292c9c85fSStefano Zampini   if (!dm->periodic) PetscFunctionReturn(0);
6763f7cbd40bSStefano Zampini   ierr = DMGetCoordinatesLocalized(dm, &alreadyLocalized);CHKERRQ(ierr);
6764f7cbd40bSStefano Zampini   if (alreadyLocalized) PetscFunctionReturn(0);
6765f7cbd40bSStefano Zampini 
67662e17dfb7SMatthew G. Knepley   /* We need some generic way of refering to cells/vertices */
67672e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateDM(dm, &cdm);CHKERRQ(ierr);
67682e17dfb7SMatthew G. Knepley   {
67692e17dfb7SMatthew G. Knepley     PetscBool isplex;
67702e17dfb7SMatthew G. Knepley 
67712e17dfb7SMatthew G. Knepley     ierr = PetscObjectTypeCompare((PetscObject) cdm, DMPLEX, &isplex);CHKERRQ(ierr);
67722e17dfb7SMatthew G. Knepley     if (isplex) {
67732e17dfb7SMatthew G. Knepley       ierr = DMPlexGetDepthStratum(cdm, 0, &vStart, &vEnd);CHKERRQ(ierr);
67743e922f36SToby Isaac       ierr = DMPlexGetMaxProjectionHeight(cdm,&maxHeight);CHKERRQ(ierr);
677569291d52SBarry Smith       ierr = DMGetWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
67763e922f36SToby Isaac       pEnd = &pStart[maxHeight + 1];
67773e922f36SToby Isaac       newStart = vStart;
67783e922f36SToby Isaac       newEnd   = vEnd;
67793e922f36SToby Isaac       for (h = 0; h <= maxHeight; h++) {
67803e922f36SToby Isaac         ierr = DMPlexGetHeightStratum(cdm, h, &pStart[h], &pEnd[h]);CHKERRQ(ierr);
67813e922f36SToby Isaac         newStart = PetscMin(newStart,pStart[h]);
67823e922f36SToby Isaac         newEnd   = PetscMax(newEnd,pEnd[h]);
67833e922f36SToby Isaac       }
67842e17dfb7SMatthew G. Knepley     } else SETERRQ(PetscObjectComm((PetscObject) cdm), PETSC_ERR_ARG_WRONG, "Coordinate localization requires a DMPLEX coordinate DM");
67852e17dfb7SMatthew G. Knepley   }
67862e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinatesLocal(dm, &coordinates);CHKERRQ(ierr);
678743eeeb2dSStefano Zampini   if (!coordinates) SETERRQ(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"Missing local coordinates vector");
67882e17dfb7SMatthew G. Knepley   ierr = DMGetCoordinateSection(dm, &coordSection);CHKERRQ(ierr);
67893e922f36SToby Isaac   ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr);
6790e0ae35bbSToby Isaac   ierr = PetscSectionGetChart(coordSection,&sStart,&sEnd);CHKERRQ(ierr);
67913e922f36SToby Isaac 
67922e17dfb7SMatthew G. Knepley   ierr = PetscSectionCreate(PetscObjectComm((PetscObject) dm), &cSection);CHKERRQ(ierr);
67932e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetNumFields(cSection, 1);CHKERRQ(ierr);
67942e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetFieldComponents(coordSection, 0, &Nc);CHKERRQ(ierr);
67952e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetFieldComponents(cSection, 0, Nc);CHKERRQ(ierr);
67963e922f36SToby Isaac   ierr = PetscSectionSetChart(cSection, newStart, newEnd);CHKERRQ(ierr);
67973e922f36SToby Isaac 
679869291d52SBarry Smith   ierr = DMGetWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
67993e922f36SToby Isaac   localized = &anchor[bs];
68003e922f36SToby Isaac   alreadyLocalized = alreadyLocalizedGlobal = PETSC_TRUE;
68013e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68023e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68033e922f36SToby Isaac 
68043e922f36SToby Isaac     for (c = cStart; c < cEnd; ++c) {
68053e922f36SToby Isaac       PetscScalar *cellCoords = NULL;
68063e922f36SToby Isaac       PetscInt     b;
68073e922f36SToby Isaac 
68083e922f36SToby Isaac       if (c < sStart || c >= sEnd) alreadyLocalized = PETSC_FALSE;
68093e922f36SToby Isaac       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68103e922f36SToby Isaac       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68113e922f36SToby Isaac       for (d = 0; d < dof/bs; ++d) {
68123e922f36SToby Isaac         ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], localized);CHKERRQ(ierr);
68133e922f36SToby Isaac         for (b = 0; b < bs; b++) {
68143e922f36SToby Isaac           if (cellCoords[d*bs + b] != localized[b]) break;
68153e922f36SToby Isaac         }
68163e922f36SToby Isaac         if (b < bs) break;
68173e922f36SToby Isaac       }
68183e922f36SToby Isaac       if (d < dof/bs) {
68193e922f36SToby Isaac         if (c >= sStart && c < sEnd) {
68203e922f36SToby Isaac           PetscInt cdof;
68213e922f36SToby Isaac 
68223e922f36SToby Isaac           ierr = PetscSectionGetDof(coordSection, c, &cdof);CHKERRQ(ierr);
68233e922f36SToby Isaac           if (cdof != dof) alreadyLocalized = PETSC_FALSE;
68243e922f36SToby Isaac         }
68253e922f36SToby Isaac         ierr = PetscSectionSetDof(cSection, c, dof);CHKERRQ(ierr);
68263e922f36SToby Isaac         ierr = PetscSectionSetFieldDof(cSection, c, 0, dof);CHKERRQ(ierr);
68273e922f36SToby Isaac       }
68283e922f36SToby Isaac       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68293e922f36SToby Isaac     }
68303e922f36SToby Isaac   }
68313e922f36SToby Isaac   ierr = MPI_Allreduce(&alreadyLocalized,&alreadyLocalizedGlobal,1,MPIU_BOOL,MPI_LAND,PetscObjectComm((PetscObject)dm));CHKERRQ(ierr);
68323e922f36SToby Isaac   if (alreadyLocalizedGlobal) {
683369291d52SBarry Smith     ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
68343e922f36SToby Isaac     ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
683569291d52SBarry Smith     ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
68363e922f36SToby Isaac     PetscFunctionReturn(0);
68373e922f36SToby Isaac   }
68382e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68392e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68402e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetDof(cSection, v, dof);CHKERRQ(ierr);
68412e17dfb7SMatthew G. Knepley     ierr = PetscSectionSetFieldDof(cSection, v, 0, dof);CHKERRQ(ierr);
68422e17dfb7SMatthew G. Knepley   }
68432e17dfb7SMatthew G. Knepley   ierr = PetscSectionSetUp(cSection);CHKERRQ(ierr);
68442e17dfb7SMatthew G. Knepley   ierr = PetscSectionGetStorageSize(cSection, &coordSize);CHKERRQ(ierr);
6845c2be7e5eSLisandro Dalcin   ierr = VecCreate(PETSC_COMM_SELF, &cVec);CHKERRQ(ierr);
68462e17dfb7SMatthew G. Knepley   ierr = PetscObjectSetName((PetscObject)cVec,"coordinates");CHKERRQ(ierr);
68472e17dfb7SMatthew G. Knepley   ierr = VecSetBlockSize(cVec, bs);CHKERRQ(ierr);
68482e17dfb7SMatthew G. Knepley   ierr = VecSetSizes(cVec, coordSize, PETSC_DETERMINE);CHKERRQ(ierr);
68492e17dfb7SMatthew G. Knepley   ierr = VecSetType(cVec, VECSTANDARD);CHKERRQ(ierr);
6850c2be7e5eSLisandro Dalcin   ierr = VecGetArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68512e17dfb7SMatthew G. Knepley   ierr = VecGetArray(cVec, &coords2);CHKERRQ(ierr);
68522e17dfb7SMatthew G. Knepley   for (v = vStart; v < vEnd; ++v) {
68532e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetDof(coordSection, v, &dof);CHKERRQ(ierr);
68542e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(coordSection, v, &off);CHKERRQ(ierr);
68552e17dfb7SMatthew G. Knepley     ierr = PetscSectionGetOffset(cSection,     v, &off2);CHKERRQ(ierr);
68562e17dfb7SMatthew G. Knepley     for (d = 0; d < dof; ++d) coords2[off2+d] = coords[off+d];
68572e17dfb7SMatthew G. Knepley   }
68583e922f36SToby Isaac   for (h = 0; h <= maxHeight; h++) {
68593e922f36SToby Isaac     PetscInt cStart = pStart[h], cEnd = pEnd[h], c;
68603e922f36SToby Isaac 
68612e17dfb7SMatthew G. Knepley     for (c = cStart; c < cEnd; ++c) {
68622e17dfb7SMatthew G. Knepley       PetscScalar *cellCoords = NULL;
68633e922f36SToby Isaac       PetscInt     b, cdof;
68642e17dfb7SMatthew G. Knepley 
68653e922f36SToby Isaac       ierr = PetscSectionGetDof(cSection,c,&cdof);CHKERRQ(ierr);
68663e922f36SToby Isaac       if (!cdof) continue;
68672e17dfb7SMatthew G. Knepley       ierr = DMPlexVecGetClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68682e17dfb7SMatthew G. Knepley       ierr = PetscSectionGetOffset(cSection, c, &off2);CHKERRQ(ierr);
68692e17dfb7SMatthew G. Knepley       for (b = 0; b < bs; ++b) anchor[b] = cellCoords[b];
68702e17dfb7SMatthew G. Knepley       for (d = 0; d < dof/bs; ++d) {ierr = DMLocalizeCoordinate_Internal(dm, bs, anchor, &cellCoords[d*bs], &coords2[off2+d*bs]);CHKERRQ(ierr);}
68712e17dfb7SMatthew G. Knepley       ierr = DMPlexVecRestoreClosure(cdm, coordSection, coordinates, c, &dof, &cellCoords);CHKERRQ(ierr);
68722e17dfb7SMatthew G. Knepley     }
68733e922f36SToby Isaac   }
687469291d52SBarry Smith   ierr = DMRestoreWorkArray(dm, 2 * bs, MPIU_SCALAR, &anchor);CHKERRQ(ierr);
687569291d52SBarry Smith   ierr = DMRestoreWorkArray(dm,2*(maxHeight + 1),MPIU_INT,&pStart);CHKERRQ(ierr);
6876c2be7e5eSLisandro Dalcin   ierr = VecRestoreArrayRead(coordinates, (const PetscScalar**)&coords);CHKERRQ(ierr);
68772e17dfb7SMatthew G. Knepley   ierr = VecRestoreArray(cVec, &coords2);CHKERRQ(ierr);
68782e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinateSection(dm, PETSC_DETERMINE, cSection);CHKERRQ(ierr);
68792e17dfb7SMatthew G. Knepley   ierr = DMSetCoordinatesLocal(dm, cVec);CHKERRQ(ierr);
68802e17dfb7SMatthew G. Knepley   ierr = VecDestroy(&cVec);CHKERRQ(ierr);
68812e17dfb7SMatthew G. Knepley   ierr = PetscSectionDestroy(&cSection);CHKERRQ(ierr);
68822e17dfb7SMatthew G. Knepley   PetscFunctionReturn(0);
68832e17dfb7SMatthew G. Knepley }
68842e17dfb7SMatthew G. Knepley 
6885e87bb0d3SMatthew G Knepley /*@
68863a93e3b7SToby Isaac   DMLocatePoints - Locate the points in v in the mesh and return a PetscSF of the containing cells
6887e87bb0d3SMatthew G Knepley 
6888d083f849SBarry Smith   Collective on v (see explanation below)
6889e87bb0d3SMatthew G Knepley 
6890e87bb0d3SMatthew G Knepley   Input Parameters:
6891e87bb0d3SMatthew G Knepley + dm - The DM
68923a93e3b7SToby Isaac . v - The Vec of points
689362a38674SMatthew G. Knepley . ltype - The type of point location, e.g. DM_POINTLOCATION_NONE or DM_POINTLOCATION_NEAREST
68943a93e3b7SToby Isaac - cells - Points to either NULL, or a PetscSF with guesses for which cells contain each point.
6895e87bb0d3SMatthew G Knepley 
689661e3bb9bSMatthew G Knepley   Output Parameter:
689762a38674SMatthew G. Knepley + v - The Vec of points, which now contains the nearest mesh points to the given points if DM_POINTLOCATION_NEAREST is used
689862a38674SMatthew G. Knepley - cells - The PetscSF containing the ranks and local indices of the containing points.
68993a93e3b7SToby Isaac 
6900e87bb0d3SMatthew G Knepley 
6901e87bb0d3SMatthew G Knepley   Level: developer
690261e3bb9bSMatthew G Knepley 
690362a38674SMatthew G. Knepley   Notes:
69043a93e3b7SToby Isaac   To do a search of the local cells of the mesh, v should have PETSC_COMM_SELF as its communicator.
690562a38674SMatthew G. Knepley   To do a search of all the cells in the distributed mesh, v should have the same communicator as dm.
69063a93e3b7SToby Isaac 
69073a93e3b7SToby Isaac   If *cellSF is NULL on input, a PetscSF will be created.
690862a38674SMatthew G. Knepley   If *cellSF is not NULL on input, it should point to an existing PetscSF, whose graph will be used as initial guesses.
69093a93e3b7SToby Isaac 
69103a93e3b7SToby Isaac   An array that maps each point to its containing cell can be obtained with
69113a93e3b7SToby Isaac 
691262a38674SMatthew G. Knepley $    const PetscSFNode *cells;
691362a38674SMatthew G. Knepley $    PetscInt           nFound;
6914a6216909SToby Isaac $    const PetscInt    *found;
691562a38674SMatthew G. Knepley $
6916a6216909SToby Isaac $    PetscSFGetGraph(cellSF,NULL,&nFound,&found,&cells);
69173a93e3b7SToby Isaac 
69183a93e3b7SToby 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
69193a93e3b7SToby Isaac   the index of the cell in its rank's local numbering.
69203a93e3b7SToby Isaac 
692162a38674SMatthew G. Knepley .seealso: DMSetCoordinates(), DMSetCoordinatesLocal(), DMGetCoordinates(), DMGetCoordinatesLocal(), DMPointLocationType
692261e3bb9bSMatthew G Knepley @*/
692362a38674SMatthew G. Knepley PetscErrorCode DMLocatePoints(DM dm, Vec v, DMPointLocationType ltype, PetscSF *cellSF)
6924e87bb0d3SMatthew G Knepley {
6925735aa83eSMatthew G Knepley   PetscErrorCode ierr;
6926735aa83eSMatthew G Knepley 
6927e87bb0d3SMatthew G Knepley   PetscFunctionBegin;
6928e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
6929e87bb0d3SMatthew G Knepley   PetscValidHeaderSpecific(v,VEC_CLASSID,2);
6930e0fc9d1bSMatthew G. Knepley   PetscValidPointer(cellSF,4);
69313a93e3b7SToby Isaac   if (*cellSF) {
69323a93e3b7SToby Isaac     PetscMPIInt result;
69333a93e3b7SToby Isaac 
6934e0fc9d1bSMatthew G. Knepley     PetscValidHeaderSpecific(*cellSF,PETSCSF_CLASSID,4);
6935a4f09dd6SDave May     ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)v),PetscObjectComm((PetscObject)*cellSF),&result);CHKERRQ(ierr);
69363a93e3b7SToby 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");
6937e0fc9d1bSMatthew G. Knepley   } else {
69383a93e3b7SToby Isaac     ierr = PetscSFCreate(PetscObjectComm((PetscObject)v),cellSF);CHKERRQ(ierr);
69393a93e3b7SToby Isaac   }
6940b9d85ea2SLisandro Dalcin   if (!dm->ops->locatepoints) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_SUP, "Point location not available for this DM");
694147a35634SPatrick Farrell   ierr = PetscLogEventBegin(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
694262a38674SMatthew G. Knepley   ierr = (*dm->ops->locatepoints)(dm,v,ltype,*cellSF);CHKERRQ(ierr);
694347a35634SPatrick Farrell   ierr = PetscLogEventEnd(DM_LocatePoints,dm,0,0,0);CHKERRQ(ierr);
6944e87bb0d3SMatthew G Knepley   PetscFunctionReturn(0);
6945e87bb0d3SMatthew G Knepley }
694614f150ffSMatthew G. Knepley 
6947f4d763aaSMatthew G. Knepley /*@
6948f4d763aaSMatthew G. Knepley   DMGetOutputDM - Retrieve the DM associated with the layout for output
6949f4d763aaSMatthew G. Knepley 
69508f700142SStefano Zampini   Collective on dm
69518f700142SStefano Zampini 
6952f4d763aaSMatthew G. Knepley   Input Parameter:
6953f4d763aaSMatthew G. Knepley . dm - The original DM
6954f4d763aaSMatthew G. Knepley 
6955f4d763aaSMatthew G. Knepley   Output Parameter:
6956f4d763aaSMatthew G. Knepley . odm - The DM which provides the layout for output
6957f4d763aaSMatthew G. Knepley 
6958f4d763aaSMatthew G. Knepley   Level: intermediate
6959f4d763aaSMatthew G. Knepley 
6960e87a4003SBarry Smith .seealso: VecView(), DMGetGlobalSection()
6961f4d763aaSMatthew G. Knepley @*/
696214f150ffSMatthew G. Knepley PetscErrorCode DMGetOutputDM(DM dm, DM *odm)
696314f150ffSMatthew G. Knepley {
6964c26acbdeSMatthew G. Knepley   PetscSection   section;
69652d4e4a49SMatthew G. Knepley   PetscBool      hasConstraints, ghasConstraints;
696614f150ffSMatthew G. Knepley   PetscErrorCode ierr;
696714f150ffSMatthew G. Knepley 
696814f150ffSMatthew G. Knepley   PetscFunctionBegin;
696914f150ffSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
697014f150ffSMatthew G. Knepley   PetscValidPointer(odm,2);
697192fd8e1eSJed Brown   ierr = DMGetLocalSection(dm, &section);CHKERRQ(ierr);
6972c26acbdeSMatthew G. Knepley   ierr = PetscSectionHasConstraints(section, &hasConstraints);CHKERRQ(ierr);
6973127fe6b9SMatthew G. Knepley   ierr = MPI_Allreduce(&hasConstraints, &ghasConstraints, 1, MPIU_BOOL, MPI_LOR, PetscObjectComm((PetscObject) dm));CHKERRQ(ierr);
69742d4e4a49SMatthew G. Knepley   if (!ghasConstraints) {
6975c26acbdeSMatthew G. Knepley     *odm = dm;
6976c26acbdeSMatthew G. Knepley     PetscFunctionReturn(0);
6977c26acbdeSMatthew G. Knepley   }
697814f150ffSMatthew G. Knepley   if (!dm->dmBC) {
6979c26acbdeSMatthew G. Knepley     PetscSection newSection, gsection;
698014f150ffSMatthew G. Knepley     PetscSF      sf;
698114f150ffSMatthew G. Knepley 
698214f150ffSMatthew G. Knepley     ierr = DMClone(dm, &dm->dmBC);CHKERRQ(ierr);
6983e5e52638SMatthew G. Knepley     ierr = DMCopyDisc(dm, dm->dmBC);CHKERRQ(ierr);
698414f150ffSMatthew G. Knepley     ierr = PetscSectionClone(section, &newSection);CHKERRQ(ierr);
698592fd8e1eSJed Brown     ierr = DMSetLocalSection(dm->dmBC, newSection);CHKERRQ(ierr);
698614f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&newSection);CHKERRQ(ierr);
698714f150ffSMatthew G. Knepley     ierr = DMGetPointSF(dm->dmBC, &sf);CHKERRQ(ierr);
698815b58121SMatthew G. Knepley     ierr = PetscSectionCreateGlobalSection(section, sf, PETSC_TRUE, PETSC_FALSE, &gsection);CHKERRQ(ierr);
6989e87a4003SBarry Smith     ierr = DMSetGlobalSection(dm->dmBC, gsection);CHKERRQ(ierr);
699014f150ffSMatthew G. Knepley     ierr = PetscSectionDestroy(&gsection);CHKERRQ(ierr);
699114f150ffSMatthew G. Knepley   }
699214f150ffSMatthew G. Knepley   *odm = dm->dmBC;
699314f150ffSMatthew G. Knepley   PetscFunctionReturn(0);
699414f150ffSMatthew G. Knepley }
6995f4d763aaSMatthew G. Knepley 
6996f4d763aaSMatthew G. Knepley /*@
6997cdb7a50dSMatthew G. Knepley   DMGetOutputSequenceNumber - Retrieve the sequence number/value for output
6998f4d763aaSMatthew G. Knepley 
6999f4d763aaSMatthew G. Knepley   Input Parameter:
7000f4d763aaSMatthew G. Knepley . dm - The original DM
7001f4d763aaSMatthew G. Knepley 
7002cdb7a50dSMatthew G. Knepley   Output Parameters:
7003cdb7a50dSMatthew G. Knepley + num - The output sequence number
7004cdb7a50dSMatthew G. Knepley - val - The output sequence value
7005f4d763aaSMatthew G. Knepley 
7006f4d763aaSMatthew G. Knepley   Level: intermediate
7007f4d763aaSMatthew G. Knepley 
7008f4d763aaSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7009f4d763aaSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7010f4d763aaSMatthew G. Knepley 
7011f4d763aaSMatthew G. Knepley .seealso: VecView()
7012f4d763aaSMatthew G. Knepley @*/
7013cdb7a50dSMatthew G. Knepley PetscErrorCode DMGetOutputSequenceNumber(DM dm, PetscInt *num, PetscReal *val)
7014f4d763aaSMatthew G. Knepley {
7015f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7016f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7017534a8f05SLisandro Dalcin   if (num) {PetscValidIntPointer(num,2); *num = dm->outputSequenceNum;}
7018534a8f05SLisandro Dalcin   if (val) {PetscValidRealPointer(val,3);*val = dm->outputSequenceVal;}
7019f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7020f4d763aaSMatthew G. Knepley }
7021f4d763aaSMatthew G. Knepley 
7022f4d763aaSMatthew G. Knepley /*@
7023cdb7a50dSMatthew G. Knepley   DMSetOutputSequenceNumber - Set the sequence number/value for output
7024f4d763aaSMatthew G. Knepley 
7025f4d763aaSMatthew G. Knepley   Input Parameters:
7026f4d763aaSMatthew G. Knepley + dm - The original DM
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 DMSetOutputSequenceNumber(DM dm, PetscInt num, PetscReal val)
7038f4d763aaSMatthew G. Knepley {
7039f4d763aaSMatthew G. Knepley   PetscFunctionBegin;
7040f4d763aaSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7041f4d763aaSMatthew G. Knepley   dm->outputSequenceNum = num;
7042cdb7a50dSMatthew G. Knepley   dm->outputSequenceVal = val;
7043cdb7a50dSMatthew G. Knepley   PetscFunctionReturn(0);
7044cdb7a50dSMatthew G. Knepley }
7045cdb7a50dSMatthew G. Knepley 
7046cdb7a50dSMatthew G. Knepley /*@C
7047cdb7a50dSMatthew G. Knepley   DMOutputSequenceLoad - Retrieve the sequence value from a Viewer
7048cdb7a50dSMatthew G. Knepley 
7049cdb7a50dSMatthew G. Knepley   Input Parameters:
7050cdb7a50dSMatthew G. Knepley + dm   - The original DM
7051cdb7a50dSMatthew G. Knepley . name - The sequence name
7052cdb7a50dSMatthew G. Knepley - num  - The output sequence number
7053cdb7a50dSMatthew G. Knepley 
7054cdb7a50dSMatthew G. Knepley   Output Parameter:
7055cdb7a50dSMatthew G. Knepley . val  - The output sequence value
7056cdb7a50dSMatthew G. Knepley 
7057cdb7a50dSMatthew G. Knepley   Level: intermediate
7058cdb7a50dSMatthew G. Knepley 
7059cdb7a50dSMatthew G. Knepley   Note: This is intended for output that should appear in sequence, for instance
7060cdb7a50dSMatthew G. Knepley   a set of timesteps in an HDF5 file, or a set of realizations of a stochastic system.
7061cdb7a50dSMatthew G. Knepley 
7062cdb7a50dSMatthew G. Knepley .seealso: DMGetOutputSequenceNumber(), DMSetOutputSequenceNumber(), VecView()
7063cdb7a50dSMatthew G. Knepley @*/
7064cdb7a50dSMatthew G. Knepley PetscErrorCode DMOutputSequenceLoad(DM dm, PetscViewer viewer, const char *name, PetscInt num, PetscReal *val)
7065cdb7a50dSMatthew G. Knepley {
7066cdb7a50dSMatthew G. Knepley   PetscBool      ishdf5;
7067cdb7a50dSMatthew G. Knepley   PetscErrorCode ierr;
7068cdb7a50dSMatthew G. Knepley 
7069cdb7a50dSMatthew G. Knepley   PetscFunctionBegin;
7070cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
7071cdb7a50dSMatthew G. Knepley   PetscValidHeaderSpecific(viewer,PETSC_VIEWER_CLASSID,2);
7072534a8f05SLisandro Dalcin   PetscValidRealPointer(val,4);
7073cdb7a50dSMatthew G. Knepley   ierr = PetscObjectTypeCompare((PetscObject) viewer, PETSCVIEWERHDF5, &ishdf5);CHKERRQ(ierr);
7074cdb7a50dSMatthew G. Knepley   if (ishdf5) {
7075cdb7a50dSMatthew G. Knepley #if defined(PETSC_HAVE_HDF5)
7076cdb7a50dSMatthew G. Knepley     PetscScalar value;
7077cdb7a50dSMatthew G. Knepley 
707839d25373SMatthew G. Knepley     ierr = DMSequenceLoad_HDF5_Internal(dm, name, num, &value, viewer);CHKERRQ(ierr);
70794aeb217fSMatthew G. Knepley     *val = PetscRealPart(value);
7080cdb7a50dSMatthew G. Knepley #endif
7081cdb7a50dSMatthew G. Knepley   } else SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Invalid viewer; open viewer with PetscViewerHDF5Open()");
7082f4d763aaSMatthew G. Knepley   PetscFunctionReturn(0);
7083f4d763aaSMatthew G. Knepley }
70848e4ac7eaSMatthew G. Knepley 
70858e4ac7eaSMatthew G. Knepley /*@
70868e4ac7eaSMatthew G. Knepley   DMGetUseNatural - Get the flag for creating a mapping to the natural order on distribution
70878e4ac7eaSMatthew G. Knepley 
70888e4ac7eaSMatthew G. Knepley   Not collective
70898e4ac7eaSMatthew G. Knepley 
70908e4ac7eaSMatthew G. Knepley   Input Parameter:
70918e4ac7eaSMatthew G. Knepley . dm - The DM
70928e4ac7eaSMatthew G. Knepley 
70938e4ac7eaSMatthew G. Knepley   Output Parameter:
70948e4ac7eaSMatthew G. Knepley . useNatural - The flag to build the mapping to a natural order during distribution
70958e4ac7eaSMatthew G. Knepley 
70968e4ac7eaSMatthew G. Knepley   Level: beginner
70978e4ac7eaSMatthew G. Knepley 
70988e4ac7eaSMatthew G. Knepley .seealso: DMSetUseNatural(), DMCreate()
70998e4ac7eaSMatthew G. Knepley @*/
71008e4ac7eaSMatthew G. Knepley PetscErrorCode DMGetUseNatural(DM dm, PetscBool *useNatural)
71018e4ac7eaSMatthew G. Knepley {
71028e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71038e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7104534a8f05SLisandro Dalcin   PetscValidBoolPointer(useNatural, 2);
71058e4ac7eaSMatthew G. Knepley   *useNatural = dm->useNatural;
71068e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71078e4ac7eaSMatthew G. Knepley }
71088e4ac7eaSMatthew G. Knepley 
71098e4ac7eaSMatthew G. Knepley /*@
71105d3b26e6SMatthew G. Knepley   DMSetUseNatural - Set the flag for creating a mapping to the natural order after distribution
71118e4ac7eaSMatthew G. Knepley 
71128e4ac7eaSMatthew G. Knepley   Collective on dm
71138e4ac7eaSMatthew G. Knepley 
71148e4ac7eaSMatthew G. Knepley   Input Parameters:
71158e4ac7eaSMatthew G. Knepley + dm - The DM
71168e4ac7eaSMatthew G. Knepley - useNatural - The flag to build the mapping to a natural order during distribution
71178e4ac7eaSMatthew G. Knepley 
71185d3b26e6SMatthew G. Knepley   Note: This also causes the map to be build after DMCreateSubDM() and DMCreateSuperDM()
71195d3b26e6SMatthew G. Knepley 
71208e4ac7eaSMatthew G. Knepley   Level: beginner
71218e4ac7eaSMatthew G. Knepley 
71225d3b26e6SMatthew G. Knepley .seealso: DMGetUseNatural(), DMCreate(), DMPlexDistribute(), DMCreateSubDM(), DMCreateSuperDM()
71238e4ac7eaSMatthew G. Knepley @*/
71248e4ac7eaSMatthew G. Knepley PetscErrorCode DMSetUseNatural(DM dm, PetscBool useNatural)
71258e4ac7eaSMatthew G. Knepley {
71268e4ac7eaSMatthew G. Knepley   PetscFunctionBegin;
71278e4ac7eaSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
71288833efb5SBlaise Bourdin   PetscValidLogicalCollectiveBool(dm, useNatural, 2);
71298e4ac7eaSMatthew G. Knepley   dm->useNatural = useNatural;
71308e4ac7eaSMatthew G. Knepley   PetscFunctionReturn(0);
71318e4ac7eaSMatthew G. Knepley }
7132c58f1c22SToby Isaac 
7133c58f1c22SToby Isaac 
7134c58f1c22SToby Isaac /*@C
7135c58f1c22SToby Isaac   DMCreateLabel - Create a label of the given name if it does not already exist
7136c58f1c22SToby Isaac 
7137c58f1c22SToby Isaac   Not Collective
7138c58f1c22SToby Isaac 
7139c58f1c22SToby Isaac   Input Parameters:
7140c58f1c22SToby Isaac + dm   - The DM object
7141c58f1c22SToby Isaac - name - The label name
7142c58f1c22SToby Isaac 
7143c58f1c22SToby Isaac   Level: intermediate
7144c58f1c22SToby Isaac 
7145c58f1c22SToby Isaac .seealso: DMLabelCreate(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7146c58f1c22SToby Isaac @*/
7147c58f1c22SToby Isaac PetscErrorCode DMCreateLabel(DM dm, const char name[])
7148c58f1c22SToby Isaac {
71495d80c0bfSVaclav Hapla   PetscBool      flg;
71505d80c0bfSVaclav Hapla   DMLabel        label;
7151c58f1c22SToby Isaac   PetscErrorCode ierr;
7152c58f1c22SToby Isaac 
7153c58f1c22SToby Isaac   PetscFunctionBegin;
7154c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7155c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
71565d80c0bfSVaclav Hapla   ierr = DMHasLabel(dm, name, &flg);CHKERRQ(ierr);
7157c58f1c22SToby Isaac   if (!flg) {
71585d80c0bfSVaclav Hapla     ierr = DMLabelCreate(PETSC_COMM_SELF, name, &label);CHKERRQ(ierr);
71595d80c0bfSVaclav Hapla     ierr = DMAddLabel(dm, label);CHKERRQ(ierr);
71605d80c0bfSVaclav Hapla     ierr = DMLabelDestroy(&label);CHKERRQ(ierr);
7161c58f1c22SToby Isaac   }
7162c58f1c22SToby Isaac   PetscFunctionReturn(0);
7163c58f1c22SToby Isaac }
7164c58f1c22SToby Isaac 
7165c58f1c22SToby Isaac /*@C
7166c58f1c22SToby Isaac   DMGetLabelValue - Get the value in a Sieve Label for the given point, with 0 as the default
7167c58f1c22SToby Isaac 
7168c58f1c22SToby Isaac   Not Collective
7169c58f1c22SToby Isaac 
7170c58f1c22SToby Isaac   Input Parameters:
7171c58f1c22SToby Isaac + dm   - The DM object
7172c58f1c22SToby Isaac . name - The label name
7173c58f1c22SToby Isaac - point - The mesh point
7174c58f1c22SToby Isaac 
7175c58f1c22SToby Isaac   Output Parameter:
7176c58f1c22SToby Isaac . value - The label value for this point, or -1 if the point is not in the label
7177c58f1c22SToby Isaac 
7178c58f1c22SToby Isaac   Level: beginner
7179c58f1c22SToby Isaac 
7180c58f1c22SToby Isaac .seealso: DMLabelGetValue(), DMSetLabelValue(), DMGetStratumIS()
7181c58f1c22SToby Isaac @*/
7182c58f1c22SToby Isaac PetscErrorCode DMGetLabelValue(DM dm, const char name[], PetscInt point, PetscInt *value)
7183c58f1c22SToby Isaac {
7184c58f1c22SToby Isaac   DMLabel        label;
7185c58f1c22SToby Isaac   PetscErrorCode ierr;
7186c58f1c22SToby Isaac 
7187c58f1c22SToby Isaac   PetscFunctionBegin;
7188c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7189c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7190c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
719113903a91SSatish Balay   if (!label) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "No label named %s was found", name);
7192c58f1c22SToby Isaac   ierr = DMLabelGetValue(label, point, value);CHKERRQ(ierr);
7193c58f1c22SToby Isaac   PetscFunctionReturn(0);
7194c58f1c22SToby Isaac }
7195c58f1c22SToby Isaac 
7196c58f1c22SToby Isaac /*@C
7197c58f1c22SToby Isaac   DMSetLabelValue - Add a point to a Sieve Label with given value
7198c58f1c22SToby Isaac 
7199c58f1c22SToby Isaac   Not Collective
7200c58f1c22SToby Isaac 
7201c58f1c22SToby Isaac   Input Parameters:
7202c58f1c22SToby Isaac + dm   - The DM object
7203c58f1c22SToby Isaac . name - The label name
7204c58f1c22SToby Isaac . point - The mesh point
7205c58f1c22SToby Isaac - value - The label value for this point
7206c58f1c22SToby Isaac 
7207c58f1c22SToby Isaac   Output Parameter:
7208c58f1c22SToby Isaac 
7209c58f1c22SToby Isaac   Level: beginner
7210c58f1c22SToby Isaac 
7211c58f1c22SToby Isaac .seealso: DMLabelSetValue(), DMGetStratumIS(), DMClearLabelValue()
7212c58f1c22SToby Isaac @*/
7213c58f1c22SToby Isaac PetscErrorCode DMSetLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7214c58f1c22SToby Isaac {
7215c58f1c22SToby Isaac   DMLabel        label;
7216c58f1c22SToby Isaac   PetscErrorCode ierr;
7217c58f1c22SToby Isaac 
7218c58f1c22SToby Isaac   PetscFunctionBegin;
7219c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7220c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7221c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7222c58f1c22SToby Isaac   if (!label) {
7223c58f1c22SToby Isaac     ierr = DMCreateLabel(dm, name);CHKERRQ(ierr);
7224c58f1c22SToby Isaac     ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7225c58f1c22SToby Isaac   }
7226c58f1c22SToby Isaac   ierr = DMLabelSetValue(label, point, value);CHKERRQ(ierr);
7227c58f1c22SToby Isaac   PetscFunctionReturn(0);
7228c58f1c22SToby Isaac }
7229c58f1c22SToby Isaac 
7230c58f1c22SToby Isaac /*@C
7231c58f1c22SToby Isaac   DMClearLabelValue - Remove a point from a Sieve Label with given value
7232c58f1c22SToby Isaac 
7233c58f1c22SToby Isaac   Not Collective
7234c58f1c22SToby Isaac 
7235c58f1c22SToby Isaac   Input Parameters:
7236c58f1c22SToby Isaac + dm   - The DM object
7237c58f1c22SToby Isaac . name - The label name
7238c58f1c22SToby Isaac . point - The mesh point
7239c58f1c22SToby Isaac - value - The label value for this point
7240c58f1c22SToby Isaac 
7241c58f1c22SToby Isaac   Output Parameter:
7242c58f1c22SToby Isaac 
7243c58f1c22SToby Isaac   Level: beginner
7244c58f1c22SToby Isaac 
7245c58f1c22SToby Isaac .seealso: DMLabelClearValue(), DMSetLabelValue(), DMGetStratumIS()
7246c58f1c22SToby Isaac @*/
7247c58f1c22SToby Isaac PetscErrorCode DMClearLabelValue(DM dm, const char name[], PetscInt point, PetscInt value)
7248c58f1c22SToby Isaac {
7249c58f1c22SToby Isaac   DMLabel        label;
7250c58f1c22SToby Isaac   PetscErrorCode ierr;
7251c58f1c22SToby Isaac 
7252c58f1c22SToby Isaac   PetscFunctionBegin;
7253c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7254c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7255c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7256c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7257c58f1c22SToby Isaac   ierr = DMLabelClearValue(label, point, value);CHKERRQ(ierr);
7258c58f1c22SToby Isaac   PetscFunctionReturn(0);
7259c58f1c22SToby Isaac }
7260c58f1c22SToby Isaac 
7261c58f1c22SToby Isaac /*@C
7262c58f1c22SToby Isaac   DMGetLabelSize - Get the number of different integer ids in a Label
7263c58f1c22SToby Isaac 
7264c58f1c22SToby Isaac   Not Collective
7265c58f1c22SToby Isaac 
7266c58f1c22SToby Isaac   Input Parameters:
7267c58f1c22SToby Isaac + dm   - The DM object
7268c58f1c22SToby Isaac - name - The label name
7269c58f1c22SToby Isaac 
7270c58f1c22SToby Isaac   Output Parameter:
7271c58f1c22SToby Isaac . size - The number of different integer ids, or 0 if the label does not exist
7272c58f1c22SToby Isaac 
7273c58f1c22SToby Isaac   Level: beginner
7274c58f1c22SToby Isaac 
7275df813f42SMatthew G. Knepley .seealso: DMLabelGetNumValues(), DMSetLabelValue()
7276c58f1c22SToby Isaac @*/
7277c58f1c22SToby Isaac PetscErrorCode DMGetLabelSize(DM dm, const char name[], PetscInt *size)
7278c58f1c22SToby Isaac {
7279c58f1c22SToby Isaac   DMLabel        label;
7280c58f1c22SToby Isaac   PetscErrorCode ierr;
7281c58f1c22SToby Isaac 
7282c58f1c22SToby Isaac   PetscFunctionBegin;
7283c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7284c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7285534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 3);
7286c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7287c58f1c22SToby Isaac   *size = 0;
7288c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7289c58f1c22SToby Isaac   ierr = DMLabelGetNumValues(label, size);CHKERRQ(ierr);
7290c58f1c22SToby Isaac   PetscFunctionReturn(0);
7291c58f1c22SToby Isaac }
7292c58f1c22SToby Isaac 
7293c58f1c22SToby Isaac /*@C
7294c58f1c22SToby Isaac   DMGetLabelIdIS - Get the integer ids in a label
7295c58f1c22SToby Isaac 
7296c58f1c22SToby Isaac   Not Collective
7297c58f1c22SToby Isaac 
7298c58f1c22SToby Isaac   Input Parameters:
7299c58f1c22SToby Isaac + mesh - The DM object
7300c58f1c22SToby Isaac - name - The label name
7301c58f1c22SToby Isaac 
7302c58f1c22SToby Isaac   Output Parameter:
7303c58f1c22SToby Isaac . ids - The integer ids, or NULL if the label does not exist
7304c58f1c22SToby Isaac 
7305c58f1c22SToby Isaac   Level: beginner
7306c58f1c22SToby Isaac 
7307c58f1c22SToby Isaac .seealso: DMLabelGetValueIS(), DMGetLabelSize()
7308c58f1c22SToby Isaac @*/
7309c58f1c22SToby Isaac PetscErrorCode DMGetLabelIdIS(DM dm, const char name[], IS *ids)
7310c58f1c22SToby Isaac {
7311c58f1c22SToby Isaac   DMLabel        label;
7312c58f1c22SToby Isaac   PetscErrorCode ierr;
7313c58f1c22SToby Isaac 
7314c58f1c22SToby Isaac   PetscFunctionBegin;
7315c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7316c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7317c58f1c22SToby Isaac   PetscValidPointer(ids, 3);
7318c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7319c58f1c22SToby Isaac   *ids = NULL;
7320dab2e251SBlaise Bourdin  if (label) {
7321c58f1c22SToby Isaac     ierr = DMLabelGetValueIS(label, ids);CHKERRQ(ierr);
7322dab2e251SBlaise Bourdin   } else {
7323dab2e251SBlaise Bourdin     /* returning an empty IS */
7324dab2e251SBlaise Bourdin     ierr = ISCreateGeneral(PETSC_COMM_SELF,0,NULL,PETSC_USE_POINTER,ids);CHKERRQ(ierr);
7325dab2e251SBlaise Bourdin   }
7326c58f1c22SToby Isaac   PetscFunctionReturn(0);
7327c58f1c22SToby Isaac }
7328c58f1c22SToby Isaac 
7329c58f1c22SToby Isaac /*@C
7330c58f1c22SToby Isaac   DMGetStratumSize - Get the number of points in a label stratum
7331c58f1c22SToby Isaac 
7332c58f1c22SToby Isaac   Not Collective
7333c58f1c22SToby Isaac 
7334c58f1c22SToby Isaac   Input Parameters:
7335c58f1c22SToby Isaac + dm - The DM object
7336c58f1c22SToby Isaac . name - The label name
7337c58f1c22SToby Isaac - value - The stratum value
7338c58f1c22SToby Isaac 
7339c58f1c22SToby Isaac   Output Parameter:
7340c58f1c22SToby Isaac . size - The stratum size
7341c58f1c22SToby Isaac 
7342c58f1c22SToby Isaac   Level: beginner
7343c58f1c22SToby Isaac 
7344c58f1c22SToby Isaac .seealso: DMLabelGetStratumSize(), DMGetLabelSize(), DMGetLabelIds()
7345c58f1c22SToby Isaac @*/
7346c58f1c22SToby Isaac PetscErrorCode DMGetStratumSize(DM dm, const char name[], PetscInt value, PetscInt *size)
7347c58f1c22SToby Isaac {
7348c58f1c22SToby Isaac   DMLabel        label;
7349c58f1c22SToby Isaac   PetscErrorCode ierr;
7350c58f1c22SToby Isaac 
7351c58f1c22SToby Isaac   PetscFunctionBegin;
7352c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7353c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7354534a8f05SLisandro Dalcin   PetscValidIntPointer(size, 4);
7355c58f1c22SToby Isaac   ierr  = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7356c58f1c22SToby Isaac   *size = 0;
7357c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7358c58f1c22SToby Isaac   ierr = DMLabelGetStratumSize(label, value, size);CHKERRQ(ierr);
7359c58f1c22SToby Isaac   PetscFunctionReturn(0);
7360c58f1c22SToby Isaac }
7361c58f1c22SToby Isaac 
7362c58f1c22SToby Isaac /*@C
7363c58f1c22SToby Isaac   DMGetStratumIS - Get the points in a label stratum
7364c58f1c22SToby Isaac 
7365c58f1c22SToby Isaac   Not Collective
7366c58f1c22SToby Isaac 
7367c58f1c22SToby Isaac   Input Parameters:
7368c58f1c22SToby Isaac + dm - The DM object
7369c58f1c22SToby Isaac . name - The label name
7370c58f1c22SToby Isaac - value - The stratum value
7371c58f1c22SToby Isaac 
7372c58f1c22SToby Isaac   Output Parameter:
7373c58f1c22SToby Isaac . points - The stratum points, or NULL if the label does not exist or does not have that value
7374c58f1c22SToby Isaac 
7375c58f1c22SToby Isaac   Level: beginner
7376c58f1c22SToby Isaac 
7377c58f1c22SToby Isaac .seealso: DMLabelGetStratumIS(), DMGetStratumSize()
7378c58f1c22SToby Isaac @*/
7379c58f1c22SToby Isaac PetscErrorCode DMGetStratumIS(DM dm, const char name[], PetscInt value, IS *points)
7380c58f1c22SToby Isaac {
7381c58f1c22SToby Isaac   DMLabel        label;
7382c58f1c22SToby Isaac   PetscErrorCode ierr;
7383c58f1c22SToby Isaac 
7384c58f1c22SToby Isaac   PetscFunctionBegin;
7385c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7386c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7387c58f1c22SToby Isaac   PetscValidPointer(points, 4);
7388c58f1c22SToby Isaac   ierr    = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7389c58f1c22SToby Isaac   *points = NULL;
7390c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7391c58f1c22SToby Isaac   ierr = DMLabelGetStratumIS(label, value, points);CHKERRQ(ierr);
7392c58f1c22SToby Isaac   PetscFunctionReturn(0);
7393c58f1c22SToby Isaac }
7394c58f1c22SToby Isaac 
73954de306b1SToby Isaac /*@C
73969044fa66SMatthew G. Knepley   DMSetStratumIS - Set the points in a label stratum
73974de306b1SToby Isaac 
73984de306b1SToby Isaac   Not Collective
73994de306b1SToby Isaac 
74004de306b1SToby Isaac   Input Parameters:
74014de306b1SToby Isaac + dm - The DM object
74024de306b1SToby Isaac . name - The label name
74034de306b1SToby Isaac . value - The stratum value
74044de306b1SToby Isaac - points - The stratum points
74054de306b1SToby Isaac 
74064de306b1SToby Isaac   Level: beginner
74074de306b1SToby Isaac 
74084de306b1SToby Isaac .seealso: DMLabelSetStratumIS(), DMGetStratumSize()
74094de306b1SToby Isaac @*/
74104de306b1SToby Isaac PetscErrorCode DMSetStratumIS(DM dm, const char name[], PetscInt value, IS points)
74114de306b1SToby Isaac {
74124de306b1SToby Isaac   DMLabel        label;
74134de306b1SToby Isaac   PetscErrorCode ierr;
74144de306b1SToby Isaac 
74154de306b1SToby Isaac   PetscFunctionBegin;
74164de306b1SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
74174de306b1SToby Isaac   PetscValidCharPointer(name, 2);
74184de306b1SToby Isaac   PetscValidPointer(points, 4);
74194de306b1SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
74204de306b1SToby Isaac   if (!label) PetscFunctionReturn(0);
74214de306b1SToby Isaac   ierr = DMLabelSetStratumIS(label, value, points);CHKERRQ(ierr);
74224de306b1SToby Isaac   PetscFunctionReturn(0);
74234de306b1SToby Isaac }
74244de306b1SToby Isaac 
7425c58f1c22SToby Isaac /*@C
7426c58f1c22SToby Isaac   DMClearLabelStratum - Remove all points from a stratum from a Sieve Label
7427c58f1c22SToby Isaac 
7428c58f1c22SToby Isaac   Not Collective
7429c58f1c22SToby Isaac 
7430c58f1c22SToby Isaac   Input Parameters:
7431c58f1c22SToby Isaac + dm   - The DM object
7432c58f1c22SToby Isaac . name - The label name
7433c58f1c22SToby Isaac - value - The label value for this point
7434c58f1c22SToby Isaac 
7435c58f1c22SToby Isaac   Output Parameter:
7436c58f1c22SToby Isaac 
7437c58f1c22SToby Isaac   Level: beginner
7438c58f1c22SToby Isaac 
7439c58f1c22SToby Isaac .seealso: DMLabelClearStratum(), DMSetLabelValue(), DMGetStratumIS(), DMClearLabelValue()
7440c58f1c22SToby Isaac @*/
7441c58f1c22SToby Isaac PetscErrorCode DMClearLabelStratum(DM dm, const char name[], PetscInt value)
7442c58f1c22SToby Isaac {
7443c58f1c22SToby Isaac   DMLabel        label;
7444c58f1c22SToby Isaac   PetscErrorCode ierr;
7445c58f1c22SToby Isaac 
7446c58f1c22SToby Isaac   PetscFunctionBegin;
7447c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7448c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7449c58f1c22SToby Isaac   ierr = DMGetLabel(dm, name, &label);CHKERRQ(ierr);
7450c58f1c22SToby Isaac   if (!label) PetscFunctionReturn(0);
7451c58f1c22SToby Isaac   ierr = DMLabelClearStratum(label, value);CHKERRQ(ierr);
7452c58f1c22SToby Isaac   PetscFunctionReturn(0);
7453c58f1c22SToby Isaac }
7454c58f1c22SToby Isaac 
7455c58f1c22SToby Isaac /*@
7456c58f1c22SToby Isaac   DMGetNumLabels - Return the number of labels defined by the mesh
7457c58f1c22SToby Isaac 
7458c58f1c22SToby Isaac   Not Collective
7459c58f1c22SToby Isaac 
7460c58f1c22SToby Isaac   Input Parameter:
7461c58f1c22SToby Isaac . dm   - The DM object
7462c58f1c22SToby Isaac 
7463c58f1c22SToby Isaac   Output Parameter:
7464c58f1c22SToby Isaac . numLabels - the number of Labels
7465c58f1c22SToby Isaac 
7466c58f1c22SToby Isaac   Level: intermediate
7467c58f1c22SToby Isaac 
7468c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7469c58f1c22SToby Isaac @*/
7470c58f1c22SToby Isaac PetscErrorCode DMGetNumLabels(DM dm, PetscInt *numLabels)
7471c58f1c22SToby Isaac {
74725d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7473c58f1c22SToby Isaac   PetscInt  n    = 0;
7474c58f1c22SToby Isaac 
7475c58f1c22SToby Isaac   PetscFunctionBegin;
7476c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7477534a8f05SLisandro Dalcin   PetscValidIntPointer(numLabels, 2);
7478c58f1c22SToby Isaac   while (next) {++n; next = next->next;}
7479c58f1c22SToby Isaac   *numLabels = n;
7480c58f1c22SToby Isaac   PetscFunctionReturn(0);
7481c58f1c22SToby Isaac }
7482c58f1c22SToby Isaac 
7483c58f1c22SToby Isaac /*@C
7484c58f1c22SToby Isaac   DMGetLabelName - Return the name of nth label
7485c58f1c22SToby Isaac 
7486c58f1c22SToby Isaac   Not Collective
7487c58f1c22SToby Isaac 
7488c58f1c22SToby Isaac   Input Parameters:
7489c58f1c22SToby Isaac + dm - The DM object
7490c58f1c22SToby Isaac - n  - the label number
7491c58f1c22SToby Isaac 
7492c58f1c22SToby Isaac   Output Parameter:
7493c58f1c22SToby Isaac . name - the label name
7494c58f1c22SToby Isaac 
7495c58f1c22SToby Isaac   Level: intermediate
7496c58f1c22SToby Isaac 
7497c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7498c58f1c22SToby Isaac @*/
7499c58f1c22SToby Isaac PetscErrorCode DMGetLabelName(DM dm, PetscInt n, const char **name)
7500c58f1c22SToby Isaac {
75015d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7502c58f1c22SToby Isaac   PetscInt       l    = 0;
7503d67d17b1SMatthew G. Knepley   PetscErrorCode ierr;
7504c58f1c22SToby Isaac 
7505c58f1c22SToby Isaac   PetscFunctionBegin;
7506c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7507c58f1c22SToby Isaac   PetscValidPointer(name, 3);
7508c58f1c22SToby Isaac   while (next) {
7509c58f1c22SToby Isaac     if (l == n) {
7510d67d17b1SMatthew G. Knepley       ierr = PetscObjectGetName((PetscObject) next->label, name);CHKERRQ(ierr);
7511c58f1c22SToby Isaac       PetscFunctionReturn(0);
7512c58f1c22SToby Isaac     }
7513c58f1c22SToby Isaac     ++l;
7514c58f1c22SToby Isaac     next = next->next;
7515c58f1c22SToby Isaac   }
7516c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7517c58f1c22SToby Isaac }
7518c58f1c22SToby Isaac 
7519c58f1c22SToby Isaac /*@C
7520c58f1c22SToby Isaac   DMHasLabel - Determine whether the mesh has a label of a given name
7521c58f1c22SToby Isaac 
7522c58f1c22SToby Isaac   Not Collective
7523c58f1c22SToby Isaac 
7524c58f1c22SToby Isaac   Input Parameters:
7525c58f1c22SToby Isaac + dm   - The DM object
7526c58f1c22SToby Isaac - name - The label name
7527c58f1c22SToby Isaac 
7528c58f1c22SToby Isaac   Output Parameter:
7529c58f1c22SToby Isaac . hasLabel - PETSC_TRUE if the label is present
7530c58f1c22SToby Isaac 
7531c58f1c22SToby Isaac   Level: intermediate
7532c58f1c22SToby Isaac 
7533c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7534c58f1c22SToby Isaac @*/
7535c58f1c22SToby Isaac PetscErrorCode DMHasLabel(DM dm, const char name[], PetscBool *hasLabel)
7536c58f1c22SToby Isaac {
75375d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7538d67d17b1SMatthew G. Knepley   const char    *lname;
7539c58f1c22SToby Isaac   PetscErrorCode ierr;
7540c58f1c22SToby Isaac 
7541c58f1c22SToby Isaac   PetscFunctionBegin;
7542c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7543c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7544534a8f05SLisandro Dalcin   PetscValidBoolPointer(hasLabel, 3);
7545c58f1c22SToby Isaac   *hasLabel = PETSC_FALSE;
7546c58f1c22SToby Isaac   while (next) {
7547d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7548d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, hasLabel);CHKERRQ(ierr);
7549c58f1c22SToby Isaac     if (*hasLabel) break;
7550c58f1c22SToby Isaac     next = next->next;
7551c58f1c22SToby Isaac   }
7552c58f1c22SToby Isaac   PetscFunctionReturn(0);
7553c58f1c22SToby Isaac }
7554c58f1c22SToby Isaac 
7555c58f1c22SToby Isaac /*@C
7556c58f1c22SToby Isaac   DMGetLabel - Return the label of a given name, or NULL
7557c58f1c22SToby Isaac 
7558c58f1c22SToby Isaac   Not Collective
7559c58f1c22SToby Isaac 
7560c58f1c22SToby Isaac   Input Parameters:
7561c58f1c22SToby Isaac + dm   - The DM object
7562c58f1c22SToby Isaac - name - The label name
7563c58f1c22SToby Isaac 
7564c58f1c22SToby Isaac   Output Parameter:
7565c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7566c58f1c22SToby Isaac 
7567c58f1c22SToby Isaac   Level: intermediate
7568c58f1c22SToby Isaac 
7569c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7570c58f1c22SToby Isaac @*/
7571c58f1c22SToby Isaac PetscErrorCode DMGetLabel(DM dm, const char name[], DMLabel *label)
7572c58f1c22SToby Isaac {
75735d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7574c58f1c22SToby Isaac   PetscBool      hasLabel;
7575d67d17b1SMatthew G. Knepley   const char    *lname;
7576c58f1c22SToby Isaac   PetscErrorCode ierr;
7577c58f1c22SToby Isaac 
7578c58f1c22SToby Isaac   PetscFunctionBegin;
7579c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7580c58f1c22SToby Isaac   PetscValidCharPointer(name, 2);
7581c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7582c58f1c22SToby Isaac   *label = NULL;
7583c58f1c22SToby Isaac   while (next) {
7584d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7585d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7586c58f1c22SToby Isaac     if (hasLabel) {
7587c58f1c22SToby Isaac       *label = next->label;
7588c58f1c22SToby Isaac       break;
7589c58f1c22SToby Isaac     }
7590c58f1c22SToby Isaac     next = next->next;
7591c58f1c22SToby Isaac   }
7592c58f1c22SToby Isaac   PetscFunctionReturn(0);
7593c58f1c22SToby Isaac }
7594c58f1c22SToby Isaac 
7595c58f1c22SToby Isaac /*@C
7596c58f1c22SToby Isaac   DMGetLabelByNum - Return the nth label
7597c58f1c22SToby Isaac 
7598c58f1c22SToby Isaac   Not Collective
7599c58f1c22SToby Isaac 
7600c58f1c22SToby Isaac   Input Parameters:
7601c58f1c22SToby Isaac + dm - The DM object
7602c58f1c22SToby Isaac - n  - the label number
7603c58f1c22SToby Isaac 
7604c58f1c22SToby Isaac   Output Parameter:
7605c58f1c22SToby Isaac . label - the label
7606c58f1c22SToby Isaac 
7607c58f1c22SToby Isaac   Level: intermediate
7608c58f1c22SToby Isaac 
7609c58f1c22SToby Isaac .seealso: DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7610c58f1c22SToby Isaac @*/
7611c58f1c22SToby Isaac PetscErrorCode DMGetLabelByNum(DM dm, PetscInt n, DMLabel *label)
7612c58f1c22SToby Isaac {
76135d80c0bfSVaclav Hapla   DMLabelLink next = dm->labels;
7614c58f1c22SToby Isaac   PetscInt    l    = 0;
7615c58f1c22SToby Isaac 
7616c58f1c22SToby Isaac   PetscFunctionBegin;
7617c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7618c58f1c22SToby Isaac   PetscValidPointer(label, 3);
7619c58f1c22SToby Isaac   while (next) {
7620c58f1c22SToby Isaac     if (l == n) {
7621c58f1c22SToby Isaac       *label = next->label;
7622c58f1c22SToby Isaac       PetscFunctionReturn(0);
7623c58f1c22SToby Isaac     }
7624c58f1c22SToby Isaac     ++l;
7625c58f1c22SToby Isaac     next = next->next;
7626c58f1c22SToby Isaac   }
7627c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %D does not exist in this DM", n);
7628c58f1c22SToby Isaac }
7629c58f1c22SToby Isaac 
7630c58f1c22SToby Isaac /*@C
7631c58f1c22SToby Isaac   DMAddLabel - Add the label to this mesh
7632c58f1c22SToby Isaac 
7633c58f1c22SToby Isaac   Not Collective
7634c58f1c22SToby Isaac 
7635c58f1c22SToby Isaac   Input Parameters:
7636c58f1c22SToby Isaac + dm   - The DM object
7637c58f1c22SToby Isaac - label - The DMLabel
7638c58f1c22SToby Isaac 
7639c58f1c22SToby Isaac   Level: developer
7640c58f1c22SToby Isaac 
7641c58f1c22SToby Isaac .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7642c58f1c22SToby Isaac @*/
7643c58f1c22SToby Isaac PetscErrorCode DMAddLabel(DM dm, DMLabel label)
7644c58f1c22SToby Isaac {
76455d80c0bfSVaclav Hapla   DMLabelLink    l, *p, tmpLabel;
7646c58f1c22SToby Isaac   PetscBool      hasLabel;
7647d67d17b1SMatthew G. Knepley   const char    *lname;
76485d80c0bfSVaclav Hapla   PetscBool      flg;
7649c58f1c22SToby Isaac   PetscErrorCode ierr;
7650c58f1c22SToby Isaac 
7651c58f1c22SToby Isaac   PetscFunctionBegin;
7652c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7653d67d17b1SMatthew G. Knepley   ierr = PetscObjectGetName((PetscObject) label, &lname);CHKERRQ(ierr);
7654d67d17b1SMatthew G. Knepley   ierr = DMHasLabel(dm, lname, &hasLabel);CHKERRQ(ierr);
7655d67d17b1SMatthew G. Knepley   if (hasLabel) SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Label %s already exists in this DM", lname);
7656c58f1c22SToby Isaac   ierr = PetscCalloc1(1, &tmpLabel);CHKERRQ(ierr);
7657c58f1c22SToby Isaac   tmpLabel->label  = label;
7658c58f1c22SToby Isaac   tmpLabel->output = PETSC_TRUE;
76595d80c0bfSVaclav Hapla   for (p=&dm->labels; (l=*p); p=&l->next) {}
76605d80c0bfSVaclav Hapla   *p = tmpLabel;
766108f633c4SVaclav Hapla   ierr = PetscObjectReference((PetscObject)label);CHKERRQ(ierr);
76625d80c0bfSVaclav Hapla   ierr = PetscStrcmp(lname, "depth", &flg);CHKERRQ(ierr);
76635d80c0bfSVaclav Hapla   if (flg) dm->depthLabel = label;
7664ba2698f1SMatthew G. Knepley   ierr = PetscStrcmp(lname, "celltype", &flg);CHKERRQ(ierr);
7665ba2698f1SMatthew G. Knepley   if (flg) dm->celltypeLabel = label;
7666c58f1c22SToby Isaac   PetscFunctionReturn(0);
7667c58f1c22SToby Isaac }
7668c58f1c22SToby Isaac 
7669c58f1c22SToby Isaac /*@C
7670e5472504SVaclav Hapla   DMRemoveLabel - Remove the label given by name from this mesh
7671c58f1c22SToby Isaac 
7672c58f1c22SToby Isaac   Not Collective
7673c58f1c22SToby Isaac 
7674c58f1c22SToby Isaac   Input Parameters:
7675c58f1c22SToby Isaac + dm   - The DM object
7676c58f1c22SToby Isaac - name - The label name
7677c58f1c22SToby Isaac 
7678c58f1c22SToby Isaac   Output Parameter:
7679c58f1c22SToby Isaac . label - The DMLabel, or NULL if the label is absent
7680c58f1c22SToby Isaac 
7681c58f1c22SToby Isaac   Level: developer
7682c58f1c22SToby Isaac 
7683e5472504SVaclav Hapla   Notes:
7684e5472504SVaclav Hapla   DMRemoveLabel(dm,name,NULL) removes the label from dm and calls
7685e5472504SVaclav Hapla   DMLabelDestroy() on the label.
7686e5472504SVaclav Hapla 
7687e5472504SVaclav Hapla   DMRemoveLabel(dm,name,&label) removes the label from dm, but it DOES NOT
7688e5472504SVaclav Hapla   call DMLabelDestroy(). Instead, the label is returned and the user is
7689e5472504SVaclav Hapla   responsible of calling DMLabelDestroy() at some point.
7690e5472504SVaclav Hapla 
7691e5472504SVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel(), DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabelBySelf()
7692c58f1c22SToby Isaac @*/
7693c58f1c22SToby Isaac PetscErrorCode DMRemoveLabel(DM dm, const char name[], DMLabel *label)
7694c58f1c22SToby Isaac {
769595d578d6SVaclav Hapla   DMLabelLink    link, *pnext;
7696c58f1c22SToby Isaac   PetscBool      hasLabel;
7697d67d17b1SMatthew G. Knepley   const char    *lname;
7698c58f1c22SToby Isaac   PetscErrorCode ierr;
7699c58f1c22SToby Isaac 
7700c58f1c22SToby Isaac   PetscFunctionBegin;
7701c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7702e5472504SVaclav Hapla   PetscValidCharPointer(name, 2);
7703e5472504SVaclav Hapla   if (label) {
7704e5472504SVaclav Hapla     PetscValidPointer(label, 3);
7705c58f1c22SToby Isaac     *label = NULL;
7706e5472504SVaclav Hapla   }
77075d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
770895d578d6SVaclav Hapla     ierr = PetscObjectGetName((PetscObject) link->label, &lname);CHKERRQ(ierr);
7709d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &hasLabel);CHKERRQ(ierr);
7710c58f1c22SToby Isaac     if (hasLabel) {
771195d578d6SVaclav Hapla       *pnext = link->next; /* Remove from list */
7712c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &hasLabel);CHKERRQ(ierr);
771395d578d6SVaclav Hapla       if (hasLabel) dm->depthLabel = NULL;
7714ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &hasLabel);CHKERRQ(ierr);
7715ba2698f1SMatthew G. Knepley       if (hasLabel) dm->celltypeLabel = NULL;
771695d578d6SVaclav Hapla       if (label) *label = link->label;
771795d578d6SVaclav Hapla       else       {ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);}
771895d578d6SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7719c58f1c22SToby Isaac       break;
7720c58f1c22SToby Isaac     }
7721c58f1c22SToby Isaac   }
7722c58f1c22SToby Isaac   PetscFunctionReturn(0);
7723c58f1c22SToby Isaac }
7724c58f1c22SToby Isaac 
7725306894acSVaclav Hapla /*@
7726306894acSVaclav Hapla   DMRemoveLabelBySelf - Remove the label from this mesh
7727306894acSVaclav Hapla 
7728306894acSVaclav Hapla   Not Collective
7729306894acSVaclav Hapla 
7730306894acSVaclav Hapla   Input Parameters:
7731306894acSVaclav Hapla + dm   - The DM object
7732306894acSVaclav Hapla . label - (Optional) The DMLabel to be removed from the DM
7733306894acSVaclav Hapla - failNotFound - Should it fail if the label is not found in the DM?
7734306894acSVaclav Hapla 
7735306894acSVaclav Hapla   Level: developer
7736306894acSVaclav Hapla 
7737306894acSVaclav Hapla   Notes:
7738306894acSVaclav Hapla   Only exactly the same instance is removed if found, name match is ignored.
7739306894acSVaclav Hapla   If the DM has an exclusive reference to the label, it gets destroyed and
7740306894acSVaclav Hapla   *label nullified.
7741306894acSVaclav Hapla 
7742306894acSVaclav Hapla .seealso: DMCreateLabel(), DMHasLabel(), DMGetLabel() DMGetLabelValue(), DMSetLabelValue(), DMLabelDestroy(), DMRemoveLabel()
7743306894acSVaclav Hapla @*/
7744306894acSVaclav Hapla PetscErrorCode DMRemoveLabelBySelf(DM dm, DMLabel *label, PetscBool failNotFound)
7745306894acSVaclav Hapla {
774643e45a93SVaclav Hapla   DMLabelLink    link, *pnext;
7747306894acSVaclav Hapla   PetscBool      hasLabel = PETSC_FALSE;
7748306894acSVaclav Hapla   PetscErrorCode ierr;
7749306894acSVaclav Hapla 
7750306894acSVaclav Hapla   PetscFunctionBegin;
7751306894acSVaclav Hapla   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7752306894acSVaclav Hapla   PetscValidPointer(label, 2);
7753f39a9ae0SVaclav Hapla   if (!*label && !failNotFound) PetscFunctionReturn(0);
7754306894acSVaclav Hapla   PetscValidHeaderSpecific(*label, DMLABEL_CLASSID, 2);
7755306894acSVaclav Hapla   PetscValidLogicalCollectiveBool(dm,failNotFound,3);
77565d80c0bfSVaclav Hapla   for (pnext=&dm->labels; (link=*pnext); pnext=&link->next) {
775743e45a93SVaclav Hapla     if (*label == link->label) {
7758306894acSVaclav Hapla       hasLabel = PETSC_TRUE;
775943e45a93SVaclav Hapla       *pnext = link->next; /* Remove from list */
7760306894acSVaclav Hapla       if (*label == dm->depthLabel) dm->depthLabel = NULL;
7761ba2698f1SMatthew G. Knepley       if (*label == dm->celltypeLabel) dm->celltypeLabel = NULL;
776243e45a93SVaclav Hapla       if (((PetscObject) link->label)->refct < 2) *label = NULL; /* nullify if exclusive reference */
776343e45a93SVaclav Hapla       ierr = DMLabelDestroy(&link->label);CHKERRQ(ierr);
776443e45a93SVaclav Hapla       ierr = PetscFree(link);CHKERRQ(ierr);
7765306894acSVaclav Hapla       break;
7766306894acSVaclav Hapla     }
7767306894acSVaclav Hapla   }
7768306894acSVaclav Hapla   if (!hasLabel && failNotFound) SETERRQ(PetscObjectComm((PetscObject)dm), PETSC_ERR_ARG_WRONG, "Given label not found in DM");
7769306894acSVaclav Hapla   PetscFunctionReturn(0);
7770306894acSVaclav Hapla }
7771306894acSVaclav Hapla 
7772c58f1c22SToby Isaac /*@C
7773c58f1c22SToby Isaac   DMGetLabelOutput - Get the output flag for a given label
7774c58f1c22SToby Isaac 
7775c58f1c22SToby Isaac   Not Collective
7776c58f1c22SToby Isaac 
7777c58f1c22SToby Isaac   Input Parameters:
7778c58f1c22SToby Isaac + dm   - The DM object
7779c58f1c22SToby Isaac - name - The label name
7780c58f1c22SToby Isaac 
7781c58f1c22SToby Isaac   Output Parameter:
7782c58f1c22SToby Isaac . output - The flag for output
7783c58f1c22SToby Isaac 
7784c58f1c22SToby Isaac   Level: developer
7785c58f1c22SToby Isaac 
7786c58f1c22SToby Isaac .seealso: DMSetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7787c58f1c22SToby Isaac @*/
7788c58f1c22SToby Isaac PetscErrorCode DMGetLabelOutput(DM dm, const char name[], PetscBool *output)
7789c58f1c22SToby Isaac {
77905d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7791d67d17b1SMatthew G. Knepley   const char    *lname;
7792c58f1c22SToby Isaac   PetscErrorCode ierr;
7793c58f1c22SToby Isaac 
7794c58f1c22SToby Isaac   PetscFunctionBegin;
7795c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7796c58f1c22SToby Isaac   PetscValidPointer(name, 2);
7797c58f1c22SToby Isaac   PetscValidPointer(output, 3);
7798c58f1c22SToby Isaac   while (next) {
7799c58f1c22SToby Isaac     PetscBool flg;
7800c58f1c22SToby Isaac 
7801d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7802d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7803c58f1c22SToby Isaac     if (flg) {*output = next->output; PetscFunctionReturn(0);}
7804c58f1c22SToby Isaac     next = next->next;
7805c58f1c22SToby Isaac   }
7806c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7807c58f1c22SToby Isaac }
7808c58f1c22SToby Isaac 
7809c58f1c22SToby Isaac /*@C
7810c58f1c22SToby Isaac   DMSetLabelOutput - Set the output flag for a given label
7811c58f1c22SToby Isaac 
7812c58f1c22SToby Isaac   Not Collective
7813c58f1c22SToby Isaac 
7814c58f1c22SToby Isaac   Input Parameters:
7815c58f1c22SToby Isaac + dm     - The DM object
7816c58f1c22SToby Isaac . name   - The label name
7817c58f1c22SToby Isaac - output - The flag for output
7818c58f1c22SToby Isaac 
7819c58f1c22SToby Isaac   Level: developer
7820c58f1c22SToby Isaac 
7821c58f1c22SToby Isaac .seealso: DMGetLabelOutput(), DMCreateLabel(), DMHasLabel(), DMGetLabelValue(), DMSetLabelValue(), DMGetStratumIS()
7822c58f1c22SToby Isaac @*/
7823c58f1c22SToby Isaac PetscErrorCode DMSetLabelOutput(DM dm, const char name[], PetscBool output)
7824c58f1c22SToby Isaac {
78255d80c0bfSVaclav Hapla   DMLabelLink    next = dm->labels;
7826d67d17b1SMatthew G. Knepley   const char    *lname;
7827c58f1c22SToby Isaac   PetscErrorCode ierr;
7828c58f1c22SToby Isaac 
7829c58f1c22SToby Isaac   PetscFunctionBegin;
7830c58f1c22SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7831534a8f05SLisandro Dalcin   PetscValidCharPointer(name, 2);
7832c58f1c22SToby Isaac   while (next) {
7833c58f1c22SToby Isaac     PetscBool flg;
7834c58f1c22SToby Isaac 
7835d67d17b1SMatthew G. Knepley     ierr = PetscObjectGetName((PetscObject) next->label, &lname);CHKERRQ(ierr);
7836d67d17b1SMatthew G. Knepley     ierr = PetscStrcmp(name, lname, &flg);CHKERRQ(ierr);
7837c58f1c22SToby Isaac     if (flg) {next->output = output; PetscFunctionReturn(0);}
7838c58f1c22SToby Isaac     next = next->next;
7839c58f1c22SToby Isaac   }
7840c58f1c22SToby Isaac   SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "No label named %s was present in this dm", name);
7841c58f1c22SToby Isaac }
7842c58f1c22SToby Isaac 
7843c58f1c22SToby Isaac /*@
7844c58f1c22SToby Isaac   DMCopyLabels - Copy labels from one mesh to another with a superset of the points
7845c58f1c22SToby Isaac 
7846d083f849SBarry Smith   Collective on dmA
7847c58f1c22SToby Isaac 
7848c58f1c22SToby Isaac   Input Parameter:
78495d80c0bfSVaclav Hapla + dmA - The DM object with initial labels
78502e17dfb7SMatthew G. Knepley . dmB - The DM object with copied labels
78515d80c0bfSVaclav Hapla . mode - Copy labels by pointers (PETSC_OWN_POINTER) or duplicate them (PETSC_COPY_VALUES)
7852ba2698f1SMatthew G. Knepley - all  - Copy all labels including "depth", "dim", and "celltype" (PETSC_TRUE) which are otherwise ignored (PETSC_FALSE)
7853c58f1c22SToby Isaac 
7854c58f1c22SToby Isaac   Level: intermediate
7855c58f1c22SToby Isaac 
7856c58f1c22SToby Isaac   Note: This is typically used when interpolating or otherwise adding to a mesh
7857c58f1c22SToby Isaac 
78585d80c0bfSVaclav Hapla .seealso: DMGetCoordinates(), DMGetCoordinatesLocal(), DMGetCoordinateDM(), DMGetCoordinateSection(), DMShareLabels()
7859c58f1c22SToby Isaac @*/
78605d80c0bfSVaclav Hapla PetscErrorCode DMCopyLabels(DM dmA, DM dmB, PetscCopyMode mode, PetscBool all)
7861c58f1c22SToby Isaac {
7862c58f1c22SToby Isaac   DMLabel        label, labelNew;
7863c58f1c22SToby Isaac   const char    *name;
7864c58f1c22SToby Isaac   PetscBool      flg;
78655d80c0bfSVaclav Hapla   DMLabelLink    link;
78665d80c0bfSVaclav Hapla   PetscErrorCode ierr;
7867c58f1c22SToby Isaac 
78685d80c0bfSVaclav Hapla   PetscFunctionBegin;
78695d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmA, DM_CLASSID, 1);
78705d80c0bfSVaclav Hapla   PetscValidHeaderSpecific(dmB, DM_CLASSID, 2);
78715d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveEnum(dmA, mode,3);
78725d80c0bfSVaclav Hapla   PetscValidLogicalCollectiveBool(dmA, all, 4);
78735d80c0bfSVaclav Hapla   if (mode==PETSC_USE_POINTER) SETERRQ(PetscObjectComm((PetscObject)dmA), PETSC_ERR_SUP, "PETSC_USE_POINTER not supported for objects");
78745d80c0bfSVaclav Hapla   if (dmA == dmB) PetscFunctionReturn(0);
78755d80c0bfSVaclav Hapla   for (link=dmA->labels; link; link=link->next) {
78765d80c0bfSVaclav Hapla     label=link->label;
78775d80c0bfSVaclav Hapla     ierr = PetscObjectGetName((PetscObject)label, &name);CHKERRQ(ierr);
78785d80c0bfSVaclav Hapla     if (!all) {
7879c58f1c22SToby Isaac       ierr = PetscStrcmp(name, "depth", &flg);CHKERRQ(ierr);
7880c58f1c22SToby Isaac       if (flg) continue;
78817d5acc75SStefano Zampini       ierr = PetscStrcmp(name, "dim", &flg);CHKERRQ(ierr);
78827d5acc75SStefano Zampini       if (flg) continue;
7883ba2698f1SMatthew G. Knepley       ierr = PetscStrcmp(name, "celltype", &flg);CHKERRQ(ierr);
7884ba2698f1SMatthew G. Knepley       if (flg) continue;
78855d80c0bfSVaclav Hapla     }
78865d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {
7887c58f1c22SToby Isaac       ierr = DMLabelDuplicate(label, &labelNew);CHKERRQ(ierr);
78885d80c0bfSVaclav Hapla     } else {
78895d80c0bfSVaclav Hapla       labelNew = label;
78905d80c0bfSVaclav Hapla     }
7891c58f1c22SToby Isaac     ierr = DMAddLabel(dmB, labelNew);CHKERRQ(ierr);
78925d80c0bfSVaclav Hapla     if (mode==PETSC_COPY_VALUES) {ierr = DMLabelDestroy(&labelNew);CHKERRQ(ierr);}
7893c58f1c22SToby Isaac   }
7894c58f1c22SToby Isaac   PetscFunctionReturn(0);
7895c58f1c22SToby Isaac }
7896a8fb8f29SToby Isaac 
7897a8fb8f29SToby Isaac /*@
7898a8fb8f29SToby Isaac   DMGetCoarseDM - Get the coarse mesh from which this was obtained by refinement
7899a8fb8f29SToby Isaac 
7900a8fb8f29SToby Isaac   Input Parameter:
7901a8fb8f29SToby Isaac . dm - The DM object
7902a8fb8f29SToby Isaac 
7903a8fb8f29SToby Isaac   Output Parameter:
7904a8fb8f29SToby Isaac . cdm - The coarse DM
7905a8fb8f29SToby Isaac 
7906a8fb8f29SToby Isaac   Level: intermediate
7907a8fb8f29SToby Isaac 
7908a8fb8f29SToby Isaac .seealso: DMSetCoarseDM()
7909a8fb8f29SToby Isaac @*/
7910a8fb8f29SToby Isaac PetscErrorCode DMGetCoarseDM(DM dm, DM *cdm)
7911a8fb8f29SToby Isaac {
7912a8fb8f29SToby Isaac   PetscFunctionBegin;
7913a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7914a8fb8f29SToby Isaac   PetscValidPointer(cdm, 2);
7915a8fb8f29SToby Isaac   *cdm = dm->coarseMesh;
7916a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7917a8fb8f29SToby Isaac }
7918a8fb8f29SToby Isaac 
7919a8fb8f29SToby Isaac /*@
7920a8fb8f29SToby Isaac   DMSetCoarseDM - Set the coarse mesh from which this was obtained by refinement
7921a8fb8f29SToby Isaac 
7922a8fb8f29SToby Isaac   Input Parameters:
7923a8fb8f29SToby Isaac + dm - The DM object
7924a8fb8f29SToby Isaac - cdm - The coarse DM
7925a8fb8f29SToby Isaac 
7926a8fb8f29SToby Isaac   Level: intermediate
7927a8fb8f29SToby Isaac 
7928a8fb8f29SToby Isaac .seealso: DMGetCoarseDM()
7929a8fb8f29SToby Isaac @*/
7930a8fb8f29SToby Isaac PetscErrorCode DMSetCoarseDM(DM dm, DM cdm)
7931a8fb8f29SToby Isaac {
7932a8fb8f29SToby Isaac   PetscErrorCode ierr;
7933a8fb8f29SToby Isaac 
7934a8fb8f29SToby Isaac   PetscFunctionBegin;
7935a8fb8f29SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
7936a8fb8f29SToby Isaac   if (cdm) PetscValidHeaderSpecific(cdm, DM_CLASSID, 2);
7937a8fb8f29SToby Isaac   ierr = PetscObjectReference((PetscObject)cdm);CHKERRQ(ierr);
7938a8fb8f29SToby Isaac   ierr = DMDestroy(&dm->coarseMesh);CHKERRQ(ierr);
7939a8fb8f29SToby Isaac   dm->coarseMesh = cdm;
7940a8fb8f29SToby Isaac   PetscFunctionReturn(0);
7941a8fb8f29SToby Isaac }
7942a8fb8f29SToby Isaac 
794388bdff64SToby Isaac /*@
794488bdff64SToby Isaac   DMGetFineDM - Get the fine mesh from which this was obtained by refinement
794588bdff64SToby Isaac 
794688bdff64SToby Isaac   Input Parameter:
794788bdff64SToby Isaac . dm - The DM object
794888bdff64SToby Isaac 
794988bdff64SToby Isaac   Output Parameter:
795088bdff64SToby Isaac . fdm - The fine DM
795188bdff64SToby Isaac 
795288bdff64SToby Isaac   Level: intermediate
795388bdff64SToby Isaac 
795488bdff64SToby Isaac .seealso: DMSetFineDM()
795588bdff64SToby Isaac @*/
795688bdff64SToby Isaac PetscErrorCode DMGetFineDM(DM dm, DM *fdm)
795788bdff64SToby Isaac {
795888bdff64SToby Isaac   PetscFunctionBegin;
795988bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
796088bdff64SToby Isaac   PetscValidPointer(fdm, 2);
796188bdff64SToby Isaac   *fdm = dm->fineMesh;
796288bdff64SToby Isaac   PetscFunctionReturn(0);
796388bdff64SToby Isaac }
796488bdff64SToby Isaac 
796588bdff64SToby Isaac /*@
796688bdff64SToby Isaac   DMSetFineDM - Set the fine mesh from which this was obtained by refinement
796788bdff64SToby Isaac 
796888bdff64SToby Isaac   Input Parameters:
796988bdff64SToby Isaac + dm - The DM object
797088bdff64SToby Isaac - fdm - The fine DM
797188bdff64SToby Isaac 
797288bdff64SToby Isaac   Level: intermediate
797388bdff64SToby Isaac 
797488bdff64SToby Isaac .seealso: DMGetFineDM()
797588bdff64SToby Isaac @*/
797688bdff64SToby Isaac PetscErrorCode DMSetFineDM(DM dm, DM fdm)
797788bdff64SToby Isaac {
797888bdff64SToby Isaac   PetscErrorCode ierr;
797988bdff64SToby Isaac 
798088bdff64SToby Isaac   PetscFunctionBegin;
798188bdff64SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
798288bdff64SToby Isaac   if (fdm) PetscValidHeaderSpecific(fdm, DM_CLASSID, 2);
798388bdff64SToby Isaac   ierr = PetscObjectReference((PetscObject)fdm);CHKERRQ(ierr);
798488bdff64SToby Isaac   ierr = DMDestroy(&dm->fineMesh);CHKERRQ(ierr);
798588bdff64SToby Isaac   dm->fineMesh = fdm;
798688bdff64SToby Isaac   PetscFunctionReturn(0);
798788bdff64SToby Isaac }
798888bdff64SToby Isaac 
7989a6ba4734SToby Isaac /*=== DMBoundary code ===*/
7990a6ba4734SToby Isaac 
7991a6ba4734SToby Isaac PetscErrorCode DMCopyBoundary(DM dm, DM dmNew)
7992a6ba4734SToby Isaac {
7993e5e52638SMatthew G. Knepley   PetscInt       d;
7994a6ba4734SToby Isaac   PetscErrorCode ierr;
7995a6ba4734SToby Isaac 
7996a6ba4734SToby Isaac   PetscFunctionBegin;
7997e5e52638SMatthew G. Knepley   for (d = 0; d < dm->Nds; ++d) {
7998e5e52638SMatthew G. Knepley     ierr = PetscDSCopyBoundary(dm->probs[d].ds, dmNew->probs[d].ds);CHKERRQ(ierr);
7999e5e52638SMatthew G. Knepley   }
8000a6ba4734SToby Isaac   PetscFunctionReturn(0);
8001a6ba4734SToby Isaac }
8002a6ba4734SToby Isaac 
8003a6ba4734SToby Isaac /*@C
8004a6ba4734SToby Isaac   DMAddBoundary - Add a boundary condition to the model
8005a6ba4734SToby Isaac 
8006783e2ec8SMatthew G. Knepley   Collective on dm
8007783e2ec8SMatthew G. Knepley 
8008a6ba4734SToby Isaac   Input Parameters:
80094c258f51SMatthew G. Knepley + dm          - The DM, with a PetscDS that matches the problem being constrained
8010f971fd6bSMatthew G. Knepley . type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8011a6ba4734SToby Isaac . name        - The BC name
8012a6ba4734SToby Isaac . labelname   - The label defining constrained points
8013a6ba4734SToby Isaac . field       - The field to constrain
8014e8ecbf3fSStefano Zampini . numcomps    - The number of constrained field components (0 will constrain all fields)
8015a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8016a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
801756cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time deriative of the boundary values, or NULL
8018a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8019a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8020a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8021a6ba4734SToby Isaac 
8022a6ba4734SToby Isaac   Options Database Keys:
8023a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8024a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8025a6ba4734SToby Isaac 
802656cf3b9cSMatthew G. Knepley   Note:
802756cf3b9cSMatthew 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:
802856cf3b9cSMatthew G. Knepley 
802956cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar bcval[])
803056cf3b9cSMatthew G. Knepley 
803156cf3b9cSMatthew G. Knepley   If the type is DM_BC_ESSENTIAL_FIELD or other _FIELD value, then the calling sequence is:
803256cf3b9cSMatthew G. Knepley 
803356cf3b9cSMatthew G. Knepley $ bcFunc(PetscInt dim, PetscInt Nf, PetscInt NfAux,
803456cf3b9cSMatthew G. Knepley $        const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
803556cf3b9cSMatthew G. Knepley $        const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
803656cf3b9cSMatthew G. Knepley $        PetscReal time, const PetscReal x[], PetscScalar bcval[])
803756cf3b9cSMatthew G. Knepley 
803856cf3b9cSMatthew G. Knepley + dim - the spatial dimension
803956cf3b9cSMatthew G. Knepley . Nf - the number of fields
804056cf3b9cSMatthew G. Knepley . uOff - the offset into u[] and u_t[] for each field
804156cf3b9cSMatthew G. Knepley . uOff_x - the offset into u_x[] for each field
804256cf3b9cSMatthew G. Knepley . u - each field evaluated at the current point
804356cf3b9cSMatthew G. Knepley . u_t - the time derivative of each field evaluated at the current point
804456cf3b9cSMatthew G. Knepley . u_x - the gradient of each field evaluated at the current point
804556cf3b9cSMatthew G. Knepley . aOff - the offset into a[] and a_t[] for each auxiliary field
804656cf3b9cSMatthew G. Knepley . aOff_x - the offset into a_x[] for each auxiliary field
804756cf3b9cSMatthew G. Knepley . a - each auxiliary field evaluated at the current point
804856cf3b9cSMatthew G. Knepley . a_t - the time derivative of each auxiliary field evaluated at the current point
804956cf3b9cSMatthew G. Knepley . a_x - the gradient of auxiliary each field evaluated at the current point
805056cf3b9cSMatthew G. Knepley . t - current time
805156cf3b9cSMatthew G. Knepley . x - coordinates of the current point
805256cf3b9cSMatthew G. Knepley . numConstants - number of constant parameters
805356cf3b9cSMatthew G. Knepley . constants - constant parameters
805456cf3b9cSMatthew G. Knepley - bcval - output values at the current point
805556cf3b9cSMatthew G. Knepley 
8056a6ba4734SToby Isaac   Level: developer
8057a6ba4734SToby Isaac 
805856cf3b9cSMatthew G. Knepley .seealso: DMGetBoundary(), PetscDSAddBoundary()
8059a6ba4734SToby Isaac @*/
806056cf3b9cSMatthew 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)
8061a6ba4734SToby Isaac {
8062e5e52638SMatthew G. Knepley   PetscDS        ds;
8063a6ba4734SToby Isaac   PetscErrorCode ierr;
8064a6ba4734SToby Isaac 
8065a6ba4734SToby Isaac   PetscFunctionBegin;
8066a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8067783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveEnum(dm, type, 2);
8068783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, field, 5);
8069783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numcomps, 6);
8070783e2ec8SMatthew G. Knepley   PetscValidLogicalCollectiveInt(dm, numids, 9);
8071e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8072783e2ec8SMatthew G. Knepley   ierr = DMCompleteBoundaryLabel_Internal(dm, ds, field, PETSC_MAX_INT, labelname);CHKERRQ(ierr);
807356cf3b9cSMatthew G. Knepley   ierr = PetscDSAddBoundary(ds, type,name, labelname, field, numcomps, comps, bcFunc, bcFunc_t, numids, ids, ctx);CHKERRQ(ierr);
8074a6ba4734SToby Isaac   PetscFunctionReturn(0);
8075a6ba4734SToby Isaac }
8076a6ba4734SToby Isaac 
8077a6ba4734SToby Isaac /*@
8078a6ba4734SToby Isaac   DMGetNumBoundary - Get the number of registered BC
8079a6ba4734SToby Isaac 
8080a6ba4734SToby Isaac   Input Parameters:
8081a6ba4734SToby Isaac . dm - The mesh object
8082a6ba4734SToby Isaac 
8083a6ba4734SToby Isaac   Output Parameters:
8084a6ba4734SToby Isaac . numBd - The number of BC
8085a6ba4734SToby Isaac 
8086a6ba4734SToby Isaac   Level: intermediate
8087a6ba4734SToby Isaac 
8088a6ba4734SToby Isaac .seealso: DMAddBoundary(), DMGetBoundary()
8089a6ba4734SToby Isaac @*/
8090a6ba4734SToby Isaac PetscErrorCode DMGetNumBoundary(DM dm, PetscInt *numBd)
8091a6ba4734SToby Isaac {
8092e5e52638SMatthew G. Knepley   PetscDS        ds;
809358ebd649SToby Isaac   PetscErrorCode ierr;
8094a6ba4734SToby Isaac 
8095a6ba4734SToby Isaac   PetscFunctionBegin;
8096a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8097e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8098e5e52638SMatthew G. Knepley   ierr = PetscDSGetNumBoundary(ds, numBd);CHKERRQ(ierr);
8099a6ba4734SToby Isaac   PetscFunctionReturn(0);
8100a6ba4734SToby Isaac }
8101a6ba4734SToby Isaac 
8102a6ba4734SToby Isaac /*@C
81031c531cf8SMatthew G. Knepley   DMGetBoundary - Get a model boundary condition
8104a6ba4734SToby Isaac 
8105a6ba4734SToby Isaac   Input Parameters:
8106a6ba4734SToby Isaac + dm          - The mesh object
8107a6ba4734SToby Isaac - bd          - The BC number
8108a6ba4734SToby Isaac 
8109a6ba4734SToby Isaac   Output Parameters:
8110f971fd6bSMatthew G. Knepley + type        - The type of condition, e.g. DM_BC_ESSENTIAL_ANALYTIC/DM_BC_ESSENTIAL_FIELD (Dirichlet), or DM_BC_NATURAL (Neumann)
8111a6ba4734SToby Isaac . name        - The BC name
8112a6ba4734SToby Isaac . labelname   - The label defining constrained points
8113a6ba4734SToby Isaac . field       - The field to constrain
8114a6ba4734SToby Isaac . numcomps    - The number of constrained field components
8115a6ba4734SToby Isaac . comps       - An array of constrained component numbers
8116a6ba4734SToby Isaac . bcFunc      - A pointwise function giving boundary values
811756cf3b9cSMatthew G. Knepley . bcFunc_t    - A pointwise function giving the time derviative of the boundary values
8118a6ba4734SToby Isaac . numids      - The number of DMLabel ids for constrained points
8119a6ba4734SToby Isaac . ids         - An array of ids for constrained points
8120a6ba4734SToby Isaac - ctx         - An optional user context for bcFunc
8121a6ba4734SToby Isaac 
8122a6ba4734SToby Isaac   Options Database Keys:
8123a6ba4734SToby Isaac + -bc_<boundary name> <num> - Overrides the boundary ids
8124a6ba4734SToby Isaac - -bc_<boundary name>_comp <num> - Overrides the boundary components
8125a6ba4734SToby Isaac 
8126a6ba4734SToby Isaac   Level: developer
8127a6ba4734SToby Isaac 
8128a6ba4734SToby Isaac .seealso: DMAddBoundary()
8129a6ba4734SToby Isaac @*/
813056cf3b9cSMatthew 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)
8131a6ba4734SToby Isaac {
8132e5e52638SMatthew G. Knepley   PetscDS        ds;
813358ebd649SToby Isaac   PetscErrorCode ierr;
8134a6ba4734SToby Isaac 
8135a6ba4734SToby Isaac   PetscFunctionBegin;
8136a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8137e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
813856cf3b9cSMatthew G. Knepley   ierr = PetscDSGetBoundary(ds, bd, type, name, labelname, field, numcomps, comps, func, func_t, numids, ids, ctx);CHKERRQ(ierr);
8139a6ba4734SToby Isaac   PetscFunctionReturn(0);
8140a6ba4734SToby Isaac }
8141a6ba4734SToby Isaac 
8142e6f8dbb6SToby Isaac static PetscErrorCode DMPopulateBoundary(DM dm)
8143e6f8dbb6SToby Isaac {
8144e5e52638SMatthew G. Knepley   PetscDS        ds;
8145dff059c6SToby Isaac   DMBoundary    *lastnext;
8146e6f8dbb6SToby Isaac   DSBoundary     dsbound;
8147e6f8dbb6SToby Isaac   PetscErrorCode ierr;
8148e6f8dbb6SToby Isaac 
8149e6f8dbb6SToby Isaac   PetscFunctionBegin;
8150e5e52638SMatthew G. Knepley   ierr = DMGetDS(dm, &ds);CHKERRQ(ierr);
8151e5e52638SMatthew G. Knepley   dsbound = ds->boundary;
815247a1f5adSToby Isaac   if (dm->boundary) {
815347a1f5adSToby Isaac     DMBoundary next = dm->boundary;
815447a1f5adSToby Isaac 
815547a1f5adSToby Isaac     /* quick check to see if the PetscDS has changed */
815647a1f5adSToby Isaac     if (next->dsboundary == dsbound) PetscFunctionReturn(0);
815747a1f5adSToby Isaac     /* the PetscDS has changed: tear down and rebuild */
815847a1f5adSToby Isaac     while (next) {
815947a1f5adSToby Isaac       DMBoundary b = next;
816047a1f5adSToby Isaac 
816147a1f5adSToby Isaac       next = b->next;
816247a1f5adSToby Isaac       ierr = PetscFree(b);CHKERRQ(ierr);
8163a6ba4734SToby Isaac     }
816447a1f5adSToby Isaac     dm->boundary = NULL;
8165a6ba4734SToby Isaac   }
816647a1f5adSToby Isaac 
8167dff059c6SToby Isaac   lastnext = &(dm->boundary);
8168e6f8dbb6SToby Isaac   while (dsbound) {
8169e6f8dbb6SToby Isaac     DMBoundary dmbound;
8170e6f8dbb6SToby Isaac 
8171e6f8dbb6SToby Isaac     ierr = PetscNew(&dmbound);CHKERRQ(ierr);
8172e6f8dbb6SToby Isaac     dmbound->dsboundary = dsbound;
8173e6f8dbb6SToby Isaac     ierr = DMGetLabel(dm, dsbound->labelname, &(dmbound->label));CHKERRQ(ierr);
8174994fe344SLisandro 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);}
817547a1f5adSToby Isaac     /* push on the back instead of the front so that it is in the same order as in the PetscDS */
8176dff059c6SToby Isaac     *lastnext = dmbound;
8177dff059c6SToby Isaac     lastnext = &(dmbound->next);
8178dff059c6SToby Isaac     dsbound = dsbound->next;
8179a6ba4734SToby Isaac   }
8180a6ba4734SToby Isaac   PetscFunctionReturn(0);
8181a6ba4734SToby Isaac }
8182a6ba4734SToby Isaac 
8183a6ba4734SToby Isaac PetscErrorCode DMIsBoundaryPoint(DM dm, PetscInt point, PetscBool *isBd)
8184a6ba4734SToby Isaac {
8185b95f2879SToby Isaac   DMBoundary     b;
8186a6ba4734SToby Isaac   PetscErrorCode ierr;
8187a6ba4734SToby Isaac 
8188a6ba4734SToby Isaac   PetscFunctionBegin;
8189a6ba4734SToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8190534a8f05SLisandro Dalcin   PetscValidBoolPointer(isBd, 3);
8191a6ba4734SToby Isaac   *isBd = PETSC_FALSE;
8192e6f8dbb6SToby Isaac   ierr = DMPopulateBoundary(dm);CHKERRQ(ierr);
8193b95f2879SToby Isaac   b = dm->boundary;
8194a6ba4734SToby Isaac   while (b && !(*isBd)) {
8195e6f8dbb6SToby Isaac     DMLabel    label = b->label;
8196e6f8dbb6SToby Isaac     DSBoundary dsb = b->dsboundary;
81973424c85cSToby Isaac 
81983424c85cSToby Isaac     if (label) {
8199a6ba4734SToby Isaac       PetscInt i;
8200a6ba4734SToby Isaac 
8201e6f8dbb6SToby Isaac       for (i = 0; i < dsb->numids && !(*isBd); ++i) {
8202e6f8dbb6SToby Isaac         ierr = DMLabelStratumHasPoint(label, dsb->ids[i], point, isBd);CHKERRQ(ierr);
8203a6ba4734SToby Isaac       }
8204a6ba4734SToby Isaac     }
8205a6ba4734SToby Isaac     b = b->next;
8206a6ba4734SToby Isaac   }
8207a6ba4734SToby Isaac   PetscFunctionReturn(0);
8208a6ba4734SToby Isaac }
82094d6f44ffSToby Isaac 
82104d6f44ffSToby Isaac /*@C
8211a6e0b375SMatthew G. Knepley   DMProjectFunction - This projects the given function into the function space provided, putting the coefficients in a global vector.
8212a6e0b375SMatthew G. Knepley 
8213a6e0b375SMatthew G. Knepley   Collective on DM
82144d6f44ffSToby Isaac 
82154d6f44ffSToby Isaac   Input Parameters:
82164d6f44ffSToby Isaac + dm      - The DM
82170709b2feSToby Isaac . time    - The time
82184d6f44ffSToby Isaac . funcs   - The coordinate functions to evaluate, one per field
82194d6f44ffSToby Isaac . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
82204d6f44ffSToby Isaac - mode    - The insertion mode for values
82214d6f44ffSToby Isaac 
82224d6f44ffSToby Isaac   Output Parameter:
82234d6f44ffSToby Isaac . X - vector
82244d6f44ffSToby Isaac 
82254d6f44ffSToby Isaac    Calling sequence of func:
82260709b2feSToby Isaac $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
82274d6f44ffSToby Isaac 
82284d6f44ffSToby Isaac +  dim - The spatial dimension
82298ec8862eSJed Brown .  time - The time at which to sample
82304d6f44ffSToby Isaac .  x   - The coordinates
82314d6f44ffSToby Isaac .  Nf  - The number of fields
82324d6f44ffSToby Isaac .  u   - The output field values
82334d6f44ffSToby Isaac -  ctx - optional user-defined function context
82344d6f44ffSToby Isaac 
82354d6f44ffSToby Isaac   Level: developer
82364d6f44ffSToby Isaac 
8237a6e0b375SMatthew G. Knepley .seealso: DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
82384d6f44ffSToby Isaac @*/
82390709b2feSToby Isaac PetscErrorCode DMProjectFunction(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec X)
82404d6f44ffSToby Isaac {
82414d6f44ffSToby Isaac   Vec            localX;
82424d6f44ffSToby Isaac   PetscErrorCode ierr;
82434d6f44ffSToby Isaac 
82444d6f44ffSToby Isaac   PetscFunctionBegin;
82454d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
82464d6f44ffSToby Isaac   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
82470709b2feSToby Isaac   ierr = DMProjectFunctionLocal(dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82484d6f44ffSToby Isaac   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
82494d6f44ffSToby Isaac   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
82504d6f44ffSToby Isaac   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
82514d6f44ffSToby Isaac   PetscFunctionReturn(0);
82524d6f44ffSToby Isaac }
82534d6f44ffSToby Isaac 
8254a6e0b375SMatthew G. Knepley /*@C
8255a6e0b375SMatthew G. Knepley   DMProjectFunctionLocal - This projects the given function into the function space provided, putting the coefficients in a local vector.
8256a6e0b375SMatthew G. Knepley 
8257a6e0b375SMatthew G. Knepley   Not collective
8258a6e0b375SMatthew G. Knepley 
8259a6e0b375SMatthew G. Knepley   Input Parameters:
8260a6e0b375SMatthew G. Knepley + dm      - The DM
8261a6e0b375SMatthew G. Knepley . time    - The time
8262a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8263a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8264a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8265a6e0b375SMatthew G. Knepley 
8266a6e0b375SMatthew G. Knepley   Output Parameter:
8267a6e0b375SMatthew G. Knepley . localX - vector
8268a6e0b375SMatthew G. Knepley 
8269a6e0b375SMatthew G. Knepley    Calling sequence of func:
8270a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8271a6e0b375SMatthew G. Knepley 
8272a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8273a6e0b375SMatthew G. Knepley .  x   - The coordinates
8274a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8275a6e0b375SMatthew G. Knepley .  u   - The output field values
8276a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8277a6e0b375SMatthew G. Knepley 
8278a6e0b375SMatthew G. Knepley   Level: developer
8279a6e0b375SMatthew G. Knepley 
8280a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLabel(), DMComputeL2Diff()
8281a6e0b375SMatthew G. Knepley @*/
82820709b2feSToby Isaac PetscErrorCode DMProjectFunctionLocal(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, InsertMode mode, Vec localX)
82834d6f44ffSToby Isaac {
82844d6f44ffSToby Isaac   PetscErrorCode ierr;
82854d6f44ffSToby Isaac 
82864d6f44ffSToby Isaac   PetscFunctionBegin;
82874d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
82884d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
82890918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLocal",((PetscObject)dm)->type_name);
82900709b2feSToby Isaac   ierr = (dm->ops->projectfunctionlocal) (dm, time, funcs, ctxs, mode, localX);CHKERRQ(ierr);
82914d6f44ffSToby Isaac   PetscFunctionReturn(0);
82924d6f44ffSToby Isaac }
82934d6f44ffSToby Isaac 
8294a6e0b375SMatthew G. Knepley /*@C
8295a6e0b375SMatthew 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.
8296a6e0b375SMatthew G. Knepley 
8297a6e0b375SMatthew G. Knepley   Collective on DM
8298a6e0b375SMatthew G. Knepley 
8299a6e0b375SMatthew G. Knepley   Input Parameters:
8300a6e0b375SMatthew G. Knepley + dm      - The DM
8301a6e0b375SMatthew G. Knepley . time    - The time
8302a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8303a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8304a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8305a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8306a6e0b375SMatthew G. Knepley 
8307a6e0b375SMatthew G. Knepley   Output Parameter:
8308a6e0b375SMatthew G. Knepley . X - vector
8309a6e0b375SMatthew G. Knepley 
8310a6e0b375SMatthew G. Knepley    Calling sequence of func:
8311a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8312a6e0b375SMatthew G. Knepley 
8313a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8314a6e0b375SMatthew G. Knepley .  x   - The coordinates
8315a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8316a6e0b375SMatthew G. Knepley .  u   - The output field values
8317a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8318a6e0b375SMatthew G. Knepley 
8319a6e0b375SMatthew G. Knepley   Level: developer
8320a6e0b375SMatthew G. Knepley 
8321a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabelLocal(), DMComputeL2Diff()
8322a6e0b375SMatthew G. Knepley @*/
83232c53366bSMatthew 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)
83242c53366bSMatthew G. Knepley {
83252c53366bSMatthew G. Knepley   Vec            localX;
83262c53366bSMatthew G. Knepley   PetscErrorCode ierr;
83272c53366bSMatthew G. Knepley 
83282c53366bSMatthew G. Knepley   PetscFunctionBegin;
83292c53366bSMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
83302c53366bSMatthew G. Knepley   ierr = DMGetLocalVector(dm, &localX);CHKERRQ(ierr);
83312c53366bSMatthew G. Knepley   ierr = DMProjectFunctionLabelLocal(dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83322c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalBegin(dm, localX, mode, X);CHKERRQ(ierr);
83332c53366bSMatthew G. Knepley   ierr = DMLocalToGlobalEnd(dm, localX, mode, X);CHKERRQ(ierr);
83342c53366bSMatthew G. Knepley   ierr = DMRestoreLocalVector(dm, &localX);CHKERRQ(ierr);
83352c53366bSMatthew G. Knepley   PetscFunctionReturn(0);
83362c53366bSMatthew G. Knepley }
83372c53366bSMatthew G. Knepley 
8338a6e0b375SMatthew G. Knepley /*@C
8339a6e0b375SMatthew 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.
8340a6e0b375SMatthew G. Knepley 
8341a6e0b375SMatthew G. Knepley   Not collective
8342a6e0b375SMatthew G. Knepley 
8343a6e0b375SMatthew G. Knepley   Input Parameters:
8344a6e0b375SMatthew G. Knepley + dm      - The DM
8345a6e0b375SMatthew G. Knepley . time    - The time
8346a6e0b375SMatthew G. Knepley . label   - The DMLabel selecting the portion of the mesh for projection
8347a6e0b375SMatthew G. Knepley . funcs   - The coordinate functions to evaluate, one per field
8348a6e0b375SMatthew G. Knepley . ctxs    - Optional array of contexts to pass to each coordinate function.  ctxs itself may be null.
8349a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8350a6e0b375SMatthew G. Knepley 
8351a6e0b375SMatthew G. Knepley   Output Parameter:
8352a6e0b375SMatthew G. Knepley . localX - vector
8353a6e0b375SMatthew G. Knepley 
8354a6e0b375SMatthew G. Knepley    Calling sequence of func:
8355a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf, PetscScalar u[], void *ctx);
8356a6e0b375SMatthew G. Knepley 
8357a6e0b375SMatthew G. Knepley +  dim - The spatial dimension
8358a6e0b375SMatthew G. Knepley .  x   - The coordinates
8359a6e0b375SMatthew G. Knepley .  Nf  - The number of fields
8360a6e0b375SMatthew G. Knepley .  u   - The output field values
8361a6e0b375SMatthew G. Knepley -  ctx - optional user-defined function context
8362a6e0b375SMatthew G. Knepley 
8363a6e0b375SMatthew G. Knepley   Level: developer
8364a6e0b375SMatthew G. Knepley 
8365a6e0b375SMatthew G. Knepley .seealso: DMProjectFunction(), DMProjectFunctionLocal(), DMProjectFunctionLabel(), DMComputeL2Diff()
8366a6e0b375SMatthew G. Knepley @*/
83671c531cf8SMatthew 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)
83684d6f44ffSToby Isaac {
83694d6f44ffSToby Isaac   PetscErrorCode ierr;
83704d6f44ffSToby Isaac 
83714d6f44ffSToby Isaac   PetscFunctionBegin;
83724d6f44ffSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
83734d6f44ffSToby Isaac   PetscValidHeaderSpecific(localX,VEC_CLASSID,5);
83740918c465SMatthew G. Knepley   if (!dm->ops->projectfunctionlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFunctionLabelLocal",((PetscObject)dm)->type_name);
83751c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfunctionlabellocal) (dm, time, label, numIds, ids, Nc, comps, funcs, ctxs, mode, localX);CHKERRQ(ierr);
83764d6f44ffSToby Isaac   PetscFunctionReturn(0);
83774d6f44ffSToby Isaac }
83782716604bSToby Isaac 
8379a6e0b375SMatthew G. Knepley /*@C
8380a6e0b375SMatthew G. Knepley   DMProjectFieldLocal - This projects the given function of the input fields into the function space provided, putting the coefficients in a local vector.
8381a6e0b375SMatthew G. Knepley 
8382a6e0b375SMatthew G. Knepley   Not collective
8383a6e0b375SMatthew G. Knepley 
8384a6e0b375SMatthew G. Knepley   Input Parameters:
8385a6e0b375SMatthew G. Knepley + dm      - The DM
8386a6e0b375SMatthew G. Knepley . time    - The time
8387a6e0b375SMatthew G. Knepley . localU  - The input field vector
8388a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8389a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8390a6e0b375SMatthew G. Knepley 
8391a6e0b375SMatthew G. Knepley   Output Parameter:
8392a6e0b375SMatthew G. Knepley . localX  - The output vector
8393a6e0b375SMatthew G. Knepley 
8394a6e0b375SMatthew G. Knepley    Calling sequence of func:
8395a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8396a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8397a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8398a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8399a6e0b375SMatthew G. Knepley 
8400a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8401a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8402a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8403a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8404a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8405a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8406a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8407a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8408a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8409a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8410a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8411a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8412a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8413a6e0b375SMatthew G. Knepley .  t            - The current time
8414a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8415a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8416a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8417a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8418a6e0b375SMatthew G. Knepley 
8419a6e0b375SMatthew 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.
8420a6e0b375SMatthew 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
8421a6e0b375SMatthew 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
8422a6e0b375SMatthew 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.
8423a6e0b375SMatthew G. Knepley 
8424a6e0b375SMatthew G. Knepley   Level: intermediate
8425a6e0b375SMatthew G. Knepley 
8426a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8427a6e0b375SMatthew G. Knepley @*/
84288c6c5593SMatthew G. Knepley PetscErrorCode DMProjectFieldLocal(DM dm, PetscReal time, Vec localU,
84298c6c5593SMatthew G. Knepley                                    void (**funcs)(PetscInt, PetscInt, PetscInt,
84308c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
84318c6c5593SMatthew G. Knepley                                                   const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8432191494d9SMatthew G. Knepley                                                   PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
84338c6c5593SMatthew G. Knepley                                    InsertMode mode, Vec localX)
84348c6c5593SMatthew G. Knepley {
84358c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
84368c6c5593SMatthew G. Knepley 
84378c6c5593SMatthew G. Knepley   PetscFunctionBegin;
84388c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
84398c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,3);
84408c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,6);
84410918c465SMatthew G. Knepley   if (!dm->ops->projectfieldlocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLocal",((PetscObject)dm)->type_name);
84428c6c5593SMatthew G. Knepley   ierr = (dm->ops->projectfieldlocal) (dm, time, localU, funcs, mode, localX);CHKERRQ(ierr);
84438c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
84448c6c5593SMatthew G. Knepley }
84458c6c5593SMatthew G. Knepley 
8446a6e0b375SMatthew G. Knepley /*@C
8447a6e0b375SMatthew 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.
8448a6e0b375SMatthew G. Knepley 
8449a6e0b375SMatthew G. Knepley   Not collective
8450a6e0b375SMatthew G. Knepley 
8451a6e0b375SMatthew G. Knepley   Input Parameters:
8452a6e0b375SMatthew G. Knepley + dm      - The DM
8453a6e0b375SMatthew G. Knepley . time    - The time
8454a6e0b375SMatthew G. Knepley . label   - The DMLabel marking the portion of the domain to output
8455a6e0b375SMatthew G. Knepley . numIds  - The number of label ids to use
8456a6e0b375SMatthew G. Knepley . ids     - The label ids to use for marking
8457a6e0b375SMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8458a6e0b375SMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8459a6e0b375SMatthew G. Knepley . localU  - The input field vector
8460a6e0b375SMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8461a6e0b375SMatthew G. Knepley - mode    - The insertion mode for values
8462a6e0b375SMatthew G. Knepley 
8463a6e0b375SMatthew G. Knepley   Output Parameter:
8464a6e0b375SMatthew G. Knepley . localX  - The output vector
8465a6e0b375SMatthew G. Knepley 
8466a6e0b375SMatthew G. Knepley    Calling sequence of func:
8467a6e0b375SMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8468a6e0b375SMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8469a6e0b375SMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8470a6e0b375SMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8471a6e0b375SMatthew G. Knepley 
8472a6e0b375SMatthew G. Knepley +  dim          - The spatial dimension
8473a6e0b375SMatthew G. Knepley .  Nf           - The number of input fields
8474a6e0b375SMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8475a6e0b375SMatthew G. Knepley .  uOff         - The offset of each field in u[]
8476a6e0b375SMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8477a6e0b375SMatthew G. Knepley .  u            - The field values at this point in space
8478a6e0b375SMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8479a6e0b375SMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8480a6e0b375SMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8481a6e0b375SMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8482a6e0b375SMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8483a6e0b375SMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8484a6e0b375SMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8485a6e0b375SMatthew G. Knepley .  t            - The current time
8486a6e0b375SMatthew G. Knepley .  x            - The coordinates of this point
8487a6e0b375SMatthew G. Knepley .  numConstants - The number of constants
8488a6e0b375SMatthew G. Knepley .  constants    - The value of each constant
8489a6e0b375SMatthew G. Knepley -  f            - The value of the function at this point in space
8490a6e0b375SMatthew G. Knepley 
8491a6e0b375SMatthew 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.
8492a6e0b375SMatthew 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
8493a6e0b375SMatthew 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
8494a6e0b375SMatthew 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.
8495a6e0b375SMatthew G. Knepley 
8496a6e0b375SMatthew G. Knepley   Level: intermediate
8497a6e0b375SMatthew G. Knepley 
8498a6e0b375SMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8499a6e0b375SMatthew G. Knepley @*/
85001c531cf8SMatthew G. Knepley PetscErrorCode DMProjectFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
85018c6c5593SMatthew G. Knepley                                         void (**funcs)(PetscInt, PetscInt, PetscInt,
85028c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
85038c6c5593SMatthew G. Knepley                                                        const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8504191494d9SMatthew G. Knepley                                                        PetscReal, const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
85058c6c5593SMatthew G. Knepley                                         InsertMode mode, Vec localX)
85068c6c5593SMatthew G. Knepley {
85078c6c5593SMatthew G. Knepley   PetscErrorCode ierr;
85088c6c5593SMatthew G. Knepley 
85098c6c5593SMatthew G. Knepley   PetscFunctionBegin;
85108c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
85118c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
85128c6c5593SMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8513ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectFieldLabelLocal",((PetscObject)dm)->type_name);
85141c531cf8SMatthew G. Knepley   ierr = (dm->ops->projectfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
85158c6c5593SMatthew G. Knepley   PetscFunctionReturn(0);
85168c6c5593SMatthew G. Knepley }
85178c6c5593SMatthew G. Knepley 
85182716604bSToby Isaac /*@C
8519ece3a9fcSMatthew 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.
8520ece3a9fcSMatthew G. Knepley 
8521ece3a9fcSMatthew G. Knepley   Not collective
8522ece3a9fcSMatthew G. Knepley 
8523ece3a9fcSMatthew G. Knepley   Input Parameters:
8524ece3a9fcSMatthew G. Knepley + dm      - The DM
8525ece3a9fcSMatthew G. Knepley . time    - The time
8526ece3a9fcSMatthew G. Knepley . label   - The DMLabel marking the portion of the domain boundary to output
8527ece3a9fcSMatthew G. Knepley . numIds  - The number of label ids to use
8528ece3a9fcSMatthew G. Knepley . ids     - The label ids to use for marking
8529ece3a9fcSMatthew G. Knepley . Nc      - The number of components to set in the output, or PETSC_DETERMINE for all components
8530ece3a9fcSMatthew G. Knepley . comps   - The components to set in the output, or NULL for all components
8531ece3a9fcSMatthew G. Knepley . localU  - The input field vector
8532ece3a9fcSMatthew G. Knepley . funcs   - The functions to evaluate, one per field
8533ece3a9fcSMatthew G. Knepley - mode    - The insertion mode for values
8534ece3a9fcSMatthew G. Knepley 
8535ece3a9fcSMatthew G. Knepley   Output Parameter:
8536ece3a9fcSMatthew G. Knepley . localX  - The output vector
8537ece3a9fcSMatthew G. Knepley 
8538ece3a9fcSMatthew G. Knepley    Calling sequence of func:
8539ece3a9fcSMatthew G. Knepley $    func(PetscInt dim, PetscInt Nf, PetscInt NfAux,
8540ece3a9fcSMatthew G. Knepley $         const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[],
8541ece3a9fcSMatthew G. Knepley $         const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[],
8542ece3a9fcSMatthew G. Knepley $         PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f[]);
8543ece3a9fcSMatthew G. Knepley 
8544ece3a9fcSMatthew G. Knepley +  dim          - The spatial dimension
8545ece3a9fcSMatthew G. Knepley .  Nf           - The number of input fields
8546ece3a9fcSMatthew G. Knepley .  NfAux        - The number of input auxiliary fields
8547ece3a9fcSMatthew G. Knepley .  uOff         - The offset of each field in u[]
8548ece3a9fcSMatthew G. Knepley .  uOff_x       - The offset of each field in u_x[]
8549ece3a9fcSMatthew G. Knepley .  u            - The field values at this point in space
8550ece3a9fcSMatthew G. Knepley .  u_t          - The field time derivative at this point in space (or NULL)
8551ece3a9fcSMatthew G. Knepley .  u_x          - The field derivatives at this point in space
8552ece3a9fcSMatthew G. Knepley .  aOff         - The offset of each auxiliary field in u[]
8553ece3a9fcSMatthew G. Knepley .  aOff_x       - The offset of each auxiliary field in u_x[]
8554ece3a9fcSMatthew G. Knepley .  a            - The auxiliary field values at this point in space
8555ece3a9fcSMatthew G. Knepley .  a_t          - The auxiliary field time derivative at this point in space (or NULL)
8556ece3a9fcSMatthew G. Knepley .  a_x          - The auxiliary field derivatives at this point in space
8557ece3a9fcSMatthew G. Knepley .  t            - The current time
8558ece3a9fcSMatthew G. Knepley .  x            - The coordinates of this point
8559ece3a9fcSMatthew G. Knepley .  n            - The face normal
8560ece3a9fcSMatthew G. Knepley .  numConstants - The number of constants
8561ece3a9fcSMatthew G. Knepley .  constants    - The value of each constant
8562ece3a9fcSMatthew G. Knepley -  f            - The value of the function at this point in space
8563ece3a9fcSMatthew G. Knepley 
8564ece3a9fcSMatthew G. Knepley   Note:
8565ece3a9fcSMatthew 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.
8566ece3a9fcSMatthew 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
8567ece3a9fcSMatthew 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
8568ece3a9fcSMatthew 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.
8569ece3a9fcSMatthew G. Knepley 
8570ece3a9fcSMatthew G. Knepley   Level: intermediate
8571ece3a9fcSMatthew G. Knepley 
8572ece3a9fcSMatthew G. Knepley .seealso: DMProjectField(), DMProjectFieldLabelLocal(), DMProjectFunction(), DMComputeL2Diff()
8573ece3a9fcSMatthew G. Knepley @*/
8574ece3a9fcSMatthew G. Knepley PetscErrorCode DMProjectBdFieldLabelLocal(DM dm, PetscReal time, DMLabel label, PetscInt numIds, const PetscInt ids[], PetscInt Nc, const PetscInt comps[], Vec localU,
8575ece3a9fcSMatthew G. Knepley                                           void (**funcs)(PetscInt, PetscInt, PetscInt,
8576ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8577ece3a9fcSMatthew G. Knepley                                                          const PetscInt[], const PetscInt[], const PetscScalar[], const PetscScalar[], const PetscScalar[],
8578ece3a9fcSMatthew G. Knepley                                                          PetscReal, const PetscReal[], const PetscReal[], PetscInt, const PetscScalar[], PetscScalar[]),
8579ece3a9fcSMatthew G. Knepley                                           InsertMode mode, Vec localX)
8580ece3a9fcSMatthew G. Knepley {
8581ece3a9fcSMatthew G. Knepley   PetscErrorCode ierr;
8582ece3a9fcSMatthew G. Knepley 
8583ece3a9fcSMatthew G. Knepley   PetscFunctionBegin;
8584ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8585ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localU,VEC_CLASSID,6);
8586ece3a9fcSMatthew G. Knepley   PetscValidHeaderSpecific(localX,VEC_CLASSID,9);
8587ece3a9fcSMatthew G. Knepley   if (!dm->ops->projectbdfieldlabellocal) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMProjectBdFieldLabelLocal",((PetscObject)dm)->type_name);
8588ece3a9fcSMatthew G. Knepley   ierr = (dm->ops->projectbdfieldlabellocal)(dm, time, label, numIds, ids, Nc, comps, localU, funcs, mode, localX);CHKERRQ(ierr);
8589ece3a9fcSMatthew G. Knepley   PetscFunctionReturn(0);
8590ece3a9fcSMatthew G. Knepley }
8591ece3a9fcSMatthew G. Knepley 
8592ece3a9fcSMatthew G. Knepley /*@C
85932716604bSToby Isaac   DMComputeL2Diff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h.
85942716604bSToby Isaac 
85952716604bSToby Isaac   Input Parameters:
85962716604bSToby Isaac + dm    - The DM
85970709b2feSToby Isaac . time  - The time
85982716604bSToby Isaac . funcs - The functions to evaluate for each field component
85992716604bSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8600574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86012716604bSToby Isaac 
86022716604bSToby Isaac   Output Parameter:
86032716604bSToby Isaac . diff - The diff ||u - u_h||_2
86042716604bSToby Isaac 
86052716604bSToby Isaac   Level: developer
86062716604bSToby Isaac 
86071189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86082716604bSToby Isaac @*/
86090709b2feSToby Isaac PetscErrorCode DMComputeL2Diff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal *diff)
86102716604bSToby Isaac {
86112716604bSToby Isaac   PetscErrorCode ierr;
86122716604bSToby Isaac 
86132716604bSToby Isaac   PetscFunctionBegin;
86142716604bSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8615b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
86160918c465SMatthew G. Knepley   if (!dm->ops->computel2diff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2Diff",((PetscObject)dm)->type_name);
86170709b2feSToby Isaac   ierr = (dm->ops->computel2diff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
86182716604bSToby Isaac   PetscFunctionReturn(0);
86192716604bSToby Isaac }
8620b698f381SToby Isaac 
8621b698f381SToby Isaac /*@C
8622b698f381SToby Isaac   DMComputeL2GradientDiff - This function computes the L_2 difference between the gradient of a function u and an FEM interpolant solution grad u_h.
8623b698f381SToby Isaac 
8624d083f849SBarry Smith   Collective on dm
8625d083f849SBarry Smith 
8626b698f381SToby Isaac   Input Parameters:
8627b698f381SToby Isaac + dm    - The DM
8628b698f381SToby Isaac , time  - The time
8629b698f381SToby Isaac . funcs - The gradient functions to evaluate for each field component
8630b698f381SToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8631574a98acSMatthew G. Knepley . X     - The coefficient vector u_h, a global vector
8632b698f381SToby Isaac - n     - The vector to project along
8633b698f381SToby Isaac 
8634b698f381SToby Isaac   Output Parameter:
8635b698f381SToby Isaac . diff - The diff ||(grad u - grad u_h) . n||_2
8636b698f381SToby Isaac 
8637b698f381SToby Isaac   Level: developer
8638b698f381SToby Isaac 
8639b698f381SToby Isaac .seealso: DMProjectFunction(), DMComputeL2Diff()
8640b698f381SToby Isaac @*/
8641b698f381SToby 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)
8642b698f381SToby Isaac {
8643b698f381SToby Isaac   PetscErrorCode ierr;
8644b698f381SToby Isaac 
8645b698f381SToby Isaac   PetscFunctionBegin;
8646b698f381SToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
8647b698f381SToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
8648b698f381SToby Isaac   if (!dm->ops->computel2gradientdiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2GradientDiff",((PetscObject)dm)->type_name);
8649b698f381SToby Isaac   ierr = (dm->ops->computel2gradientdiff)(dm,time,funcs,ctxs,X,n,diff);CHKERRQ(ierr);
8650b698f381SToby Isaac   PetscFunctionReturn(0);
8651b698f381SToby Isaac }
8652b698f381SToby Isaac 
86532a16baeaSToby Isaac /*@C
86542a16baeaSToby Isaac   DMComputeL2FieldDiff - This function computes the L_2 difference between a function u and an FEM interpolant solution u_h, separated into field components.
86552a16baeaSToby Isaac 
8656d083f849SBarry Smith   Collective on dm
8657d083f849SBarry Smith 
86582a16baeaSToby Isaac   Input Parameters:
86592a16baeaSToby Isaac + dm    - The DM
86602a16baeaSToby Isaac . time  - The time
86612a16baeaSToby Isaac . funcs - The functions to evaluate for each field component
86622a16baeaSToby Isaac . ctxs  - Optional array of contexts to pass to each function, or NULL.
8663574a98acSMatthew G. Knepley - X     - The coefficient vector u_h, a global vector
86642a16baeaSToby Isaac 
86652a16baeaSToby Isaac   Output Parameter:
86662a16baeaSToby Isaac . diff - The array of differences, ||u^f - u^f_h||_2
86672a16baeaSToby Isaac 
86682a16baeaSToby Isaac   Level: developer
86692a16baeaSToby Isaac 
86701189c1efSToby Isaac .seealso: DMProjectFunction(), DMComputeL2FieldDiff(), DMComputeL2GradientDiff()
86712a16baeaSToby Isaac @*/
86721189c1efSToby Isaac PetscErrorCode DMComputeL2FieldDiff(DM dm, PetscReal time, PetscErrorCode (**funcs)(PetscInt, PetscReal, const PetscReal [], PetscInt, PetscScalar *, void *), void **ctxs, Vec X, PetscReal diff[])
86732a16baeaSToby Isaac {
86742a16baeaSToby Isaac   PetscErrorCode ierr;
86752a16baeaSToby Isaac 
86762a16baeaSToby Isaac   PetscFunctionBegin;
86772a16baeaSToby Isaac   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
86782a16baeaSToby Isaac   PetscValidHeaderSpecific(X,VEC_CLASSID,5);
86790918c465SMatthew G. Knepley   if (!dm->ops->computel2fielddiff) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMComputeL2FieldDiff",((PetscObject)dm)->type_name);
86802a16baeaSToby Isaac   ierr = (dm->ops->computel2fielddiff)(dm,time,funcs,ctxs,X,diff);CHKERRQ(ierr);
86812a16baeaSToby Isaac   PetscFunctionReturn(0);
86822a16baeaSToby Isaac }
86832a16baeaSToby Isaac 
8684df0b854cSToby Isaac /*@C
8685df0b854cSToby Isaac   DMAdaptLabel - Adapt a dm based on a label with values interpreted as coarsening and refining flags.  Specific implementations of DM maybe have
8686cd3c525cSToby Isaac                  specialized flags, but all implementations should accept flag values DM_ADAPT_DETERMINE, DM_ADAPT_KEEP, DM_ADAPT_REFINE, and DM_ADAPT_COARSEN.
8687df0b854cSToby Isaac 
8688df0b854cSToby Isaac   Collective on dm
8689df0b854cSToby Isaac 
8690df0b854cSToby Isaac   Input parameters:
8691df0b854cSToby Isaac + dm - the pre-adaptation DM object
8692a1b0c543SToby Isaac - label - label with the flags
8693df0b854cSToby Isaac 
8694df0b854cSToby Isaac   Output parameters:
86950d1cd5e0SMatthew G. Knepley . dmAdapt - the adapted DM object: may be NULL if an adapted DM could not be produced.
8696df0b854cSToby Isaac 
8697df0b854cSToby Isaac   Level: intermediate
86980d1cd5e0SMatthew G. Knepley 
86990d1cd5e0SMatthew G. Knepley .seealso: DMAdaptMetric(), DMCoarsen(), DMRefine()
8700df0b854cSToby Isaac @*/
87010d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptLabel(DM dm, DMLabel label, DM *dmAdapt)
8702df0b854cSToby Isaac {
8703df0b854cSToby Isaac   PetscErrorCode ierr;
8704df0b854cSToby Isaac 
8705df0b854cSToby Isaac   PetscFunctionBegin;
8706df0b854cSToby Isaac   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8707a1b0c543SToby Isaac   PetscValidPointer(label,2);
87080d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt,3);
87090d1cd5e0SMatthew G. Knepley   *dmAdapt = NULL;
87106f25b0d8SLisandro Dalcin   if (!dm->ops->adaptlabel) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptLabel",((PetscObject)dm)->type_name);
87110d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptlabel)(dm, label, dmAdapt);CHKERRQ(ierr);
87120d1cd5e0SMatthew G. Knepley   PetscFunctionReturn(0);
87130d1cd5e0SMatthew G. Knepley }
87140d1cd5e0SMatthew G. Knepley 
87150d1cd5e0SMatthew G. Knepley /*@C
87160d1cd5e0SMatthew G. Knepley   DMAdaptMetric - Generates a mesh adapted to the specified metric field using the pragmatic library.
87170d1cd5e0SMatthew G. Knepley 
87180d1cd5e0SMatthew G. Knepley   Input Parameters:
87190d1cd5e0SMatthew G. Knepley + dm - The DM object
87200d1cd5e0SMatthew G. Knepley . metric - The metric to which the mesh is adapted, defined vertex-wise.
87216f25b0d8SLisandro 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_".
87220d1cd5e0SMatthew G. Knepley 
87230d1cd5e0SMatthew G. Knepley   Output Parameter:
87240d1cd5e0SMatthew G. Knepley . dmAdapt  - Pointer to the DM object containing the adapted mesh
87250d1cd5e0SMatthew G. Knepley 
87260d1cd5e0SMatthew G. Knepley   Note: The label in the adapted mesh will be registered under the name of the input DMLabel object
87270d1cd5e0SMatthew G. Knepley 
87280d1cd5e0SMatthew G. Knepley   Level: advanced
87290d1cd5e0SMatthew G. Knepley 
87300d1cd5e0SMatthew G. Knepley .seealso: DMAdaptLabel(), DMCoarsen(), DMRefine()
87310d1cd5e0SMatthew G. Knepley @*/
87320d1cd5e0SMatthew G. Knepley PetscErrorCode DMAdaptMetric(DM dm, Vec metric, DMLabel bdLabel, DM *dmAdapt)
87330d1cd5e0SMatthew G. Knepley {
87340d1cd5e0SMatthew G. Knepley   PetscErrorCode ierr;
87350d1cd5e0SMatthew G. Knepley 
87360d1cd5e0SMatthew G. Knepley   PetscFunctionBegin;
87370d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
87380d1cd5e0SMatthew G. Knepley   PetscValidHeaderSpecific(metric, VEC_CLASSID, 2);
87390d1cd5e0SMatthew G. Knepley   if (bdLabel) PetscValidPointer(bdLabel, 3);
87400d1cd5e0SMatthew G. Knepley   PetscValidPointer(dmAdapt, 4);
87416f25b0d8SLisandro Dalcin   *dmAdapt = NULL;
87426f25b0d8SLisandro Dalcin   if (!dm->ops->adaptmetric) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMAdaptMetric",((PetscObject)dm)->type_name);
87430d1cd5e0SMatthew G. Knepley   ierr = (dm->ops->adaptmetric)(dm, metric, bdLabel, dmAdapt);CHKERRQ(ierr);
8744df0b854cSToby Isaac   PetscFunctionReturn(0);
8745df0b854cSToby Isaac }
8746c4088d22SMatthew G. Knepley 
8747502a2867SDave May /*@C
8748502a2867SDave May  DMGetNeighbors - Gets an array containing the MPI rank of all the processes neighbors
8749502a2867SDave May 
8750502a2867SDave May  Not Collective
8751502a2867SDave May 
8752502a2867SDave May  Input Parameter:
8753502a2867SDave May .  dm    - The DM
8754502a2867SDave May 
87550a19bb7dSprj-  Output Parameters:
87560a19bb7dSprj- +  nranks - the number of neighbours
87570a19bb7dSprj- -  ranks - the neighbors ranks
8758502a2867SDave May 
8759502a2867SDave May  Notes:
8760502a2867SDave May  Do not free the array, it is freed when the DM is destroyed.
8761502a2867SDave May 
8762502a2867SDave May  Level: beginner
8763502a2867SDave May 
8764dec1416fSJunchao Zhang  .seealso: DMDAGetNeighbors(), PetscSFGetRootRanks()
8765502a2867SDave May @*/
8766502a2867SDave May PetscErrorCode DMGetNeighbors(DM dm,PetscInt *nranks,const PetscMPIInt *ranks[])
8767502a2867SDave May {
8768502a2867SDave May   PetscErrorCode ierr;
8769502a2867SDave May 
8770502a2867SDave May   PetscFunctionBegin;
8771502a2867SDave May   PetscValidHeaderSpecific(dm,DM_CLASSID,1);
87720918c465SMatthew G. Knepley   if (!dm->ops->getneighbors) SETERRQ1(PetscObjectComm((PetscObject)dm),PETSC_ERR_SUP,"DM type %s does not implement DMGetNeighbors",((PetscObject)dm)->type_name);
8773502a2867SDave May   ierr = (dm->ops->getneighbors)(dm,nranks,ranks);CHKERRQ(ierr);
8774502a2867SDave May   PetscFunctionReturn(0);
8775502a2867SDave May }
8776502a2867SDave May 
8777531c7667SBarry Smith #include <petsc/private/matimpl.h> /* Needed because of coloring->ctype below */
8778531c7667SBarry Smith 
8779531c7667SBarry Smith /*
8780531c7667SBarry Smith     Converts the input vector to a ghosted vector and then calls the standard coloring code.
8781531c7667SBarry Smith     This has be a different function because it requires DM which is not defined in the Mat library
8782531c7667SBarry Smith */
8783531c7667SBarry Smith PetscErrorCode  MatFDColoringApply_AIJDM(Mat J,MatFDColoring coloring,Vec x1,void *sctx)
8784531c7667SBarry Smith {
8785531c7667SBarry Smith   PetscErrorCode ierr;
8786531c7667SBarry Smith 
8787531c7667SBarry Smith   PetscFunctionBegin;
8788531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8789531c7667SBarry Smith     Vec x1local;
8790531c7667SBarry Smith     DM  dm;
8791531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8792531c7667SBarry Smith     if (!dm) SETERRQ(PetscObjectComm((PetscObject)J),PETSC_ERR_ARG_INCOMP,"IS_COLORING_LOCAL requires a DM");
8793531c7667SBarry Smith     ierr = DMGetLocalVector(dm,&x1local);CHKERRQ(ierr);
8794531c7667SBarry Smith     ierr = DMGlobalToLocalBegin(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8795531c7667SBarry Smith     ierr = DMGlobalToLocalEnd(dm,x1,INSERT_VALUES,x1local);CHKERRQ(ierr);
8796531c7667SBarry Smith     x1   = x1local;
8797531c7667SBarry Smith   }
8798531c7667SBarry Smith   ierr = MatFDColoringApply_AIJ(J,coloring,x1,sctx);CHKERRQ(ierr);
8799531c7667SBarry Smith   if (coloring->ctype == IS_COLORING_LOCAL) {
8800531c7667SBarry Smith     DM  dm;
8801531c7667SBarry Smith     ierr = MatGetDM(J,&dm);CHKERRQ(ierr);
8802531c7667SBarry Smith     ierr = DMRestoreLocalVector(dm,&x1);CHKERRQ(ierr);
8803531c7667SBarry Smith   }
8804531c7667SBarry Smith   PetscFunctionReturn(0);
8805531c7667SBarry Smith }
8806531c7667SBarry Smith 
8807531c7667SBarry Smith /*@
8808531c7667SBarry Smith     MatFDColoringUseDM - allows a MatFDColoring object to use the DM associated with the matrix to use a IS_COLORING_LOCAL coloring
8809531c7667SBarry Smith 
8810531c7667SBarry Smith     Input Parameter:
8811531c7667SBarry Smith .    coloring - the MatFDColoring object
8812531c7667SBarry Smith 
881395452b02SPatrick Sanan     Developer Notes:
881495452b02SPatrick Sanan     this routine exists because the PETSc Mat library does not know about the DM objects
8815531c7667SBarry Smith 
88161b266c99SBarry Smith     Level: advanced
88171b266c99SBarry Smith 
8818531c7667SBarry Smith .seealso: MatFDColoring, MatFDColoringCreate(), ISColoringType
8819531c7667SBarry Smith @*/
8820531c7667SBarry Smith PetscErrorCode  MatFDColoringUseDM(Mat coloring,MatFDColoring fdcoloring)
8821531c7667SBarry Smith {
8822531c7667SBarry Smith   PetscFunctionBegin;
8823531c7667SBarry Smith   coloring->ops->fdcoloringapply = MatFDColoringApply_AIJDM;
8824531c7667SBarry Smith   PetscFunctionReturn(0);
8825531c7667SBarry Smith }
88268320bc6fSPatrick Sanan 
88278320bc6fSPatrick Sanan /*@
88288320bc6fSPatrick Sanan     DMGetCompatibility - determine if two DMs are compatible
88298320bc6fSPatrick Sanan 
88308320bc6fSPatrick Sanan     Collective
88318320bc6fSPatrick Sanan 
88328320bc6fSPatrick Sanan     Input Parameters:
8833a5bc1bf3SBarry Smith +    dm1 - the first DM
88348320bc6fSPatrick Sanan -    dm2 - the second DM
88358320bc6fSPatrick Sanan 
88368320bc6fSPatrick Sanan     Output Parameters:
88378320bc6fSPatrick Sanan +    compatible - whether or not the two DMs are compatible
88388320bc6fSPatrick Sanan -    set - whether or not the compatible value was set
88398320bc6fSPatrick Sanan 
88408320bc6fSPatrick Sanan     Notes:
88418320bc6fSPatrick Sanan     Two DMs are deemed compatible if they represent the same parallel decomposition
88423d862458SPatrick Sanan     of the same topology. This implies that the section (field data) on one
88438320bc6fSPatrick Sanan     "makes sense" with respect to the topology and parallel decomposition of the other.
88443d862458SPatrick Sanan     Loosely speaking, compatible DMs represent the same domain and parallel
88453d862458SPatrick Sanan     decomposition, but hold different data.
88468320bc6fSPatrick Sanan 
88478320bc6fSPatrick Sanan     Typically, one would confirm compatibility if intending to simultaneously iterate
88488320bc6fSPatrick Sanan     over a pair of vectors obtained from different DMs.
88498320bc6fSPatrick Sanan 
88508320bc6fSPatrick Sanan     For example, two DMDA objects are compatible if they have the same local
88518320bc6fSPatrick Sanan     and global sizes and the same stencil width. They can have different numbers
88528320bc6fSPatrick Sanan     of degrees of freedom per node. Thus, one could use the node numbering from
88538320bc6fSPatrick Sanan     either DM in bounds for a loop over vectors derived from either DM.
88548320bc6fSPatrick Sanan 
88558320bc6fSPatrick Sanan     Consider the operation of summing data living on a 2-dof DMDA to data living
88568320bc6fSPatrick Sanan     on a 1-dof DMDA, which should be compatible, as in the following snippet.
88578320bc6fSPatrick Sanan .vb
88588320bc6fSPatrick Sanan   ...
88598320bc6fSPatrick Sanan   ierr = DMGetCompatibility(da1,da2,&compatible,&set);CHKERRQ(ierr);
88608320bc6fSPatrick Sanan   if (set && compatible)  {
88618320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88628320bc6fSPatrick Sanan     ierr = DMDAVecGetArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88633d862458SPatrick Sanan     ierr = DMDAGetCorners(da1,&x,&y,NULL,&m,&n,NULL);CHKERRQ(ierr);
88648320bc6fSPatrick Sanan     for (j=y; j<y+n; ++j) {
88658320bc6fSPatrick Sanan       for (i=x; i<x+m, ++i) {
88668320bc6fSPatrick Sanan         arr1[j][i][0] = arr2[j][i][0] + arr2[j][i][1];
88678320bc6fSPatrick Sanan       }
88688320bc6fSPatrick Sanan     }
88698320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da1,vec1,&arr1);CHKERRQ(ierr);
88708320bc6fSPatrick Sanan     ierr = DMDAVecRestoreArrayDOF(da2,vec2,&arr2);CHKERRQ(ierr);
88718320bc6fSPatrick Sanan   } else {
88728320bc6fSPatrick Sanan     SETERRQ(PetscObjectComm((PetscObject)da1,PETSC_ERR_ARG_INCOMP,"DMDA objects incompatible");
88738320bc6fSPatrick Sanan   }
88748320bc6fSPatrick Sanan   ...
88758320bc6fSPatrick Sanan .ve
88768320bc6fSPatrick Sanan 
88778320bc6fSPatrick Sanan     Checking compatibility might be expensive for a given implementation of DM,
88788320bc6fSPatrick Sanan     or might be impossible to unambiguously confirm or deny. For this reason,
88798320bc6fSPatrick Sanan     this function may decline to determine compatibility, and hence users should
88808320bc6fSPatrick Sanan     always check the "set" output parameter.
88818320bc6fSPatrick Sanan 
88828320bc6fSPatrick Sanan     A DM is always compatible with itself.
88838320bc6fSPatrick Sanan 
88848320bc6fSPatrick Sanan     In the current implementation, DMs which live on "unequal" communicators
88858320bc6fSPatrick Sanan     (MPI_UNEQUAL in the terminology of MPI_Comm_compare()) are always deemed
88868320bc6fSPatrick Sanan     incompatible.
88878320bc6fSPatrick Sanan 
88888320bc6fSPatrick Sanan     This function is labeled "Collective," as information about all subdomains
88898320bc6fSPatrick Sanan     is required on each rank. However, in DM implementations which store all this
88908320bc6fSPatrick Sanan     information locally, this function may be merely "Logically Collective".
88918320bc6fSPatrick Sanan 
88928320bc6fSPatrick Sanan     Developer Notes:
88933d862458SPatrick Sanan     Compatibility is assumed to be a symmetric concept; DM A is compatible with DM B
88943d862458SPatrick Sanan     iff B is compatible with A. Thus, this function checks the implementations
8895a5bc1bf3SBarry Smith     of both dm and dmc (if they are of different types), attempting to determine
88968320bc6fSPatrick Sanan     compatibility. It is left to DM implementers to ensure that symmetry is
88978320bc6fSPatrick Sanan     preserved. The simplest way to do this is, when implementing type-specific
88983d862458SPatrick Sanan     logic for this function, is to check for existing logic in the implementation
88993d862458SPatrick Sanan     of other DM types and let *set = PETSC_FALSE if found.
89008320bc6fSPatrick Sanan 
89018320bc6fSPatrick Sanan     Level: advanced
89028320bc6fSPatrick Sanan 
89033d862458SPatrick Sanan .seealso: DM, DMDACreateCompatibleDMDA(), DMStagCreateCompatibleDMStag()
89048320bc6fSPatrick Sanan @*/
89058320bc6fSPatrick Sanan 
8906a5bc1bf3SBarry Smith PetscErrorCode DMGetCompatibility(DM dm1,DM dm2,PetscBool *compatible,PetscBool *set)
89078320bc6fSPatrick Sanan {
89088320bc6fSPatrick Sanan   PetscErrorCode ierr;
89098320bc6fSPatrick Sanan   PetscMPIInt    compareResult;
89108320bc6fSPatrick Sanan   DMType         type,type2;
89118320bc6fSPatrick Sanan   PetscBool      sameType;
89128320bc6fSPatrick Sanan 
89138320bc6fSPatrick Sanan   PetscFunctionBegin;
8914a5bc1bf3SBarry Smith   PetscValidHeaderSpecific(dm1,DM_CLASSID,1);
89158320bc6fSPatrick Sanan   PetscValidHeaderSpecific(dm2,DM_CLASSID,2);
89168320bc6fSPatrick Sanan 
89178320bc6fSPatrick Sanan   /* Declare a DM compatible with itself */
8918a5bc1bf3SBarry Smith   if (dm1 == dm2) {
89198320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89208320bc6fSPatrick Sanan     *compatible = PETSC_TRUE;
89218320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89228320bc6fSPatrick Sanan   }
89238320bc6fSPatrick Sanan 
89248320bc6fSPatrick Sanan   /* Declare a DM incompatible with a DM that lives on an "unequal"
89258320bc6fSPatrick Sanan      communicator. Note that this does not preclude compatibility with
89268320bc6fSPatrick Sanan      DMs living on "congruent" or "similar" communicators, but this must be
89278320bc6fSPatrick Sanan      determined by the implementation-specific logic */
8928a5bc1bf3SBarry Smith   ierr = MPI_Comm_compare(PetscObjectComm((PetscObject)dm1),PetscObjectComm((PetscObject)dm2),&compareResult);CHKERRQ(ierr);
89298320bc6fSPatrick Sanan   if (compareResult == MPI_UNEQUAL) {
89308320bc6fSPatrick Sanan     *set = PETSC_TRUE;
89318320bc6fSPatrick Sanan     *compatible = PETSC_FALSE;
89328320bc6fSPatrick Sanan     PetscFunctionReturn(0);
89338320bc6fSPatrick Sanan   }
89348320bc6fSPatrick Sanan 
89358320bc6fSPatrick Sanan   /* Pass to the implementation-specific routine, if one exists. */
8936a5bc1bf3SBarry Smith   if (dm1->ops->getcompatibility) {
8937a5bc1bf3SBarry Smith     ierr = (*dm1->ops->getcompatibility)(dm1,dm2,compatible,set);CHKERRQ(ierr);
8938b9d85ea2SLisandro Dalcin     if (*set) PetscFunctionReturn(0);
89398320bc6fSPatrick Sanan   }
89408320bc6fSPatrick Sanan 
8941a5bc1bf3SBarry Smith   /* If dm1 and dm2 are of different types, then attempt to check compatibility
89428320bc6fSPatrick Sanan      with an implementation of this function from dm2 */
8943a5bc1bf3SBarry Smith   ierr = DMGetType(dm1,&type);CHKERRQ(ierr);
89448320bc6fSPatrick Sanan   ierr = DMGetType(dm2,&type2);CHKERRQ(ierr);
89458320bc6fSPatrick Sanan   ierr = PetscStrcmp(type,type2,&sameType);CHKERRQ(ierr);
89468320bc6fSPatrick Sanan   if (!sameType && dm2->ops->getcompatibility) {
8947a5bc1bf3SBarry Smith     ierr = (*dm2->ops->getcompatibility)(dm2,dm1,compatible,set);CHKERRQ(ierr); /* Note argument order */
89488320bc6fSPatrick Sanan   } else {
89498320bc6fSPatrick Sanan     *set = PETSC_FALSE;
89508320bc6fSPatrick Sanan   }
89518320bc6fSPatrick Sanan   PetscFunctionReturn(0);
89528320bc6fSPatrick Sanan }
8953c0f0dcc3SMatthew G. Knepley 
8954c0f0dcc3SMatthew G. Knepley /*@C
8955c0f0dcc3SMatthew G. Knepley   DMMonitorSet - Sets an ADDITIONAL function that is to be used after a solve to monitor discretization performance.
8956c0f0dcc3SMatthew G. Knepley 
8957c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
8958c0f0dcc3SMatthew G. Knepley 
8959c0f0dcc3SMatthew G. Knepley   Input Parameters:
8960c0f0dcc3SMatthew G. Knepley + DM - the DM
8961c0f0dcc3SMatthew G. Knepley . f - the monitor function
8962c0f0dcc3SMatthew G. Knepley . mctx - [optional] user-defined context for private data for the monitor routine (use NULL if no context is desired)
8963c0f0dcc3SMatthew G. Knepley - monitordestroy - [optional] routine that frees monitor context (may be NULL)
8964c0f0dcc3SMatthew G. Knepley 
8965c0f0dcc3SMatthew G. Knepley   Options Database Keys:
8966c0f0dcc3SMatthew G. Knepley - -dm_monitor_cancel - cancels all monitors that have been hardwired into a code by calls to DMMonitorSet(), but
8967c0f0dcc3SMatthew G. Knepley                             does not cancel those set via the options database.
8968c0f0dcc3SMatthew G. Knepley 
8969c0f0dcc3SMatthew G. Knepley   Notes:
8970c0f0dcc3SMatthew G. Knepley   Several different monitoring routines may be set by calling
8971c0f0dcc3SMatthew G. Knepley   DMMonitorSet() multiple times; all will be called in the
8972c0f0dcc3SMatthew G. Knepley   order in which they were set.
8973c0f0dcc3SMatthew G. Knepley 
8974c0f0dcc3SMatthew G. Knepley   Fortran Notes:
8975c0f0dcc3SMatthew G. Knepley   Only a single monitor function can be set for each DM object
8976c0f0dcc3SMatthew G. Knepley 
8977c0f0dcc3SMatthew G. Knepley   Level: intermediate
8978c0f0dcc3SMatthew G. Knepley 
8979c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorCancel()
8980c0f0dcc3SMatthew G. Knepley @*/
8981c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorSet(DM dm, PetscErrorCode (*f)(DM, void *), void *mctx, PetscErrorCode (*monitordestroy)(void**))
8982c0f0dcc3SMatthew G. Knepley {
8983c0f0dcc3SMatthew G. Knepley   PetscInt       m;
8984c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
8985c0f0dcc3SMatthew G. Knepley 
8986c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
8987c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
8988c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
8989c0f0dcc3SMatthew G. Knepley     PetscBool identical;
8990c0f0dcc3SMatthew G. Knepley 
8991c0f0dcc3SMatthew G. Knepley     ierr = PetscMonitorCompare((PetscErrorCode (*)(void)) f, mctx, monitordestroy, (PetscErrorCode (*)(void)) dm->monitor[m], dm->monitorcontext[m], dm->monitordestroy[m], &identical);CHKERRQ(ierr);
8992c0f0dcc3SMatthew G. Knepley     if (identical) PetscFunctionReturn(0);
8993c0f0dcc3SMatthew G. Knepley   }
8994c0f0dcc3SMatthew G. Knepley   if (dm->numbermonitors >= MAXDMMONITORS) SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Too many monitors set");
8995c0f0dcc3SMatthew G. Knepley   dm->monitor[dm->numbermonitors]          = f;
8996c0f0dcc3SMatthew G. Knepley   dm->monitordestroy[dm->numbermonitors]   = monitordestroy;
8997c0f0dcc3SMatthew G. Knepley   dm->monitorcontext[dm->numbermonitors++] = (void *) mctx;
8998c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
8999c0f0dcc3SMatthew G. Knepley }
9000c0f0dcc3SMatthew G. Knepley 
9001c0f0dcc3SMatthew G. Knepley /*@
9002c0f0dcc3SMatthew G. Knepley   DMMonitorCancel - Clears all the monitor functions for a DM object.
9003c0f0dcc3SMatthew G. Knepley 
9004c0f0dcc3SMatthew G. Knepley   Logically Collective on DM
9005c0f0dcc3SMatthew G. Knepley 
9006c0f0dcc3SMatthew G. Knepley   Input Parameter:
9007c0f0dcc3SMatthew G. Knepley . dm - the DM
9008c0f0dcc3SMatthew G. Knepley 
9009c0f0dcc3SMatthew G. Knepley   Options Database Key:
9010c0f0dcc3SMatthew G. Knepley . -dm_monitor_cancel - cancels all monitors that have been hardwired
9011c0f0dcc3SMatthew G. Knepley   into a code by calls to DMonitorSet(), but does not cancel those
9012c0f0dcc3SMatthew G. Knepley   set via the options database
9013c0f0dcc3SMatthew G. Knepley 
9014c0f0dcc3SMatthew G. Knepley   Notes:
9015c0f0dcc3SMatthew G. Knepley   There is no way to clear one specific monitor from a DM object.
9016c0f0dcc3SMatthew G. Knepley 
9017c0f0dcc3SMatthew G. Knepley   Level: intermediate
9018c0f0dcc3SMatthew G. Knepley 
9019c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9020c0f0dcc3SMatthew G. Knepley @*/
9021c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitorCancel(DM dm)
9022c0f0dcc3SMatthew G. Knepley {
9023c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9024c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9025c0f0dcc3SMatthew G. Knepley 
9026c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9027c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9028c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9029c0f0dcc3SMatthew G. Knepley     if (dm->monitordestroy[m]) {ierr = (*dm->monitordestroy[m])(&dm->monitorcontext[m]);CHKERRQ(ierr);}
9030c0f0dcc3SMatthew G. Knepley   }
9031c0f0dcc3SMatthew G. Knepley   dm->numbermonitors = 0;
9032c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9033c0f0dcc3SMatthew G. Knepley }
9034c0f0dcc3SMatthew G. Knepley 
9035c0f0dcc3SMatthew G. Knepley /*@C
9036c0f0dcc3SMatthew G. Knepley   DMMonitorSetFromOptions - Sets a monitor function and viewer appropriate for the type indicated by the user
9037c0f0dcc3SMatthew G. Knepley 
9038c0f0dcc3SMatthew G. Knepley   Collective on DM
9039c0f0dcc3SMatthew G. Knepley 
9040c0f0dcc3SMatthew G. Knepley   Input Parameters:
9041c0f0dcc3SMatthew G. Knepley + dm   - DM object you wish to monitor
9042c0f0dcc3SMatthew G. Knepley . name - the monitor type one is seeking
9043c0f0dcc3SMatthew G. Knepley . help - message indicating what monitoring is done
9044c0f0dcc3SMatthew G. Knepley . manual - manual page for the monitor
9045c0f0dcc3SMatthew G. Knepley . monitor - the monitor function
9046c0f0dcc3SMatthew 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
9047c0f0dcc3SMatthew G. Knepley 
9048c0f0dcc3SMatthew G. Knepley   Output Parameter:
9049c0f0dcc3SMatthew G. Knepley . flg - Flag set if the monitor was created
9050c0f0dcc3SMatthew G. Knepley 
9051c0f0dcc3SMatthew G. Knepley   Level: developer
9052c0f0dcc3SMatthew G. Knepley 
9053c0f0dcc3SMatthew G. Knepley .seealso: PetscOptionsGetViewer(), PetscOptionsGetReal(), PetscOptionsHasName(), PetscOptionsGetString(),
9054c0f0dcc3SMatthew G. Knepley           PetscOptionsGetIntArray(), PetscOptionsGetRealArray(), PetscOptionsBool()
9055c0f0dcc3SMatthew G. Knepley           PetscOptionsInt(), PetscOptionsString(), PetscOptionsReal(), PetscOptionsBool(),
9056c0f0dcc3SMatthew G. Knepley           PetscOptionsName(), PetscOptionsBegin(), PetscOptionsEnd(), PetscOptionsHead(),
9057c0f0dcc3SMatthew G. Knepley           PetscOptionsStringArray(),PetscOptionsRealArray(), PetscOptionsScalar(),
9058c0f0dcc3SMatthew G. Knepley           PetscOptionsBoolGroupBegin(), PetscOptionsBoolGroup(), PetscOptionsBoolGroupEnd(),
9059c0f0dcc3SMatthew G. Knepley           PetscOptionsFList(), PetscOptionsEList()
9060c0f0dcc3SMatthew G. Knepley @*/
9061c0f0dcc3SMatthew 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)
9062c0f0dcc3SMatthew G. Knepley {
9063c0f0dcc3SMatthew G. Knepley   PetscViewer       viewer;
9064c0f0dcc3SMatthew G. Knepley   PetscViewerFormat format;
9065c0f0dcc3SMatthew G. Knepley   PetscErrorCode    ierr;
9066c0f0dcc3SMatthew G. Knepley 
9067c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9068c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9069c0f0dcc3SMatthew G. Knepley   ierr = PetscOptionsGetViewer(PetscObjectComm((PetscObject) dm), ((PetscObject) dm)->options, ((PetscObject) dm)->prefix, name, &viewer, &format, flg);CHKERRQ(ierr);
9070c0f0dcc3SMatthew G. Knepley   if (*flg) {
9071c0f0dcc3SMatthew G. Knepley     PetscViewerAndFormat *vf;
9072c0f0dcc3SMatthew G. Knepley 
9073c0f0dcc3SMatthew G. Knepley     ierr = PetscViewerAndFormatCreate(viewer, format, &vf);CHKERRQ(ierr);
9074c0f0dcc3SMatthew G. Knepley     ierr = PetscObjectDereference((PetscObject) viewer);CHKERRQ(ierr);
9075c0f0dcc3SMatthew G. Knepley     if (monitorsetup) {ierr = (*monitorsetup)(dm, vf);CHKERRQ(ierr);}
9076c0f0dcc3SMatthew G. Knepley     ierr = DMMonitorSet(dm,(PetscErrorCode (*)(DM, void *)) monitor, vf, (PetscErrorCode (*)(void **)) PetscViewerAndFormatDestroy);CHKERRQ(ierr);
9077c0f0dcc3SMatthew G. Knepley   }
9078c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9079c0f0dcc3SMatthew G. Knepley }
9080c0f0dcc3SMatthew G. Knepley 
9081c0f0dcc3SMatthew G. Knepley /*@
9082c0f0dcc3SMatthew G. Knepley    DMMonitor - runs the user provided monitor routines, if they exist
9083c0f0dcc3SMatthew G. Knepley 
9084c0f0dcc3SMatthew G. Knepley    Collective on DM
9085c0f0dcc3SMatthew G. Knepley 
9086c0f0dcc3SMatthew G. Knepley    Input Parameters:
9087c0f0dcc3SMatthew G. Knepley .  dm - The DM
9088c0f0dcc3SMatthew G. Knepley 
9089c0f0dcc3SMatthew G. Knepley    Level: developer
9090c0f0dcc3SMatthew G. Knepley 
9091c0f0dcc3SMatthew G. Knepley .seealso: DMMonitorSet()
9092c0f0dcc3SMatthew G. Knepley @*/
9093c0f0dcc3SMatthew G. Knepley PetscErrorCode DMMonitor(DM dm)
9094c0f0dcc3SMatthew G. Knepley {
9095c0f0dcc3SMatthew G. Knepley   PetscInt       m;
9096c0f0dcc3SMatthew G. Knepley   PetscErrorCode ierr;
9097c0f0dcc3SMatthew G. Knepley 
9098c0f0dcc3SMatthew G. Knepley   PetscFunctionBegin;
9099c0f0dcc3SMatthew G. Knepley   if (!dm) PetscFunctionReturn(0);
9100c0f0dcc3SMatthew G. Knepley   PetscValidHeaderSpecific(dm, DM_CLASSID, 1);
9101c0f0dcc3SMatthew G. Knepley   for (m = 0; m < dm->numbermonitors; ++m) {
9102c0f0dcc3SMatthew G. Knepley     ierr = (*dm->monitor[m])(dm, dm->monitorcontext[m]);CHKERRQ(ierr);
9103c0f0dcc3SMatthew G. Knepley   }
9104c0f0dcc3SMatthew G. Knepley   PetscFunctionReturn(0);
9105c0f0dcc3SMatthew G. Knepley }
9106